From 9eced41d76545bd2921605b7b81bd14b875ce541 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 10 Jan 2019 17:30:03 +0100 Subject: Formset: delete empty forms --- ishtar_common/forms.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'ishtar_common/forms.py') diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 18e32cd76..91e9fb3e9 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -436,6 +436,22 @@ class FormSet(CustomForm, BaseFormSet): form.fields[DELETION_FIELD_NAME].label = '' form.fields[DELETION_FIELD_NAME].widget = self.delete_widget() + def _should_delete_form(self, form): + """ + Returns whether or not the form was marked for deletion. + If no data, set deletion to True + """ + if form.cleaned_data.get(DELETION_FIELD_NAME, False): + return True + if not form.cleaned_data or not [ + __ for __ in form.cleaned_data + if __ != DELETION_FIELD_NAME and + form.cleaned_data[__] is not None and + form.cleaned_data[__] != '']: + form.cleaned_data[DELETION_FIELD_NAME] = True + return True + return False + class FormSetWithDeleteSwitches(FormSet): delete_widget = widgets.DeleteSwitchWidget -- cgit v1.2.3