Design organization list page and add organization search (#119)

This commit is contained in:
Phuoc Anh Kha Le 2024-06-18 22:11:36 -05:00 committed by GitHub
parent 02ba30a29e
commit 326b3d5dd3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 553 additions and 286 deletions

View file

@ -159,6 +159,8 @@ class ContestList(
def get_default_sort_order(self, request):
if request.GET.get("contest") and settings.ENABLE_FTS:
return "-relevance"
if self.current_tab == "future":
return "start_time"
return "-start_time"
@cached_property
@ -279,12 +281,24 @@ class ContestList(
return (
self._get_queryset()
.filter(start_time__gt=self._now)
.order_by("start_time", "key")
.order_by(self.order, "key")
)
def _get_active_participations_queryset(self):
active_contests = self._get_queryset().filter(id__in=self._active_contests_ids)
return self._active_participations().filter(contest_id__in=active_contests)
active_contests = (
self._get_queryset()
.filter(id__in=self._active_contests_ids)
.order_by(self.order, "key")
)
ordered_ids = list(active_contests.values_list("id", flat=True))
participations = self._active_participations().filter(
contest_id__in=ordered_ids
)
participations = sorted(
participations, key=lambda p: ordered_ids.index(p.contest_id)
)
return participations
def get_queryset(self):
if self.current_tab == "past":
@ -303,7 +317,7 @@ class ContestList(
context["current_count"] = self._get_current_contests_queryset().count()
context["future_count"] = self._get_future_contests_queryset().count()
context["active_count"] = self._get_active_participations_queryset().count()
context["active_count"] = len(self._get_active_participations_queryset())
context["now"] = self._now
context["first_page_href"] = "."

View file

@ -238,33 +238,87 @@ class OrganizationHomeView(OrganizationMixin):
return context
class OrganizationList(TitleMixin, ListView, OrganizationBase):
class OrganizationList(
QueryStringSortMixin, DiggPaginatorMixin, TitleMixin, ListView, OrganizationBase
):
model = Organization
context_object_name = "organizations"
template_name = "organization/list.html"
title = gettext_lazy("Groups")
paginate_by = 12
all_sorts = frozenset(("name", "member_count"))
default_desc = frozenset(("name", "member_count"))
def get_queryset(self):
return (
def get_default_sort_order(self, request):
return "-member_count"
def get(self, request, *args, **kwargs):
default_tab = "mine"
if not self.request.user.is_authenticated:
default_tab = "public"
self.current_tab = self.request.GET.get("tab", default_tab)
self.organization_query = request.GET.get("organization", "")
return super(OrganizationList, self).get(request, *args, **kwargs)
def _get_queryset(self):
queryset = (
super(OrganizationList, self)
.get_queryset()
.annotate(member_count=Count("member"))
.defer("about")
)
def get_context_data(self, **kwargs):
context = super(OrganizationList, self).get_context_data(**kwargs)
context["my_organizations"] = []
context["page_type"] = "organizations"
if self.organization_query:
queryset = queryset.filter(
Q(slug__icontains=self.organization_query)
| Q(name__icontains=self.organization_query)
| Q(short_name__icontains=self.organization_query)
)
return queryset
def get_queryset(self):
organization_list = self._get_queryset()
my_organizations = []
if self.request.profile:
context["my_organizations"] = context["organizations"].filter(
my_organizations = organization_list.filter(
id__in=self.request.profile.organizations.values("id")
)
other_organizations = context["organizations"].exclude(
id__in=context["my_organizations"]
)
context["open_organizations"] = other_organizations.filter(is_open=True)
context["private_organizations"] = other_organizations.filter(is_open=False)
if self.current_tab == "public":
queryset = organization_list.exclude(id__in=my_organizations).filter(
is_open=True
)
elif self.current_tab == "private":
queryset = organization_list.exclude(id__in=my_organizations).filter(
is_open=False
)
else:
queryset = my_organizations
if queryset:
queryset = queryset.order_by(self.order)
return queryset
def get_context_data(self, **kwargs):
context = super(OrganizationList, self).get_context_data(**kwargs)
context["first_page_href"] = "."
context["current_tab"] = self.current_tab
context["page_type"] = self.current_tab
context["organization_query"] = self.organization_query
context["selected_order"] = self.request.GET.get("order")
context["all_sort_options"] = [
("name", _("Name (asc.)")),
("-name", _("Name (desc.)")),
("member_count", _("Member count (asc.)")),
("-member_count", _("Member count (desc.)")),
]
context.update(self.get_sort_context())
context.update(self.get_sort_paginate_context())
return context