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:
parent
a4a61b556f
commit
93bee3544a
@ -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
|
||||
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -16,3 +16,6 @@ RECIPE_TYPE_CHOICE = [
|
||||
RECIPE_TYPE_VEGETABLE,
|
||||
RECIPE_TYPE_SOUP,
|
||||
]
|
||||
|
||||
FILTER_EXACT = ['exact']
|
||||
FILTER_GTE_LTE = ['exact', 'gte', 'gt', 'lte', 'lt']
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user