Skip to content

Commit

Permalink
migrate send_password_reset_mail to .mail and delete _old
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed Sep 20, 2024
1 parent 5e6504f commit eaec37c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 52 deletions.
4 changes: 1 addition & 3 deletions pycroft/lib/user/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from ._old import (
send_password_reset_mail,
)
from .user_id import (
encode_type1_user_id,
decode_type1_user_id,
Expand Down Expand Up @@ -70,6 +67,7 @@
group_send_mail,
send_member_request_merged_email,
send_confirmation_email,
send_password_reset_mail,
)
from .mail_confirmation import (
confirm_mail_address,
Expand Down
45 changes: 0 additions & 45 deletions pycroft/lib/user/_old.py

This file was deleted.

23 changes: 23 additions & 0 deletions pycroft/lib/user/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
MailTemplate,
Mail,
UserConfirmEmailTemplate,
UserResetPasswordTemplate,
MemberRequestMergedTemplate,
)
from pycroft.model import session
Expand All @@ -27,6 +28,7 @@
)

mail_confirm_url = os.getenv("MAIL_CONFIRM_URL")
password_reset_url = os.getenv("PASSWORD_RESET_URL")


def format_user_mail(user: User, text: str) -> str:
Expand Down Expand Up @@ -159,3 +161,24 @@ def send_confirmation_email(user: BaseUser) -> None:
email_confirm_url=mail_confirm_url.format(user.email_confirmation_key)
),
)


def send_password_reset_mail(user: User) -> bool:
user.password_reset_token = generate_random_str(64)

if not password_reset_url:
raise ValueError("No url specified in PASSWORD_RESET_URL")

try:
user_send_mail(
user,
UserResetPasswordTemplate(
password_reset_url=password_reset_url.format(user.password_reset_token)
),
use_internal=False,
)
except ValueError:
user.password_reset_token = None
return False

return True
2 changes: 1 addition & 1 deletion pycroft/lib/user/mail_confirmation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)

from .member_request import finish_member_request
from ._old import user_send_mail
from .mail import user_send_mail


@with_transaction
Expand Down
4 changes: 1 addition & 3 deletions pycroft/lib/user/member_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
RoomHistoryEntry,
)

from ._old import (
user_send_mail,
)
from .edit import (
edit_birthdate,
edit_name,
Expand All @@ -48,6 +45,7 @@
)
from .mail import (
send_confirmation_email,
user_send_mail,
)
from .user_id import (
check_user_id,
Expand Down

0 comments on commit eaec37c

Please sign in to comment.