Source code for smarter.apps.account.email_links

"""
The links that the platform emails to a user: making one, and reading one back.

A link is ``/<path>/<uidb64>/<token>/``: the user's encoded id and a token that expires, made by
:class:`~smarter.lib.django.token_generators.ExpiringTokenGenerator`. The token hashes the user's password
and last sign-in, so a link stops working once its user changes their password or signs in. Password
reset links (:mod:`smarter.apps.account.password_reset`) and account activation links
(:mod:`smarter.apps.account.activation`) are both made and read here.
"""

from http import HTTPStatus

from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _

from smarter.apps.account.models import User
from smarter.common.exceptions import SmarterValueError
from smarter.lib.django.token_generators import (
    ExpiringTokenGenerator,
    SmarterTokenConversionError,
    SmarterTokenExpiredError,
    SmarterTokenIntegrityError,
    SmarterTokenParseError,
)
from smarter.lib.django.views import SmarterView

expiring_token = ExpiringTokenGenerator()


[docs] class EmailLinkError(Exception): """ An emailed link that can't be used. :param message: Why, for the user. :param status: The http status that answers it: 404 for an unknown user, 403 for an expired link, 400 for a malformed or used one. """
[docs] def __init__(self, message: str, status: HTTPStatus): super().__init__(message) self.status = status
[docs] def render_email(request: HttpRequest, template_path: str, context: dict[str, dict[str, str]]) -> str: """ An email's html, rendered from its template, without comments and minified. :param request: The request, for the template's context processors, e.g. the branding. :param template_path: The email's template. :param context: The template's context, e.g. the link. :returns: The html. :raises RuntimeError: when the template can't be rendered. """ body = SmarterView().render_clean_html(request, template_path=template_path, context=context) if isinstance(body, HttpResponse): # render_clean_html() logged why the template failed. raise RuntimeError(f"could not render {template_path}.") return body