forked from beba/foerderbarometer
split form processing in multiple methods
This commit is contained in:
parent
621941c6e4
commit
5d107dab96
|
|
@ -210,15 +210,28 @@ class ApplicationView(FormView):
|
|||
- 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
|
||||
data = form.cleaned_data.copy()
|
||||
data['choice'] = self.type_code
|
||||
|
||||
data = {**form.cleaned_data, 'choice': self.type_code}
|
||||
|
||||
# Special rule for literature applications
|
||||
if self.type_code == TYPE_LIT and data.get('selfbuy') == 'TRUE':
|
||||
data['selfbuy_give_data'] = 'False'
|
||||
|
||||
return data
|
||||
|
||||
def save_obj(self, form, data):
|
||||
# Save model instance
|
||||
|
||||
modell = form.save(commit=False)
|
||||
|
||||
# Username from session if present
|
||||
|
|
@ -228,6 +241,7 @@ class ApplicationView(FormView):
|
|||
# Copy common fields if provided by the form
|
||||
if 'realname' in data:
|
||||
modell.realname = data['realname']
|
||||
|
||||
if 'email' in data:
|
||||
modell.email = data['email']
|
||||
|
||||
|
|
@ -240,11 +254,16 @@ class ApplicationView(FormView):
|
|||
modell.selfbuy_give_data = data['selfbuy_give_data']
|
||||
|
||||
modell.save()
|
||||
|
||||
if hasattr(form, 'save_m2m'):
|
||||
form.save_m2m()
|
||||
|
||||
return modell
|
||||
|
||||
def send_mail(self, obj, data):
|
||||
# Prepare minimal mail context and send mails
|
||||
data['pk'] = modell.pk
|
||||
|
||||
data['pk'] = obj.pk
|
||||
data['url_prefix'] = settings.EMAIL_URL_PREFIX
|
||||
data['type_label'] = self.type_info.label
|
||||
context = {'data': data}
|
||||
|
|
@ -260,22 +279,14 @@ class ApplicationView(FormView):
|
|||
applicant_files = collect_attachment_paths(kind='applicant', choice=self.type_code)
|
||||
attach_files(msg1, applicant_files)
|
||||
msg1.send()
|
||||
type_label_html = TYPE_CHOICES.get(self.type_code, self.type_code)
|
||||
type_label = strip_tags(str(type_label_html))
|
||||
type_label_html = self.type_info.label
|
||||
type_label_plain = strip_tags(type_label_html)
|
||||
# Mail to IF
|
||||
txt2 = get_template('input/if_mail.txt').render(context)
|
||||
html2 = get_template('input/if_mail.html').render(context)
|
||||
applicant_name = (
|
||||
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'
|
||||
)
|
||||
applicant_name = self.get_recipient_name(obj, data)
|
||||
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')
|
||||
staff_files = collect_attachment_paths(kind='staff', choice=self.type_code)
|
||||
|
|
@ -283,10 +294,16 @@ class ApplicationView(FormView):
|
|||
msg2.send()
|
||||
|
||||
except BadHeaderError:
|
||||
modell.delete()
|
||||
obj.delete()
|
||||
return HttpResponse('Invalid header found. Data not saved!')
|
||||
except SMTPException:
|
||||
modell.delete()
|
||||
obj.delete()
|
||||
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'
|
||||
|
|
|
|||
Loading…
Reference in New Issue