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.

198 lines
6.8 KiB

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