From bc972150e42eced423375a6d15133fcf6c5f6f29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Tue, 3 Jun 2025 13:00:05 +0200 Subject: [PATCH] Allow setting game to Finished when creating PlayEvent --- games/forms.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/games/forms.py b/games/forms.py index 86b04c4..a4254af 100644 --- a/games/forms.py +++ b/games/forms.py @@ -227,6 +227,12 @@ class PlayEventForm(forms.ModelForm): widget=forms.Select(attrs={"autofocus": "autofocus"}), ) + mark_as_finished = forms.BooleanField( + required=False, + initial={"mark_as_finished": True}, + label="Set game status to Finished", + ) + class Meta: model = PlayEvent fields = [ @@ -234,11 +240,21 @@ class PlayEventForm(forms.ModelForm): "started", "ended", "note", + "mark_as_finished" ] widgets = { "started": custom_date_widget, "ended": custom_date_widget, - } + + def save(self, commit=True): + with transaction.atomic(): + session = super().save(commit=False) + if self.cleaned_data.get("mark_as_finished"): + game_instance = session.game + game_instance.status = "f" + game_instance.save() + session.save() + return session class GameStatusChangeForm(forms.ModelForm):