Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split up pycroft.lib.user into separate packages #746

Open
wants to merge 28 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8b9c9d7
qa: extract `user_from_pre_member`
lukasjuhrich Sep 4, 2024
b6d9590
Turn pycroft.lib.user into package
lukasjuhrich Sep 4, 2024
21eb6d9
move `pycroft.lib.user` contents into `_old` module
lukasjuhrich Sep 4, 2024
ab30dbf
Extract `lib.user.member_request`, `.exc`, and dependees
lukasjuhrich Sep 4, 2024
a0b172b
Extract `pycroft.lib.user_id`
lukasjuhrich Sep 4, 2024
b735b84
extract `lib.user.edit`
lukasjuhrich Sep 4, 2024
7a7315a
extract `lib.user.passwords`
lukasjuhrich Sep 4, 2024
d765be1
extract `lib.user.mail`
lukasjuhrich Sep 4, 2024
8eb0331
extract `can_target` to `.permissions` module
lukasjuhrich Sep 4, 2024
3b8fda3
Extract `lib.user.user_sheet`
lukasjuhrich Sep 17, 2024
4dec3db
Move user identification heuristics to `lib.user.member_request`
lukasjuhrich Sep 17, 2024
d61e72c
Extract `lib.user.info`
lukasjuhrich Sep 17, 2024
d28c991
Migrate `lib.user.migrate_user_host` to `lib.host.migrate_host`
lukasjuhrich Sep 20, 2024
5e4c796
Explicitly pass `session.session` in `migrate_host`
lukasjuhrich Sep 20, 2024
e7a84c3
Migrate `lib.user.setup_ipv4_networking` to `lib.host`
lukasjuhrich Sep 20, 2024
96a7d4c
Move `membership_{begin,end}_date` to `lib.user.info`
lukasjuhrich Sep 20, 2024
6cac442
Improve naming of `membership_*_date` functions
lukasjuhrich Sep 20, 2024
104219c
Make `scheduled_membership_*` implementations more concise
lukasjuhrich Sep 20, 2024
1977309
extract `lib.user.lifecycle`
lukasjuhrich Sep 20, 2024
a4a9060
Stricter typing for `lib.user`
lukasjuhrich Sep 20, 2024
bbd4ae0
typing: Strict nullability for `lib.user.mail_confirmation`
lukasjuhrich Sep 20, 2024
40137d4
Remove superfluous args from `check_new_user_data` and fix call
lukasjuhrich Sep 20, 2024
dbdd8f9
Strict nullability for `lib.user.member_request`
lukasjuhrich Sep 20, 2024
8c72119
Strict nullability for `lib.user.lifecycle`
lukasjuhrich Sep 20, 2024
5e6504f
extract `lib.user.blocking`
lukasjuhrich Sep 20, 2024
cd4b437
Strict nullability for rest of `lib.user`
lukasjuhrich Sep 20, 2024
eaec37c
migrate `send_password_reset_mail` to `.mail` and delete `_old`
lukasjuhrich Sep 20, 2024
634b88e
Merge branch 'lib_user_nullability' into lib_user_explode
lukasjuhrich Sep 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pycroft/helpers/printing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def generate_user_sheet(
new_user: bool,
wifi: bool,
bank_account: BankAccount,
user: User | None = None,
user: User = None,
user_id: str | None = None,
plain_user_password: str | None = None,
generation_purpose: str = "",
Expand Down
52 changes: 50 additions & 2 deletions pycroft/lib/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from pycroft.helpers.net import port_name_sort_key
from pycroft.lib.logging import log_user_event
from pycroft.lib.net import get_subnets_for_room, get_free_ip, delete_ip
from pycroft.lib.user import migrate_user_host
from pycroft.model.facilities import Room
from pycroft.model.host import Interface, IP, Host, SwitchPort
from pycroft.model.port import PatchPort
Expand Down Expand Up @@ -95,7 +94,46 @@ def host_edit(host: Host, owner: User, room: Room, name: str, processor: User) -
host.owner = owner

if host.room != room:
migrate_user_host(host, room, processor)
migrate_host(session, host, room, processor)


def migrate_host(session: Session, host: Host, new_room: Room, processor: User) -> None:
"""
Migrate a Host to a new room and if necessary to a new subnet.
If the host changes subnet, it will get a new IP address.

:param host: Host to be migrated
:param new_room: new room of the host
:param processor: User processing the migration
:return:
"""
old_room = host.room
host.room = new_room

subnets_old = get_subnets_for_room(old_room)
subnets = get_subnets_for_room(new_room)

if subnets_old != subnets:
for interface in host.interfaces:
old_ips = tuple(ip for ip in interface.ips)
for old_ip in old_ips:
ip_address, subnet = get_free_ip(subnets)
new_ip = IP(interface=interface, address=ip_address, subnet=subnet)
session.add(new_ip)

old_address = old_ip.address
session.delete(old_ip)

message = deferred_gettext("Changed IP of {mac} from {old_ip} to {new_ip}.").format(
old_ip=str(old_address), new_ip=str(new_ip.address), mac=interface.mac
)
log_user_event(author=processor, user=host.owner, message=message.to_json())

message = deferred_gettext("Moved host '{name}' from {room_old} to {room_new}.").format(
name=host.name, room_old=old_room.short_name, room_new=new_room.short_name
)

log_user_event(author=processor, user=host.owner, message=message.to_json())


@with_transaction
Expand Down Expand Up @@ -234,3 +272,13 @@ def get_conflicting_interface(
if new_mac == current_mac:
return None
return session.scalar(select(Interface).filter_by(mac=new_mac))


def setup_ipv4_networking(session: Session, host: Host) -> None:
"""Add suitable ips for every interface of a host"""
subnets = get_subnets_for_room(host.room)

for interface in host.interfaces:
ip_address, subnet = get_free_ip(subnets)
new_ip = IP(interface=interface, address=ip_address, subnet=subnet)
session.add(new_ip)
24 changes: 12 additions & 12 deletions pycroft/lib/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

from pycroft.lib.exc import PycroftLibException

mail_envelope_from = os.environ.get('PYCROFT_MAIL_ENVELOPE_FROM')
mail_from = os.environ.get('PYCROFT_MAIL_FROM')
mail_reply_to = os.environ.get('PYCROFT_MAIL_REPLY_TO')
smtp_host = os.environ.get('PYCROFT_SMTP_HOST')
mail_envelope_from = os.environ["PYCROFT_MAIL_ENVELOPE_FROM"]
mail_from = os.environ["PYCROFT_MAIL_FROM"]
mail_reply_to = os.environ["PYCROFT_MAIL_REPLY_TO"]
smtp_host = os.environ["PYCROFT_SMTP_HOST"]
smtp_port = int(os.environ.get('PYCROFT_SMTP_PORT', 465))
smtp_user = os.environ.get('PYCROFT_SMTP_USER')
smtp_password = os.environ.get('PYCROFT_SMTP_PASSWORD')
smtp_user = os.environ["PYCROFT_SMTP_USER"]
smtp_password = os.environ["PYCROFT_SMTP_PASSWORD"]
smtp_ssl = os.environ.get('PYCROFT_SMTP_SSL', 'ssl')
template_path_type = os.environ.get('PYCROFT_TEMPLATE_PATH_TYPE', 'filesystem')
template_path = os.environ.get('PYCROFT_TEMPLATE_PATH', 'pycroft/templates')
Expand Down Expand Up @@ -68,12 +68,12 @@ def render(self, **kwargs: t.Any) -> tuple[str, str]:


def compose_mail(mail: Mail) -> MIMEMultipart:
msg = MIMEMultipart('alternative', _charset='utf-8')
msg['Message-Id'] = make_msgid()
msg['From'] = mail_from
msg['To'] = Header(mail.to_address)
msg['Subject'] = mail.subject
msg['Date'] = formatdate(localtime=True)
msg = MIMEMultipart("alternative", _charset="utf-8")
msg["Message-Id"] = make_msgid()
msg["From"] = mail_from
msg["To"] = str(Header(mail.to_address))
msg["Subject"] = mail.subject
msg["Date"] = formatdate(localtime=True)

msg.attach(MIMEText(mail.body_plain, 'plain', _charset='utf-8'))

Expand Down
8 changes: 5 additions & 3 deletions pycroft/lib/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ class UserMoveTaskImpl(UserTaskImpl[UserMoveParams]):

def _execute(self, task: UserTask, parameters: UserMoveParams) -> None:
from pycroft.lib import user as lib_user
from pycroft.lib.facilities import get_room
if task.user.room is None:
self.errors.append("Tried to move in user, "
"but user was already living in a dormitory.")
return

room = lib_user.get_room(
room = get_room(
room_number=parameters.room_number,
level=parameters.level,
building_id=parameters.building_id,
Expand Down Expand Up @@ -147,13 +148,14 @@ class UserMoveInTaskImpl(UserTaskImpl):

def _execute(self, task: UserTask, parameters: UserMoveInParams) -> None:
from pycroft.lib import user as lib_user
from pycroft.lib.facilities import get_room

if task.user.room is not None:
self.errors.append("Tried to move in user, "
"but user was already living in a dormitory.")
return

room = lib_user.get_room(
room = get_room(
room_number=parameters.room_number,
level=parameters.level,
building_id=parameters.building_id,
Expand Down Expand Up @@ -195,7 +197,7 @@ def schedule_user_task(
due: DateTimeTz,
user: User,
parameters: TaskParams,
processor: User,
processor: User | None,
) -> UserTask:
if due < session.utcnow():
raise ValueError("the due date must be in the future")
Expand Down
Loading
Loading