sprint/Main/views/SendCodeView.py
Administrator 261b0d34df send code
2022-08-21 12:52:06 +03:00

49 lines
1.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from random import randrange
from django.contrib.auth import login
from django.contrib.auth.models import User
from Sprint import settings
from SprintLib.BaseView import BaseView
from SprintLib.queue import send_to_queue
class SendCodeView(BaseView):
endpoint = "send_code"
def post_create(self):
username = self.request.POST["username"]
user = User.objects.filter(username=username).first()
if not user:
return {
"success": False,
"message": "Пользователя с таким именем не существует",
}
code = randrange(10000, 100000)
print(f"Отправлен код для {username}", code)
user.userinfo.code = code
user.userinfo.save()
send_to_queue("notification", {
"type": "send_code",
"chat_id": user.userinfo.telegram_chat_id,
"text": "Код для входа в сервис: " + str(code)
})
return {"success": True, "message": "Код отправлен"}
def post_check(self):
username = self.request.POST["username"]
user = User.objects.filter(username=username).first()
if not user:
return {
"success": False,
"message": "Пользователя с таким именем не существует",
}
code = int(self.request.POST["code"])
if code == user.userinfo.code or settings.DEBUG and code == 12345:
user.userinfo.code = None
user.userinfo.save()
login(self.request, user)
return {"success": True, "message": "Успешно"}
else:
return {"success": False, "message": "Код неверен"}