From d950936f4b5e888a36bb1bd4f280038313461504 Mon Sep 17 00:00:00 2001 From: Heekyeong Park Date: Tue, 11 Jul 2023 15:11:21 +0900 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20date,=20user=5Fid=20=ED=95=84?= =?UTF-8?q?=ED=84=B0=EB=A7=81=20=EB=90=9C=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gdiary/views.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/gdiary/views.py b/gdiary/views.py index 8af0aec..a0a99b9 100644 --- a/gdiary/views.py +++ b/gdiary/views.py @@ -197,37 +197,40 @@ 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) + + 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() From 3bfd6755346693c9b40fe9a9548230574ae8cd94 Mon Sep 17 00:00:00 2001 From: Heekyeong Park Date: Fri, 14 Jul 2023 16:15:48 +0900 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20base=20model=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/base_model.py | 10 ++++++++++ gdiary/models.py | 8 +------- 2 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 config/base_model.py 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/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): From 3b705c8ab8237127c16315f818c44d5f85ecd088 Mon Sep 17 00:00:00 2001 From: Heekyeong Park Date: Fri, 14 Jul 2023 16:16:18 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20title,=20contents=20=ED=95=84?= =?UTF-8?q?=EB=93=9C=EC=97=90=20=EB=8C=80=ED=95=9C=20=EA=B2=80=EC=83=89=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/settings.py | 1 + gdiary/views.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) 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/views.py b/gdiary/views.py index a0a99b9..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( @@ -213,14 +218,17 @@ def list(self, request, *args, **kwargs): # 일기 리스트 조회 및 날짜 별 일기 조회 def get_queryset(self): diaries = Diary.objects.filter(is_deleted = False) - + + 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& + + # api/v1/diaries/?user_id=1 if user_id: diaries = diaries.filter(user_id = user_id) return diaries From fa5cb380248bf5ed1a498355bb51ce9c0d5ba641 Mon Sep 17 00:00:00 2001 From: Heekyeong Park Date: Tue, 18 Jul 2023 11:30:16 +0900 Subject: [PATCH 4/4] =?UTF-8?q?docs:=20requirements.txt=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 1 + 1 file changed, 1 insertion(+) 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