NDOJ/judge/views/blog.py

222 lines
7.8 KiB
Python
Raw Normal View History

2020-01-21 06:35:58 +00:00
from django.db.models import Count, Max, Q
from django.http import Http404
from django.urls import reverse
from django.utils import timezone
from django.utils.functional import lazy
from django.utils.translation import ugettext as _
from django.views.generic import ListView
2024-04-13 22:02:54 +00:00
from judge.views.comment import CommentedDetailView
from judge.views.pagevote import PageVoteDetailView
from judge.views.bookmark import BookMarkDetailView
2022-05-14 17:57:27 +00:00
from judge.models import (
BlogPost,
Comment,
Contest,
Language,
Problem,
ContestProblemClarification,
2022-05-14 17:57:27 +00:00
Profile,
Submission,
Ticket,
)
2022-10-17 20:48:07 +00:00
from judge.models.profile import Organization, OrganizationProfile
2020-01-21 06:35:58 +00:00
from judge.utils.cachedict import CacheDict
from judge.utils.diggpaginator import DiggPaginator
from judge.utils.tickets import filter_visible_tickets
from judge.utils.views import TitleMixin
2024-05-22 04:09:22 +00:00
from judge.utils.users import get_rating_rank, get_points_rank, get_awards
from judge.views.feed import FeedView
2020-01-21 06:35:58 +00:00
2022-03-21 21:09:16 +00:00
# General view for all content list on home feed
class HomeFeedView(FeedView):
2022-05-14 17:57:27 +00:00
template_name = "blog/list.html"
2020-01-21 06:35:58 +00:00
title = None
def get_context_data(self, **kwargs):
context = super(HomeFeedView, self).get_context_data(**kwargs)
2022-05-14 17:57:27 +00:00
context["has_clarifications"] = False
2020-01-21 06:35:58 +00:00
if self.request.user.is_authenticated:
participation = self.request.profile.current_contest
if participation:
clarifications = ContestProblemClarification.objects.filter(
problem__in=participation.contest.contest_problems.all()
2022-05-14 17:57:27 +00:00
)
context["has_clarifications"] = clarifications.count() > 0
context["clarifications"] = clarifications.order_by("-date")
2021-10-19 22:41:53 +00:00
if participation.contest.is_editable_by(self.request.user):
2022-05-14 17:57:27 +00:00
context["can_edit_contest"] = True
2022-03-21 21:09:16 +00:00
2020-01-21 06:35:58 +00:00
now = timezone.now()
2022-05-14 17:57:27 +00:00
visible_contests = (
2022-10-10 07:07:50 +00:00
Contest.get_visible_contests(self.request.user, show_own_contests_only=True)
2022-05-14 17:57:27 +00:00
.filter(is_visible=True)
.order_by("start_time")
)
2023-01-24 02:36:44 +00:00
if self.request.organization:
visible_contests = visible_contests.filter(
is_organization_private=True, organizations=self.request.organization
)
2022-10-17 20:48:07 +00:00
2022-05-14 17:57:27 +00:00
context["current_contests"] = visible_contests.filter(
start_time__lte=now, end_time__gt=now
)
context["future_contests"] = visible_contests.filter(start_time__gt=now)
context[
"recent_organizations"
] = OrganizationProfile.get_most_recent_organizations(self.request.profile)
2023-01-24 02:36:44 +00:00
profile_queryset = Profile.objects
if self.request.organization:
profile_queryset = self.request.organization.members
2024-04-13 22:02:54 +00:00
context["top_rated"] = (
profile_queryset.filter(is_unlisted=False)
.order_by("-rating")
.only("id", "rating")[:10]
)
context["top_scorer"] = (
profile_queryset.filter(is_unlisted=False)
.order_by("-performance_points")
.only("id", "performance_points")[:10]
)
Profile.prefetch_profile_cache([p.id for p in context["top_rated"]])
Profile.prefetch_profile_cache([p.id for p in context["top_scorer"]])
2024-05-22 04:09:22 +00:00
if self.request.user.is_authenticated:
context["rating_rank"] = get_rating_rank(self.request.profile)
context["points_rank"] = get_points_rank(self.request.profile)
medals_list = get_awards(self.request.profile)
context["awards"] = {
"medals": medals_list,
"gold_count": 0,
"silver_count": 0,
"bronze_count": 0,
}
for medal in medals_list:
if medal["ranking"] == 1:
context["awards"]["gold_count"] += 1
elif medal["ranking"] == 2:
context["awards"]["silver_count"] += 1
elif medal["ranking"] == 3:
context["awards"]["bronze_count"] += 1
2022-03-21 21:09:16 +00:00
return context
2022-05-14 17:57:27 +00:00
2022-03-21 21:09:16 +00:00
class PostList(HomeFeedView):
2022-03-21 21:09:16 +00:00
model = BlogPost
paginate_by = 4
2022-05-14 17:57:27 +00:00
context_object_name = "posts"
feed_content_template_name = "blog/content.html"
url_name = "blog_post_list"
2022-03-21 21:09:16 +00:00
def get_queryset(self):
2022-05-14 17:57:27 +00:00
queryset = (
BlogPost.objects.filter(visible=True, publish_on__lte=timezone.now())
.order_by("-sticky", "-publish_on")
2024-04-13 22:02:54 +00:00
.prefetch_related("organizations")
2022-05-14 17:57:27 +00:00
)
2022-05-31 04:35:30 +00:00
filter = Q(is_organization_private=False)
if self.request.user.is_authenticated:
filter |= Q(organizations__in=self.request.profile.organizations.all())
2023-01-24 02:36:44 +00:00
if self.request.organization:
filter &= Q(organizations=self.request.organization)
2022-05-31 04:35:30 +00:00
queryset = queryset.filter(filter)
2022-03-21 21:09:16 +00:00
return queryset
def get_context_data(self, **kwargs):
context = super(PostList, self).get_context_data(**kwargs)
2022-05-14 17:57:27 +00:00
context["title"] = (
self.title or _("Page %d of Posts") % context["page_obj"].number
)
context["page_type"] = "blog"
2022-03-21 21:09:16 +00:00
return context
class TicketFeed(HomeFeedView):
2022-03-21 21:09:16 +00:00
model = Ticket
2022-05-14 17:57:27 +00:00
context_object_name = "tickets"
paginate_by = 8
feed_content_template_name = "ticket/feed.html"
2022-03-21 21:09:16 +00:00
def get_queryset(self, is_own=True):
profile = self.request.profile
if is_own:
if self.request.user.is_authenticated:
2022-05-14 17:57:27 +00:00
return (
Ticket.objects.filter(
Q(user=profile) | Q(assignees__in=[profile]), is_open=True
)
.order_by("-id")
.prefetch_related("linked_item")
)
2022-03-21 21:09:16 +00:00
else:
return []
2020-01-21 06:35:58 +00:00
else:
2022-03-21 21:09:16 +00:00
# Superusers better be staffs, not the spell-casting kind either.
if self.request.user.is_staff:
2022-05-14 17:57:27 +00:00
tickets = (
Ticket.objects.order_by("-id")
.filter(is_open=True)
.prefetch_related("linked_item")
)
2022-03-21 21:09:16 +00:00
return filter_visible_tickets(tickets, self.request.user, profile)
else:
return []
def get_context_data(self, **kwargs):
context = super(TicketFeed, self).get_context_data(**kwargs)
2022-05-14 17:57:27 +00:00
context["page_type"] = "ticket"
context["title"] = _("Ticket feed")
2022-03-21 21:09:16 +00:00
return context
class CommentFeed(HomeFeedView):
2022-03-21 21:09:16 +00:00
model = Comment
2022-05-14 17:57:27 +00:00
context_object_name = "comments"
2024-04-27 01:40:49 +00:00
paginate_by = 15
feed_content_template_name = "comments/feed.html"
2022-03-21 21:09:16 +00:00
def get_queryset(self):
2023-01-24 02:36:44 +00:00
return Comment.most_recent(
self.request.user, 100, organization=self.request.organization
2023-01-24 02:36:44 +00:00
)
2022-03-21 21:09:16 +00:00
def get_context_data(self, **kwargs):
context = super(CommentFeed, self).get_context_data(**kwargs)
2022-05-14 17:57:27 +00:00
context["title"] = _("Comment feed")
context["page_type"] = "comment"
2020-01-21 06:35:58 +00:00
return context
2022-11-17 19:17:45 +00:00
class PostView(TitleMixin, CommentedDetailView, PageVoteDetailView, BookMarkDetailView):
2020-01-21 06:35:58 +00:00
model = BlogPost
2022-05-14 17:57:27 +00:00
pk_url_kwarg = "id"
context_object_name = "post"
template_name = "blog/blog.html"
2020-01-21 06:35:58 +00:00
def get_title(self):
return self.object.title
def get_context_data(self, **kwargs):
context = super(PostView, self).get_context_data(**kwargs)
2022-05-14 17:57:27 +00:00
context["og_image"] = self.object.og_image
2024-04-23 20:36:51 +00:00
context["editable_orgs"] = []
2022-10-25 04:59:04 +00:00
2024-04-23 20:36:51 +00:00
orgs = list(self.object.organizations.all())
2024-04-23 20:53:58 +00:00
if self.request.profile:
for org in orgs:
if self.request.profile.can_edit_organization(org):
context["editable_orgs"].append(org)
2022-10-17 16:04:39 +00:00
2020-01-21 06:35:58 +00:00
return context
def get_object(self, queryset=None):
post = super(PostView, self).get_object(queryset)
2024-04-13 22:02:54 +00:00
if not post.is_accessible_by(self.request.user):
2020-01-21 06:35:58 +00:00
raise Http404()
return post