This commit is contained in:
cuom1999 2023-07-06 22:39:16 +07:00
parent 1595063463
commit 1ca0d51f67
7 changed files with 41 additions and 41 deletions

View file

@ -156,7 +156,7 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
target_comment = None target_comment = None
if "comment-id" in request.GET: if "comment-id" in request.GET:
comment_id = int(request.GET["comment-id"]) comment_id = int(request.GET["comment-id"])
try: try:
comment_obj = Comment.objects.get(id=comment_id) comment_obj = Comment.objects.get(id=comment_id)
@ -179,9 +179,7 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
queryset.select_related("author__user") queryset.select_related("author__user")
.filter(hidden=False) .filter(hidden=False)
.defer("author__about") .defer("author__about")
.annotate( .annotate(revisions=Count("versions", distinct=True))
revisions=Count("versions", distinct=True)
)
) )
else: else:
queryset = self.object.comments queryset = self.object.comments
@ -203,7 +201,7 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
"votes", condition=Q(votes__voter_id=profile.id) "votes", condition=Q(votes__voter_id=profile.id)
), ),
).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0))) ).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0)))
return queryset return queryset
def get_context_data(self, target_comment=None, **kwargs): def get_context_data(self, target_comment=None, **kwargs):
@ -211,9 +209,9 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
queryset = self._get_queryset(target_comment) queryset = self._get_queryset(target_comment)
comment_count = self.object.comments.filter(parent=None, hidden=False).count() comment_count = self.object.comments.filter(parent=None, hidden=False).count()
context["target_comment"] = -1 context["target_comment"] = -1
if (target_comment != None): if target_comment != None:
context["target_comment"] = target_comment.id context["target_comment"] = target_comment.id
if self.request.user.is_authenticated: if self.request.user.is_authenticated:
context["is_new_user"] = ( context["is_new_user"] = (
not self.request.user.is_staff not self.request.user.is_staff
@ -229,7 +227,7 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
context["vote_hide_threshold"] = settings.DMOJ_COMMENT_VOTE_HIDE_THRESHOLD context["vote_hide_threshold"] = settings.DMOJ_COMMENT_VOTE_HIDE_THRESHOLD
if queryset.exists(): if queryset.exists():
context["comment_root_id"] = queryset[0].id context["comment_root_id"] = queryset[0].id
else: else:
context["comment_root_id"] = 0 context["comment_root_id"] = 0
context["comment_parent_none"] = 1 context["comment_parent_none"] = 1
if target_comment != None: if target_comment != None:
@ -238,7 +236,7 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
else: else:
context["offset"] = DEFAULT_OFFSET context["offset"] = DEFAULT_OFFSET
context["comment_more"] = comment_count - DEFAULT_OFFSET context["comment_more"] = comment_count - DEFAULT_OFFSET
context["limit"] = DEFAULT_OFFSET context["limit"] = DEFAULT_OFFSET
context["comment_count"] = comment_count context["comment_count"] = comment_count
return context return context

View file

@ -56,7 +56,7 @@ class Comment(MPTTModel):
related_name="replies", related_name="replies",
on_delete=CASCADE, on_delete=CASCADE,
) )
versions = VersionRelation() versions = VersionRelation()
class Meta: class Meta:
@ -112,7 +112,7 @@ class Comment(MPTTModel):
if len(output) >= n: if len(output) >= n:
return output return output
return output return output
@cached_property @cached_property
def get_replies(self): def get_replies(self):
query = Comment.filter(parent=self) query = Comment.filter(parent=self)

View file

@ -68,7 +68,7 @@ class Course(models.Model):
return False return False
@classmethod @classmethod
def is_accessible_by(cls,course, profile): def is_accessible_by(cls, course, profile):
userqueryset = CourseRole.objects.filter(course=course, user=profile) userqueryset = CourseRole.objects.filter(course=course, user=profile)
if userqueryset.exists(): if userqueryset.exists():
return True return True
@ -76,29 +76,29 @@ class Course(models.Model):
return False return False
@classmethod @classmethod
def get_students(cls,course): def get_students(cls, course):
return CourseRole.objects.filter(course=course, role="ST").values("user") return CourseRole.objects.filter(course=course, role="ST").values("user")
@classmethod @classmethod
def get_assistants(cls,course): def get_assistants(cls, course):
return CourseRole.objects.filter(course=course, role="AS").values("user") return CourseRole.objects.filter(course=course, role="AS").values("user")
@classmethod @classmethod
def get_teachers(cls,course): def get_teachers(cls, course):
return CourseRole.objects.filter(course=course, role="TE").values("user") return CourseRole.objects.filter(course=course, role="TE").values("user")
@classmethod @classmethod
def add_student(cls,course, profiles): def add_student(cls, course, profiles):
for profile in profiles: for profile in profiles:
CourseRole.make_role(course=course, user=profile, role="ST") CourseRole.make_role(course=course, user=profile, role="ST")
@classmethod @classmethod
def add_teachers(cls,course, profiles): def add_teachers(cls, course, profiles):
for profile in profiles: for profile in profiles:
CourseRole.make_role(course=course, user=profile, role="TE") CourseRole.make_role(course=course, user=profile, role="TE")
@classmethod @classmethod
def add_assistants(cls,course, profiles): def add_assistants(cls, course, profiles):
for profile in profiles: for profile in profiles:
CourseRole.make_role(course=course, user=profile, role="AS") CourseRole.make_role(course=course, user=profile, role="AS")

View file

@ -128,19 +128,19 @@ def get_comments(request, limit=10):
offset = 0 offset = 0
if "offset" in request.GET: if "offset" in request.GET:
offset = int(request.GET["offset"]) offset = int(request.GET["offset"])
target_comment = -1 target_comment = -1
if "target_comment" in request.GET: if "target_comment" in request.GET:
target_comment = int(request.GET["target_comment"]) target_comment = int(request.GET["target_comment"])
comment_root_id = 0 comment_root_id = 0
if comment_id: if comment_id:
comment_obj = Comment.objects.get(pk=comment_id) comment_obj = Comment.objects.get(pk=comment_id)
comment_root_id = comment_obj.id comment_root_id = comment_obj.id
else: else:
comment_obj = None comment_obj = None
queryset = comment_obj.linked_object.comments queryset = comment_obj.linked_object.comments
if parent_none: if parent_none:
queryset = queryset.filter(parent=None, hidden=False) queryset = queryset.filter(parent=None, hidden=False)
@ -152,35 +152,33 @@ def get_comments(request, limit=10):
queryset.select_related("author__user") queryset.select_related("author__user")
.defer("author__about") .defer("author__about")
.annotate( .annotate(
count_replies=Count("replies", distinct=True), count_replies=Count("replies", distinct=True),
revisions=Count("versions", distinct=True), revisions=Count("versions", distinct=True),
)[offset:offset+limit] )[offset : offset + limit]
) )
if request.user.is_authenticated: if request.user.is_authenticated:
profile = request.profile profile = request.profile
queryset = queryset.annotate( queryset = queryset.annotate(
my_vote=FilteredRelation( my_vote=FilteredRelation("votes", condition=Q(votes__voter_id=profile.id)),
"votes", condition=Q(votes__voter_id=profile.id)
),
).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0))) ).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0)))
new_offset = offset + min(len(queryset), limit) new_offset = offset + min(len(queryset), limit)
comment_html = loader.render_to_string( comment_html = loader.render_to_string(
"comments/content-list.html", "comments/content-list.html",
{ {
"request": request, "request": request,
"comment_root_id": comment_root_id, "comment_root_id": comment_root_id,
"comment_list": queryset, "comment_list": queryset,
"vote_hide_threshold" : settings.DMOJ_COMMENT_VOTE_HIDE_THRESHOLD, "vote_hide_threshold": settings.DMOJ_COMMENT_VOTE_HIDE_THRESHOLD,
"perms": PermWrapper(request.user), "perms": PermWrapper(request.user),
"offset": new_offset, "offset": new_offset,
"limit": limit, "limit": limit,
"comment_count": comment_count, "comment_count": comment_count,
"comment_parent_none": parent_none, "comment_parent_none": parent_none,
"target_comment": target_comment, "target_comment": target_comment,
"comment_more": comment_count - new_offset "comment_more": comment_count - new_offset,
} },
) )
return HttpResponse(comment_html) return HttpResponse(comment_html)

View file

@ -10,22 +10,25 @@ __all__ = [
"CourseStudentResults", "CourseStudentResults",
"CourseEdit", "CourseEdit",
"CourseResourceDetailEdit", "CourseResourceDetailEdit",
"CourseResourceEdit", "CourseResourceEdit",
] ]
course_directory_file = "" course_directory_file = ""
class CourseListMixin(object): class CourseListMixin(object):
def get_queryset(self): def get_queryset(self):
return Course.objects.filter(is_open = "true").values() return Course.objects.filter(is_open="true").values()
class CourseList(ListView): class CourseList(ListView):
model = Course model = Course
template_name = "course/list.html" template_name = "course/list.html"
queryset = Course.objects.filter(is_public=True).filter(is_open=True) queryset = Course.objects.filter(is_public=True).filter(is_open=True)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(CourseList,self).get_context_data(**kwargs) context = super(CourseList, self).get_context_data(**kwargs)
available , enrolling = [] , [] available, enrolling = [], []
for course in Course.objects.filter(is_public=True).filter(is_open=True): for course in Course.objects.filter(is_public=True).filter(is_open=True):
if Course.is_accessible_by(course, self.request.profile): if Course.is_accessible_by(course, self.request.profile):
enrolling.append(course) enrolling.append(course)
@ -34,4 +37,3 @@ class CourseList(ListView):
context["available"] = available context["available"] = available
context["enrolling"] = enrolling context["enrolling"] = enrolling
return context return context

View file

@ -441,7 +441,7 @@ class OrganizationSubmissions(
LoginRequiredMixin, MemberOrganizationMixin, SubmissionsListBase LoginRequiredMixin, MemberOrganizationMixin, SubmissionsListBase
): ):
template_name = "organization/submissions.html" template_name = "organization/submissions.html"
@cached_property @cached_property
def in_contest(self): def in_contest(self):
return False return False

View file

@ -351,7 +351,9 @@ class SubmissionsListBase(DiggPaginatorMixin, TitleMixin, ListView):
def _get_entire_queryset(self): def _get_entire_queryset(self):
organization = self.organization or self.request.organization organization = self.organization or self.request.organization
if organization: if organization:
queryset = Submission.objects.filter(contest_object__organizations=organization) queryset = Submission.objects.filter(
contest_object__organizations=organization
)
else: else:
queryset = Submission.objects.all() queryset = Submission.objects.all()
use_straight_join(queryset) use_straight_join(queryset)