mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 10:05:21 +00:00
added task to send otp on email
This commit is contained in:
@ -90,7 +90,8 @@ ERROR_CODE = {
|
||||
# email not null
|
||||
"2062": "Please enter email address",
|
||||
"2063": "Unauthorized access.",
|
||||
"2064": "To change your password first request an OTP and get it verify then change your password."
|
||||
"2064": "To change your password first request an OTP and get it verify then change your password.",
|
||||
"2065": "Passwords do not match. Please try again."
|
||||
}
|
||||
"""Success message code"""
|
||||
SUCCESS_CODE = {
|
||||
|
29
base/tasks.py
Normal file
29
base/tasks.py
Normal file
@ -0,0 +1,29 @@
|
||||
"""
|
||||
web_admin tasks file
|
||||
"""
|
||||
# third party imports
|
||||
from celery import shared_task
|
||||
from templated_email import send_templated_mail
|
||||
|
||||
# django imports
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
@shared_task
|
||||
def send_email_otp(email, verification_code):
|
||||
"""
|
||||
used to send otp on email
|
||||
:param email: e-mail
|
||||
:param verification_code: otp
|
||||
"""
|
||||
from_email = settings.EMAIL_FROM_ADDRESS
|
||||
recipient_list = [email]
|
||||
send_templated_mail(
|
||||
template_name='email_reset_verification.email',
|
||||
from_email=from_email,
|
||||
recipient_list=recipient_list,
|
||||
context={
|
||||
'verification_code': verification_code
|
||||
}
|
||||
)
|
||||
return True
|
@ -7,15 +7,14 @@ from datetime import datetime
|
||||
# django imports
|
||||
from rest_framework import serializers
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
from templated_email import send_templated_mail
|
||||
|
||||
# local imports
|
||||
from account.models import UserEmailOtp
|
||||
from base.constants import USER_TYPE
|
||||
from base.messages import ERROR_CODE
|
||||
from guardian.tasks import generate_otp
|
||||
from base.tasks import send_email_otp
|
||||
|
||||
USER = get_user_model()
|
||||
|
||||
@ -53,26 +52,16 @@ class AdminOTPSerializer(serializers.ModelSerializer):
|
||||
verification_code = generate_otp()
|
||||
|
||||
# Send the verification code to the user's email
|
||||
from_email = settings.EMAIL_FROM_ADDRESS
|
||||
recipient_list = [email]
|
||||
send_templated_mail(
|
||||
template_name='email_reset_verification.email',
|
||||
from_email=from_email,
|
||||
recipient_list=recipient_list,
|
||||
context={
|
||||
'verification_code': verification_code
|
||||
}
|
||||
)
|
||||
send_email_otp.delay(email, verification_code)
|
||||
|
||||
expiry = timezone.now() + timezone.timedelta(days=1)
|
||||
user_data, created = UserEmailOtp.objects.get_or_create(email=email)
|
||||
if created:
|
||||
user_data.expired_at = expiry
|
||||
user_data.user_type = dict(USER_TYPE).get('3')
|
||||
if user_data:
|
||||
user_data.otp = verification_code
|
||||
user_data.expired_at = expiry
|
||||
user_data.user_type = dict(USER_TYPE).get('3')
|
||||
user_data.save()
|
||||
user_data, created = UserEmailOtp.objects.update_or_create(email=email,
|
||||
defaults={
|
||||
"otp": verification_code,
|
||||
"expired_at": expiry,
|
||||
"user_type": dict(USER_TYPE).get('3'),
|
||||
})
|
||||
|
||||
return user_data
|
||||
|
||||
|
||||
@ -134,7 +123,7 @@ class AdminCreatePasswordSerializer(serializers.ModelSerializer):
|
||||
|
||||
# matching password
|
||||
if new_password != confirm_password:
|
||||
raise serializers.ValidationError('password do not match')
|
||||
raise serializers.ValidationError(ERROR_CODE['2065'])
|
||||
|
||||
user_otp_details = UserEmailOtp.objects.filter(email=email).last()
|
||||
if not user_otp_details:
|
||||
|
Reference in New Issue
Block a user