diff --git a/apps/users/models.py b/apps/users/models.py index aa47c36..76b65f1 100644 --- a/apps/users/models.py +++ b/apps/users/models.py @@ -9,13 +9,14 @@ from django.contrib.auth.validators import UnicodeUsernameValidator from django.db import models from django.utils import timezone from django.utils.translation import gettext_lazy as _ -from rest_framework.exceptions import APIException import uuid from .provinces_and_cities import state from .tasks import send_notification from apps.users.constans import MAX_OTP_TRY, DEVELOPMENT_PHONE_NUMBERS +from .validators import PhoneNumberValidator + class UserManager(BaseUserManager): use_in_migrations = True @@ -106,7 +107,8 @@ class User(AbstractUser): blank=True, null=True ) - phone_number = models.CharField(_('phone number'), max_length=30, blank=True, null=True, unique=True) + phone_number = models.CharField(_('phone number'), max_length=30, blank=True, null=True, unique=True, + validators=[PhoneNumberValidator],) otp = models.CharField(_('otp'), max_length=6, blank=True, null=True) otp_expire = models.DateTimeField(_('otp expire'), blank=True, null=True) otp_try = models.IntegerField(_('otp try'), blank=True, null=True, default=0) diff --git a/apps/users/validators.py b/apps/users/validators.py new file mode 100644 index 0000000..5638afe --- /dev/null +++ b/apps/users/validators.py @@ -0,0 +1,12 @@ +from django.core import validators +from django.utils.deconstruct import deconstructible +from django.utils.translation import gettext_lazy as _ + + +@deconstructible +class PhoneNumberValidator(validators.RegexValidator): + regex = r'^\+98\d{10,10}$' + message = _( + "Phone number must be entered in the format: '+989999999999'." + ) + flags = 0