Fakultas Ilmu Komputer UI

Commit 1faf6b8e authored by Dave Nathanael's avatar Dave Nathanael
Browse files

Merge branch 'dave/PBI-8-User-pagination-filter' into 'staging'

Implement pagination and filter for Account model

See merge request !18
parents f5dec017 1e0e5c6b
Pipeline #38066 passed with stages
in 2 minutes and 29 seconds
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",
]
......@@ -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) + "/"
......
......@@ -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)
......
......@@ -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)
......
......@@ -15,6 +15,7 @@ from apps.cases.tests.factories.cases import (
)
from apps.constants import TIMEZONE
class MonitoringCaseViewTest(TestCase):
@classmethod
def setUpTestData(self):
......
......@@ -13,6 +13,7 @@ from apps.commons.permissions import (
)
from apps.constants import HEADER_PREFIX
class IsAuthenticatedPermissionTest(APITestCase):
@classmethod
def setUpTestData(self):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment