From d75a498d18afbbe087be8d0409bd5e4de118dec0 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Thu, 18 Jan 2024 12:33:36 -0600 Subject: [PATCH] Small optimizations --- judge/models/profile.py | 21 +++++++++++---------- judge/models/submission.py | 30 +++++++++++++++--------------- judge/views/blog.py | 1 - judge/views/submission.py | 1 - 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/judge/models/profile.py b/judge/models/profile.py index 7c5b59c..61c5d5f 100644 --- a/judge/models/profile.py +++ b/judge/models/profile.py @@ -255,17 +255,18 @@ class Profile(models.Model): @cache_wrapper(prefix="Pgbi2") def _get_basic_info(self): - profile_image_url = None - if self.profile_image: - profile_image_url = self.profile_image.url - return { - "first_name": self.user.first_name, - "last_name": self.user.last_name, + res = { "email": self.user.email, "username": self.user.username, "mute": self.mute, - "profile_image_url": profile_image_url, } + if self.user.first_name: + res["first_name"] = self.user.first_name + if self.user.last_name: + res["last_name"] = self.user.last_name + if self.profile_image: + res["profile_image_url"] = self.profile_image.url + return res @cached_property def _cached_info(self): @@ -283,11 +284,11 @@ class Profile(models.Model): @cached_property def first_name(self): - return self._cached_info["first_name"] + return self._cached_info.get("first_name", "") @cached_property def last_name(self): - return self._cached_info["last_name"] + return self._cached_info.get("last_name", "") @cached_property def email(self): @@ -299,7 +300,7 @@ class Profile(models.Model): @cached_property def profile_image_url(self): - return self._cached_info["profile_image_url"] + return self._cached_info.get("profile_image_url") @cached_property def count_unseen_notifications(self): diff --git a/judge/models/submission.py b/judge/models/submission.py index 00c5941..5bc6863 100644 --- a/judge/models/submission.py +++ b/judge/models/submission.py @@ -221,12 +221,6 @@ class Submission(models.Model): return self.get_id_secret(self.id) def is_accessible_by(self, profile): - from judge.utils.problems import ( - user_completed_ids, - user_tester_ids, - user_editable_ids, - ) - if not profile: return False @@ -236,15 +230,6 @@ class Submission(models.Model): if profile.id == self.user_id: return True - if problem_id in user_editable_ids(profile): - return True - - if self.problem_id in user_completed_ids(profile): - if self.problem.is_public: - return True - if problem_id in user_tester_ids(profile): - return True - if user.has_perm("judge.change_submission"): return True @@ -258,6 +243,21 @@ class Submission(models.Model): if contest and contest.is_editable_by(user): return True + from judge.utils.problems import ( + user_completed_ids, + user_tester_ids, + user_editable_ids, + ) + + if problem_id in user_editable_ids(profile): + return True + + if self.problem_id in user_completed_ids(profile): + if self.problem.is_public: + return True + if problem_id in user_tester_ids(profile): + return True + return False class Meta: diff --git a/judge/views/blog.py b/judge/views/blog.py index 0310e27..2e29127 100644 --- a/judge/views/blog.py +++ b/judge/views/blog.py @@ -23,7 +23,6 @@ from judge.models import ( from judge.models.profile import Organization, OrganizationProfile from judge.utils.cachedict import CacheDict from judge.utils.diggpaginator import DiggPaginator -from judge.utils.problems import user_completed_ids from judge.utils.tickets import filter_visible_tickets from judge.utils.views import TitleMixin from judge.views.feed import FeedView diff --git a/judge/views/submission.py b/judge/views/submission.py index 26300b0..7158c01 100644 --- a/judge/views/submission.py +++ b/judge/views/submission.py @@ -41,7 +41,6 @@ from judge.models import ProblemTranslation from judge.models import Profile from judge.models import Submission from judge.utils.problems import get_result_data -from judge.utils.problems import user_completed_ids, user_editable_ids, user_tester_ids from judge.utils.problem_data import get_problem_case from judge.utils.raw_sql import join_sql_subquery, use_straight_join from judge.utils.views import DiggPaginatorMixin