Skip to content

Commit

Permalink
Merge pull request #9 from snnbotchway/feature/feedback-app
Browse files Browse the repository at this point in the history
Implement list responses with pagination
  • Loading branch information
snnbotchway authored Mar 13, 2023
2 parents 0458da2 + 474c942 commit 23d6195
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
8 changes: 8 additions & 0 deletions b2b/feedback/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Feedback app pagination."""
from rest_framework.pagination import PageNumberPagination


class ResponsePagination(PageNumberPagination):
"""Response pagination class."""

page_size = 1
29 changes: 29 additions & 0 deletions b2b/feedback/tests/test_feedback_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,32 @@ def test_anonymous_user_cannot_respond_to_questionnaires(

assert response.status_code == status.HTTP_401_UNAUTHORIZED
assert Response.objects.count() == 0

def test_sales_manager_list_responses_200(
self, api_client, response_list_url, sales_manager
):
"""Test sales manager can list responses."""
api_client.force_authenticate(user=sales_manager)
questionnaire = baker.make(Questionnaire)
baker.make(Response, questionnaire=questionnaire, _quantity=2)
baker.make(Response)
url = response_list_url(questionnaire.id)

response = api_client.get(url)

assert response.status_code == status.HTTP_200_OK
assert Response.objects.count() == 3
assert response.data["count"] == 2
assert len(response.data["results"]) == 1

def test_client_rep_cannot_list_responses_403(
self, api_client, client_rep, response_list_url
):
"""Test client reps cannot list responses."""
api_client.force_authenticate(user=client_rep)
questionnaire = baker.make(Questionnaire)
url = response_list_url(questionnaire.id)

response = api_client.get(url)

assert response.status_code == status.HTTP_403_FORBIDDEN
16 changes: 15 additions & 1 deletion b2b/feedback/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rest_framework.viewsets import GenericViewSet

from .models import Client, Questionnaire, Response
from .pagination import ResponsePagination
from .permissions import (
IsClientRepresentative,
IsSalesManager,
Expand Down Expand Up @@ -92,13 +93,26 @@ def perform_create(self, serializer):

class ResponseViewSet(
CreateModelMixin,
ListModelMixin,
GenericViewSet,
):
"""The Response viewset."""

queryset = Response.objects.all()
serializer_class = ResponseSerializer
permission_classes = [IsClientRepresentative]
pagination_class = ResponsePagination

def get_queryset(self):
"""Filter responses with questionnaire id in url."""
return self.queryset.filter(
questionnaire_id=self.kwargs["questionnaire_pk"]
).order_by("id")

def get_permissions(self):
"""Return appropriate permissions."""
if self.request.method in SAFE_METHODS:
return [IsSalesManager()]
return [IsClientRepresentative()]

def get_serializer_context(self):
"""Pass user and questionnaire id to serializer for validation."""
Expand Down

0 comments on commit 23d6195

Please sign in to comment.