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.

171 lines
5.9 KiB

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