feat(recipe list api): [A] add filtering to apis

[A] add filtering to apis

Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
Ching 2021-10-06 12:12:19 +08:00
parent a4a61b556f
commit 93bee3544a
5 changed files with 25 additions and 22 deletions

View File

@ -3,7 +3,11 @@ from rest_framework import pagination
from rest_framework.response import Response
class PagePaginationWithPageCount(pagination.PageNumberPagination):
page_size_query_param = 'page_size'
def get_paginated_response(self, data):
response = super().get_paginated_response(data)
response.data['page_count'] = self.page.paginator.num_pages
return response

View File

@ -1,20 +1,5 @@
from .settings_default import *
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
# 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'DEFAULT_PAGINATION_CLASS': 'core.pagination.PagePaginationWithPageCount',
'PAGE_SIZE': 10
}
ALLOWED_HOSTS = ['*']
CORS_ALLOW_CREDENTIALS = True # 允许携带cookie

View File

@ -43,6 +43,7 @@ INSTALLED_APPS = [
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
'django_filters',
# user apps
'timer',
@ -132,14 +133,18 @@ STATIC_URL = '/static/'
# rest framework
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
# 'DEFAULT_PERMISSION_CLASSES': [
# 'rest_framework.permissions.IsAuthenticatedOrReadOnly'
# ],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
# 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'DEFAULT_PAGINATION_CLASS': 'core.pagination.PagePaginationWithPageCount',
'PAGE_SIZE': 10,
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}
# CORS

View File

@ -9,6 +9,7 @@ from rest_framework.views import APIView
import recipe.models
import recipe.serializers
from utils import const
class RecipeAPI(rest_framework.generics.RetrieveUpdateAPIView):
@ -29,6 +30,11 @@ class RecipeListAPI(rest_framework.generics.ListAPIView,
# permission_classes = (permissions.IsAuthenticated,)
queryset = recipe.models.Recipe.objects.all()
serializer_class = recipe.serializers.RecipeSerializer
filterset_fields = {
'recipe_type': const.FILTER_EXACT,
'difficulty': const.FILTER_GTE_LTE,
'rate': const.FILTER_GTE_LTE,
}
class WeekRecipeListAPI(rest_framework.generics.ListAPIView,

View File

@ -16,3 +16,6 @@ RECIPE_TYPE_CHOICE = [
RECIPE_TYPE_VEGETABLE,
RECIPE_TYPE_SOUP,
]
FILTER_EXACT = ['exact']
FILTER_GTE_LTE = ['exact', 'gte', 'gt', 'lte', 'lt']