Skip to content

Commit

Permalink
Started working on the filtering for status and status for feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
LeMiTam committed Oct 30, 2024
1 parent 92ecc34 commit f644199
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 10 deletions.
52 changes: 52 additions & 0 deletions app/feedback/filters/feedback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.db.models import Q
from django_filters import rest_framework as filters
from django_filters.rest_framework import OrderingFilter

from app.feedback.models import Feedback
from app.feedback.models.bug import Bug
from app.feedback.models.idea import Idea


class FeedbackFilter(filters.FilterSet):

feedback_type = filters.CharFilter(
method="fiter_feedback_type", label="List of feedback type"
)

status = filters.ModelChoiceFilter(
method="filter_status",
field_name="status",
label="List of feedback status",
queryset=Feedback.objects.all(),
)

ordering = OrderingFilter(
fields=(
"created_at",
"updated_at",
)
)

def fiter_feedback_type(self, queryset, _name, feedback_type):

if feedback_type == "Idea":
return queryset.filter(Q(instance_of=Idea))

elif feedback_type == "Bug":
return queryset.filter(Q(instance_of=Bug))

else:
return queryset

def filter_status(self, queryset, _name, value):
return queryset.filter(status=value)

class Meta:
model = Feedback
fields = [
"title",
"author",
"status",
"created_at",
"updated_at",
]
10 changes: 9 additions & 1 deletion app/feedback/views/feedback.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from rest_framework import status
from rest_framework import filters, status
from rest_framework.response import Response

from app.common.pagination import BasePagination
from app.common.permissions import BasicViewPermission
from app.common.viewsets import BaseViewSet
from app.feedback.filters.feedback import FeedbackFilter
from app.feedback.models.feedback import Feedback
from app.feedback.serializers import (
BugCreateSerializer,
Expand All @@ -20,6 +21,13 @@ class FeedbackViewSet(BaseViewSet):
pagination_class = BasePagination
permission_classes = [BasicViewPermission]

filterset_class = FeedbackFilter
search_fields = [
"title",
"author__first_name",
"author__last_name",
]

def create(self, request, *_args, **_kwargs):
data = request.data

Expand Down
59 changes: 50 additions & 9 deletions app/tests/feedback/test_feedback_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ def get_data(type):


@pytest.mark.django_db
def test_list_feedback_with_both_bug_and_idea_as_member(
member, feedback_bug, feedback_idea
):
def test_list_feedback_with_both_bug_and_idea_as_member(member):
"""All members should be able to list all types of feedbacks."""

BugFactory(author=member)
IdeaFactory(author=member)

url = FEEDBACK_BASE_URL
client = get_api_client(member)
response = client.get(url)
Expand All @@ -33,10 +34,10 @@ def test_list_feedback_with_both_bug_and_idea_as_member(
idea_type = list(filter(lambda x: "Idea" == x["feedback_type"], results))

assert response.status_code == status.HTTP_200_OK
assert data["count"] == 2
assert bug_type
assert idea_type

assert data["count"] == 2
assert len(bug_type) == 1
assert len(idea_type) == 1

@pytest.mark.django_db
def test_list_feedback_as_anonymous_user(default_client):
Expand Down Expand Up @@ -206,7 +207,7 @@ def test_retrieve_details_idea_as_member(member):


@pytest.mark.django_db
def test_retrieve_details_bug_as_anonymous_user(default_client,member):
def test_retrieve_details_bug_as_anonymous_user(default_client, member):
"""Non TIHLDE users should not be able to retrieve bug feedbacks"""
feedback_bug = BugFactory(author=member)

Expand All @@ -217,7 +218,7 @@ def test_retrieve_details_bug_as_anonymous_user(default_client,member):


@pytest.mark.django_db
def test_retrieve_details_idea_as_anonymous_user(default_client,member):
def test_retrieve_details_idea_as_anonymous_user(default_client, member):
"""Non TIHLDE users should not be able to retrieve idea feedbacks"""
feedback_idea = IdeaFactory(author=member)

Expand Down Expand Up @@ -254,3 +255,43 @@ def test_retrieve_details_idea_as_index_user(index_member, member):

assert response.data["feedback_type"] == "Idea"
assert response.status_code == status.HTTP_200_OK


@pytest.mark.django_db
@pytest.mark.parametrize("feedback_type", ["Bug", "Idea"])
def test_filter_feedback_type_as_member(member, feedback_type):
"""A member should be able to filter feedbacks by type"""

BugFactory()
IdeaFactory()

url = f"{FEEDBACK_BASE_URL}?feedback_type={feedback_type}"
client = get_api_client(member)
response = client.get(url)

data = response.data
results = data["results"]

assert response.status_code == status.HTTP_200_OK
assert data["count"] == 1
assert results[0]["feedback_type"] == feedback_type


@pytest.mark.django_db
def test_status_filter_as_member(member):
"""A member should be able to filter feedbacks by status"""

BugFactory(author=member)
IdeaFactory(author=member)

url = f"{FEEDBACK_BASE_URL}?status=1"
client = get_api_client(member)
response = client.get(url)

data = response.data
results = data["results"]

assert response.status_code == status.HTTP_200_OK
assert data["count"] == 2
assert results[0]["status"] == "OPEN"
assert results[1]["status"] == "OPEN"

0 comments on commit f644199

Please sign in to comment.