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
|
from rest_framework.response import Response
|
||||||
|
|
||||||
class PagePaginationWithPageCount(pagination.PageNumberPagination):
|
class PagePaginationWithPageCount(pagination.PageNumberPagination):
|
||||||
|
page_size_query_param = 'page_size'
|
||||||
|
|
||||||
def get_paginated_response(self, data):
|
def get_paginated_response(self, data):
|
||||||
response = super().get_paginated_response(data)
|
response = super().get_paginated_response(data)
|
||||||
response.data['page_count'] = self.page.paginator.num_pages
|
response.data['page_count'] = self.page.paginator.num_pages
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,20 +1,5 @@
|
|||||||
from .settings_default import *
|
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 = ['*']
|
ALLOWED_HOSTS = ['*']
|
||||||
|
|
||||||
CORS_ALLOW_CREDENTIALS = True # 允许携带cookie
|
CORS_ALLOW_CREDENTIALS = True # 允许携带cookie
|
||||||
|
|||||||
@ -43,6 +43,7 @@ INSTALLED_APPS = [
|
|||||||
'corsheaders',
|
'corsheaders',
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
'rest_framework.authtoken',
|
'rest_framework.authtoken',
|
||||||
|
'django_filters',
|
||||||
|
|
||||||
# user apps
|
# user apps
|
||||||
'timer',
|
'timer',
|
||||||
@ -132,14 +133,18 @@ STATIC_URL = '/static/'
|
|||||||
|
|
||||||
# rest framework
|
# rest framework
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
# Use Django's standard `django.contrib.auth` permissions,
|
'DEFAULT_AUTHENTICATION_CLASSES': (
|
||||||
# or allow read-only access for unauthenticated users.
|
'rest_framework.authentication.BasicAuthentication',
|
||||||
'DEFAULT_PERMISSION_CLASSES': [
|
'rest_framework.authentication.SessionAuthentication',
|
||||||
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
|
),
|
||||||
],
|
# 'DEFAULT_PERMISSION_CLASSES': [
|
||||||
|
# 'rest_framework.permissions.IsAuthenticatedOrReadOnly'
|
||||||
|
# ],
|
||||||
|
|
||||||
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
# 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
||||||
'PAGE_SIZE': 10
|
'DEFAULT_PAGINATION_CLASS': 'core.pagination.PagePaginationWithPageCount',
|
||||||
|
'PAGE_SIZE': 10,
|
||||||
|
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
|
||||||
}
|
}
|
||||||
|
|
||||||
# CORS
|
# CORS
|
||||||
|
|||||||
@ -9,6 +9,7 @@ from rest_framework.views import APIView
|
|||||||
|
|
||||||
import recipe.models
|
import recipe.models
|
||||||
import recipe.serializers
|
import recipe.serializers
|
||||||
|
from utils import const
|
||||||
|
|
||||||
class RecipeAPI(rest_framework.generics.RetrieveUpdateAPIView):
|
class RecipeAPI(rest_framework.generics.RetrieveUpdateAPIView):
|
||||||
|
|
||||||
@ -29,6 +30,11 @@ class RecipeListAPI(rest_framework.generics.ListAPIView,
|
|||||||
# permission_classes = (permissions.IsAuthenticated,)
|
# permission_classes = (permissions.IsAuthenticated,)
|
||||||
queryset = recipe.models.Recipe.objects.all()
|
queryset = recipe.models.Recipe.objects.all()
|
||||||
serializer_class = recipe.serializers.RecipeSerializer
|
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,
|
class WeekRecipeListAPI(rest_framework.generics.ListAPIView,
|
||||||
|
|||||||
@ -16,3 +16,6 @@ RECIPE_TYPE_CHOICE = [
|
|||||||
RECIPE_TYPE_VEGETABLE,
|
RECIPE_TYPE_VEGETABLE,
|
||||||
RECIPE_TYPE_SOUP,
|
RECIPE_TYPE_SOUP,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
FILTER_EXACT = ['exact']
|
||||||
|
FILTER_GTE_LTE = ['exact', 'gte', 'gt', 'lte', 'lt']
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user