Skip to content

Commit

Permalink
#517 feat: Add Forest User Catetory (#520)
Browse files Browse the repository at this point in the history
* #517 feat: Add Forest User Catetory

* Delete forest/migrations/0006_forestusercategory.py

* #517 delete forest/migrations/0007_delete_forestusercategory.py

---------

Co-authored-by: SeoJimin <jnuni004@naver.com>
  • Loading branch information
SeoJimin1234 and SeoJimin authored Sep 12, 2023
1 parent c3fd9af commit c6578a3
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 1 deletion.
22 changes: 22 additions & 0 deletions forest/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,25 @@ def like_or_dislike(forest_comment: ForestComment, user: User) -> bool:
forest_comment.save()

return True

class ForestUserCategoryService:
def __init__(self, user: User):
self.user = user

def save_usercategory(self, semi_categories:list[str]) -> User:

self.user.semi_categories.clear()

if semi_categories:
for category_id_str in semi_categories:
try:
category_id = int(category_id_str)
# 'semi_categories'가 many-to-many 필드인 경우, 각 ID를 연결하기 위해 'add'를 사용
self.user.semi_categories.add(category_id)
except ValueError:
# 유효하지 않은 ID를 무시하거나 로깅할 수 있습니다.
pass

self.user.save()

return self.user
2 changes: 2 additions & 0 deletions forest/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@
ForestCommentDeleteApi.as_view(), name='forest_comment_delete'),
path('<int:forest_id>/comments/<int:forest_comment_id>/like/',
ForestCommentLikeApi.as_view(), name='forest_comment_like'),
path('user_categories/save/',
ForestUserCategoryApi.as_view(), name='forest_user_categories_create'),
]
66 changes: 65 additions & 1 deletion forest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
from drf_yasg.utils import swagger_auto_schema

from core.views import get_paginated_response
from .services import ForestCoordinatorService, ForestPhotoService, ForestService, ForestCommentService
from .services import ForestCoordinatorService, ForestPhotoService, ForestService, ForestCommentService, ForestUserCategoryService
from .selectors import ForestSelector, CategorySelector, ForestCommentSelector
from .permissions import IsWriter
from .models import Forest, ForestComment
from users.serializers import UserSerializer


class ForestCreateApi(APIView):
Expand Down Expand Up @@ -849,3 +850,66 @@ def post(self, request, forest_id):
return Response({
'status': 'success',
}, status=status.HTTP_201_CREATED)


class ForestUserCategoryApi(APIView):
permission_classes = (IsAuthenticated, )


class ForestUserCategoryInputSerializer(serializers.Serializer):
semi_categories = serializers.ListField()

class Meta:
examples ={
'semi_categories' : ['1','2','3']
}

@swagger_auto_schema(
request_body=ForestUserCategoryInputSerializer,
operation_id='나만의 카테고리 저장',
operation_description='''
전달된 세미카테고리 id 값을 기반으로 유저가 선택한 나만의 카테고리 id 리스트를 저장합니다.
''',
responses={
"200": openapi.Response(
description="OK",
examples={
"application/json":{
"status": 'success',
"id": 1,
"gender": "male",
"nickname": "sdpofficial",
"birthdate": "2000.03.12",
"email": "sdptech@gmail.com",
"address": "서대문구 연세로",
"profile_image": "https://abc.com/1.jpg",
"is_sdp_admin": 'true',
"is_verified": 'false',
"introduction": "안녕하세요",
'semi_categories': ['1','2','3']
}
}
),
"400": openapi.Response(
description="Bad Request",
),
},
)
def post(self, request):
serializer = self.ForestUserCategoryInputSerializer(data = request.data, partial=True)
serializer.is_valid(raise_exception=True)
data = serializer.validated_data

service = ForestUserCategoryService(user=request.user)

semi_category = service.save_usercategory(
semi_categories=data.get('semi_categories', None),
)

user_serializer = UserSerializer(semi_category)

return Response({
'status':'success',
'data' : user_serializer.data

},status=status.HTTP_200_OK)
19 changes: 19 additions & 0 deletions users/migrations/0011_user_semi_categories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.0 on 2023-09-09 07:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('forest', '0007_delete_forestusercategory'),
('users', '0010_user_introduction'),
]

operations = [
migrations.AddField(
model_name='user',
name='semi_categories',
field=models.ManyToManyField(blank=True, related_name='semi_category', to='forest.SemiCategory'),
),
]
2 changes: 2 additions & 0 deletions users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class User(AbstractBaseUser, PermissionsMixin):
follows = models.ManyToManyField(
"users.User", related_name='followers', blank=True)

semi_categories = models.ManyToManyField("forest.SemiCategory", related_name='semi_category')


USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
Expand Down
1 change: 1 addition & 0 deletions users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Meta:
"address",
"profile_image",
"is_sdp_admin",
"semi_categories"
)
read_only_fields = ("id",)

Expand Down

0 comments on commit c6578a3

Please sign in to comment.