Add friend submissions
This commit is contained in:
parent
3eda48f3ea
commit
fc852d1bc7
8 changed files with 70 additions and 34 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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"] = (
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue