Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions makourse/account/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
path('<str:provider>/login/', SocialLoginAPIView.as_view(), name='social-login'), # 소셜 로그인
path('<str:provider>/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/update', ProfileAPIView.as_view(), name='profile-update'), # 프로필 수정 (patch)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

profile에서 이름과 사진을 모두 변경 가능한 api여서 url 이름을 바꿔주었습니다

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변경한 이름이 맞는거 같슴다

path('profile/image-reset', ProfileAPIView.as_view(), name='profile-image-reset'), # 프로필 기본 이미지로 변경 (post)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

profile이라는걸로 하나 묶고 /image-reset하는게 더 보기 편할거 같아서 변경해주었어요!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 그냥 하나의 url로 해도 괜찮지 않나요? POST/ GET/PATCH 사용하는거 같은데.. 뭔가 구분하기 어려울라나?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 그렇네요.. 어떻게 할까요..? 하나로 해야 swagger에도 여러개로 안나올거 같은데 프론트에서만 잘 구별만 해준다면..
swagger를 보면서 한다면 3개를 하나로 (profile) 묶어도 괜찮을거 같아요
그렇게 수정할까요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇게 하시죠!
아니 근데 왜 안나오지

path('profile', ProfileAPIView.as_view(), name='profile'), # 유저정보(프로필 사진 포함)

# 유저에 따른 schedule 정보 보기
Expand Down
119 changes: 78 additions & 41 deletions makourse/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Binary file added makourse/media/user_photo/짱구.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.