refactored email reconfirm view
This commit is contained in:
parent
9fb05f0407
commit
af0e6af5c4
|
|
@ -172,38 +172,6 @@ class ChangeEmailSerializer(serializers.ModelSerializer):
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
class ConfirmEmailSerializer(serializers.ModelSerializer):
|
|
||||||
"""Confirm user email serializer"""
|
|
||||||
x = serializers.CharField(default=None)
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
"""Meta class"""
|
|
||||||
model = models.User
|
|
||||||
fields = (
|
|
||||||
'email',
|
|
||||||
)
|
|
||||||
|
|
||||||
def validate(self, attrs):
|
|
||||||
"""Override validate method"""
|
|
||||||
email_confirmed = self.instance.email_confirmed
|
|
||||||
if email_confirmed:
|
|
||||||
raise utils_exceptions.EmailConfirmedError()
|
|
||||||
|
|
||||||
return attrs
|
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
|
||||||
"""
|
|
||||||
Override update method
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Send verification link on user email for change email address
|
|
||||||
if settings.USE_CELERY:
|
|
||||||
tasks.confirm_new_email_address.delay(instance.id)
|
|
||||||
else:
|
|
||||||
tasks.confirm_new_email_address(instance.id)
|
|
||||||
return instance
|
|
||||||
|
|
||||||
|
|
||||||
# Firebase Cloud Messaging serializers
|
# Firebase Cloud Messaging serializers
|
||||||
class FCMDeviceSerializer(serializers.ModelSerializer):
|
class FCMDeviceSerializer(serializers.ModelSerializer):
|
||||||
"""FCM Device model serializer"""
|
"""FCM Device model serializer"""
|
||||||
|
|
|
||||||
|
|
@ -8,5 +8,6 @@ app_name = 'account'
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('user/', views.UserRetrieveUpdateView.as_view(), name='user-retrieve-update'),
|
path('user/', views.UserRetrieveUpdateView.as_view(), name='user-retrieve-update'),
|
||||||
path('change-password/', views.ChangePasswordView.as_view(), name='change-password'),
|
path('change-password/', views.ChangePasswordView.as_view(), name='change-password'),
|
||||||
|
path('email/confirm/', views.SendConfirmationEmailView.as_view(), name='send-confirm-email'),
|
||||||
path('email/confirm/<uidb64>/<token>/', views.ConfirmEmailView.as_view(), name='confirm-email'),
|
path('email/confirm/<uidb64>/<token>/', views.ConfirmEmailView.as_view(), name='confirm-email'),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
"""Common account views"""
|
"""Common account views"""
|
||||||
|
from django.conf import settings
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.utils.http import urlsafe_base64_decode
|
from django.utils.http import urlsafe_base64_decode
|
||||||
from fcm_django.models import FCMDevice
|
from fcm_django.models import FCMDevice
|
||||||
|
|
@ -9,6 +10,7 @@ from rest_framework.response import Response
|
||||||
|
|
||||||
from account import models
|
from account import models
|
||||||
from account.serializers import common as serializers
|
from account.serializers import common as serializers
|
||||||
|
from authorization.tasks import send_confirm_email
|
||||||
from utils import exceptions as utils_exceptions
|
from utils import exceptions as utils_exceptions
|
||||||
from utils.models import GMTokenGenerator
|
from utils.models import GMTokenGenerator
|
||||||
from utils.views import JWTGenericViewMixin
|
from utils.views import JWTGenericViewMixin
|
||||||
|
|
@ -38,19 +40,26 @@ class ChangePasswordView(generics.GenericAPIView):
|
||||||
return Response(status=status.HTTP_200_OK)
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
class SendConfirmationEmailView(JWTGenericViewMixin):
|
class SendConfirmationEmailView(generics.GenericAPIView):
|
||||||
"""Confirm email view."""
|
"""Confirm email view."""
|
||||||
serializer_class = serializers.ConfirmEmailSerializer
|
|
||||||
queryset = models.User.objects.all()
|
|
||||||
|
|
||||||
def patch(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
"""Implement PATCH-method"""
|
"""Override create method"""
|
||||||
# Get user instance
|
user = self.request.user
|
||||||
instance = self.request.user
|
country_code = self.request.country_code
|
||||||
|
|
||||||
serializer = self.get_serializer(data=request.data, instance=instance)
|
if user.email_confirmed:
|
||||||
serializer.is_valid(raise_exception=True)
|
raise utils_exceptions.EmailConfirmedError()
|
||||||
serializer.save()
|
|
||||||
|
# Send verification link on user email for change email address
|
||||||
|
if settings.USE_CELERY:
|
||||||
|
send_confirm_email.delay(
|
||||||
|
user_id=user.id,
|
||||||
|
country_code=country_code)
|
||||||
|
else:
|
||||||
|
send_confirm_email(
|
||||||
|
user_id=user.id,
|
||||||
|
country_code=country_code)
|
||||||
return Response(status=status.HTTP_200_OK)
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -76,39 +76,6 @@ class SignupSerializer(serializers.ModelSerializer):
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
class ReconfirmSerializer(serializers.ModelSerializer):
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = account_models.User
|
|
||||||
fields = ('email',)
|
|
||||||
|
|
||||||
def validate_email(self, value):
|
|
||||||
"""Validate email"""
|
|
||||||
users = list(account_models.User.objects.filter(email=value.lower()).all())
|
|
||||||
if not users:
|
|
||||||
raise serializers.ValidationError(detail='User with mentioned email does not exist.')
|
|
||||||
users = list(filter(lambda user: not user.email_confirmed, users))
|
|
||||||
if not users:
|
|
||||||
raise serializers.ValidationError(detail='User with this email is confirmed.')
|
|
||||||
return value
|
|
||||||
|
|
||||||
def create(self, validated_data):
|
|
||||||
"""Override create method"""
|
|
||||||
queryset = account_models.User.objects.all()
|
|
||||||
email = validated_data.get('email').lower()
|
|
||||||
country_code = self.context.get('request').country_code
|
|
||||||
obj = get_object_or_404(queryset, email=email)
|
|
||||||
if settings.USE_CELERY:
|
|
||||||
tasks.send_confirm_email.delay(
|
|
||||||
user_id=obj.id,
|
|
||||||
country_code=country_code)
|
|
||||||
else:
|
|
||||||
tasks.send_confirm_email(
|
|
||||||
user_id=obj.id,
|
|
||||||
country_code=country_code)
|
|
||||||
return obj
|
|
||||||
|
|
||||||
|
|
||||||
class LoginByUsernameOrEmailSerializer(SourceSerializerMixin,
|
class LoginByUsernameOrEmailSerializer(SourceSerializerMixin,
|
||||||
serializers.ModelSerializer):
|
serializers.ModelSerializer):
|
||||||
"""Serializer for login user"""
|
"""Serializer for login user"""
|
||||||
|
|
|
||||||
|
|
@ -4,18 +4,19 @@ from django.utils.translation import gettext_lazy as _
|
||||||
from celery import shared_task
|
from celery import shared_task
|
||||||
|
|
||||||
from account import models as account_models
|
from account import models as account_models
|
||||||
|
from smtplib import SMTPException
|
||||||
|
|
||||||
logging.basicConfig(format='[%(levelname)s] %(message)s', level=logging.INFO)
|
logging.basicConfig(format='[%(levelname)s] %(message)s', level=logging.INFO)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@shared_task
|
@shared_task
|
||||||
def send_confirm_email(user_id, country_code):
|
def send_confirm_email(user_id: int, country_code: str):
|
||||||
"""Send verification email to user."""
|
"""Send verification email to user."""
|
||||||
try:
|
try:
|
||||||
obj = account_models.User.objects.get(id=user_id)
|
obj = account_models.User.objects.get(id=user_id)
|
||||||
obj.send_email(subject=_('Email confirmation'),
|
obj.send_email(subject=_('Email confirmation'),
|
||||||
message=obj.confirm_email_template(country_code))
|
message=obj.confirm_email_template(country_code))
|
||||||
except:
|
except Exception as e:
|
||||||
logger.error(f'METHOD_NAME: {send_confirm_email.__name__}\n'
|
logger.error(f'METHOD_NAME: {send_confirm_email.__name__}\n'
|
||||||
f'DETAIL: Exception occurred for user: {user_id}')
|
f'DETAIL: user {user_id}, - {e}')
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,6 @@ urlpatterns_oauth2 = [
|
||||||
|
|
||||||
urlpatterns_jwt = [
|
urlpatterns_jwt = [
|
||||||
path('signup/', views.SignUpView.as_view(), name='signup'),
|
path('signup/', views.SignUpView.as_view(), name='signup'),
|
||||||
path('signup/reconfirm/', views.ReconfirmView.as_view(), name='signup-reconfirm'),
|
|
||||||
path('signup/confirm/<uidb64>/<token>/', views.ConfirmationEmailView.as_view(),
|
path('signup/confirm/<uidb64>/<token>/', views.ConfirmationEmailView.as_view(),
|
||||||
name='signup-confirm'),
|
name='signup-confirm'),
|
||||||
path('login/', views.LoginByUsernameOrEmailView.as_view(), name='login'),
|
path('login/', views.LoginByUsernameOrEmailView.as_view(), name='login'),
|
||||||
|
|
|
||||||
|
|
@ -147,17 +147,6 @@ class SignUpView(generics.GenericAPIView):
|
||||||
return Response(status=status.HTTP_201_CREATED)
|
return Response(status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
|
||||||
class ReconfirmView(generics.CreateAPIView):
|
|
||||||
""" Resends confirmation email whether user's still not confirmed """
|
|
||||||
permission_classes = (permissions.AllowAny,)
|
|
||||||
serializer_class = serializers.ReconfirmSerializer
|
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
|
||||||
serializer = self.get_serializer(data=request.data)
|
|
||||||
serializer.is_valid(raise_exception=True)
|
|
||||||
return Response(status=status.HTTP_201_CREATED)
|
|
||||||
|
|
||||||
|
|
||||||
class ConfirmationEmailView(JWTGenericViewMixin):
|
class ConfirmationEmailView(JWTGenericViewMixin):
|
||||||
"""View for confirmation email"""
|
"""View for confirmation email"""
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user