NDOJ/judge/jinja2/rating.py

34 lines
752 B
Python
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
from judge.ratings import rating_class, rating_name, rating_progress
from . import registry
def _get_rating_value(func, obj):
if obj is None:
return None
2022-11-01 01:43:06 +00:00
if isinstance(obj, int):
2020-01-21 06:35:58 +00:00
return func(obj)
else:
return func(obj.rating)
2022-05-14 17:57:27 +00:00
@registry.function("rating_class")
2020-01-21 06:35:58 +00:00
def get_rating_class(obj):
2022-05-14 17:57:27 +00:00
return _get_rating_value(rating_class, obj) or "rate-none"
2020-01-21 06:35:58 +00:00
2022-05-14 17:57:27 +00:00
@registry.function(name="rating_name")
2020-01-21 06:35:58 +00:00
def get_name(obj):
2022-05-14 17:57:27 +00:00
return _get_rating_value(rating_name, obj) or "Unrated"
2020-01-21 06:35:58 +00:00
2022-05-14 17:57:27 +00:00
@registry.function(name="rating_progress")
2020-01-21 06:35:58 +00:00
def get_progress(obj):
return _get_rating_value(rating_progress, obj) or 0.0
@registry.function
2022-05-14 17:57:27 +00:00
@registry.render_with("user/rating.html")
2020-01-21 06:35:58 +00:00
def rating_number(obj):
2022-05-14 17:57:27 +00:00
return {"rating": obj}