Skip to content

Commit

Permalink
Change comment style (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaospei authored May 22, 2023
1 parent d80ec96 commit 1056a47
Show file tree
Hide file tree
Showing 19 changed files with 678 additions and 304 deletions.
2 changes: 2 additions & 0 deletions dmoj/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ def paged_list_view(view, name, **kwargs):
url(r"^comments/upvote/$", comment.upvote_comment, name="comment_upvote"),
url(r"^comments/downvote/$", comment.downvote_comment, name="comment_downvote"),
url(r"^comments/hide/$", comment.comment_hide, name="comment_hide"),
url(r"^comments/get_replies/$", comment.get_replies, name="comment_get_replies"),
url(r"^comments/show_more/$", comment.get_show_more, name="comment_show_more"),
url(
r"^comments/(?P<id>\d+)/",
include(
Expand Down
85 changes: 69 additions & 16 deletions judge/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
HttpResponseForbidden,
HttpResponseNotFound,
HttpResponseRedirect,
Http404,
)
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
Expand Down Expand Up @@ -151,40 +152,92 @@ def post(self, request, *args, **kwargs):
return self.render_to_response(context)

def get(self, request, *args, **kwargs):
pre_query = None
if "comment-id" in request.GET:
comment_id = int(request.GET["comment-id"])
try:
comment_obj = Comment.objects.get(pk=comment_id)
except Comment.DoesNotExist:
raise Http404
pre_query = comment_obj
while comment_obj is not None:
pre_query = comment_obj
comment_obj = comment_obj.parent
self.object = self.get_object()
return self.render_to_response(
self.get_context_data(
object=self.object,
pre_query=pre_query,
comment_form=CommentForm(request, initial={"parent": None}),
)
)

def get_context_data(self, **kwargs):
def get_context_data(self, pre_query=None, **kwargs):
context = super(CommentedDetailView, self).get_context_data(**kwargs)
queryset = self.object.comments
context["has_comments"] = queryset.exists()
context["comment_lock"] = self.is_comment_locked()
queryset = (
queryset.select_related("author__user")
.filter(hidden=False)
.defer("author__about")
.annotate(revisions=Count("versions"))
)

queryset = queryset.filter(parent=None, hidden=False)
queryset_all = None
comment_count = len(queryset)
context["comment_remove"] = -1
if (pre_query != None):
comment_remove = pre_query.id
queryset_all = pre_query.get_descendants(include_self=True)
queryset_all = (
queryset_all.select_related("author__user")
.filter(hidden=False)
.defer("author__about")
.annotate(revisions=Count("versions", distinct=True))
)
context["comment_remove"] = comment_remove
else:
queryset = (
queryset.select_related("author__user")
.defer("author__about")
.filter(hidden=False)
.annotate(
count_replies=Count("replies", distinct=True),
revisions=Count("versions", distinct=True),
)[:10]
)

if self.request.user.is_authenticated:
profile = self.request.profile
queryset = queryset.annotate(
my_vote=FilteredRelation(
"votes", condition=Q(votes__voter_id=profile.id)
),
).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0)))
if (pre_query != None):
queryset_all = queryset_all.annotate(
my_vote=FilteredRelation(
"votes", condition=Q(votes__voter_id=profile.id)
),
).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0)))
else:
queryset = queryset.annotate(
my_vote=FilteredRelation(
"votes", condition=Q(votes__voter_id=profile.id)
),
).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0)))

context["is_new_user"] = (
not self.request.user.is_staff
and not profile.submission_set.filter(
points=F("problem__points")
).exists()
)

context["has_comments"] = queryset.exists()
context["comment_lock"] = self.is_comment_locked()
context["comment_list"] = queryset
context["comment_count"] = len(queryset)
context["comment_all_list"] = queryset_all

context["vote_hide_threshold"] = settings.DMOJ_COMMENT_VOTE_HIDE_THRESHOLD
if queryset.exists():
context["comment_root_id"] = queryset[0].id
else:
context["comment_root_id"] = 0
context["comment_parrent_none"] = 1
if (pre_query != None):
context["offset"] = 1
else:
context["offset"] = 10

context["limit"] = 10
context["comment_count"] = comment_count
return context
12 changes: 11 additions & 1 deletion judge/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Comment(MPTTModel):
related_name="replies",
on_delete=CASCADE,
)

versions = VersionRelation()

class Meta:
Expand Down Expand Up @@ -111,6 +112,15 @@ def most_recent(cls, user, n, batch=None, organization=None):
if len(output) >= n:
return output
return output

@cached_property
def get_replies(self):
query = Comment.filter(parent=self)
return len(query)

@cached_property
def get_revisions(self):
return self.versions.count()

@cached_property
def page_title(self):
Expand Down Expand Up @@ -141,7 +151,7 @@ def link(self):
)

def get_absolute_url(self):
return "%s#comment-%d" % (self.link, self.id)
return "%s?comment-id=%d#comment-%d" % (self.link, self.id, self.id)


class CommentVote(models.Model):
Expand Down
84 changes: 81 additions & 3 deletions judge/views/comment.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from django.conf import settings

from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.contrib.auth.context_processors import PermWrapper
from django.core.exceptions import PermissionDenied
from django.db import IntegrityError, transaction
from django.db.models import F
from django.db.models import Q, F, Count, FilteredRelation
from django.db.models.functions import Coalesce
from django.db.models.expressions import F, Value
from django.forms.models import ModelForm
from django.http import (
Http404,
Expand All @@ -15,11 +20,12 @@
from django.views.decorators.http import require_POST
from django.views.generic import DetailView, UpdateView
from django.urls import reverse_lazy
from django.template import loader
from reversion import revisions
from reversion.models import Version

from judge.dblock import LockModel
from judge.models import Comment, CommentVote, Notification
from judge.models import Comment, CommentVote, Notification, BlogPost
from judge.utils.views import TitleMixin
from judge.widgets import MathJaxPagedownWidget, HeavyPreviewPageDownWidget
from judge.comments import add_mention_notifications, del_mention_notifications
Expand All @@ -36,6 +42,11 @@


@login_required

# def get_more_reply(request, id):
# queryset = Comment.get_pk(id)


def vote_comment(request, delta):
if abs(delta) != 1:
return HttpResponseBadRequest(
Expand Down Expand Up @@ -99,10 +110,77 @@ def vote_comment(request, delta):
def upvote_comment(request):
return vote_comment(request, 1)


def downvote_comment(request):
return vote_comment(request, -1)

def get_comments(request, limit=10):
try:
comment_id = int(request.GET["id"])
parrent_none = int(request.GET["parrent_none"])
except ValueError:
return HttpResponseBadRequest()
else:
if comment_id and not Comment.objects.filter(id=comment_id).exists():
raise Http404()
offset = 0
if "offset" in request.GET:
offset = int(request.GET["offset"])
comment_remove = -1
if "comment_remove" in request.GET:
comment_remove = int(request.GET["comment_remove"])
comment_root_id = 0
if (comment_id):
comment_obj = Comment.objects.get(pk=comment_id)
comment_root_id = comment_obj.id
else:
comment_obj = None
queryset = comment_obj.linked_object.comments
if parrent_none:
queryset = queryset.filter(parent=None, hidden=False)
if (comment_remove != -1):
queryset.get(pk=comment_remove).delete()
else:
queryset = queryset.filter(parent=comment_obj, hidden=False)
comment_count = len(queryset)
queryset = (
queryset.select_related("author__user")
.defer("author__about")
.annotate(
count_replies=Count("replies", distinct=True),
revisions=Count("versions", distinct=True),
)[offset:offset+limit]
)
if request.user.is_authenticated:
profile = request.profile
queryset = queryset.annotate(
my_vote=FilteredRelation(
"votes", condition=Q(votes__voter_id=profile.id)
),
).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0)))

comment_html = loader.render_to_string(
"comments/content-list.html",
{
"request": request,
"comment_root_id": comment_root_id,
"comment_list": queryset,
"vote_hide_threshold" : settings.DMOJ_COMMENT_VOTE_HIDE_THRESHOLD,
"perms": PermWrapper(request.user),
"offset": offset + min(len(queryset), limit),
"limit": limit,
"comment_count": comment_count,
"comment_parrent_none": parrent_none,
"comment_remove": comment_remove,
}
)

return HttpResponse(comment_html)

def get_show_more(request):
return get_comments(request)

def get_replies(request):
return get_comments(request)

class CommentMixin(object):
model = Comment
Expand Down
Loading

0 comments on commit 1056a47

Please sign in to comment.