Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat/#5] 제목+내용에 대한 검색 기능 구현 #6

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/base_model.py
Original file line number Diff line number Diff line change
@@ -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 # 이 모델은 데이터베이스 테이블을 만드는데 사용되지 않겠다.
1 change: 1 addition & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'rest_framework_simplejwt', #jwt
'corsheaders', # cors 오류 해결
'storages', #storages
'django_filters', # django-filter
]

#s3
Expand Down
8 changes: 1 addition & 7 deletions gdiary/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
43 changes: 27 additions & 16 deletions gdiary/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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(
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down