From 0f2c31038e399b715d01005771b60ba648aa7be7 Mon Sep 17 00:00:00 2001 From: Lukas Juhrich Date: Tue, 17 Sep 2024 14:43:35 +0200 Subject: [PATCH] Extract `lib.user.user_sheet` --- pycroft/lib/user/__init__.py | 8 ++- pycroft/lib/user/_old.py | 100 +----------------------------- pycroft/lib/user/user_sheet.py | 107 +++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 102 deletions(-) create mode 100644 pycroft/lib/user/user_sheet.py diff --git a/pycroft/lib/user/__init__.py b/pycroft/lib/user/__init__.py index 505d65a0e..d7afeb516 100644 --- a/pycroft/lib/user/__init__.py +++ b/pycroft/lib/user/__init__.py @@ -1,7 +1,5 @@ from ._old import ( setup_ipv4_networking, - store_user_sheet, - get_user_sheet, create_user, login_available, move_in, @@ -16,7 +14,6 @@ move_out, UserStatus, status, - generate_user_sheet, membership_ending_task, membership_end_date, membership_beginning_task, @@ -76,6 +73,11 @@ confirm_mail_address, ) from .permission import can_target +from .user_sheet import ( + generate_user_sheet, + get_user_sheet, + store_user_sheet, +) from .exc import ( HostAliasExists, diff --git a/pycroft/lib/user/_old.py b/pycroft/lib/user/_old.py index 3e595cd24..15ffadd9c 100644 --- a/pycroft/lib/user/_old.py +++ b/pycroft/lib/user/_old.py @@ -13,7 +13,7 @@ import re import typing import typing as t -from datetime import timedelta, date +from datetime import date from difflib import SequenceMatcher from collections.abc import Iterable @@ -24,7 +24,6 @@ from pycroft.helpers import user as user_helper from pycroft.helpers.i18n import deferred_gettext from pycroft.helpers.interval import closed, Interval, starting_from -from pycroft.helpers.printing import generate_user_sheet as generate_pdf from pycroft.helpers.user import generate_random_str, login_hash from pycroft.helpers.utc import DateTimeTz from pycroft.lib.facilities import get_room @@ -57,12 +56,10 @@ PropertyGroup, ) from pycroft.model.unix_account import UnixAccount, UnixTombstone -from pycroft.model.webstorage import WebStorage from .exc import LoginTakenException, UserExistsInRoomException from .user_id import ( decode_type1_user_id, - encode_type2_user_id, decode_type2_user_id, check_user_id, ) @@ -84,57 +81,6 @@ def setup_ipv4_networking(host: Host) -> None: session.session.add(new_ip) -def store_user_sheet( - new_user: bool, - wifi: bool, - user: User | None = None, - timeout: int = 15, - plain_user_password: str = None, - generation_purpose: str = "", - plain_wifi_password: str = "", -) -> WebStorage: - """Generate a user sheet and store it in the WebStorage. - - Returns the generated :class:`WebStorage ` object holding the pdf. - - :param new_user: generate page with user details - :param wifi: generate page with wifi credantials - :param user: A pycroft user. Necessary in every case - :param timeout: The lifetime in minutes - :param plain_user_password: Only necessary if ``new_user is True`` - :param plain_wifi_password: The password for wifi. Only necessary if ``wifi is True`` - :param generation_purpose: Optional - """ - - pdf_data = generate_user_sheet( - new_user, wifi, user, - plain_user_password=plain_user_password, - generation_purpose=generation_purpose, - plain_wifi_password=plain_wifi_password, - ) - - pdf_storage = WebStorage(data=pdf_data, - expiry=session.utcnow() + timedelta(minutes=timeout)) - session.session.add(pdf_storage) - - return pdf_storage - - -def get_user_sheet(sheet_id: int) -> bytes | None: - """Fetch the storage object given an id. - - If not existent, return None. - """ - WebStorage.auto_expire() - - if sheet_id is None: - return None - if (storage := session.session.get(WebStorage, sheet_id)) is None: - return None - - return storage.data - - def create_user( name: str, login: str, email: str, birthdate: date, groups: t.Iterable[PropertyGroup], processor: User | None, address: Address, @@ -650,50 +596,6 @@ def status(user: User) -> UserStatus: ) -def generate_user_sheet( - new_user: bool, - wifi: bool, - user: User | None = None, - plain_user_password: str | None = None, - generation_purpose: str = "", - plain_wifi_password: str = "", -) -> bytes: - """Create a new datasheet for the given user. - This usersheet can hold information about a user or about the wifi credentials of a user. - - This is a wrapper for - :py:func:`pycroft.helpers.printing.generate_user_sheet` equipping - it with the correct user id. - - This function cannot be exported to a `wrappers` module because it - depends on `encode_type2_user_id` and is required by - `(store|get)_user_sheet`, both in this module. - - :param new_user: Generate a page for a new created user - :param wifi: Generate a page with the wifi credantials - - Necessary in every case: - :param user: A pycroft user - - Only necessary if new_user=True: - :param plain_user_password: The password - - Only necessary if wifi=True: - :param generation_purpose: Optional purpose why this usersheet was printed - """ - from pycroft.helpers import printing - return generate_pdf( - new_user=new_user, - wifi=wifi, - bank_account=config.membership_fee_bank_account, - user=t.cast(printing.User, user), - user_id=encode_type2_user_id(user.id), - plain_user_password=plain_user_password, - generation_purpose=generation_purpose, - plain_wifi_password=plain_wifi_password, - ) - - def membership_ending_task(user: User) -> UserTask: """ :return: Next task that will end the membership of the user diff --git a/pycroft/lib/user/user_sheet.py b/pycroft/lib/user/user_sheet.py new file mode 100644 index 000000000..af899183d --- /dev/null +++ b/pycroft/lib/user/user_sheet.py @@ -0,0 +1,107 @@ +import typing as t +from datetime import timedelta + +from pycroft import config +from pycroft.helpers.printing import generate_user_sheet as generate_pdf +from pycroft.model import session +from pycroft.model.webstorage import WebStorage +from pycroft.model.user import User + +from .user_id import encode_type2_user_id + + +def store_user_sheet( + new_user: bool, + wifi: bool, + user: User | None = None, + timeout: int = 15, + plain_user_password: str = None, + generation_purpose: str = "", + plain_wifi_password: str = "", +) -> WebStorage: + """Generate a user sheet and store it in the WebStorage. + + Returns the generated :class:`WebStorage ` object holding the pdf. + + :param new_user: generate page with user details + :param wifi: generate page with wifi credantials + :param user: A pycroft user. Necessary in every case + :param timeout: The lifetime in minutes + :param plain_user_password: Only necessary if ``new_user is True`` + :param plain_wifi_password: The password for wifi. Only necessary if ``wifi is True`` + :param generation_purpose: Optional + """ + + pdf_data = generate_user_sheet( + new_user, + wifi, + user, + plain_user_password=plain_user_password, + generation_purpose=generation_purpose, + plain_wifi_password=plain_wifi_password, + ) + + pdf_storage = WebStorage(data=pdf_data, expiry=session.utcnow() + timedelta(minutes=timeout)) + session.session.add(pdf_storage) + + return pdf_storage + + +def get_user_sheet(sheet_id: int) -> bytes | None: + """Fetch the storage object given an id. + + If not existent, return None. + """ + WebStorage.auto_expire() + + if sheet_id is None: + return None + if (storage := session.session.get(WebStorage, sheet_id)) is None: + return None + + return storage.data + + +def generate_user_sheet( + new_user: bool, + wifi: bool, + user: User | None = None, + plain_user_password: str | None = None, + generation_purpose: str = "", + plain_wifi_password: str = "", +) -> bytes: + """Create a new datasheet for the given user. + This usersheet can hold information about a user or about the wifi credentials of a user. + + This is a wrapper for + :py:func:`pycroft.helpers.printing.generate_user_sheet` equipping + it with the correct user id. + + This function cannot be exported to a `wrappers` module because it + depends on `encode_type2_user_id` and is required by + `(store|get)_user_sheet`, both in this module. + + :param new_user: Generate a page for a new created user + :param wifi: Generate a page with the wifi credantials + + Necessary in every case: + :param user: A pycroft user + + Only necessary if new_user=True: + :param plain_user_password: The password + + Only necessary if wifi=True: + :param generation_purpose: Optional purpose why this usersheet was printed + """ + from pycroft.helpers import printing + + return generate_pdf( + new_user=new_user, + wifi=wifi, + bank_account=config.membership_fee_bank_account, + user=t.cast(printing.User, user), + user_id=encode_type2_user_id(user.id), + plain_user_password=plain_user_password, + generation_purpose=generation_purpose, + plain_wifi_password=plain_wifi_password, + )