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
|
|
|
|
|
|
|
|
from judge.comments import CommentedDetailView
|
2022-05-14 17:57:27 +00:00
|
|
|
from judge.models import (
|
|
|
|
BlogPost,
|
|
|
|
Comment,
|
|
|
|
Contest,
|
|
|
|
Language,
|
|
|
|
Problem,
|
2022-10-13 02:19:22 +00:00
|
|
|
ContestProblemClarification,
|
2022-05-14 17:57:27 +00:00
|
|
|
Profile,
|
|
|
|
Submission,
|
|
|
|
Ticket,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-03-21 21:09:16 +00:00
|
|
|
# General view for all content list on home feed
|
|
|
|
class FeedView(ListView):
|
2022-05-14 17:57:27 +00:00
|
|
|
template_name = "blog/list.html"
|
2020-01-21 06:35:58 +00:00
|
|
|
title = None
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
def get_paginator(
|
|
|
|
self, queryset, per_page, orphans=0, allow_empty_first_page=True, **kwargs
|
|
|
|
):
|
|
|
|
return DiggPaginator(
|
|
|
|
queryset,
|
|
|
|
per_page,
|
|
|
|
body=6,
|
|
|
|
padding=2,
|
|
|
|
orphans=orphans,
|
|
|
|
allow_empty_first_page=allow_empty_first_page,
|
|
|
|
**kwargs
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2022-03-21 21:09:16 +00:00
|
|
|
context = super(FeedView, 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:
|
2022-10-13 02:19:22 +00:00
|
|
|
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
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
context["page_titles"] = CacheDict(lambda page: Comment.get_page_title(page))
|
2022-03-21 21:09:16 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
context["user_count"] = lazy(Profile.objects.count, int, int)
|
|
|
|
context["problem_count"] = lazy(
|
|
|
|
Problem.objects.filter(is_public=True).count, int, int
|
|
|
|
)
|
|
|
|
context["submission_count"] = lazy(Submission.objects.count, int, int)
|
|
|
|
context["language_count"] = lazy(Language.objects.count, int, int)
|
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")
|
|
|
|
)
|
|
|
|
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["top_rated"] = Profile.objects.filter(is_unlisted=False).order_by(
|
|
|
|
"-rating"
|
|
|
|
)[:10]
|
|
|
|
context["top_scorer"] = Profile.objects.filter(is_unlisted=False).order_by(
|
|
|
|
"-performance_points"
|
|
|
|
)[:10]
|
2022-04-01 01:33:37 +00:00
|
|
|
|
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(FeedView):
|
|
|
|
model = BlogPost
|
|
|
|
paginate_by = 10
|
2022-05-14 17:57:27 +00:00
|
|
|
context_object_name = "posts"
|
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")
|
|
|
|
.prefetch_related("authors__user", "organizations")
|
|
|
|
)
|
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())
|
|
|
|
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["first_page_href"] = reverse("home")
|
|
|
|
context["page_prefix"] = reverse("blog_post_list")
|
|
|
|
context["page_type"] = "blog"
|
|
|
|
context["post_comment_counts"] = {
|
|
|
|
int(page[2:]): count
|
|
|
|
for page, count in Comment.objects.filter(
|
|
|
|
page__in=["b:%d" % post.id for post in context["posts"]], hidden=False
|
|
|
|
)
|
|
|
|
.values_list("page")
|
|
|
|
.annotate(count=Count("page"))
|
|
|
|
.order_by()
|
2022-03-21 21:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class TicketFeed(FeedView):
|
|
|
|
model = Ticket
|
2022-05-14 17:57:27 +00:00
|
|
|
context_object_name = "tickets"
|
2022-03-21 21:09:16 +00:00
|
|
|
paginate_by = 30
|
|
|
|
|
|
|
|
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")
|
|
|
|
.select_related("user__user")
|
|
|
|
)
|
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")
|
|
|
|
.select_related("user__user")
|
|
|
|
)
|
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["first_page_href"] = self.request.path
|
|
|
|
context["page_prefix"] = "?page="
|
|
|
|
context["title"] = _("Ticket feed")
|
2022-03-21 21:09:16 +00:00
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class CommentFeed(FeedView):
|
|
|
|
model = Comment
|
2022-05-14 17:57:27 +00:00
|
|
|
context_object_name = "comments"
|
2022-03-21 21:09:16 +00:00
|
|
|
paginate_by = 50
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Comment.most_recent(self.request.user, 1000)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(CommentFeed, self).get_context_data(**kwargs)
|
2022-05-14 17:57:27 +00:00
|
|
|
context["page_type"] = "comment"
|
|
|
|
context["first_page_href"] = self.request.path
|
|
|
|
context["page_prefix"] = "?page="
|
|
|
|
context["title"] = _("Comment feed")
|
2021-07-19 01:22:44 +00:00
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class PostView(TitleMixin, CommentedDetailView):
|
|
|
|
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_comment_page(self):
|
2022-05-14 17:57:27 +00:00
|
|
|
return "b:%s" % self.object.id
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
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
|
2022-10-17 18:06:50 +00:00
|
|
|
context["valid_user_to_show_edit"] = False
|
|
|
|
context["valid_org_to_show_edit"] = []
|
|
|
|
|
|
|
|
if self.request.profile in self.object.authors.all():
|
|
|
|
context["valid_user_to_show_edit"] = True
|
|
|
|
|
|
|
|
for valid_org_to_show_edit in self.object.organizations.all():
|
|
|
|
if self.request.profile in valid_org_to_show_edit.admins.all():
|
|
|
|
context["valid_user_to_show_edit"] = True
|
|
|
|
|
|
|
|
if context["valid_user_to_show_edit"]:
|
2022-10-17 16:04:39 +00:00
|
|
|
for post_org in self.object.organizations.all():
|
2022-10-17 18:06:50 +00:00
|
|
|
if post_org in self.request.profile.organizations.all():
|
|
|
|
context["valid_org_to_show_edit"].append(post_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)
|
|
|
|
if not post.can_see(self.request.user):
|
|
|
|
raise Http404()
|
|
|
|
return post
|