Skip to content

Commit

Permalink
Merge branch 'bug_512' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed May 6, 2024
2 parents b23a6b7 + 2efcf8b commit 396302d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
25 changes: 23 additions & 2 deletions pycroft/lib/membership.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

from sqlalchemy import and_, func, distinct, Result
from sqlalchemy.future import select
from sqlalchemy.orm import aliased
from sqlalchemy.orm import aliased, Session

from pycroft.helpers import utc
from pycroft.helpers.i18n import deferred_gettext
from pycroft.helpers.interval import UnboundedInterval, IntervalSet, Interval
from pycroft.helpers.interval import UnboundedInterval, IntervalSet, Interval, closedopen
from pycroft.helpers.utc import DateTimeTz
from pycroft.lib.logging import log_user_event, log_event
from pycroft.model import session
Expand Down Expand Up @@ -203,3 +204,23 @@ def user_memberships_query(
.group_by(Membership.id)
)
return session.session.execute(memberships)


def change_membership_active_during(
session: Session,
membership_id: int,
begins_at: DateTimeTz,
ends_at: DateTimeTz | None,
processor: User,
) -> None:
"""modify the active_during field of a membership"""

membership = session.get(Membership, membership_id)
membership.active_during = closedopen(utc.with_min_time(begins_at), ends_at)

message = (
deferred_gettext("Edited the membership of group '{group}'. During: {during}")
.format(group=membership.group.name, during=membership.active_during)
.to_json()
)
log_user_event(message, processor, membership.user)
24 changes: 24 additions & 0 deletions tests/lib/test_membership.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest

from pycroft.lib.membership import change_membership_active_during
from tests import factories as f


def test_user_change_membership(session, membership, processor, utcnow):
change_membership_active_during(
session,
membership.id,
begins_at=utcnow,
ends_at=utcnow,
processor=processor,
)
assert len(les := membership.user.log_entries) == 1
log_entry = les[0]
assert "Edited the membership" in log_entry.message


@pytest.fixture
def membership(module_session):
m = f.MembershipFactory(group=f.PropertyGroupFactory())
module_session.flush()
return m
18 changes: 9 additions & 9 deletions web/blueprints/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from pycroft.helpers.net import ip_regex, mac_regex
from pycroft.lib.facilities import get_room
from pycroft.lib.logging import log_user_event
from pycroft.lib.membership import make_member_of, remove_member_of
from pycroft.lib.membership import make_member_of, remove_member_of, change_membership_active_during
from pycroft.lib.traffic import get_users_with_highest_traffic
from pycroft.lib.user import encode_type1_user_id, encode_type2_user_id, \
traffic_history, generate_user_sheet, get_blocked_groups, \
Expand Down Expand Up @@ -809,15 +809,15 @@ def edit_membership(user_id: int, membership_id: int) -> ResponseReturnValue:
form = UserEditGroupMembership(**membership_data)

if form.validate_on_submit():
membership.active_during = closedopen(
utc.with_min_time(form.begins_at.data),
None if form.ends_at.unlimited.data else utc.with_min_time(form.ends_at.date.data),
change_membership_active_during(
session=session.session,
membership_id=membership_id,
begins_at=utc.with_min_time(form.begins_at.data),
ends_at=(
None if form.ends_at.unlimited.data else utc.with_min_time(form.ends_at.date.data)
),
processor=current_user,
)

message = deferred_gettext("Edited the membership of group '{group}'. During: {during}")\
.format(group=membership.group.name, during=membership.active_during)\
.to_json()
lib.logging.log_user_event(message, current_user, membership.user)
session.session.commit()
flash('Gruppenmitgliedschaft bearbeitet', 'success')
return redirect(url_for('.user_show',
Expand Down

0 comments on commit 396302d

Please sign in to comment.