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.

172 lines
6.0 KiB

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, ExternForm, LibraryForm, IFGForm,\
  10. HonoraryCertificateForm, InternForm, TravelForm
  11. from .models import Project, TYPE_CHOICES, Library
  12. from .settings import URLPREFIX, IF_EMAIL
  13. def authorize(request, choice, pk):
  14. '''If IF grant a support they click a link in a mail which leads here.
  15. We write the granted field in the database here and set a timestamp.'''
  16. # TODO: write a timestamp which is needed to determine time of next mail
  17. if choice in ('BIB', 'ELIT', 'SOFT'):
  18. Library.set_granted(pk,True)
  19. return HttpResponse(f"AUTHORIZED! choice: {choice}, pk: {pk}")
  20. else:
  21. return HttpResponse(f'ERROR! UNKNWON CHOICE TYPE! {choice}')
  22. def deny(request, choice, pk):
  23. '''If IF denies a support they click a link in a mail which leads here
  24. We write the granted field in the database here.'''
  25. if choice in ('BIB', 'ELIT', 'SOFT'):
  26. Library.set_granted(pk,False)
  27. return HttpResponse(f"DENIED! choice: {choice}, pk: {pk}")
  28. else:
  29. return HttpResponse(f'ERROR! UNKNWON CHOICE TYPE {choice}!')
  30. def done(request):
  31. return HttpResponse("Your data is save now.")
  32. class InternView(CookieWizardView):
  33. '''This View is for WMDE-employees only'''
  34. template_name = 'input/extern.html'
  35. form_list = [InternForm, ProjectForm]
  36. def get_form(self, step=None, data=None, files=None):
  37. '''this function determines which part of the multipart form is
  38. displayed next'''
  39. if step is None:
  40. step = self.steps.current
  41. print ("get_form() step " + step)
  42. if step == '1':
  43. prev_data = self.get_cleaned_data_for_step('0')
  44. choice = prev_data.get('choice')
  45. if choice == 'HON':
  46. print ('Ehrenamtsbescheinigung detected!')
  47. form = HonoraryCertificateForm(data)
  48. elif choice == 'PRO':
  49. print ('Projektsteckbrief erreicht!')
  50. form = ProjectForm(data)
  51. elif choice == 'TRAV':
  52. print('Reisekosten erreicht')
  53. form = TravelForm(data)
  54. else:
  55. raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice}')
  56. else:
  57. form = super().get_form(step, data, files)
  58. return form
  59. def done(self, form_list, **kwargs):
  60. print('InternView.done() reached')
  61. # gather data from all forms
  62. data = {}
  63. for form in form_list:
  64. data = {**data, **form.cleaned_data}
  65. print(data)
  66. # write data to database
  67. form = form.save(commit=False)
  68. # we have to copy the data from the first form here
  69. # this is ugly code. how can we copy this without explicit writing?
  70. # i found no way to access the ModelForm.Meta.exclude-tupel
  71. form.realname = data['realname']
  72. # form.username = data['username']
  73. form.email = data['email']
  74. form.save()
  75. return done(self.request)
  76. class ExternView(CookieWizardView):
  77. '''This View is for Volunteers'''
  78. template_name = "input/extern.html"
  79. form_list = [ExternForm, LibraryForm]
  80. def get_form(self, step=None, data=None, files=None):
  81. '''this function determines which part of the multipart form is
  82. displayed next'''
  83. if step is None:
  84. step = self.steps.current
  85. print ("get_form() step " + step)
  86. if step == '1':
  87. prev_data = self.get_cleaned_data_for_step('0')
  88. choice = prev_data.get('choice')
  89. if choice == 'IFG':
  90. print ('IFG detected!')
  91. form = IFGForm(data)
  92. elif choice in ('BIB', 'SOFT', 'ELIT'):
  93. print ('one of the famous three detected!')
  94. form = LibraryForm(data)
  95. form.fields['library'].label = TYPE_CHOICES[choice]
  96. else:
  97. raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice}')
  98. else:
  99. form = super().get_form(step, data, files)
  100. return form
  101. def done(self, form_list, **kwargs):
  102. print('ExternView.done() reached')
  103. # gather data from all forms
  104. data = {}
  105. for form in form_list:
  106. data = {**data, **form.cleaned_data}
  107. print(data)
  108. # write data to database
  109. form = form.save(commit=False)
  110. # we have to copy the data from the first form here
  111. # this is a bit ugly code. how can we copy this without explicit writing?
  112. form.realname = data['realname']
  113. # form.username = data['username']
  114. form.email = data['email']
  115. # write type of form in some cases
  116. if data['choice'] in ('BIB', 'ELIT', 'SOFT'):
  117. form.type = data['choice']
  118. form.save()
  119. # add some data to context for mail templates
  120. data['pk'] = form.pk
  121. data['urlprefix'] = URLPREFIX
  122. # we need to send the following mails here:
  123. context = { 'data': data }
  124. try:
  125. # - mail with entered data to the Volunteer
  126. mail_template = get_template('input/ifg_volunteer_mail.txt')
  127. send_mail(
  128. 'Formular ausgefüllt',
  129. mail_template.render(context),
  130. IF_EMAIL,
  131. [form.email],
  132. fail_silently=False)
  133. # - mail to IF with link to accept/decline
  134. mail_template = get_template('input/if_mail.txt')
  135. send_mail(
  136. 'Formular ausgefüllt',
  137. mail_template.render(context),
  138. IF_EMAIL,
  139. [IF_EMAIL],
  140. fail_silently=False)
  141. except BadHeaderError:
  142. return HttpResponse('Invalid header found.')
  143. return done(self.request)