Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
Openlobby Server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TO
openlobby
Openlobby Server
Commits
e912cae2
Commit
e912cae2
authored
6 years ago
by
jan.bednarik
Browse files
Options
Downloads
Patches
Plain Diff
Use Enum for User sort options
parent
efb7d92b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
openlobby/core/api/schema.py
+7
-13
7 additions, 13 deletions
openlobby/core/api/schema.py
openlobby/core/models.py
+9
-9
9 additions, 9 deletions
openlobby/core/models.py
tests/test_models.py
+7
-11
7 additions, 11 deletions
tests/test_models.py
with
23 additions
and
33 deletions
openlobby/core/api/schema.py
+
7
−
13
View file @
e912cae2
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
=
Autho
rSortEnum
(),
sort
=
Use
rSortEnum
(),
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
()
...
...
This diff is collapsed.
Click to expand it.
openlobby/core/models.py
+
9
−
9
View file @
e912cae2
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
"
,
...
...
This diff is collapsed.
Click to expand it.
tests/test_models.py
+
7
−
11
View file @
e912cae2
...
...
@@ -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
"
],
),
],
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment