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.

196 lines
6.6 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. data['grant'] = ('LIT', 'SOFT', 'ELIT', 'BIB', 'IFG')
  141. data['DOMAIN'] = ('MAIL', 'LIST')
  142. data['typestring'] = TYPE_CHOICES[data['choice']]
  143. # we need to send the following mails here:
  144. context = { 'data': data }
  145. try:
  146. # - mail with entered data to the Volunteer
  147. mail_template = get_template('input/ifg_volunteer_mail.txt')
  148. send_mail(
  149. 'Formular ausgefüllt',
  150. mail_template.render(context),
  151. IF_EMAIL,
  152. [form.email],
  153. fail_silently=False)
  154. # - mail to IF with link to accept/decline
  155. mail_template = get_template('input/if_mail.txt')
  156. send_mail(
  157. 'Formular ausgefüllt',
  158. mail_template.render(context),
  159. IF_EMAIL,
  160. [IF_EMAIL],
  161. fail_silently=False)
  162. except BadHeaderError:
  163. return HttpResponse('Invalid header found.')
  164. return done(self.request)