mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-18 13:39:41 -04:00
Refactor some of the logic into form
It’s messy having a lot of logic in the view methods. Handling the form stuff can be better encapsulated inside the `Form` subclass itself.
This commit is contained in:
@@ -682,15 +682,37 @@ class ServiceSwitchLettersForm(StripWhitespaceForm):
|
||||
)
|
||||
|
||||
|
||||
class BrandingStyle(RadioField):
|
||||
|
||||
def post_validate(self, form, validation_stopped):
|
||||
if self.data == 'None':
|
||||
self.data = None
|
||||
|
||||
|
||||
class ServiceSetBranding(StripWhitespaceForm):
|
||||
|
||||
branding_style = RadioField(
|
||||
branding_style = BrandingStyle(
|
||||
'Branding style',
|
||||
validators=[
|
||||
DataRequired()
|
||||
]
|
||||
)
|
||||
|
||||
DEFAULT = ('None', 'GOV.UK')
|
||||
|
||||
def __init__(self, all_email_brandings, current_email_branding):
|
||||
|
||||
super().__init__(branding_style=current_email_branding)
|
||||
|
||||
self.branding_style.choices = sorted(
|
||||
all_email_brandings + [self.DEFAULT],
|
||||
key=lambda branding: (
|
||||
branding[0] != current_email_branding,
|
||||
branding[0] is not self.DEFAULT[0],
|
||||
branding[1].lower(),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class ServicePreviewBranding(StripWhitespaceForm):
|
||||
|
||||
|
||||
@@ -887,24 +887,17 @@ def set_free_sms_allowance(service_id):
|
||||
def service_set_email_branding(service_id):
|
||||
email_branding = email_branding_client.get_all_email_branding()
|
||||
|
||||
form = ServiceSetBranding()
|
||||
|
||||
# dynamically create org choices, including the null option
|
||||
form.branding_style.choices = sorted(
|
||||
get_branding_as_value_and_label(email_branding) + [('None', 'GOV.UK')],
|
||||
key=lambda branding: (
|
||||
branding[0] != current_service.email_branding,
|
||||
branding[0] is not 'None',
|
||||
branding[1].lower(),
|
||||
),
|
||||
form = ServiceSetBranding(
|
||||
all_email_brandings=get_branding_as_value_and_label(email_branding),
|
||||
current_email_branding=current_service.email_branding,
|
||||
)
|
||||
|
||||
if form.validate_on_submit():
|
||||
branding_style = None if form.branding_style.data == 'None' else form.branding_style.data
|
||||
return redirect(url_for('.service_preview_email_branding', service_id=service_id,
|
||||
branding_style=branding_style))
|
||||
|
||||
form.branding_style.data = current_service['email_branding'] or 'None'
|
||||
return redirect(url_for(
|
||||
'.service_preview_email_branding',
|
||||
service_id=service_id,
|
||||
branding_style=form.branding_style.data,
|
||||
))
|
||||
|
||||
return render_template(
|
||||
'views/service-settings/set-email-branding.html',
|
||||
|
||||
Reference in New Issue
Block a user