2022-11-17 18:16:34 -08:00
|
|
|
import random
|
|
|
|
import string
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.mail import send_mail
|
|
|
|
from django.db import models
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
|
|
|
|
from core.models import Config
|
|
|
|
from stator.models import State, StateField, StateGraph, StatorModel
|
|
|
|
|
|
|
|
|
|
|
|
class PasswordResetStates(StateGraph):
|
2022-11-18 07:28:15 -08:00
|
|
|
new = State(try_interval=300)
|
2022-11-17 18:16:34 -08:00
|
|
|
sent = State()
|
|
|
|
|
|
|
|
new.transitions_to(sent)
|
|
|
|
|
|
|
|
@classmethod
|
2023-07-16 23:18:00 -07:00
|
|
|
def handle_new(cls, instance: "PasswordReset"):
|
2022-11-17 18:16:34 -08:00
|
|
|
"""
|
|
|
|
Sends the password reset email.
|
|
|
|
"""
|
2023-07-16 23:18:00 -07:00
|
|
|
if instance.new_account:
|
|
|
|
send_mail(
|
2022-11-17 18:16:34 -08:00
|
|
|
subject=f"{Config.system.site_name}: Confirm new account",
|
|
|
|
message=render_to_string(
|
2022-11-17 23:09:04 -08:00
|
|
|
"emails/account_new.txt",
|
2022-11-17 18:16:34 -08:00
|
|
|
{
|
2023-07-16 23:18:00 -07:00
|
|
|
"reset": instance,
|
2022-11-17 18:16:34 -08:00
|
|
|
"config": Config.system,
|
|
|
|
"settings": settings,
|
|
|
|
},
|
|
|
|
),
|
2022-12-30 15:03:11 -08:00
|
|
|
html_message=render_to_string(
|
|
|
|
"emails/account_new.html",
|
|
|
|
{
|
2023-07-16 23:18:00 -07:00
|
|
|
"reset": instance,
|
2022-12-30 15:03:11 -08:00
|
|
|
"config": Config.system,
|
|
|
|
"settings": settings,
|
|
|
|
},
|
|
|
|
),
|
2022-11-18 16:24:43 -08:00
|
|
|
from_email=settings.SERVER_EMAIL,
|
2023-07-16 23:18:00 -07:00
|
|
|
recipient_list=[instance.user.email],
|
2022-11-17 18:16:34 -08:00
|
|
|
)
|
|
|
|
else:
|
2023-07-16 23:18:00 -07:00
|
|
|
send_mail(
|
2022-11-17 18:16:34 -08:00
|
|
|
subject=f"{Config.system.site_name}: Reset password",
|
|
|
|
message=render_to_string(
|
|
|
|
"emails/password_reset.txt",
|
|
|
|
{
|
2023-07-16 23:18:00 -07:00
|
|
|
"reset": instance,
|
2022-11-17 18:16:34 -08:00
|
|
|
"config": Config.system,
|
|
|
|
"settings": settings,
|
|
|
|
},
|
|
|
|
),
|
2022-12-30 15:03:11 -08:00
|
|
|
html_message=render_to_string(
|
|
|
|
"emails/password_reset.html",
|
|
|
|
{
|
2023-07-16 23:18:00 -07:00
|
|
|
"reset": instance,
|
2022-12-30 15:03:11 -08:00
|
|
|
"config": Config.system,
|
|
|
|
"settings": settings,
|
|
|
|
},
|
|
|
|
),
|
2022-11-18 16:24:43 -08:00
|
|
|
from_email=settings.SERVER_EMAIL,
|
2023-07-16 23:18:00 -07:00
|
|
|
recipient_list=[instance.user.email],
|
2022-11-17 18:16:34 -08:00
|
|
|
)
|
|
|
|
return cls.sent
|
|
|
|
|
|
|
|
|
|
|
|
class PasswordReset(StatorModel):
|
|
|
|
"""
|
|
|
|
A password reset for a user (this is also how we create accounts)
|
|
|
|
"""
|
|
|
|
|
|
|
|
state = StateField(PasswordResetStates)
|
|
|
|
|
|
|
|
user = models.ForeignKey(
|
|
|
|
"users.user",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="password_resets",
|
|
|
|
)
|
|
|
|
|
|
|
|
token = models.CharField(max_length=500, unique=True)
|
|
|
|
new_account = models.BooleanField()
|
|
|
|
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_for_user(cls, user):
|
|
|
|
return cls.objects.create(
|
|
|
|
user=user,
|
|
|
|
token="".join(random.choice(string.ascii_lowercase) for i in range(42)),
|
|
|
|
new_account=not user.password,
|
|
|
|
)
|