From da82f45f1b8355fef7894e3b937fa5ed2a285394 Mon Sep 17 00:00:00 2001
From: sahayana <saokhueyang@gmail.com>
Date: Thu, 9 Nov 2023 22:07:35 +0400
Subject: [PATCH] =?UTF-8?q?=EC=B6=94=EA=B0=80:=20=EC=9E=84=EC=8B=9C?=
 =?UTF-8?q?=EB=B9=84=EB=B0=80=EB=B2=88=ED=98=B8=20api=20=EC=83=9D=EC=84=B1?=
 =?UTF-8?q?=20-=20random=20string=2010=EC=9E=90=EB=A6=AC=EB=A1=9C=20?=
 =?UTF-8?q?=EC=A1=B0=EC=A0=95=20-=20=EC=9E=84=EC=8B=9C=EB=B9=84=EB=B0=80?=
 =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EA=B3=A0=EC=A7=80=20=EC=9D=B4=EB=A9=94?=
 =?UTF-8?q?=EC=9D=BC=20celery=20=EB=B9=84=EB=8F=99=EA=B8=B0=20=EC=B2=98?=
 =?UTF-8?q?=EB=A6=AC=20-=20=EC=9D=B4=EB=A9=94=EC=9D=BC=EB=A7=8C=20?=
 =?UTF-8?q?=EC=9E=85=EB=A0=A5=ED=95=98=EB=A9=B4=20=EB=B9=84=EB=B0=80?=
 =?UTF-8?q?=EB=B2=88=ED=98=B8=EA=B0=80=20=EC=A0=84=EC=86=A1=EB=90=98?=
 =?UTF-8?q?=EB=8A=94=20=EC=A0=90=EC=97=90=EC=84=9C=20=EB=B3=B4=EC=95=88?=
 =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EC=9D=B4=EC=8A=88=20=EC=A1=B4=EC=9E=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 alaltalk/cache_key.py                 |  7 ++++++
 apps/account/services/user_service.py | 32 ++++++++++++++++++++++-----
 apps/account/tasks.py                 | 15 +++++++++++--
 apps/account/v1/apis/user_api.py      | 17 ++++++++++++++
 apps/account/v1/urls.py               |  5 +++++
 5 files changed, 69 insertions(+), 7 deletions(-)

diff --git a/alaltalk/cache_key.py b/alaltalk/cache_key.py
index 2894798..c1e8cf0 100644
--- a/alaltalk/cache_key.py
+++ b/alaltalk/cache_key.py
@@ -3,3 +3,10 @@
 # Friend
 RECOMMEND_FRIEND = "recommend_friend_{user_id}"
 USER_LIKE_DATA = "user_like_data_{user_id}"
+
+# Search
+
+USER_YOUTUBE = "youtube_user_{user_id}"
+USER_BOOK = "book_user_{user_id}"
+USER_NEWS = "news_user_{user_id}"
+USER_SHOPPING = "shopping_user_{user_id}"
diff --git a/apps/account/services/user_service.py b/apps/account/services/user_service.py
index 31e5446..6002362 100644
--- a/apps/account/services/user_service.py
+++ b/apps/account/services/user_service.py
@@ -3,8 +3,8 @@
 from django.utils.http import urlsafe_base64_decode
 
 from apps.account.models import CustomUser, UserLikeKeyWord, UserProfileImage
-from apps.account.tasks import send_email_verification
-from apps.account.utils import accounts_verify_token
+from apps.account.tasks import send_email_verification, send_temporary_password
+from apps.account.utils import accounts_verify_token, random_string_generator
 
 
 class UserService:
@@ -26,9 +26,7 @@ def create_single_user(
             email=email, nickname=nickname, bio=bio, password=password
         )
         if img:
-            image = UserProfileImage.objects.create(user=user, img=img)
-            user.profile_image = image
-            user.save()
+            UserProfileImage.objects.create(user=user, img=img)
 
         # TODO: Celery 비동기 처리
         transaction.on_commit(lambda: send_email_verification.delay(user.id))
@@ -84,3 +82,27 @@ def like_public_setting(cls, user_id: int, value: str) -> CustomUser:
             raise KeyError("value 값은 'ON'/'OFF'만 가능합니다.")
 
         return user
+
+    @classmethod
+    def delete_user_account(cls, user_id: int):
+
+        user = CustomUser.objects.get(id=user_id)
+        user.is_deleted = True
+        user.save()
+        return user
+
+    @classmethod
+    @transaction.atomic()
+    def change_temporary_password(cls, user_id: int):
+
+        user = CustomUser.objects.get(id=user_id)
+        temp_password = random_string_generator(length=10)
+
+        user.set_password(temp_password)
+        user.save()
+
+        transaction.on_commit(
+            lambda: send_temporary_password.delay(user.id, temp_password)
+        )
+
+        return user
diff --git a/apps/account/tasks.py b/apps/account/tasks.py
index 65d6c40..12d2481 100644
--- a/apps/account/tasks.py
+++ b/apps/account/tasks.py
@@ -1,5 +1,5 @@
 from celery import shared_task
-from django.core.mail import EmailMessage
+from django.core.mail import EmailMessage, send_mail
 from django.template.loader import render_to_string
 from django.utils.encoding import force_bytes
 from django.utils.http import urlsafe_base64_encode
@@ -11,7 +11,7 @@
 
 
 @shared_task
-def send_email_verification(user_id: int) -> None:
+def send_email_verification(user_id: int):
     """
     새로 생성한 유저에게 사용자 인증 이메일을 전송합니다.
     """
@@ -27,3 +27,14 @@ def send_email_verification(user_id: int) -> None:
     )
     email_message = EmailMessage(EMAIL_VERIFY_TITLE, message, to=[user.email])
     return email_message.send()
+
+
+@shared_task
+def send_temporary_password(email: str, temp_pw: str):
+    return send_mail(
+        "[alaltalk] 임시 비밀번호 메일입니다.",
+        f"회원님의 임시 비밀번호는 {temp_pw} 입니다.\n로그인 후 비밀번호를 꼭 변경해주세요.",
+        "sahayana@naver.com",
+        [email],
+        fail_silently=False,
+    )
diff --git a/apps/account/v1/apis/user_api.py b/apps/account/v1/apis/user_api.py
index 9a839a8..4a5b63d 100644
--- a/apps/account/v1/apis/user_api.py
+++ b/apps/account/v1/apis/user_api.py
@@ -150,6 +150,23 @@ def post(self, request, *args, **kwargs):
         return Response(data=data, status=status.HTTP_200_OK)
 
 
+class TemporaryPasswordView(views.APIView):
+
+    permission_classes = [permissions.AllowAny]
+
+    def post(self, request, *args, **kwargs):
+
+        email = request.query_params.get("q")
+        try:
+            user = CustomUser.objects.get(email=email)
+        except CustomUser.DoesNotExist:
+            return Response({"msg": "none-user"}, status=status.HTTP_400_BAD_REQUEST0)
+
+        after_user = UserService.change_temporary_password(user_id=user.id)
+        data = {"msg": "ok", "data": UserReadSerializer(after_user).data}
+        return Response(data=data, status=status.HTTP_200_OK)
+
+
 # TODO:친구 관련 API 개발에 추가
 class UserLikeKeywordSaveView(views.APIView):
 
diff --git a/apps/account/v1/urls.py b/apps/account/v1/urls.py
index 640dda9..35ed0b8 100644
--- a/apps/account/v1/urls.py
+++ b/apps/account/v1/urls.py
@@ -12,4 +12,9 @@
         name="user_activation",
     ),
     path("login", user_api.LoginView.as_view(), name="login"),
+    path(
+        "login/temp",
+        user_api.TemporaryPasswordView.as_view(),
+        name="temporary_password",
+    ),
 ]