Skip to content

Commit

Permalink
Merge 5d32fa0 into 14f959e
Browse files Browse the repository at this point in the history
  • Loading branch information
felixrindt committed Jan 12, 2021
2 parents 14f959e + 5d32fa0 commit 079de69
Show file tree
Hide file tree
Showing 19 changed files with 581 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.4 on 2021-01-09 23:45

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("event_management", "0003_auto_20201022_1620"),
]

operations = [
migrations.AddField(
model_name="abstractparticipation",
name="data",
field=models.JSONField(default=dict),
),
]
1 change: 1 addition & 0 deletions ephios/event_management/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class States(models.IntegerChoices):

shift = ForeignKey("Shift", on_delete=models.CASCADE, verbose_name=_("shift"))
state = IntegerField(_("state"), choices=States.choices, default=States.REQUESTED)
data = models.JSONField(default=dict)

@property
def hours_value(self):
Expand Down
38 changes: 19 additions & 19 deletions ephios/event_management/signup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def participation_for(self, shift):
"""Return the participation object for a shift. Return None if it does not exist."""
raise NotImplementedError

def collect_all_qualifications(self):
@functools.lru_cache(maxsize=1)
def collect_all_qualifications(self) -> set:
"""We collect using breadth first search with one query for every layer of inclusion."""
all_qualifications = set(self.qualifications)
current = self.qualifications
Expand Down Expand Up @@ -96,44 +97,42 @@ class ParticipationError(ValidationError):
pass


class ConfigurationForm(forms.Form):
pass


class BaseSignupView(View):
shift: Shift = ...
method: "BaseSignupMethod" = ...

def dispatch(self, request, *args, **kwargs):
if (choice := request.POST.get("signup_choice")) is not None:
if choice == "sign_up":
return self.signup_pressed(request, *args, **kwargs)
return self.signup_pressed()
if choice == "decline":
return self.decline_pressed(request, *args, **kwargs)
return self.decline_pressed()
raise ValueError(_("'{choice}' is not a valid signup action.").format(choice=choice))
return super().dispatch(request, *args, **kwargs)

def signup_pressed(self, request, *args, **kwargs):
def signup_pressed(self, **signup_kwargs):
try:
with transaction.atomic():
self.method.perform_signup(request.user.as_participant())
self.method.perform_signup(self.request.user.as_participant(), **signup_kwargs)
messages.success(
request,
self.request,
self.method.signup_success_message.format(shift=self.shift),
)
except ParticipationError as errors:
for error in errors:
messages.error(request, self.method.signup_error_message.format(error=error))
messages.error(self.request, self.method.signup_error_message.format(error=error))
return redirect(self.shift.event.get_absolute_url())

def decline_pressed(self, request, *args, **kwargs):
def decline_pressed(self, **decline_kwargs):
try:
with transaction.atomic():
self.method.perform_decline(request.user.as_participant())
messages.info(request, self.method.decline_success_message.format(shift=self.shift))
self.method.perform_decline(self.request.user.as_participant(), **decline_kwargs)
messages.info(
self.request, self.method.decline_success_message.format(shift=self.shift)
)
except ParticipationError as errors:
for error in errors:
messages.error(request, self.method.decline_error_message.format(error=error))
messages.error(self.request, self.method.decline_error_message.format(error=error))
return redirect(self.shift.event.get_absolute_url())


Expand Down Expand Up @@ -209,6 +208,7 @@ def verbose_name(self):

description = """"""
signup_view_class = BaseSignupView
configuration_form_class = forms.Form

# use _ == gettext_lazy!
registration_button_text = _("Sign up")
Expand Down Expand Up @@ -274,10 +274,10 @@ def get_participation_for(self, participant):
self.shift
)

def perform_signup(self, participant: AbstractParticipant, **kwargs):
def perform_signup(self, participant: AbstractParticipant, **kwargs) -> AbstractParticipation:
"""
Configure a participation object for the given participant according to the method's configuration.
`kwargs` may contain further instructions from a e.g. a form.
`kwargs` may contain further instructions from e.g. a form.
"""
if errors := self.get_signup_errors(participant):
raise ParticipationError(errors)
Expand All @@ -295,7 +295,7 @@ def perform_decline(self, participant, **kwargs):
def get_configuration_fields(self):
return {
"minimum_age": {
"formfield": forms.IntegerField(required=False),
"formfield": forms.IntegerField(required=False, min_value=1, max_value=999),
"default": 16,
"publish_with_label": _("Minimum age"),
},
Expand Down Expand Up @@ -341,7 +341,7 @@ def render_shift_state(self, request):
def get_configuration_form(self, *args, **kwargs):
if self.shift is not None:
kwargs.setdefault("initial", self.configuration.__dict__)
form = ConfigurationForm(*args, **kwargs)
form = self.configuration_form_class(*args, **kwargs)
for name, config in self.get_configuration_fields().items():
form.fields[name] = config["formfield"]
return form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h5 class="card-title">
{% endfor %}
{% if event.active and not without_controls %}
<form method="POST" class="form"
action="{% url "event_management:shift_action" shift.pk %}">
action="{% url "event_management:signup_action" shift.pk %}">
{% csrf_token %}
<dt>{{ shift|shift_status:request.user }}</dt>
<dd>
Expand Down
2 changes: 1 addition & 1 deletion ephios/event_management/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
path(
"shifts/<int:pk>/signup-action/",
views.ShiftSignupView.as_view(),
name="shift_action",
name="signup_action",
),
path(
"shifts/<int:pk>/edit/",
Expand Down
5 changes: 3 additions & 2 deletions ephios/plugins/basesignup/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.dispatch import receiver

from ephios.event_management.signup import register_signup_methods
from ephios.plugins.basesignup.signup import (
from ephios.plugins.basesignup.signup.section_based import SectionBasedSignupMethod
from ephios.plugins.basesignup.signup.simple import (
InstantConfirmationSignupMethod,
RequestConfirmSignupMethod,
)
Expand All @@ -12,4 +13,4 @@
dispatch_uid="ephios.plugins.basesignup.signals.register_signup_methods",
)
def register_base_signup_methods(sender, **kwargs):
return [InstantConfirmationSignupMethod, RequestConfirmSignupMethod]
return [InstantConfirmationSignupMethod, RequestConfirmSignupMethod, SectionBasedSignupMethod]
Loading

0 comments on commit 079de69

Please sign in to comment.