Repository: ephios-dev/ephios

Issue:
Switching from signup method with requested state to one w/o breaks disposition
```

2021-12-03 14:39:00,166 DEBG 'uwsgi' stderr output:
Traceback (most recent call last):
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner

2021-12-03 14:39:00,167 DEBG 'uwsgi' stderr output:
    response = get_response(request)
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response

2021-12-03 14:39:00,168 DEBG 'uwsgi' stderr output:
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view

2021-12-03 14:39:00,168 DEBG 'uwsgi' stderr output:
    return self.dispatch(request, *args, **kwargs)
  File "/home/juhsp/.local/lib/python3.8/site-packages/ephios/core/signup/disposition.py", line 118, in dispatch

2021-12-03 14:39:00,169 DEBG 'uwsgi' stderr output:
    return super().dispatch(request, *args, **kwargs)
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/contrib/auth/mixins.py", line 104, in dispatch

2021-12-03 14:39:00,169 DEBG 'uwsgi' stderr output:
    return super().dispatch(request, *args, **kwargs)
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/juhsp/.local/lib/python3.8/site-packages/ephios/core/signup/disposition.py", line 224, in post

2021-12-03 14:39:00,170 DEBG 'uwsgi' stderr output:
    if not formset.is_valid():
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/forms/formsets.py", line 321, in is_valid

2021-12-03 14:39:00,171 DEBG 'uwsgi' stderr output:
    self.errors
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/forms/formsets.py", line 304, in errors

2021-12-03 14:39:00,171 DEBG 'uwsgi' stderr output:
    self.full_clean()
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/forms/formsets.py", line 355, in full_clean

2021-12-03 14:39:00,172 DEBG 'uwsgi' stderr output:
    for i, form in enumerate(self.forms):
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__

2021-12-03 14:39:00,172 DEBG 'uwsgi' stderr output:
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/forms/formsets.py", line 155, in forms
    return [
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/forms/formsets.py", line 156, in <listcomp>

2021-12-03 14:39:00,175 DEBG 'uwsgi' stderr output:
    self._construct_form(i, **self.get_form_kwargs(i))
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/forms/models.py", line 632, in _construct_form
    form = super()._construct_form(i, **kwargs)
  File "/home/juhsp/.local/lib/python3.8/site-packages/django/forms/formsets.py", line 193, in _construct_form
    form = self.form(**defaults)
  File "/home/juhsp/.local/lib/python3.8/site-packages/ephios/core/signup/disposition.py", line 31, in __init__
    super().__init__(**kwargs)
  File "/home/juhsp/.local/lib/python3.8/site-packages/ephios/core/signup/methods.py", line 141, in __init__
    instance = kwargs["instance"]
KeyError: 'instance'

```


Discussion:
A requested participation had been created for the affected shift. It was later changed to use a signup-method that is not using the requested state. Hence the disposition queryset wasn't rendering the requested participation to the template, so the pk was missing in the post data, which lead to a key error when dealing with the POST data.

Resolving patch:
diff --git a/ephios/core/signup/disposition.py b/ephios/core/signup/disposition.py
--- a/ephios/core/signup/disposition.py
+++ b/ephios/core/signup/disposition.py
@@ -239,6 +239,13 @@ def get_context_data(self, **kwargs):
             "participant_template",
             self.object.signup_method.disposition_participation_form_class.disposition_participation_template,
         )
+        kwargs.setdefault(
+            "render_requested_state",
+            self.object.signup_method.uses_requested_state
+            or self.object.participations.filter(
+                state=AbstractParticipation.States.REQUESTED
+            ).exists(),
+        )
         kwargs.setdefault(
             "add_user_form",
             AddUserForm(user_queryset=addable_users(self.object)),


Test patch:
diff --git a/tests/plugins/basesignup/test_common.py b/tests/plugins/basesignup/test_common.py
--- a/tests/plugins/basesignup/test_common.py
+++ b/tests/plugins/basesignup/test_common.py
@@ -1,6 +1,7 @@
 from django.urls import reverse
 
 from ephios.core.models import AbstractParticipation, LocalParticipation
+from ephios.plugins.basesignup.signup.instant import InstantConfirmationSignupMethod
 
 
 def test_getting_dispatched_state_is_overwritten_by_participant_signup(
@@ -31,6 +32,25 @@ def test_getting_dispatched_state_is_overwritten_by_participant_signup(
     )
 
 
+def test_requested_participations_are_always_rendered(django_app, volunteer, planner, event):
+    """
+    regression test for https://github.com/ephios-dev/ephios/issues/633
+    InstantConfirmationSignupMethod doesn't use the requested state, but a requested participation exists. Test that.
+    """
+    shift = event.shifts.first()
+    shift.signup_method_slug = InstantConfirmationSignupMethod.slug
+    shift.save()
+
+    participation = shift.signup_method.get_participation_for(volunteer.as_participant())
+    participation.state = AbstractParticipation.States.REQUESTED
+    participation.save()
+
+    django_app.get(
+        reverse("core:shift_disposition", kwargs=dict(pk=shift.pk)),
+        user=planner,
+    ).forms["participations-form"].submit()
+
+
 def test_disposition_delete_participations_getting_dispatched(
     django_app, volunteer, planner, event
 ):