diff --git a/contests/handlers/compare.py b/contests/handlers/compare.py index c4a1ee2..8d2a5c4 100644 --- a/contests/handlers/compare.py +++ b/contests/handlers/compare.py @@ -80,7 +80,7 @@ def get_list_articles(self, contest): 'gpllimit': 'max', } response = requests.get(contest.api_endpoint, params=list_api_params).json() - if not 'query' in response: + if 'query' not in response: return list_ list_.extend(response['query']['pages']) @@ -107,7 +107,7 @@ def get_category_articles(self, contest): "gcmlimit": "max", } response = requests.get(contest.api_endpoint, params=categorymembers_api_params).json() - if not 'query' in response: + if 'query' not in response: return list_ list_.extend(response['query']['pages']) @@ -132,7 +132,7 @@ def get_deletion_pages(self, contest): 'cmprop': 'title', } response = requests.get(contest.api_endpoint, params=deletion_api_params).json() - if not 'query' in response: + if 'query' not in response: return list_ list_.extend(response['query']['categorymembers']) diff --git a/contests/handlers/contest.py b/contests/handlers/contest.py index 7a15c15..db80732 100644 --- a/contests/handlers/contest.py +++ b/contests/handlers/contest.py @@ -108,7 +108,7 @@ def count_articles(self, contest): 'format': 'json' } response = requests.get(contest.api_endpoint, params=api_params).json() - if not 'query' in response: + if 'query' not in response: return list_ list_.extend(response['query']['pages']) diff --git a/contests/handlers/evaluators.py b/contests/handlers/evaluators.py index 5187bf0..bd4c321 100644 --- a/contests/handlers/evaluators.py +++ b/contests/handlers/evaluators.py @@ -10,13 +10,12 @@ def __init__(self, contest): def execute(self, request): evaluator = self.get_current_evaluator(request) - if request.method == 'POST': - if evaluator.user_status == 'G': - if request.POST.get('new'): - self.add_new_evaluator(request.POST.get('new')) + if request.method == 'POST' and evaluator.user_status == 'G': + if request.POST.get('new'): + self.add_new_evaluator(request.POST.get('new')) - if request.POST.get('user'): - self.update_evaluator_status(request) + if request.POST.get('user'): + self.update_evaluator_status(request) return { 'contest': self.contest, diff --git a/contests/handlers/graph.py b/contests/handlers/graph.py index e9921e2..c4f79da 100644 --- a/contests/handlers/graph.py +++ b/contests/handlers/graph.py @@ -15,11 +15,11 @@ def __init__(self, contest): self.counter = CounterHandler(contest=self.contest) self.start_day = self.contest.start_time.strftime("%Y-%m-%d") - def execute(self, request): + def execute(self): if self.is_contest_not_started(): return self._response_with_empty_graph() - end_day, finished = self.get_end_day_and_status() + end_day = self.get_end_day() days, last_day = self.prepare_days_range(end_day) all_points = self.collect_points_for_days(days + [last_day]) best_users = self.get_top_users(all_points, last_day) @@ -33,11 +33,11 @@ def execute(self, request): def is_contest_not_started(self): return now().date() < self.contest.start_time.date() - def get_end_day_and_status(self): + def get_end_day(self): if now().date() > self.contest.end_time.date(): end_day = (self.contest.end_time + timedelta(days=1)).strftime("%Y-%m-%d") - return end_day, True - return now().strftime("%Y-%m-%d"), False + return end_day + return now().strftime("%Y-%m-%d") def prepare_days_range(self, end_day): days = self.generate_date_range(self.start_day, end_day) diff --git a/contests/handlers/modify.py b/contests/handlers/modify.py index ab39437..7a123ce 100644 --- a/contests/handlers/modify.py +++ b/contests/handlers/modify.py @@ -10,77 +10,85 @@ def __init__(self, contest): self.contest = contest def execute(self, request): - contest = self.contest - edit = None - diff = None - content = None - author = None - comment = None - evaluation = None - allowed = False - history_qualifications = None - history_evaluations = None - - if Evaluator.objects.get( - contest=contest, - profile=request.user.profile - ).user_status == 'G' or edit.last_evaluation.evaluator.profile == request.user.profile: - allowed = True - if request.method == 'POST' and request.POST.get('diff'): diff = request.POST.get('diff') - edit = Edit.objects.get(contest=contest, diff=diff) + edit = self.get_edit(diff) + + if not edit: + return {'contest': self.contest, 'diff': diff, 'content': None} + + allowed = self.is_allowed(request, edit) + content, author, comment = self.get_comparison_data(diff) + history_qualifications = self.get_history_qualifications(edit) + history_evaluations = self.get_history_evaluations(edit) + + evaluation = None + if request.POST.get('obs'): + evaluation = self.create_evaluation(request, edit, allowed) - compare_params = { - 'action': 'compare', - 'prop': 'title|diff|comment|user|ids', - 'format': 'json', - 'fromrev': diff, - 'torelative': 'prev', + return { + 'contest': self.contest, + 'edit': edit, + 'diff': diff, + 'evaluation': evaluation, + 'allowed': allowed, + 'content': content, + 'author': author, + 'comment': comment, + 'history_qualifications': history_qualifications, + 'history_evaluations': history_evaluations, } - compare = requests.get(contest.api_endpoint, params=compare_params).json().get('compare', {}) + else: + return {'contest': self.contest, 'edit': None} - content = compare.get('*', '') - author = compare.get('touser', '') - comment = compare.get('tocomment', '') + def get_edit(self, diff): + try: + return Edit.objects.get(contest=self.contest, diff=diff) + except Edit.DoesNotExist: + return False - history_qualifications = Qualification.objects.filter( - contest=contest, - diff=edit - ).select_related('evaluator__profile').order_by('-when') + def is_allowed(self, request, edit): + evaluator = Evaluator.objects.get(contest=self.contest, profile=request.user.profile) + return evaluator.user_status == 'G' or edit.last_evaluation.evaluator.profile == request.user.profile - history_evaluations = Evaluation.objects.filter( - contest=contest, - diff=edit - ).select_related('evaluator__profile').order_by('-when') - - if request.POST.get('obs'): - if allowed: - evaluation = Evaluation.objects.create( - contest=contest, - evaluator=Evaluator.objects.get(contest=contest, profile=request.user.profile), - diff=edit, - valid_edit=True if request.POST.get('valid') == '1' else False, - pictures=request.POST.get('pic') if request.POST.get('pic').isnumeric() else 0, - real_bytes=request.POST.get('overwrite') or Edit.objects.get(contest=contest, diff=diff).orig_bytes, - status=1, - obs=request.POST.get('obs'), - ) - edit.last_evaluation = evaluation - edit.save() - else: - raise PermissionDenied("You are not allowed to perform this action.") - - return_dict = { - 'contest': contest, - 'edit': edit, - 'diff': diff, - 'evaluation': evaluation, - 'allowed': allowed, - 'content': content, - 'author': author, - 'comment': comment, - 'history_qualifications': history_qualifications, - 'history_evaluations': history_evaluations, + def get_comparison_data(self, diff): + compare_params = { + 'action': 'compare', + 'prop': 'title|diff|comment|user|ids', + 'format': 'json', + 'fromrev': diff, + 'torelative': 'prev', } - return return_dict \ No newline at end of file + compare = requests.get(self.contest.api_endpoint, params=compare_params).json().get('compare', {}) + return compare.get('*', ''), compare.get('touser', ''), compare.get('tocomment', '') + + def get_history_qualifications(self, edit): + return Qualification.objects.filter( + contest=self.contest, + diff=edit + ).select_related('evaluator__profile').order_by('-when') + + def get_history_evaluations(self, edit): + return Evaluation.objects.filter( + contest=self.contest, + diff=edit + ).select_related('evaluator__profile').order_by('-when') + + def create_evaluation(self, request, edit, allowed): + if allowed: + evaluation = Evaluation.objects.create( + contest=self.contest, + evaluator=Evaluator.objects.get(contest=self.contest, profile=request.user.profile), + diff=edit, + valid_edit=True if request.POST.get('valid') == '1' else False, + pictures=request.POST.get('pic') if request.POST.get('pic').isnumeric() else 0, + real_bytes=request.POST.get('overwrite') or edit.orig_bytes, + status=1, + obs=request.POST.get('obs'), + ) + edit.last_evaluation = evaluation + edit.save() + return True + else: + raise PermissionDenied("You are not allowed to perform this action.") + \ No newline at end of file diff --git a/contests/handlers/triage.py b/contests/handlers/triage.py index a5fab96..8c19703 100644 --- a/contests/handlers/triage.py +++ b/contests/handlers/triage.py @@ -185,7 +185,7 @@ def unhold_edit(self): ) for locked in lockeds: - evalution = Evaluation.objects.create( + evaluation = Evaluation.objects.create( contest=self.contest, diff=locked.diff, status='0' # Status '0' indicates the edit is pending @@ -298,7 +298,14 @@ def build_context(self, revision_history, current_diff): delta = edit['size'] - revision_history[i + 1]['size'] else: delta = edit['size'] - delta_color = 'green' if delta > 0 else ('red' if delta < 0 else 'grey') + + if delta > 0: + delta_color = 'green' + elif delta < 0: + delta_color = 'red' + else: + delta_color = 'grey' + history_class = 'w3-small w3-leftbar w3-border-grey w3-padding-small' if edit['revid'] == current_diff else 'w3-small' if edit['revid'] != 0: diff --git a/contests/management/commands/load_edits.py b/contests/management/commands/load_edits.py index f0ea749..421f6ee 100644 --- a/contests/management/commands/load_edits.py +++ b/contests/management/commands/load_edits.py @@ -94,7 +94,7 @@ def get_category_articles(self, contest): "cmlimit": "max" } response = requests.get(contest.api_endpoint, params=categorymembers_api_params).json() - if not 'query' in response: + if 'query' not in response: return list_ list_.extend(response['query']['categorymembers']) diff --git a/contests/models.py b/contests/models.py index 2fcde0f..57d66e5 100644 --- a/contests/models.py +++ b/contests/models.py @@ -135,8 +135,6 @@ class Evaluation(models.Model): when = models.DateTimeField(auto_now_add=True) obs = models.TextField(blank=True, null=True) - def __str__(self): - return (f"{self.contest.name_id} - {self.diff.diff} - {self.evaluator.profile.username} - {self.status} - {self.when}") - + def __str__(self): name = self.evaluator.profile.username if self.evaluator else 'None' return (f"{self.contest.name_id} - {self.diff.diff} - {name} - {self.status} - {self.when}") \ No newline at end of file diff --git a/contests/templates/contest.html b/contests/templates/contest.html index bf41888..8789dcc 100644 --- a/contests/templates/contest.html +++ b/contests/templates/contest.html @@ -18,7 +18,10 @@ {% endif %} - +