split form processing in multiple methods

This commit is contained in:
Oliver Zander 2025-10-17 11:42:06 +02:00
parent 621941c6e4
commit 5d107dab96
1 changed files with 35 additions and 18 deletions

View File

@ -210,15 +210,28 @@ class ApplicationView(FormView):
- Return the "done" response. - Return the "done" response.
""" """
data = self.prepare_data(form)
obj = self.save_obj(form, data)
if response := self.send_mail(obj, data):
return response
return done(self.request)
def prepare_data(self, form):
# Collect cleaned data and mark the current type # Collect cleaned data and mark the current type
data = form.cleaned_data.copy()
data['choice'] = self.type_code data = {**form.cleaned_data, 'choice': self.type_code}
# Special rule for literature applications # Special rule for literature applications
if self.type_code == TYPE_LIT and data.get('selfbuy') == 'TRUE': if self.type_code == TYPE_LIT and data.get('selfbuy') == 'TRUE':
data['selfbuy_give_data'] = 'False' data['selfbuy_give_data'] = 'False'
return data
def save_obj(self, form, data):
# Save model instance # Save model instance
modell = form.save(commit=False) modell = form.save(commit=False)
# Username from session if present # Username from session if present
@ -228,6 +241,7 @@ class ApplicationView(FormView):
# Copy common fields if provided by the form # Copy common fields if provided by the form
if 'realname' in data: if 'realname' in data:
modell.realname = data['realname'] modell.realname = data['realname']
if 'email' in data: if 'email' in data:
modell.email = data['email'] modell.email = data['email']
@ -240,11 +254,16 @@ class ApplicationView(FormView):
modell.selfbuy_give_data = data['selfbuy_give_data'] modell.selfbuy_give_data = data['selfbuy_give_data']
modell.save() modell.save()
if hasattr(form, 'save_m2m'): if hasattr(form, 'save_m2m'):
form.save_m2m() form.save_m2m()
return modell
def send_mail(self, obj, data):
# Prepare minimal mail context and send mails # Prepare minimal mail context and send mails
data['pk'] = modell.pk
data['pk'] = obj.pk
data['url_prefix'] = settings.EMAIL_URL_PREFIX data['url_prefix'] = settings.EMAIL_URL_PREFIX
data['type_label'] = self.type_info.label data['type_label'] = self.type_info.label
context = {'data': data} context = {'data': data}
@ -260,22 +279,14 @@ class ApplicationView(FormView):
applicant_files = collect_attachment_paths(kind='applicant', choice=self.type_code) applicant_files = collect_attachment_paths(kind='applicant', choice=self.type_code)
attach_files(msg1, applicant_files) attach_files(msg1, applicant_files)
msg1.send() msg1.send()
type_label_html = TYPE_CHOICES.get(self.type_code, self.type_code) type_label_html = self.type_info.label
type_label = strip_tags(str(type_label_html)) type_label_plain = strip_tags(type_label_html)
# Mail to IF # Mail to IF
txt2 = get_template('input/if_mail.txt').render(context) txt2 = get_template('input/if_mail.txt').render(context)
html2 = get_template('input/if_mail.html').render(context) html2 = get_template('input/if_mail.html').render(context)
applicant_name = ( applicant_name = self.get_recipient_name(obj, data)
getattr(modell, 'username', None)
or data.get('username')
or getattr(modell, 'realname', None)
or data.get('realname')
or getattr(modell, 'email', None)
or data.get('email')
or 'Unbekannt'
)
msg2 = EmailMultiAlternatives( msg2 = EmailMultiAlternatives(
f'Anfrage {type_label} von {applicant_name}', txt2, settings.IF_EMAIL, [settings.IF_EMAIL] f'Anfrage {type_label_plain} von {applicant_name}', txt2, settings.IF_EMAIL, [settings.IF_EMAIL]
) )
msg2.attach_alternative(html2, 'text/html') msg2.attach_alternative(html2, 'text/html')
staff_files = collect_attachment_paths(kind='staff', choice=self.type_code) staff_files = collect_attachment_paths(kind='staff', choice=self.type_code)
@ -283,10 +294,16 @@ class ApplicationView(FormView):
msg2.send() msg2.send()
except BadHeaderError: except BadHeaderError:
modell.delete() obj.delete()
return HttpResponse('Invalid header found. Data not saved!') return HttpResponse('Invalid header found. Data not saved!')
except SMTPException: except SMTPException:
modell.delete() obj.delete()
return HttpResponse('Error in sending mails (probably wrong adress?). Data not saved!') return HttpResponse('Error in sending mails (probably wrong adress?). Data not saved!')
return done(self.request) @staticmethod
def get_recipient_name(obj, data):
for field in 'username', 'realname', 'email':
if name := getattr(obj, field, None) or data.get(field):
return name
return 'Unbekannt'