Проект OTP API для входа, регистрации и верификации с использованием Django rest framework
Я хочу сделать API проект, в котором человек может войти в систему, зарегистрироваться с помощью номера телефона и OTP.
Что нам нужно сделать в этом проекте
- Login and Register from the same page.
- Verify OTP from verify image.
- When user gets register for the first time mobile number, otp, a random username,a random picture from a folder,a random profile id, a name whic is the same as mobile number gets alloted in the database.
Вот мой код
modals.py
class User(models.Model):
mobile = models.CharField(max_length=20)
otp = models.CharField(max_length=6)
name = models.CharField(max_length=200)
username = models.CharField(max_length=200)
logo = models.FileField(upload_to ='profile/')
profile_id = models.CharField(max_length=200)
serializers.py
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['mobile']
def create(self, validated_data):
instance = self.Meta.model(**validated_data)
global totp
secret = pyotp.random_base32()
totp = pyotp.TOTP(secret, interval=300)
otp = totp.now()
instance = self.Meta.model.objects.update_or_create(**validated_data, defaults=dict(otp=str(random.randint(1000 , 9999))))[0]
instance.save()
return instance
class VerifyOTPSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['mobile','otp']
def create(self,validated_data):
instance = self.Meta.model(**validated_data)
mywords = "123456789"
res = "expert@" + str(''.join(random.choices(mywords,k = 6)))
path = os.path.join(BASE_DIR, 'static')
dir_list = os.listdir(path)
random_logo = random.choice(dir_list)
instance = self.Meta.model.objects.update_or_create(**validated_data, defaults = dict(username = res,name = instance.mobile ,logo = random_logo, profile_id = res))[0]
instance.save()
return instance
views.py
settings.py
STATIC_URL = 'static/'
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [STATIC_DIR]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Урлы приложения.py
path('register/', RegistrationAPIView.as_view()),
path('verify', VerifyOTPView.as_view()),
Теперь есть две проблемы.
- The random image is not getting saved in database i get error of not found.
- i am getting all the results i want when i register everything gets alloted fine except images but when i login with the same mobile number all the data gets updated again and i get everything new in the database. i don't want anything updated except otp.Otp should get updated everytime user login with the number but data should remain same as it was alloted at the time of registeration.