Skip to content

Commit

Permalink
Merge pull request #214 from ita-social-projects/#213-ImproveCodeInFi…
Browse files Browse the repository at this point in the history
…lters

#213 improve code in filters
  • Loading branch information
Lvyshnevska authored Sep 18, 2023
2 parents 1c362a6 + be70b05 commit 9b30222
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
21 changes: 3 additions & 18 deletions profiles/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

class ProfileFilter(FilterSet):
is_saved = filters.BooleanFilter(method="is_saved_filter")
company = filters.BooleanFilter(field_name="is_registered")
startup = filters.BooleanFilter(field_name="is_startup")
activity_type = filters.CharFilter(field_name="activities__name")
is_registered = filters.BooleanFilter()
is_startup = filters.BooleanFilter()
activities__name = filters.CharFilter()

def is_saved_filter(self, queryset, name, value):
if value:
Expand All @@ -15,18 +15,3 @@ def is_saved_filter(self, queryset, name, value):
else:
queryset = queryset.none()
return queryset

def company_filter(self, queryset, name, value):
if value:
return queryset.filter(is_registered=True)
return queryset

def startup_filter(self, queryset, name, value):
if value:
return queryset.filter(is_startup=True)
return queryset

def activity_type_filter(self, queryset, name, value):
if value:
queryset = queryset.filter(activities__name=value)
return queryset
40 changes: 20 additions & 20 deletions profiles/tests/test_filter_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ def test_get_profiles_unauthorized_content(self):

def test_get_profiles_filter_companies_unauthorized_count(self):
response = self.client.get(
path="/api/profiles/?company=True&page=1&page_size=12")
path="/api/profiles/?is_registered=True&page=1&page_size=12")
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(1, response.data["total_items"])

def test_get_profiles_filter_companies_unauthorized_content(self):
response = self.client.get(
path="/api/profiles/?company=True&page=1&page_size=12")
path="/api/profiles/?is_registered=True&page=1&page_size=12")
ids_from_response = [prof["id"] for prof in response.data["results"]]
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual([self.company.id], ids_from_response)

def test_get_profiles_filter_startups_unauthorized_count(self):
response = self.client.get(
path="/api/profiles/?startup=True&page=1&page_size=12")
path="/api/profiles/?is_startup=True&page=1&page_size=12")
self.assertEqual(status.HTTP_200_OK, response.status_code)
# self.assertEqual(len(self.startup), response.data["total_items"])
self.assertEqual(1, response.data["total_items"])

def test_get_profiles_filter_startups_unauthorized_content(self):
response = self.client.get(
path="/api/profiles/?startup=True&page=1&page_size=12")
path="/api/profiles/?is_startup=True&page=1&page_size=12")
ids_from_response = [prof["id"] for prof in response.data["results"]]
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual([self.startup.id], ids_from_response)
Expand All @@ -62,7 +62,7 @@ def test_get_profiles_filter_companies_authorized_count(self):
self.client.force_authenticate(self.user)

response = self.client.get(
path="/api/profiles/?company=True&page=1&page_size=12")
path="/api/profiles/?is_registered=True&page=1&page_size=12")
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(2, response.data["total_items"], msg="Total result amount doesn't match.")
self.assertEqual(
Expand All @@ -75,7 +75,7 @@ def test_get_profiles_filter_companies_authorized_content(self):
self.client.force_authenticate(self.user)

response = self.client.get(
path="/api/profiles/?company=True&page=1&page_size=12")
path="/api/profiles/?is_registered=True&page=1&page_size=12")

saved_from_response = [company for company in response.data["results"] if company["is_saved"]]

Expand All @@ -93,7 +93,7 @@ def test_get_profiles_filter_companies_saved_authorized_count(self):
self.client.force_authenticate(self.user)

response = self.client.get(
path="/api/profiles/?company=True&is_saved=True&page=1&page_size=12")
path="/api/profiles/?is_registered=True&is_saved=True&page=1&page_size=12")
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(1, response.data["total_items"])

Expand All @@ -102,7 +102,7 @@ def test_get_profiles_filter_companies_saved_authorized_content(self):
self.client.force_authenticate(self.user)

response = self.client.get(
path="/api/profiles/?company=True&is_saved=True&page=1&page_size=12")
path="/api/profiles/?is_registered=True&is_saved=True&page=1&page_size=12")
saved_from_response = [company for company in response.data["results"] if company["is_saved"]]

self.assertEqual(status.HTTP_200_OK, response.status_code)
Expand All @@ -114,7 +114,7 @@ def test_get_profiles_filter_startups_authorized_count(self):
self.client.force_authenticate(self.user)

response = self.client.get(
path="/api/profiles/?startup=True&page=1&page_size=12")
path="/api/profiles/?is_startup=True&page=1&page_size=12")
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(2, response.data["total_items"])
self.assertEqual(
Expand All @@ -127,7 +127,7 @@ def test_get_profiles_filter_startups_authorized_content(self):
self.client.force_authenticate(self.user)

response = self.client.get(
path="/api/profiles/?startup=True&page=1&page_size=12")
path="/api/profiles/?is_startup=True&page=1&page_size=12")

saved_from_response = [startup for startup in response.data["results"] if startup["is_saved"]]

Expand All @@ -145,7 +145,7 @@ def test_get_profiles_filter_startups_saved_authorized_count(self):
self.client.force_authenticate(self.user)

response = self.client.get(
path="/api/profiles/?startup=True&is_saved=True&page=1&page_size=12".format(
path="/api/profiles/?is_startup=True&is_saved=True&page=1&page_size=12".format(
company_type="startup"))
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(1, response.data["total_items"])
Expand All @@ -155,7 +155,7 @@ def test_get_profiles_filter_startups_saved_authorized_content(self):
self.client.force_authenticate(self.user)

response = self.client.get(
path="/api/profiles/?startup=True&is_saved=True&page=1&page_size=12".format(
path="/api/profiles/?is_startup=True&is_saved=True&page=1&page_size=12".format(
company_type="startup"))
saved_from_response = [startup for startup in response.data["results"] if startup["is_saved"]]

Expand Down Expand Up @@ -193,14 +193,14 @@ def setUp(self) -> None:

def test_get_profiles_any_user_filter_producer_count(self):
response = self.client.get(
path="/api/profiles/?activity_type={activity}&page=1&page_size={page_size}".format(
path="/api/profiles/?activities__name={activity}&page=1&page_size={page_size}".format(
activity="producer", page_size=self.page_size))
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(self.producers_number, response.data["total_items"])

def test_get_profiles_any_user_filter_producer_content(self):
response = self.client.get(
path="/api/profiles/?activity_type={activity}&page=1&page_size={page_size}".format(
path="/api/profiles/?activities__name={activity}&page=1&page_size={page_size}".format(
activity="producer", page_size=self.page_size))
producers_from_db = Profile.objects.filter(
activities=self.producer_activity, is_deleted=False).order_by("id")
Expand All @@ -212,14 +212,14 @@ def test_get_profiles_any_user_filter_producer_content(self):

def test_get_profiles_any_user_filter_importer_count(self):
response = self.client.get(
path="/api/profiles/?activity_type={activity}&page=1&page_size={page_size}".format(
path="/api/profiles/?activities__name={activity}&page=1&page_size={page_size}".format(
activity="importer", page_size=self.page_size))
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(self.importers_number, response.data["total_items"])

def test_get_profiles_any_user_filter_importer_content(self):
response = self.client.get(
path="/api/profiles/?activity_type={activity}&page=1&page_size={page_size}".format(
path="/api/profiles/?activities__name={activity}&page=1&page_size={page_size}".format(
activity="importer", page_size=self.page_size))
importers_from_db = Profile.objects.filter(
activities=self.importer_activity, is_deleted=False).order_by("id")
Expand All @@ -231,14 +231,14 @@ def test_get_profiles_any_user_filter_importer_content(self):

def test_get_profiles_any_user_filter_retail_count(self):
response = self.client.get(
path="/api/profiles/?activity_type={activity}&page=1&page_size={page_size}".format(
path="/api/profiles/?activities__name={activity}&page=1&page_size={page_size}".format(
activity="retail", page_size=self.page_size))
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(self.retailers_number, response.data["total_items"])

def test_get_profiles_any_user_filter_retail_content(self):
response = self.client.get(
path="/api/profiles/?activity_type={activity}&page=1&page_size={page_size}".format(
path="/api/profiles/?activities__name={activity}&page=1&page_size={page_size}".format(
activity="retail", page_size=self.page_size))
retailers_from_db = Profile.objects.filter(
activities=self.retail_activity, is_deleted=False).order_by("id")
Expand All @@ -250,14 +250,14 @@ def test_get_profiles_any_user_filter_retail_content(self):

def test_get_profiles_any_user_filter_horeca_count(self):
response = self.client.get(
path="/api/profiles/?activity_type={activity}&page=1&page_size={page_size}".format(
path="/api/profiles/?activities__name={activity}&page=1&page_size={page_size}".format(
activity="horeca", page_size=self.page_size))
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(self.horecas_number, response.data["total_items"])

def test_get_profiles_any_user_filter_horeca_content(self):
response = self.client.get(
path="/api/profiles/?activity_type={activity}&page=1&page_size={page_size}".format(
path="/api/profiles/?activities__name={activity}&page=1&page_size={page_size}".format(
activity="horeca", page_size=self.page_size))
horecas_from_db = Profile.objects.filter(
activities=self.horeca_activity, is_deleted=False).order_by("id")
Expand Down

0 comments on commit 9b30222

Please sign in to comment.