Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ppl-fasilkom-ui
2020
PPL-C
PPTI-Mobile Apps Monitoring Wabah Tuberkolosis
Neza-Backend
Commits
1e0e5c6b
Commit
1e0e5c6b
authored
Apr 05, 2020
by
Dave Nathanael
Browse files
Implement pagination and filter for Account model
parent
f5dec017
Changes
6
Hide whitespace changes
Inline
Side-by-side
apps/accounts/filters.py
0 → 100644
View file @
1e0e5c6b
from
django_filters
import
FilterSet
,
CharFilter
from
apps.accounts.models
import
(
Account
,
)
class
AccountFilter
(
FilterSet
):
username
=
CharFilter
(
field_name
=
"user__username"
)
class
Meta
:
model
=
Account
fields
=
[
"username"
,
"name"
,
"email"
,
"phone_number"
,
"area"
,
"is_admin"
,
"is_verified"
,
]
apps/accounts/tests/test_units/test_accounts.py
View file @
1e0e5c6b
...
...
@@ -33,7 +33,40 @@ class AccountViewTest(APITestCase):
url
=
"/accounts/"
response
=
self
.
client
.
get
(
url
)
response_string
=
response
.
rendered_content
.
decode
(
"utf-8"
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertIn
(
'"user_1"'
,
response_string
)
self
.
assertIn
(
'"user_2"'
,
response_string
)
def
test_list_all_accounts_paginate_failed
(
self
):
url
=
"/accounts/?page=100"
response
=
self
.
client
.
get
(
url
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_404_NOT_FOUND
)
response_string
=
response
.
rendered_content
.
decode
(
"utf-8"
)
self
.
assertIn
(
'"detail":"Invalid page."'
,
response_string
)
def
test_list_all_accounts_filter_success
(
self
):
url
=
"/accounts/?username="
+
self
.
user_1
.
username
response
=
self
.
client
.
get
(
url
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
response_string
=
response
.
rendered_content
.
decode
(
"utf-8"
)
self
.
assertIn
(
'"count":1'
,
response_string
)
def
test_list_all_accounts_filter_failed
(
self
):
url
=
"/accounts/?username=1234567890"
response
=
self
.
client
.
get
(
url
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
response_string
=
response
.
rendered_content
.
decode
(
"utf-8"
)
self
.
assertIn
(
'"count":0'
,
response_string
)
def
test_retrieve_account_success
(
self
):
url
=
"/accounts/"
+
str
(
self
.
officer
.
id
)
+
"/"
...
...
apps/accounts/views.py
View file @
1e0e5c6b
...
...
@@ -2,12 +2,15 @@ from django.contrib.auth.models import User
from
django.shortcuts
import
get_object_or_404
from
rest_framework
import
status
,
viewsets
from
rest_framework.response
import
Response
from
rest_framework.pagination
import
PageNumberPagination
from
django_filters.rest_framework
import
DjangoFilterBackend
from
apps.accounts.models
import
Account
from
apps.accounts.serializers
import
(
AccountSerializer
,
AccountRegisterSerializer
,
)
from
apps.accounts.filters
import
AccountFilter
from
apps.commons.permissions
import
(
IsSelfOrAdministrator
,
CreateOnly
,
...
...
@@ -16,14 +19,17 @@ from apps.commons.permissions import (
class
AccountViewSet
(
viewsets
.
ViewSet
):
queryset
=
Account
.
objects
.
all
().
select_related
(
"user"
)
filter_backends
=
(
DjangoFilterBackend
,)
permission_classes
=
[
IsSelfOrAdministrator
|
CreateOnly
,
]
def
list
(
self
,
request
):
queryset
=
self
.
queryset
serializer
=
AccountSerializer
(
queryset
,
many
=
True
)
return
Response
(
serializer
.
data
,
status
=
status
.
HTTP_200_OK
)
paginator
=
PageNumberPagination
()
filtered_set
=
AccountFilter
(
request
.
GET
,
queryset
=
self
.
queryset
).
qs
context
=
paginator
.
paginate_queryset
(
filtered_set
,
request
)
serializer
=
AccountSerializer
(
context
,
many
=
True
)
return
paginator
.
get_paginated_response
(
serializer
.
data
)
def
retrieve
(
self
,
request
,
pk
=
None
):
instance
=
get_object_or_404
(
self
.
queryset
,
pk
=
pk
)
...
...
apps/cases/models.py
View file @
1e0e5c6b
...
...
@@ -8,6 +8,7 @@ from apps.accounts.models import Account
from
apps.commons.managers
import
SoftObjectManager
from
apps.constants
import
TIMEZONE
class
CaseSubject
(
models
.
Model
):
revision_id
=
models
.
UUIDField
(
primary_key
=
True
,
default
=
uuid
.
uuid4
,
editable
=
False
)
subject_id
=
models
.
UUIDField
(
default
=
uuid
.
uuid4
)
...
...
apps/cases/tests/test_units/test_monitoring_cases.py
View file @
1e0e5c6b
...
...
@@ -15,6 +15,7 @@ from apps.cases.tests.factories.cases import (
)
from
apps.constants
import
TIMEZONE
class
MonitoringCaseViewTest
(
TestCase
):
@
classmethod
def
setUpTestData
(
self
):
...
...
apps/commons/tests/test_permissions.py
View file @
1e0e5c6b
...
...
@@ -13,6 +13,7 @@ from apps.commons.permissions import (
)
from
apps.constants
import
HEADER_PREFIX
class
IsAuthenticatedPermissionTest
(
APITestCase
):
@
classmethod
def
setUpTestData
(
self
):
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment