SOITA/users/managers.py

68 lines
2.4 KiB
Python
Raw Normal View History

2021-12-11 22:01:14 +01:00
import random
from django.apps import apps
2021-12-05 15:23:29 +01:00
from django.contrib.auth.base_user import BaseUserManager
2021-12-11 22:01:14 +01:00
from django.contrib.auth.tokens import default_token_generator
from config import settings
from users import cons
from tools.emails.data import EmailSender
from tools.tools import encode_uid, decode_uid
2021-12-05 15:23:29 +01:00
from .querysets import UserQuerySet
class UserManager(BaseUserManager):
def get_queryset(self):
2021-12-05 15:50:15 +01:00
return UserQuerySet(self.model, using=self._db)
def create(self, email, password=None, **kwargs):
2021-12-11 22:01:14 +01:00
user = self.model(email=email, password=password, **kwargs)
user.set_password(password)
user.save()
return user
2021-12-05 15:50:15 +01:00
2021-12-11 22:01:14 +01:00
def reset_password(self, user_email):
User = apps.get_model("users", "User")
user = User.objects.get(email=user_email["email"])
credits = self.get_uid_and_activation_token(user)
self.prepare_code(user, credits["token"], credits["uid"])
2021-12-05 15:50:15 +01:00
2021-12-11 22:01:14 +01:00
def get_uid_and_activation_token(self, user):
token_generator = default_token_generator
token = token_generator.make_token(user)
uid = encode_uid(user.id)
return {"uid": uid, "token": token}
def prepare_code(self, user, token, uid):
reset_code = int("".join(random.choice("0123456789") for _ in range(6)))
user.reset_code = reset_code
2021-12-05 15:50:15 +01:00
user.save()
2021-12-11 22:01:14 +01:00
self.send_email(user, uid, token, user.reset_code)
2021-12-05 15:50:15 +01:00
2021-12-11 22:01:14 +01:00
# def create_link(self, uid, token, code):
# return f"{settings.CLIENT_DOMAIN}/password/reset/?uid={uid}&token={token}&code={code}"
def send_email(self, user, uid, token, reset_code):
EmailSender(self.prepare_data_to_send_email(user, uid, token, reset_code)).send_email()
def prepare_data_to_send_email(self, user, uid, token, reset_code):
return {
"subject": f"{cons.PASSWORD_RESET}",
"email_to": user.email,
"user_first_name": user.first_name,
"user_last_name": user.last_name,
"uid": uid,
"token": token,
"reset_code": reset_code,
"link": "http://localhost:8000/users/password/reset/confirm"
}
def confirm_reset_password(self, **validated_data):
User = apps.get_model("users", "User")
user = User.objects.get(id=decode_uid(validated_data["uid"]))
user.set_password(validated_data["new_password"])
user.reset_code = None
user.save(update_fields=["password", "reset_code"])