Skip to content

Commit

Permalink
Extract lib.user.user_sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed Sep 17, 2024
1 parent e6b3b22 commit 0f2c310
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 102 deletions.
8 changes: 5 additions & 3 deletions pycroft/lib/user/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from ._old import (
setup_ipv4_networking,
store_user_sheet,
get_user_sheet,
create_user,
login_available,
move_in,
Expand All @@ -16,7 +14,6 @@
move_out,
UserStatus,
status,
generate_user_sheet,
membership_ending_task,
membership_end_date,
membership_beginning_task,
Expand Down Expand Up @@ -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,
Expand Down
100 changes: 1 addition & 99 deletions pycroft/lib/user/_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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,
)
Expand All @@ -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 <pycroft.model.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,
Expand Down Expand Up @@ -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
Expand Down
107 changes: 107 additions & 0 deletions pycroft/lib/user/user_sheet.py
Original file line number Diff line number Diff line change
@@ -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 <pycroft.model.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,
)

0 comments on commit 0f2c310

Please sign in to comment.