From a40a5350e4f0ed45b97d14a37ba39faf0aeb84cf Mon Sep 17 00:00:00 2001 From: Paul Eggleton Date: Fri, 29 Jun 2018 11:08:21 +1200 Subject: [PATCH] forms: clear out comparison recipe/layerbranch if status doesn't accept them If the cover status for a comparison recipe is Unknown, Not available, or Distro-specific, then we disable the recipe and layerbranch fields in the form (using Javascript), however the form could still be saved and any existing values would persist. Clear out the fields upon saving if they are set under these circumstances. Signed-off-by: Paul Eggleton --- layerindex/forms.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/layerindex/forms.py b/layerindex/forms.py index 0b75fcb..c126315 100644 --- a/layerindex/forms.py +++ b/layerindex/forms.py @@ -166,6 +166,17 @@ class ClassicRecipeForm(forms.ModelForm): model = ClassicRecipe fields = ('cover_layerbranch', 'cover_pn', 'cover_status', 'cover_verified', 'cover_comment', 'classic_category') + def clean(self): + cleaned_data = super(ClassicRecipeForm, self).clean() + cover_pn = cleaned_data.get('cover_pn') + cover_layerbranch = cleaned_data.get('cover_layerbranch') + if cleaned_data.get('cover_status') in ['U', 'N', 'S']: + if cover_layerbranch: + cleaned_data['cover_layerbranch'] = None + if cover_pn: + cleaned_data['cover_pn'] = '' + return cleaned_data + class AdvancedRecipeSearchForm(forms.Form): FIELD_CHOICES = (