From 50f7c7036a4872f84a34dc30ce4bcebb6cc7ac66 Mon Sep 17 00:00:00 2001 From: Paul Eggleton Date: Wed, 18 Apr 2018 16:30:00 +1200 Subject: [PATCH] rrs: admin: validate that email address fields are set If automated emails are enabled, we need to ensure that the other email fields are populated, so validate that. Signed-off-by: Paul Eggleton --- rrs/admin.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/rrs/admin.py b/rrs/admin.py index 682a5a6..8176b8a 100644 --- a/rrs/admin.py +++ b/rrs/admin.py @@ -8,6 +8,7 @@ from django.utils.functional import curry from django.contrib import admin from django.contrib.admin import DateFieldListFilter +from django import forms from django.forms.models import BaseInlineFormSet from django.core.exceptions import ValidationError @@ -72,8 +73,33 @@ class MaintenancePlanLayerBranchInline(admin.StackedInline): min_num = 1 extra = 0 +class MaintenancePlanAdminForm(forms.ModelForm): + model = MaintenancePlan + + def clean_email_to(self): + val = self.cleaned_data['email_to'] + if self.cleaned_data['email_enabled']: + if not val: + raise ValidationError('To email address must be specified if emails are enabled') + return val + + def clean_email_from(self): + val = self.cleaned_data['email_from'] + if self.cleaned_data['email_enabled']: + if not val: + raise ValidationError('From email address must be specified if emails are enabled') + return val + + def clean_email_subject(self): + val = self.cleaned_data['email_subject'] + if self.cleaned_data['email_enabled']: + if not val: + raise ValidationError('Email subject must be specified if emails are enabled') + return val + class MaintenancePlanAdmin(admin.ModelAdmin): model = MaintenancePlan + form = MaintenancePlanAdminForm inlines = [ MaintenancePlanLayerBranchInline, ]