diff --git a/users/helpers.py b/users/helpers.py index 85e6ad8b..5aba1998 100644 --- a/users/helpers.py +++ b/users/helpers.py @@ -10,33 +10,6 @@ User = get_user_model() -def reset_email(user, request): - access_token = RefreshToken.for_user(user).access_token - refresh_token = RefreshToken.for_user(user) - - relative_link = reverse("users:password_reset_sent") - - current_site = get_current_site(request).domain - absolute_url = ( - f"{PROTOCOL}://{current_site}{relative_link}?" - f"access_token={access_token}&refresh_token={refresh_token}" - ) - - email_body = ( - f"Здравствуйте, {user.first_name} {user.last_name}!" - f" Перейдите по данной ссылке для смены пароля:\n {absolute_url}\n\nС уважением, " - f"Procollab!" - ) - - data = { - "email_body": email_body, - "email_subject": "Procollab | Сброс пароля", - "to_email": user.email, - } - - Email.send_email(data) - - def verify_email(user, request): token = RefreshToken.for_user(user).access_token diff --git a/users/views.py b/users/views.py index 9e81bab5..25d082f7 100644 --- a/users/views.py +++ b/users/views.py @@ -1,5 +1,3 @@ -from datetime import datetime - import jwt from django.apps import apps from django.conf import settings @@ -14,7 +12,6 @@ ListAPIView, ListCreateAPIView, RetrieveUpdateDestroyAPIView, - UpdateAPIView, RetrieveAPIView, ) from rest_framework.permissions import AllowAny, IsAuthenticated @@ -33,7 +30,6 @@ ) from projects.serializers import ProjectListSerializer from users.helpers import ( - reset_email, verify_email, check_related_fields_update, ) @@ -48,8 +44,6 @@ from users.serializers import ( AchievementDetailSerializer, AchievementListSerializer, - EmailSerializer, - PasswordSerializer, UserDetailSerializer, UserListSerializer, VerifyEmailSerializer, @@ -222,85 +216,6 @@ def get(self, request): ) -class EmailResetPassword(GenericAPIView): - serializer_class = EmailSerializer - permission_classes = [AllowAny] - - def post(self, request, *args, **kwargs): - serializer = self.get_serializer(data=request.data) - serializer.is_valid() - - user = User.objects.get(email=serializer.data["email"]) - - reset_email(user, request) - - return Response(serializer.data, status=status.HTTP_201_CREATED) - - -class ResetCurrentUserPassword(UpdateAPIView): - serializer_class = PasswordSerializer - permission_classes = [AllowAny] - - def get(self, request, *args, **kwargs): - refresh_token = request.GET.get("refresh_token") - try: - RefreshToken(refresh_token).check_blacklist() - except TokenError: - return redirect( - "https://procollab.ru/auth/reset_password/", - status=status.HTTP_400_BAD_REQUEST, - message="Used token", - ) - - return Response({"message": "Enter new password"}) - - def update(self, request, *args, **kwargs): - serializer = self.get_serializer(data=request.data) - serializer.is_valid() - - try: - refresh_token = request.GET.get("refresh_token") - access_token = request.GET.get("access_token") - - payload = jwt.decode( - jwt=access_token, key=settings.SECRET_KEY, algorithms=["HS256"] - ) - user = User.objects.get(id=payload["user_id"]) - - # fixme - last_update = user.datetime_updated - frequency_update = datetime.utcnow().minute - last_update.minute - if frequency_update <= 10: - return redirect( - "https://procollab.ru/auth/reset_password/", - status=status.HTTP_400_BAD_REQUEST, - message="You can't change your password so often", - ) - - user.set_password(serializer.data["new_password"]) - user.save() - - RefreshToken(refresh_token).blacklist() - return redirect( - "https://app.procollab.ru/auth/reset_password/", - status=status.HTTP_200_OK, - message="Succeed", - ) - - except jwt.ExpiredSignatureError: - return redirect( - "https://app.procollab.ru/auth/reset_password/", - status=status.HTTP_400_BAD_REQUEST, - message="Activate Expired", - ) - except jwt.DecodeError: - return redirect( - "https://app.procollab.ru/auth/reset_password/", - status=status.HTTP_400_BAD_REQUEST, - message="Decode error", - ) - - class AchievementList(ListCreateAPIView): queryset = UserAchievement.objects.get_achievements_for_list_view() serializer_class = AchievementListSerializer