From 2c84f1a72f1cf51998dea6687514a08d904eebc0 Mon Sep 17 00:00:00 2001 From: Christiaan de Die le Clercq Date: Fri, 18 Apr 2025 23:52:06 +0200 Subject: [PATCH 1/4] WIP: Room type and max users validation --- pretix_roomsharing/checkoutflow.py | 58 ++++++++++++++++++- .../pretix_roomsharing/settings.html | 6 ++ pretix_roomsharing/views.py | 38 ++++++++++-- 3 files changed, 95 insertions(+), 7 deletions(-) diff --git a/pretix_roomsharing/checkoutflow.py b/pretix_roomsharing/checkoutflow.py index 78d1d24..cbf6c56 100644 --- a/pretix_roomsharing/checkoutflow.py +++ b/pretix_roomsharing/checkoutflow.py @@ -277,8 +277,62 @@ def is_completed(self, request, warn=False): .values("order__all_positions__subevent") .distinct() ) - # TODO: Validation of same room type - # TODO: Validation of max room quantity? + # Validation of max people + if "check_max_people" in self.request.event.settings.roomsharing__validation_option: + max_people = None + for cartPosition in self.get_cart()["positions"]: + item_id = str(cartPosition.item.id) + max_people_setting_key = f"roomsharing__max_people_{item_id}" + if max_people_setting_key in self.request.event.settings: + max_people = self.request.event.settings[max_people_setting_key] + break + + if max_people is not None and len(room.orderrooms.all()) >= int(max_people): + if warn: + messages.warning( + request, + _( + """ + The room you requested to join is already full. + Please choose a different room. + """ + ), + ) + return False + + # Validation of ticket type + if "check_ticket_type" in self.request.event.settings.roomsharing__validation_option: + room_ticket_types = set( + c["order__all_positions__item"] + for c in room.orderrooms.filter( + order__all_positions__canceled=False + ) + .values("order__all_positions__item") + .distinct() + ) + cart_ticket_types = set( + c["item"] + for c in get_cart(request).values("item").distinct() + ) + if any(c not in room_ticket_types for c in cart_ticket_types): + if warn: + messages.warning( + request, + _( + """ + You requested to join a room that participates in "{room_ticket_type}", + while you chose to participate in "{cart_ticket_type}". + Please choose a different room. + """ + ).format( + room_ticket_type=room.name, + cart_ticket_type=SubEvent.objects.get( + pk=list(cart_ticket_types)[0] + ).name, + ), + ) + return False + if room_subevents: cart_subevents = set( c["subevent"] diff --git a/pretix_roomsharing/templates/pretix_roomsharing/settings.html b/pretix_roomsharing/templates/pretix_roomsharing/settings.html index 967ccc2..cb8eae6 100644 --- a/pretix_roomsharing/templates/pretix_roomsharing/settings.html +++ b/pretix_roomsharing/templates/pretix_roomsharing/settings.html @@ -11,6 +11,12 @@

{% trans "Roomsharing Settings" %}

{% bootstrap_form_errors form %}

{% trans "Selecting a product here requires room shares to be the same product. You can get around this by using bundled products and selecting one of those here." %}

{% bootstrap_field form.roomsharing__products layout="control" %} + {% for field in form %} + {% if field.name.startswith "roomsharing__max_people_" %} + {% bootstrap_field field layout="control" %} + {% endif %} + {% endfor %} + {% bootstrap_field form.roomsharing__validation_options layout="control" %}