diff --git a/config/base_model.py b/config/base_model.py new file mode 100644 index 0000000..4e12901 --- /dev/null +++ b/config/base_model.py @@ -0,0 +1,10 @@ +from django.db import models + +# Create your models here. +class BaseModel(models.Model): + created_at = models.DateTimeField(auto_now_add=True, null=False) + updated_at = models.DateTimeField(auto_now=True, null=False) + is_deleted = models.BooleanField(default=False, null=False) + + class Meta: + abstract=True # 이 모델은 데이터베이스 테이블을 만드는데 사용되지 않겠다. \ No newline at end of file diff --git a/config/settings.py b/config/settings.py index 205e73b..9af5783 100644 --- a/config/settings.py +++ b/config/settings.py @@ -46,6 +46,7 @@ 'rest_framework_simplejwt', #jwt 'corsheaders', # cors 오류 해결 'storages', #storages + 'django_filters', # django-filter ] #s3 diff --git a/gdiary/models.py b/gdiary/models.py index 2b344fe..680142c 100644 --- a/gdiary/models.py +++ b/gdiary/models.py @@ -2,13 +2,7 @@ from django.contrib.auth.base_user import BaseUserManager, AbstractBaseUser from django.contrib.auth.models import PermissionsMixin -class BaseModel(models.Model): - created_at = models.DateTimeField(auto_now_add=True, null=False) - updated_at = models.DateTimeField(auto_now=True, null=False) - is_deleted = models.BooleanField(default=False, null=False) - - class Meta: - abstract=True # 이 모델은 데이터베이스 테이블을 만드는데 사용되지 않겠다. +from config.base_model import BaseModel class UserManager(BaseUserManager): diff --git a/gdiary/views.py b/gdiary/views.py index 8af0aec..3768b55 100644 --- a/gdiary/views.py +++ b/gdiary/views.py @@ -5,6 +5,7 @@ from rest_framework.decorators import api_view from drf_yasg import openapi from drf_yasg.utils import swagger_auto_schema +from rest_framework.filters import SearchFilter from django.shortcuts import get_object_or_404 from django.contrib.auth import authenticate from config.settings import * @@ -188,6 +189,10 @@ def delete(self, request): class DiaryViewset(viewsets.ModelViewSet): queryset = Diary.objects.all() serializer_class = DiarySerializer + + # search + filter_backends = [SearchFilter] + search_fields = ['title', 'contents'] # manual parameter param_date = openapi.Parameter( @@ -197,37 +202,43 @@ class DiaryViewset(viewsets.ModelViewSet): type=openapi.FORMAT_DATE ) + param_user = openapi.Parameter( + 'user_id', + openapi.IN_QUERY, + description='user_id', + type=openapi.TYPE_INTEGER + ) + # get_queryset에 데코레이터 인식 못하기 때문에 list 상속 받아 구현 - @swagger_auto_schema(manual_parameters=[param_date]) + @swagger_auto_schema(manual_parameters=[param_date, param_user]) def list(self, request, *args, **kwargs): return super().list(request, *args, **kwargs) - # api/v1/diaries/?date=2023-01-26 + # 일기 리스트 조회 및 날짜 별 일기 조회 def get_queryset(self): diaries = Diary.objects.filter(is_deleted = False) - date = self.request.query_params.get('date', '') - if date: - diaries = diaries.filter(diary_date=date) + search_query = self.request.query_params.get('search', '') + user_id = self.request.query_params.get('user_id', '') + date = self.request.query_params.get('diary_date', '') + + + # api/v1/diaries/?user_id=1&date=2023-01-26 + if date and user_id: + diaries = diaries.filter(diary_date = date, user_id = user_id) + + # api/v1/diaries/?user_id=1 + if user_id: + diaries = diaries.filter(user_id = user_id) return diaries def destroy(self, request, *args, **kwarg): diary = self.get_object() # 삭제할 개체 diary.is_deleted = True diary.save() - - response_data = { - "message": "SUCCESS", - "result": "change is_deleted = True" - } - return Response(response_data, status = status.HTTP_204_NO_CONTENT) - - -# class ResultViewset(viewsets.ModelViewSet): -# queryset = Result.objects.all() -# serializer_class = ResultSerializer + return Response(status = status.HTTP_204_NO_CONTENT) class KeywordViewset(viewsets.ModelViewSet): queryset = Keyword.objects.all() diff --git a/requirements.txt b/requirements.txt index 89ccb7d..37e880c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,6 +18,7 @@ Django==4.1.5 django-celery-results==2.4.0 django-cors-headers==3.13.0 django-environ==0.9.0 +django-filter==23.2 django-storages==1.13.2 djangorestframework==3.14.0 djangorestframework-simplejwt==5.2.0