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.

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