Skip to content

Commit

Permalink
Merge pull request #411 from ReYaNOW/only-staff-filter
Browse files Browse the repository at this point in the history
[#182] Добавить фильтр на списках "учитывать вклад пользователей Hexlet"
  • Loading branch information
sgmdlt authored May 29, 2024
2 parents 9711f5c + a770a01 commit a3c875c
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 72 deletions.
1 change: 0 additions & 1 deletion contributors/forms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from contributors.forms.admin_forms import OrgNamesForm, RepoNamesForm
from contributors.forms.forms import TableSortSearchForm
44 changes: 44 additions & 0 deletions contributors/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,50 @@ def helper(self):
return helper


class LeaderboardCombinedSearchForm(TableSortSearchForm):
"""Search form of contributors by organization."""

organizations = forms.CharField(
label=False,
required=False,
widget=forms.TextInput(),
help_text=_("Exact match required for this field"),
)

sample = forms.ChoiceField(
required=False,
label='',
choices=(
('all', _('All users')),
('except_staff', _('Except staff')),
('only_staff', _('Only staff')),
),
)

@property
def helper(self):
"""Control form attributes and its layout."""
helper = FormHelper()
helper.form_method = 'get'
helper.form_class = 'd-flex'
helper.layout = Layout(
Field('sample', placeholder='Sample'),
Field('search', placeholder=_("Filter by name")),
FieldWithButtons(
Field(
'organizations',
placeholder=_("Filter by organization"),
),
StrictButton(
_("Search"),
type='submit',
css_class='btn btn-outline-primary',
),
),
)
return helper


class NameStatusFilterForm(TableSortSearchForm):
"""Search form of issues by their status."""

Expand Down
32 changes: 9 additions & 23 deletions contributors/views/leaderboard_commits.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from django.views import generic

from contributors.forms.forms import CombinedSearchForm
from contributors.models import Contributor
from contributors.views.mixins import TableSortSearchAndPaginationMixin
from contributors.views.mixins import (
LeaderboardQueryMixin,
TableSortSearchAndPaginationMixin,
)


class ListView(TableSortSearchAndPaginationMixin, generic.ListView):
class ListView(
LeaderboardQueryMixin,
TableSortSearchAndPaginationMixin,
generic.ListView,
):
"""List of leaders among contributors by commits."""

queryset = Contributor.objects.visible().with_contributions()
Expand All @@ -15,23 +21,3 @@ class ListView(TableSortSearchAndPaginationMixin, generic.ListView):
)
searchable_fields = ('login', 'name')
paginate_by = 100

def get_context_data(self, **kwargs):
"""Get search form by organizations."""
context = super().get_context_data(**kwargs)
context['form_org'] = CombinedSearchForm(self.request.GET)
return context

def get_queryset(self): # noqa: WPS615
"""Get filter queryset."""
queryset = super().get_queryset().prefetch_related(
'contributors__organization',
)
form = CombinedSearchForm(self.request.GET)
if form.is_valid():
organizations = form.cleaned_data['organizations']
if organizations:
queryset = queryset.filter(
contributors__organization__name__exact=organizations,
)
return queryset
32 changes: 9 additions & 23 deletions contributors/views/leaderboard_issues.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from django.views import generic

from contributors.forms.forms import CombinedSearchForm
from contributors.models import Contributor
from contributors.views.mixins import TableSortSearchAndPaginationMixin
from contributors.views.mixins import (
LeaderboardQueryMixin,
TableSortSearchAndPaginationMixin,
)


class ListView(TableSortSearchAndPaginationMixin, generic.ListView):
class ListView(
LeaderboardQueryMixin,
TableSortSearchAndPaginationMixin,
generic.ListView,
):
"""List of leaders among contributors by issues."""

queryset = Contributor.objects.visible().with_contributions()
Expand All @@ -15,23 +21,3 @@ class ListView(TableSortSearchAndPaginationMixin, generic.ListView):
)
searchable_fields = ('login', 'name')
paginate_by = 100

def get_context_data(self, **kwargs):
"""Get search form by organizations."""
context = super().get_context_data(**kwargs)
context['form_org'] = CombinedSearchForm(self.request.GET)
return context

def get_queryset(self): # noqa: WPS615
"""Get filter queryset."""
queryset = super().get_queryset().prefetch_related(
'contributors__organization',
)
form = CombinedSearchForm(self.request.GET)
if form.is_valid():
organizations = form.cleaned_data['organizations']
if organizations:
queryset = queryset.filter(
contributors__organization__name__exact=organizations,
)
return queryset
32 changes: 9 additions & 23 deletions contributors/views/leaderboard_prs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from django.views import generic

from contributors.forms.forms import CombinedSearchForm
from contributors.models import Contributor
from contributors.views.mixins import TableSortSearchAndPaginationMixin
from contributors.views.mixins import (
LeaderboardQueryMixin,
TableSortSearchAndPaginationMixin,
)


class ListView(TableSortSearchAndPaginationMixin, generic.ListView):
class ListView(
LeaderboardQueryMixin,
TableSortSearchAndPaginationMixin,
generic.ListView,
):
"""List of leaders among contributors by pull requests."""

queryset = Contributor.objects.visible().with_contributions()
Expand All @@ -16,23 +22,3 @@ class ListView(TableSortSearchAndPaginationMixin, generic.ListView):

searchable_fields = ('login', 'name')
paginate_by = 100

def get_context_data(self, **kwargs):
"""Get search form by organizations."""
context = super().get_context_data(**kwargs)
context['form_org'] = CombinedSearchForm(self.request.GET)
return context

def get_queryset(self): # noqa: WPS615
"""Get filter queryset."""
queryset = super().get_queryset().prefetch_related(
'contributors__organization',
)
form = CombinedSearchForm(self.request.GET)
if form.is_valid():
organizations = form.cleaned_data['organizations']
if organizations:
queryset = queryset.filter(
contributors__organization__name__exact=organizations,
)
return queryset
34 changes: 33 additions & 1 deletion contributors/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from django.views.generic.list import MultipleObjectMixin
from django_cte import With

from contributors.forms import TableSortSearchForm
from contributors.forms.forms import (
LeaderboardCombinedSearchForm,
TableSortSearchForm,
)
from contributors.utils.misc import DIRECTION_TRANSLATIONS, split_ordering

MAX_PAGES_WITHOUT_SHRINKING = 7
Expand Down Expand Up @@ -245,3 +248,32 @@ def handle_no_permission(self):
"""Redirect user without permissions."""
messages.warning(self.request, self.no_permission_msg)
return redirect(self.redirect_url)


class LeaderboardQueryMixin(MultipleObjectMixin):
"""A mixin for custom queries in leaderboard views."""

def get_context_data(self, **kwargs):
"""Get search form by organizations."""
context = super().get_context_data(**kwargs)
context['form_org'] = LeaderboardCombinedSearchForm(self.request.GET)
return context

def get_queryset(self): # noqa: WPS615
"""Get filter queryset."""
queryset = super().get_queryset().prefetch_related(
'contributors__organization',
)
form = LeaderboardCombinedSearchForm(self.request.GET)
if form.is_valid():
organizations = form.cleaned_data['organizations']
if organizations:
queryset = queryset.filter(
contributors__organization__name__exact=organizations,
)
sample = form.cleaned_data['sample']
if sample == 'except_staff':
queryset = queryset.exclude(user__is_staff=True)
elif sample == 'only_staff':
queryset = queryset.filter(user__is_staff=True)
return queryset
Binary file modified locale/ru/LC_MESSAGES/django.mo
Binary file not shown.
14 changes: 13 additions & 1 deletion locale/ru/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-13 20:27+0300\n"
"POT-Creation-Date: 2024-05-28 20:03+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -85,6 +85,18 @@ msgstr "Введите точное название организации"
msgid "Filter by organization"
msgstr "Фильтрация по организации"

#: contributors/forms/forms.py
msgid "All users"
msgstr "Все пользователи"

#: contributors/forms/forms.py
msgid "Except staff"
msgstr "Кроме сотрудников"

#: contributors/forms/forms.py
msgid "Only staff"
msgstr "Только сотрудники"

#: contributors/forms/forms.py
msgid "Filter by status"
msgstr "Фильтрация по статусу"
Expand Down

0 comments on commit a3c875c

Please sign in to comment.