Filter entries by the presence of help types (#393)

Fix #392
This commit is contained in:
Gideon Thomas 2018-08-02 14:55:32 -04:00 коммит произвёл GitHub
Родитель 784756a8a0
Коммит 9a2a455d3e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 49 добавлений и 0 удалений

Просмотреть файл

@ -239,6 +239,7 @@ The schema for the payload returned is:
- `?tag=<string>` - Filter entries by a specific tag - `?tag=<string>` - Filter entries by a specific tag
- `?issue=<string>` - Filter entries by an issue area - `?issue=<string>` - Filter entries by an issue area
- `?help_type=<string>` - Filter entries by a specific help category - `?help_type=<string>` - Filter entries by a specific help category
- `?has_help_types=<True or False>` - Filter entries by whether they have help types or not. Note that `True` or `False` is case-sensitive.
- `?featured=<true or false>` - Filter featured or non-featured entries - `?featured=<true or false>` - Filter featured or non-featured entries
- `?ordering=<string>` - Order entries by a certain property e.g. `?ordering=title`. Prepend the property with a hyphen to get entries in descending order, e.g. `?ordering=-title` - `?ordering=<string>` - Order entries by a certain property e.g. `?ordering=title`. Prepend the property with a hyphen to get entries in descending order, e.g. `?ordering=-title`
- `?moderationstate=<string>` - Filter entries by its moderation state. This filter will only be applied if the API call was made by an authenticated user with moderation permissions - `?moderationstate=<string>` - Filter entries by its moderation state. This filter will only be applied if the API call was made by an authenticated user with moderation permissions

Просмотреть файл

@ -10,6 +10,7 @@ from rest_framework.request import Request
from pulseapi.creators.models import EntryCreator from pulseapi.creators.models import EntryCreator
from pulseapi.profiles.models import UserProfile from pulseapi.profiles.models import UserProfile
from pulseapi.entries.models import Entry, ModerationState from pulseapi.entries.models import Entry, ModerationState
from pulseapi.helptypes.factory import HelpTypeFactory
from pulseapi.entries.serializers import ( from pulseapi.entries.serializers import (
EntrySerializerWithV1Creators, EntrySerializerWithV1Creators,
EntrySerializerWithCreators, EntrySerializerWithCreators,
@ -152,6 +153,31 @@ class TestEntryView(PulseStaffTestCase):
) )
self.assertEqual(postresponse.status_code, 200) self.assertEqual(postresponse.status_code, 200)
def test_no_help_type_filter(self):
"""Test filtering entries that don't have a help type"""
counter = 0
for entry in self.entries:
if counter % 2 == 0:
help_type = HelpTypeFactory()
entry.help_types.add(help_type)
else:
entry.help_types.clear()
counter += 1
entries_without_help_types = Entry.objects.filter(help_types__isnull=True).count()
entries_with_help_types = Entry.objects.filter(help_types__isnull=False).count()
url = reverse('entries-list', args=[settings.API_VERSIONS['version_2'] + '/'])
response_1 = self.client.get('{url}?has_help_types=False'.format(url=url))
response_entries_without_help_types = json.loads(str(response_1.content, 'utf-8'))
response_2 = self.client.get('{url}?has_help_types=True'.format(
url=reverse('entries-list', args=[settings.API_VERSIONS['version_2'] + '/']),
))
response_entries_with_help_types = json.loads(str(response_2.content, 'utf-8'))
self.assertEqual(response_entries_without_help_types['count'], entries_without_help_types)
self.assertEqual(response_entries_with_help_types['count'], entries_with_help_types)
def test_featured_filter(self): def test_featured_filter(self):
"""Entry with all content""" """Entry with all content"""

Просмотреть файл

@ -167,6 +167,11 @@ class EntryCustomFilter(filters.FilterSet):
name='help_types__name', name='help_types__name',
lookup_expr='iexact', lookup_expr='iexact',
) )
has_help_types = django_filters.BooleanFilter(
name='help_types',
lookup_expr='isnull',
exclude=True,
)
featured = django_filters.BooleanFilter( featured = django_filters.BooleanFilter(
name='featured' name='featured'
) )
@ -301,6 +306,9 @@ class EntriesListView(ListCreateAPIView):
- `?tag=` - Allows filtering entries by a specific tag - `?tag=` - Allows filtering entries by a specific tag
- `?issue=` - Allows filtering entries by a specific issue - `?issue=` - Allows filtering entries by a specific issue
- `?help_type=` - Allows filtering entries by a specific help type - `?help_type=` - Allows filtering entries by a specific help type
- `?has_help_types=<True or False>` - Filter entries by whether they have
help types or not. Note that `True`
or `False` is case-sensitive.
- `?featured=True` (or False) - both capitalied. Boolean is set in admin UI - `?featured=True` (or False) - both capitalied. Boolean is set in admin UI
- `?page=` - Page number, defaults to 1 - `?page=` - Page number, defaults to 1
- `?page_size=` - Number of results on a page. Defaults to 48 - `?page_size=` - Number of results on a page. Defaults to 48

Просмотреть файл

@ -0,0 +1,14 @@
"""
Create fake help types for local development, tests, and Heroku's review app.
"""
from factory import DjangoModelFactory, Faker
from pulseapi.helptypes.models import HelpType
class HelpTypeFactory(DjangoModelFactory):
name = Faker('job')
class Meta:
model = HelpType