@@ -36,12 +36,13 @@ class BaseDispositionParticipationForm(BaseParticipationForm):
3636 label = _ ("Hide comment for participant" ), required = False , label_suffix = ""
3737 )
3838
39- def __init__ (self , ** kwargs ):
40- try :
41- self .shift = kwargs ["instance" ].shift
42- except (AttributeError , KeyError ) as e :
43- raise MissingParticipation ("an instance must be provided" ) from e
44-
39+ def __init__ (self , shift = None , ** kwargs ):
40+ if not shift or not kwargs ["instance" ]:
41+ # we expect to always remember the form with an instance providing participant data
42+ raise MissingParticipation ("shift and instance kwargs must be provided" )
43+ # we don't use instance.shift but the explicitly supplied shift instance to make use of
44+ # the shift model caching mechanisms (e.g. a structure caching qualification computations)
45+ self .shift = shift
4546 super ().__init__ (** kwargs )
4647 self .can_delete = self .instance .state == AbstractParticipation .States .GETTING_DISPATCHED
4748
@@ -176,6 +177,7 @@ def post(self, request, *args, **kwargs):
176177 queryset = AbstractParticipation .objects .filter (pk = instance .pk ),
177178 prefix = "participations" ,
178179 start_index = form .cleaned_data ["new_index" ],
180+ form_kwargs = {"acting_user" : self .request .user , "shift" : shift },
179181 )
180182 form = next (filter (lambda form : form .instance .id == instance .id , formset ))
181183 return self .render_to_response ({"form" : form , "shift" : shift })
@@ -209,6 +211,7 @@ def post(self, request, *args, **kwargs):
209211 queryset = AbstractParticipation .objects .filter (pk = instance .pk ),
210212 prefix = "participations" ,
211213 start_index = int (request .POST ["new_index" ]),
214+ form_kwargs = {"acting_user" : self .request .user , "shift" : self .object },
212215 )
213216 form = next (filter (lambda form : form .instance .id == instance .id , formset ))
214217 return self .render_to_response ({"form" : form , "shift" : shift })
@@ -223,9 +226,9 @@ def get_formset(self):
223226 )
224227 formset = DispositionParticipationFormset (
225228 self .request .POST or None ,
226- queryset = self .object .participations .all (),
229+ queryset = self .object .participations .all (). select_related ( "shift__event__type" ) ,
227230 prefix = "participations" ,
228- form_kwargs = {"acting_user" : self .request .user },
231+ form_kwargs = {"acting_user" : self .request .user , "shift" : self . object },
229232 )
230233 return formset
231234
0 commit comments