diff --git a/core/pagination.py b/core/pagination.py index 7730729..0b4dc50 100644 --- a/core/pagination.py +++ b/core/pagination.py @@ -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 + + diff --git a/dsite/settings.py b/dsite/settings.py index 18defa2..dbe3d83 100644 --- a/dsite/settings.py +++ b/dsite/settings.py @@ -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 diff --git a/dsite/settings_default.py b/dsite/settings_default.py index f4415e6..b4a9f57 100644 --- a/dsite/settings_default.py +++ b/dsite/settings_default.py @@ -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 diff --git a/recipe/views.py b/recipe/views.py index e2444e1..91bf71a 100644 --- a/recipe/views.py +++ b/recipe/views.py @@ -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, diff --git a/utils/const.py b/utils/const.py index 83dc137..618f1dd 100644 --- a/utils/const.py +++ b/utils/const.py @@ -16,3 +16,6 @@ RECIPE_TYPE_CHOICE = [ RECIPE_TYPE_VEGETABLE, RECIPE_TYPE_SOUP, ] + +FILTER_EXACT = ['exact'] +FILTER_GTE_LTE = ['exact', 'gte', 'gt', 'lte', 'lt']