Skip to content
Snippets Groups Projects
Commit e912cae2 authored by jan.bednarik's avatar jan.bednarik
Browse files

Use Enum for User sort options

parent efb7d92b
Branches
No related tags found
No related merge requests found
import graphene
from django.db.models import Count, Q
from graphene import relay
from . import types
......@@ -7,18 +6,10 @@ from .paginator import Paginator
from .sanitizers import extract_text
from .. import search
from ..models import OpenIdClient
from ..models import User, Report
from ..models import User, Report, UserSort
AUTHOR_SORT_LAST_NAME_ID = 1
AUTHOR_SORT_TOTAL_REPORTS_ID = 2
class AuthorSortEnum(graphene.Enum):
LAST_NAME = AUTHOR_SORT_LAST_NAME_ID
TOTAL_REPORTS = AUTHOR_SORT_TOTAL_REPORTS_ID
class Meta:
description = "Sort by field."
UserSortEnum = graphene.Enum.from_enum(UserSort)
class AuthorsConnection(relay.Connection):
......@@ -49,9 +40,9 @@ class Query:
authors = relay.ConnectionField(
AuthorsConnection,
description="List of Authors. Returns first 10 nodes if pagination is not specified.",
sort=AuthorSortEnum(),
sort=UserSortEnum(),
reversed=graphene.Boolean(
default_value=False, description="Reverse order of sort"
default_value=False, description="Reverse order of sort."
),
)
search_reports = relay.ConnectionField(
......@@ -70,6 +61,9 @@ class Query:
)
def resolve_authors(self, info, **kwargs):
if "sort" in kwargs:
kwargs["sort"] = UserSort(kwargs["sort"])
paginator = Paginator(**kwargs)
total = User.objects.filter(is_author=True).count()
......
from enum import Enum
import time
from django.conf import settings
......@@ -9,6 +10,11 @@ from django.db.models import Q
from django.utils import timezone
class UserSort(Enum):
LAST_NAME = "last_name"
TOTAL_REPORTS = "total_reports"
class CustomUserManager(UserManager):
def with_total_reports(self):
return self.get_queryset().annotate(
......@@ -19,21 +25,15 @@ class CustomUserManager(UserManager):
)
def sorted(self, **kwargs):
# inline import intentionally
from openlobby.core.api.schema import (
AUTHOR_SORT_LAST_NAME_ID,
AUTHOR_SORT_TOTAL_REPORTS_ID,
)
qs = self.with_total_reports()
sort_field = kwargs.get("sort", AUTHOR_SORT_LAST_NAME_ID)
sort_choice = kwargs.get("sort", UserSort.LAST_NAME)
if sort_field == AUTHOR_SORT_LAST_NAME_ID:
if sort_choice == UserSort.LAST_NAME:
return qs.order_by(
"{}last_name".format("-" if kwargs.get("reversed", False) else ""),
"first_name",
)
elif sort_field == AUTHOR_SORT_TOTAL_REPORTS_ID:
elif sort_choice == UserSort.TOTAL_REPORTS:
return qs.order_by(
"{}total_reports".format("" if kwargs.get("reversed", False) else "-"),
"last_name",
......
......@@ -3,12 +3,8 @@ from unittest.mock import patch
import arrow
import pytest
from django.conf import settings
from openlobby.core.api.schema import (
AUTHOR_SORT_LAST_NAME_ID,
AUTHOR_SORT_TOTAL_REPORTS_ID,
)
from openlobby.core.documents import ReportDoc
from openlobby.core.models import Report, User, OpenIdClient, LoginAttempt
from openlobby.core.models import Report, User, OpenIdClient, LoginAttempt, UserSort
from .dummy import prepare_reports
......@@ -152,22 +148,22 @@ def test_user__name_collision_excludes_self_on_update():
({}, ["Sheep", "Squarepants", "Wolfe"]),
({"reversed": False}, ["Sheep", "Squarepants", "Wolfe"]),
({"reversed": True}, ["Wolfe", "Squarepants", "Sheep"]),
({"sort": AUTHOR_SORT_LAST_NAME_ID}, ["Sheep", "Squarepants", "Wolfe"]),
({"sort": UserSort.LAST_NAME}, ["Sheep", "Squarepants", "Wolfe"]),
(
{"sort": AUTHOR_SORT_LAST_NAME_ID, "reversed": False},
{"sort": UserSort.LAST_NAME, "reversed": False},
["Sheep", "Squarepants", "Wolfe"],
),
(
{"sort": AUTHOR_SORT_LAST_NAME_ID, "reversed": True},
{"sort": UserSort.LAST_NAME, "reversed": True},
["Wolfe", "Squarepants", "Sheep"],
),
({"sort": AUTHOR_SORT_TOTAL_REPORTS_ID}, ["Wolfe", "Sheep", "Squarepants"]),
({"sort": UserSort.TOTAL_REPORTS}, ["Wolfe", "Sheep", "Squarepants"]),
(
{"sort": AUTHOR_SORT_TOTAL_REPORTS_ID, "reversed": False},
{"sort": UserSort.TOTAL_REPORTS, "reversed": False},
["Wolfe", "Sheep", "Squarepants"],
),
(
{"sort": AUTHOR_SORT_TOTAL_REPORTS_ID, "reversed": True},
{"sort": UserSort.TOTAL_REPORTS, "reversed": True},
["Squarepants", "Sheep", "Wolfe"],
),
],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment