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.

176 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
  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. if choice == 'HON':
  47. print ('Ehrenamtsbescheinigung detected!')
  48. form = HonoraryCertificateForm(data)
  49. elif choice == 'PRO':
  50. print ('Projektsteckbrief erreicht!')
  51. form = ProjectForm(data)
  52. elif choice == 'TRAV':
  53. print('Reisekosten erreicht')
  54. form = TravelForm(data)
  55. else:
  56. raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice}')
  57. else:
  58. form = super().get_form(step, data, files)
  59. return form
  60. def done(self, form_list, **kwargs):
  61. print('InternView.done() reached')
  62. # gather data from all forms
  63. data = {}
  64. for form in form_list:
  65. data = {**data, **form.cleaned_data}
  66. print(data)
  67. # write data to database
  68. form = form.save(commit=False)
  69. # we have to copy the data from the first form here
  70. # this is ugly code. how can we copy this without explicit writing?
  71. # i found no way to access the ModelForm.Meta.exclude-tupel
  72. form.realname = data['realname']
  73. # form.username = data['username']
  74. form.email = data['email']
  75. form.granted = True
  76. form.granted_date = date.today()
  77. form.save()
  78. return done(self.request)
  79. class ExternView(CookieWizardView):
  80. '''This View is for Volunteers'''
  81. template_name = "input/extern.html"
  82. form_list = [ExternForm, LibraryForm]
  83. def get_form(self, step=None, data=None, files=None):
  84. '''this function determines which part of the multipart form is
  85. displayed next'''
  86. if step is None:
  87. step = self.steps.current
  88. print ("get_form() step " + step)
  89. if step == '1':
  90. prev_data = self.get_cleaned_data_for_step('0')
  91. choice = prev_data.get('choice')
  92. if choice == 'IFG':
  93. print ('IFG detected!')
  94. form = IFGForm(data)
  95. elif choice in ('BIB', 'SOFT', 'ELIT'):
  96. print ('one of the famous three detected!')
  97. form = LibraryForm(data)
  98. form.fields['library'].label = TYPE_CHOICES[choice]
  99. else:
  100. raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice}')
  101. else:
  102. form = super().get_form(step, data, files)
  103. return form
  104. def done(self, form_list, **kwargs):
  105. print('ExternView.done() reached')
  106. # gather data from all forms
  107. data = {}
  108. for form in form_list:
  109. data = {**data, **form.cleaned_data}
  110. print(data)
  111. # write data to database
  112. form = form.save(commit=False)
  113. # we have to copy the data from the first form here
  114. # this is a bit ugly code. how can we copy this without explicit writing?
  115. form.realname = data['realname']
  116. # form.username = data['username']
  117. form.email = data['email']
  118. # write type of form in some cases
  119. if data['choice'] in ('BIB', 'ELIT', 'SOFT'):
  120. form.type = data['choice']
  121. form.save()
  122. # add some data to context for mail templates
  123. data['pk'] = form.pk
  124. data['urlprefix'] = URLPREFIX
  125. # we need to send the following mails here:
  126. context = { 'data': data }
  127. try:
  128. # - mail with entered data to the Volunteer
  129. mail_template = get_template('input/ifg_volunteer_mail.txt')
  130. send_mail(
  131. 'Formular ausgefüllt',
  132. mail_template.render(context),
  133. IF_EMAIL,
  134. [form.email],
  135. fail_silently=False)
  136. # - mail to IF with link to accept/decline
  137. mail_template = get_template('input/if_mail.txt')
  138. send_mail(
  139. 'Formular ausgefüllt',
  140. mail_template.render(context),
  141. IF_EMAIL,
  142. [IF_EMAIL],
  143. fail_silently=False)
  144. except BadHeaderError:
  145. return HttpResponse('Invalid header found.')
  146. return done(self.request)