diff --git a/makourse/account/urls.py b/makourse/account/urls.py index 693e8a7..a6a96c4 100644 --- a/makourse/account/urls.py +++ b/makourse/account/urls.py @@ -6,9 +6,8 @@ path('/login/', SocialLoginAPIView.as_view(), name='social-login'), # 소셜 로그인 path('/callback/', SocialLoginAPIView.as_view(), name='social-login-redirect'), # 소셜 로그인 path('logout/', LogoutAPIView.as_view(), name='logout'), # 로그아웃 - path('profile-image/update', ProfileAPIView.as_view(), name='profile-image-update'), # 프로필 사진 업로드 (post) - path('profile-image/reset', ProfileAPIView.as_view(), name='profile-image-reset'), # 프로필 기본 이미지로 변경 (patch) - path('profile', ProfileAPIView.as_view(), name='profile'), # 유저정보(프로필 사진 포함) + path('profile', ProfileAPIView.as_view(), name='profile'), + # Get 유저정보(프로필 사진 포함), Patch 프로필 수정, Post 프로필 기본 이미지로 변경 # 유저에 따른 schedule 정보 보기 path('schedules', UserSchedulesView.as_view(), name='user-schedules'), diff --git a/makourse/account/views.py b/makourse/account/views.py index d556e09..9984cdf 100644 --- a/makourse/account/views.py +++ b/makourse/account/views.py @@ -222,75 +222,112 @@ class ProfileAPIView(APIView): # 프로필 이미지 업로드 및 수정 @swagger_auto_schema( tags=['유저'], - operation_summary="프로필 사진 업로드", - request_body=openapi.Schema( - type=openapi.TYPE_OBJECT, - properties={ - 'profile_image': openapi.Schema(type=openapi.TYPE_FILE, description='The new profile image file.') - }, - required=['profile_image'] - ), + operation_summary="기본 프로필로 reset", responses={ - 200: openapi.Response(description="Profile image updated successfully.", schema=openapi.Schema( + 200: openapi.Response(description="Profile image reset to default.", schema=openapi.Schema( type=openapi.TYPE_OBJECT, properties={ 'message': openapi.Schema(type=openapi.TYPE_STRING, description='Success message'), - 'profile_image': openapi.Schema(type=openapi.TYPE_STRING, description='URL of the updated profile image') + 'profile_image': openapi.Schema(type=openapi.TYPE_STRING, description='URL of the default profile image') } )), - 400: openapi.Response(description="Error message (e.g., no image file provided).") + 400: openapi.Response(description="Error message if something goes wrong.") } ) - def post(self, request): - # 현재 로그인된 사용자 가져오기 + def post(self, request): # 기본이미지로 설정할 때 POST method 사용 user = request.user - # 업로드된 파일 가져오기 - profile_image = request.FILES.get('profile_image') - if not profile_image: - return Response({'error': 'No image file provided'}, status=400) - # 현재 프로필 이미지가 default가 아닌 경우 삭제 - if user.profile_image.name != 'user_photo/default.png': + if user.profile_image.name != 'user_photo/default.png': # 현재 프로필이 기본 이미지가 아닌 경우 profile_image_path = os.path.join(settings.MEDIA_ROOT, user.profile_image.name) if os.path.exists(profile_image_path): os.remove(profile_image_path) # 파일 삭제 - # 사용자 프로필 이미지 업데이트 - user.profile_image = profile_image + # 프로필 이미지를 기본 이미지로 설정 + user.profile_image = 'user_photo/default.png' user.save() - return Response({'message': 'Profile image updated successfully', 'profile_image': user.profile_image.url}) + return Response({'message': 'Profile image reset to default', 'profile_image': user.profile_image.url}) @swagger_auto_schema( tags=['유저'], - operation_summary="기본 프로필로 reset", + operation_summary="유저 정보 변경", + consumes=['multipart/form-data'], # 파일 업로드 시 꼭 추가 + request_body=openapi.Schema( + type=openapi.TYPE_OBJECT, + properties={ + 'profile_image': openapi.Schema( + type=openapi.TYPE_FILE, + description='새 프로필 이미지 파일' + ), + 'name': openapi.Schema( + type=openapi.TYPE_STRING, + description='새 유저 이름' + ), + }, + ), responses={ - 200: openapi.Response(description="Profile image reset to default.", schema=openapi.Schema( - type=openapi.TYPE_OBJECT, - properties={ - 'message': openapi.Schema(type=openapi.TYPE_STRING, description='Success message'), - 'profile_image': openapi.Schema(type=openapi.TYPE_STRING, description='URL of the default profile image') - } - )), - 400: openapi.Response(description="Error message if something goes wrong.") + 200: openapi.Response( + description="프로필 수정 성공", + schema=openapi.Schema( + type=openapi.TYPE_OBJECT, + properties={ + 'message': openapi.Schema( + type=openapi.TYPE_STRING, + description='성공 메시지' + ), + 'user': openapi.Schema( + type=openapi.TYPE_OBJECT, + properties={ + "id": openapi.Schema(type=openapi.TYPE_INTEGER, description="사용자 ID"), + "email": openapi.Schema(type=openapi.TYPE_STRING, format="email", description="사용자 이메일"), + "profile_image": openapi.Schema(type=openapi.TYPE_STRING, format="uri", description="프로필 이미지 URL"), + "name": openapi.Schema(type=openapi.TYPE_STRING, description="사용자 이름"), + "social_provider": openapi.Schema(type=openapi.TYPE_STRING, description="소셜 로그인 제공자"), + "is_logged_in": openapi.Schema(type=openapi.TYPE_BOOLEAN, description="로그인 여부") + } + ), + } + ) + ), + 400: openapi.Response(description="에러 메시지(예: 파일이 제공되지 않음).") } ) - def patch(self, request): # 기본이미지로 설정할 때 PATCH method 사용 + def patch(self, request): # 유저 정보 변경 + # 현재 로그인된 사용자 가져오기 user = request.user - # 현재 프로필 이미지가 default가 아닌 경우 삭제 - if user.profile_image.name != 'user_photo/default.png': # 현재 프로필이 기본 이미지가 아닌 경우 - profile_image_path = os.path.join(settings.MEDIA_ROOT, user.profile_image.name) - if os.path.exists(profile_image_path): - os.remove(profile_image_path) # 파일 삭제 + # 업로드된 파일 가져오기 + profile_image = request.FILES.get('profile_image') + # 이름 변경 + user_name = request.data.get('name') + + if profile_image: + # 현재 프로필 이미지가 default가 아닌 경우 삭제 + if user.profile_image.name != 'user_photo/default.png': + profile_image_path = os.path.join(settings.MEDIA_ROOT, user.profile_image.name) + if os.path.exists(profile_image_path): + os.remove(profile_image_path) # 파일 삭제 + + # 사용자 프로필 이미지 업데이트 + user.profile_image = profile_image + user.save() - # 프로필 이미지를 기본 이미지로 설정 - user.profile_image = 'user_photo/default.png' - user.save() + if user_name: + user.name = user_name + user.save() - return Response({'message': 'Profile image reset to default', 'profile_image': user.profile_image.url}) + user_data = { + "id": user.id, + "email": user.email, + "profile_image": user.profile_image.url, + "name": user.name, + "social_provider": user.social_provider, + "is_logged_in": user.is_logged_in + } + + return Response({'message': 'Profile updated successfully', 'user': user_data}) @swagger_auto_schema(