Add friend submissions

This commit is contained in:
cuom1999 2023-02-12 21:35:48 -06:00
parent 3eda48f3ea
commit fc852d1bc7
8 changed files with 70 additions and 34 deletions

View file

@ -369,6 +369,10 @@ urlpatterns = [
r"^submissions/user/(?P<user>\w+)/",
paged_list_view(submission.AllUserSubmissions, "all_user_submissions"),
),
url(
r"^submissions/friends/",
paged_list_view(submission.AllFriendSubmissions, "all_friend_submissions"),
),
url(
r"^src/(?P<submission>\d+)/raw$",
submission.SubmissionSourceRaw.as_view(),

View file

@ -333,14 +333,12 @@ class Profile(models.Model):
def css_class(self):
return self.get_user_css_class(self.display_rank, self.rating)
def get_friends(self): # list of usernames, including you
friend_obj = self.following_users.all()
ret = set()
def get_friends(self): # list of ids, including you
friend_obj = self.following_users.prefetch_related("users")
ret = []
if friend_obj:
ret = set(friend.username for friend in friend_obj[0].users.all())
ret.add(self.username)
ret = [friend.id for friend in friend_obj[0].users.all()]
ret.append(self.id)
return ret
def can_edit_organization(self, org):
@ -395,7 +393,9 @@ class OrganizationRequest(models.Model):
class Friend(models.Model):
users = models.ManyToManyField(Profile)
current_user = models.ForeignKey(
Profile, related_name="following_users", on_delete=CASCADE
Profile,
related_name="following_users",
on_delete=CASCADE,
)
@classmethod

View file

@ -161,7 +161,9 @@ class ContestList(
i
for i in self.org_query
if i
in self.request.profile.organizations.values_list("id", flat=True)
in self.request.profile.organizations.values_list(
"id", flat=True
)
]
except ValueError:
pass
@ -1024,8 +1026,8 @@ def contest_ranking_ajax(request, contest, participation=None):
queryset = contest.users.filter(virtual__gte=0)
if request.GET.get("friend") == "true" and request.profile:
friends = list(request.profile.get_friends())
queryset = queryset.filter(user__user__username__in=friends)
friends = request.profile.get_friends()
queryset = queryset.filter(user_id__in=friends)
if request.GET.get("virtual") != "true":
queryset = queryset.filter(virtual=0)

View file

@ -439,6 +439,9 @@ class SubmissionsListBase(DiggPaginatorMixin, TitleMixin, ListView):
def get_my_submissions_page(self):
return None
def get_friend_submissions_page(self):
return None
def get_all_submissions_page(self):
return reverse("all_submissions")
@ -503,6 +506,7 @@ class SubmissionsListBase(DiggPaginatorMixin, TitleMixin, ListView):
)
context["first_page_href"] = (self.first_page_href or ".") + suffix
context["my_submissions_link"] = self.get_my_submissions_page()
context["friend_submissions_link"] = self.get_friend_submissions_page()
context["all_submissions_link"] = self.get_all_submissions_page()
context["page_type"] = self.page_type
@ -558,7 +562,21 @@ class ConditionalUserTabMixin(object):
return context
class AllUserSubmissions(ConditionalUserTabMixin, UserMixin, SubmissionsListBase):
class GeneralSubmissions(SubmissionsListBase):
def get_my_submissions_page(self):
if self.request.user.is_authenticated:
return reverse(
"all_user_submissions", kwargs={"user": self.request.user.username}
)
return None
def get_friend_submissions_page(self):
if self.request.user.is_authenticated:
return reverse("all_friend_submissions")
return None
class AllUserSubmissions(ConditionalUserTabMixin, UserMixin, GeneralSubmissions):
def _get_queryset(self):
return (
super(AllUserSubmissions, self)
@ -573,19 +591,13 @@ class AllUserSubmissions(ConditionalUserTabMixin, UserMixin, SubmissionsListBase
def get_content_title(self):
if self.request.user.is_authenticated and self.request.profile == self.profile:
return format_html("All my submissions")
return format_html(_("All my submissions"))
return format_html(
'All submissions by <a href="{1}">{0}</a>',
_('All submissions by <a href="{1}">{0}</a>'),
self.username,
reverse("user_page", args=[self.username]),
)
def get_my_submissions_page(self):
if self.request.user.is_authenticated:
return reverse(
"all_user_submissions", kwargs={"user": self.request.user.username}
)
def get_context_data(self, **kwargs):
context = super(AllUserSubmissions, self).get_context_data(**kwargs)
context["dynamic_update"] = context["page_obj"].number == 1
@ -594,6 +606,25 @@ class AllUserSubmissions(ConditionalUserTabMixin, UserMixin, SubmissionsListBase
return context
class AllFriendSubmissions(LoginRequiredMixin, GeneralSubmissions):
def _get_queryset(self):
friends = self.request.profile.get_friends()
return (
super(AllFriendSubmissions, self)
._get_queryset()
.filter(user_id__in=friends)
)
def get_title(self):
return _("All friend submissions")
def get_context_data(self, **kwargs):
context = super(AllFriendSubmissions, self).get_context_data(**kwargs)
context["dynamic_update"] = False
context["page_type"] = "friend_tab"
return context
class ProblemSubmissionsBase(SubmissionsListBase):
show_problem = False
dynamic_update = True
@ -772,15 +803,9 @@ def single_submission_query(request):
return single_submission(request, int(request.GET["id"]), bool(show_problem))
class AllSubmissions(SubmissionsListBase):
class AllSubmissions(GeneralSubmissions):
stats_update_interval = 3600
def get_my_submissions_page(self):
if self.request.user.is_authenticated:
return reverse(
"all_user_submissions", kwargs={"user": self.request.user.username}
)
def get_context_data(self, **kwargs):
context = super(AllSubmissions, self).get_context_data(**kwargs)
context["dynamic_update"] = (

View file

@ -449,8 +449,8 @@ class UserList(QueryStringSortMixin, DiggPaginatorMixin, TitleMixin, ListView):
filter_friend = False
def filter_friend_queryset(self, queryset):
friends = list(self.request.profile.get_friends())
ret = queryset.filter(user__username__in=friends)
friends = self.request.profile.get_friends()
ret = queryset.filter(id__in=friends)
return ret
def get_queryset(self):

View file

@ -2,7 +2,7 @@ msgid ""
msgstr ""
"Project-Id-Version: lqdoj2\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-02-09 10:57+0700\n"
"POT-Creation-Date: 2023-02-13 09:44+0700\n"
"PO-Revision-Date: 2021-07-20 03:44\n"
"Last-Translator: Icyene\n"
"Language-Team: Vietnamese\n"
@ -3261,7 +3261,7 @@ msgstr "Bài nộp của %(user)s cho bài %(problem)s"
msgid "All submissions"
msgstr "Tất cả bài nộp"
#: judge/views/submission.py:571
#: judge/views/submission.py:571 judge/views/submission.py:576
msgid "All my submissions"
msgstr "Tất cả bài nộp của tôi"
@ -3270,6 +3270,10 @@ msgstr "Tất cả bài nộp của tôi"
msgid "All submissions by %s"
msgstr "Tất cả bài nộp của %s"
#: judge/views/submission.py:578
msgid "All submissions by <a href=\"{1}\">{0}</a>"
msgstr "Tất cả bài nộp của <a href=\"{1}\">{0}</a>"
#: judge/views/submission.py:617
#, python-format
msgid "All submissions for %s"

View file

@ -1,7 +1,5 @@
{% extends "user/base-users-table.html" %}
{% set friends = request.profile.get_friends() if request.user.is_authenticated else {} %}
{% block after_rank_head %}
{% if has_rating %}
<th class="rating-column">{{ _('Rating') }}</th>

View file

@ -388,6 +388,9 @@
{% if page_type == 'user_submissions_tab' %}
{{ make_tab_item('user_submissions_tab', 'fa fa-user', None, _("%(user)s", user=tab_username)) }}
{% endif %}
{% if friend_submissions_link %}
{{ make_tab_item('friend_tab', 'fa fa-user', friend_submissions_link, _('Friends')) }}
{% endif %}
{% if perms.judge.change_submission %}
{{ make_tab_item('admin', 'fa fa-edit', url('admin:judge_submission_changelist'), _('Admin')) }}
{% endif %}