You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.1 KiB

4 years ago
4 years ago
4 years ago
  1. from django.shortcuts import render
  2. from django.forms import modelformset_factory
  3. from django.http import HttpResponse
  4. from formtools.wizard.views import CookieWizardView
  5. from django.core.mail import send_mail, BadHeaderError
  6. from django.conf import settings
  7. from django.template.loader import get_template
  8. from django.template import Context
  9. from .forms import ProjectForm, VolunteerForm, LibraryForm, IFGForm
  10. from .models import Project
  11. def project(request):
  12. # return HttpResponse("Hello, world. You're at the input form")
  13. # ProjectFormSet = modelformset_factory(Project, fields='__all__')
  14. if request.method == 'POST':
  15. print('POST detected')
  16. form = ProjectForm(request.POST, request.FILES)
  17. if form.is_valid():
  18. print('form valid')
  19. form.save()
  20. # do something.
  21. else:
  22. print("form not valid")
  23. else:
  24. print
  25. form = ProjectForm()
  26. return render(request, 'input/project.html', {'form': form})
  27. def accreditation(request):
  28. pass
  29. def travel(request):
  30. pass
  31. def certificate(request):
  32. pass
  33. def done(request):
  34. return HttpResponse("Your data is save now.")
  35. def extern(request):
  36. return HttpResponse("The world out there is large and dangerous")
  37. class ExternView(CookieWizardView):
  38. template_name = "input/extern.html"
  39. form_list = [VolunteerForm, LibraryForm]
  40. # def process_step(self, form):
  41. # if form.cleaned_data.get('choice') == 'IFG':
  42. # print ('IFG detected!')
  43. # self.form_list = [VolunteerForm, IFGForm]
  44. # print('leaving process_step()')
  45. # return self.get_form_step_data(form)
  46. def get_form(self, step=None, data=None, files=None):
  47. if step is None:
  48. step = self.steps.current
  49. print ("get_form() step " + step)
  50. if step == '1':
  51. prev_data = self.get_cleaned_data_for_step('0')
  52. choice = prev_data.get('choice')
  53. if choice == 'IFG':
  54. print ('IFG detected!')
  55. form = IFGForm(data)
  56. elif choice in ('BIB', 'SOFT', 'ELIT'):
  57. print ('one of the famous three detected!')
  58. form = LibraryForm(data)
  59. else:
  60. print('ERROR! UNKNOWN FORMTYPE!')
  61. else:
  62. form = super().get_form(step, data, files)
  63. return form
  64. def done(self, form_list, **kwargs):
  65. print('ExternView.done() reached')
  66. # gather data from all forms
  67. data = {}
  68. for form in form_list:
  69. data = {**data, **form.cleaned_data}
  70. print(data)
  71. # write data to database
  72. form = form.save(commit=False)
  73. # we have to copy the data from the first form here
  74. # this is ugly code. how can we copy this without explicit writing?
  75. # i found no way to access the ModelForm.Meta.exclude-tupel
  76. form.realname = data['realname']
  77. form.username = data['username']
  78. form.email = data['email']
  79. if data['choice'] in ('BIB', 'ELIT', 'SOFT'):
  80. form.type = data['choice']
  81. form.save()
  82. # we need to send the following mails here:
  83. context = { 'data': data }
  84. try:
  85. # - mail with entered data to the Volunteer
  86. mail_template = get_template('input/ifg_volunteer_mail.txt')
  87. send_mail(
  88. 'form filled',
  89. mail_template.render(context),
  90. settings.IF_EMAIL,
  91. [form.email],
  92. fail_silently=False,
  93. )
  94. # - mail to IF with link to accept/decline
  95. mail_template = get_template('input/if_mail.txt')
  96. send_mail(
  97. 'form filled',
  98. mail_template.render(context),
  99. settings.IF_EMAIL,
  100. [settings.IF_EMAIL],
  101. fail_silently=False,
  102. )
  103. except BadHeaderError:
  104. return HttpResponse('Invalid header found.')
  105. return done(self.request)
  106. # return render(self.request, 'saved', {
  107. # 'form_data': [form.cleaned_data for form in form_list],
  108. # })