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.

193 lines
6.5 KiB

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