Login va Register to'girlandi

This commit is contained in:
2025-11-21 16:33:11 +05:00
parent 256e80cc23
commit 3da10c2a7f
9 changed files with 94 additions and 27 deletions

View File

@@ -1,4 +1,4 @@
#type: ignore
# type: ignore
import random
from datetime import datetime, timedelta
@@ -22,16 +22,18 @@ class SmsService:
if env.bool("OTP_PROD", False):
code = "".join(str(random.randint(0, 9)) for _ in range(env.int("OTP_SIZE", 4)))
else:
code = env.int("OTP_DEFAULT", 1111)
code = env.int("OTP_DEFAULT", 1111)
print("ishlayapti01")
sms_confirm, status = models.SmsConfirm.objects.get_or_create(phone=phone, defaults={"code": code})
print(sms_confirm)
print(status)
sms_confirm.sync_limits()
print("ishlayapti")
if sms_confirm.resend_unlock_time is not None:
expired = sms_confirm.interval(sms_confirm.resend_unlock_time)
exception = exceptions.SmsException(f"Resend blocked, try again in {expired}", expired=expired)
raise exception
print("ishlayapti2")
sms_confirm.code = code
sms_confirm.try_count = 0
@@ -41,9 +43,11 @@ class SmsService:
sms_confirm.resend_unlock_time = datetime.now() + timedelta(
seconds=models.SmsConfirm.SMS_EXPIRY_SECONDS
) # noqa
print("ishlayapti3")
sms_confirm.save()
SendConfirm.delay(phone, code)
print("ishlayapti4")
return True
@staticmethod

View File

@@ -1,7 +1,7 @@
from datetime import datetime
from core.services import sms
from django.contrib.auth import get_user_model, hashers
from django.contrib.auth import get_user_model
from django.utils.translation import gettext as _
from django_core import exceptions
from rest_framework.exceptions import PermissionDenied
@@ -17,20 +17,19 @@ class UserService(sms.SmsService):
"access": str(refresh.access_token),
}
def create_user(self, phone, first_name, last_name, password):
get_user_model().objects.update_or_create(
def create_user(self, phone):
user, created = get_user_model().objects.update_or_create(
phone=phone,
defaults={
"phone": phone,
"first_name": first_name,
"last_name": last_name,
"password": hashers.make_password(password),
},
)
user.set_unusable_password()
user.save()
def send_confirmation(self, phone) -> bool:
try:
self.send_confirm(phone)
sms.SmsService.send_confirm(phone)
return True
except exceptions.SmsException as e:
raise PermissionDenied(_("Qayta sms yuborish uchun kuting: {}").format(e.kwargs.get("expired")))