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.

170 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 django.contrib.sites.models import Site
  10. from .forms import ProjectForm, VolunteerForm, LibraryForm, IFGForm,\
  11. HonoraryCertificateForm, InternForm
  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. if step is None:
  39. step = self.steps.current
  40. print ("get_form() step " + step)
  41. if step == '1':
  42. prev_data = self.get_cleaned_data_for_step('0')
  43. choice = prev_data.get('choice')
  44. if choice == 'HON':
  45. print ('Ehrenamtsbescheinigung detected!')
  46. form = HonoraryCertificateForm(data)
  47. elif choice == 'PRO':
  48. print ('Projektsteckbrief erreicht!')
  49. form = ProjectForm(data)
  50. else:
  51. raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice}')
  52. else:
  53. form = super().get_form(step, data, files)
  54. return form
  55. def done(self, form_list, **kwargs):
  56. print('ExternView.done() reached')
  57. # gather data from all forms
  58. data = {}
  59. for form in form_list:
  60. data = {**data, **form.cleaned_data}
  61. print(data)
  62. # write data to database
  63. form = form.save(commit=False)
  64. # we have to copy the data from the first form here
  65. # this is ugly code. how can we copy this without explicit writing?
  66. # i found no way to access the ModelForm.Meta.exclude-tupel
  67. form.realname = data['realname']
  68. form.username = data['username']
  69. form.email = data['email']
  70. form.save()
  71. return done(self.request)
  72. class ExternView(CookieWizardView):
  73. '''This View is for Volunteers'''
  74. template_name = "input/extern.html"
  75. form_list = [VolunteerForm, LibraryForm]
  76. def get_form(self, step=None, data=None, files=None):
  77. if step is None:
  78. step = self.steps.current
  79. print ("get_form() step " + step)
  80. if step == '1':
  81. prev_data = self.get_cleaned_data_for_step('0')
  82. choice = prev_data.get('choice')
  83. if choice == 'IFG':
  84. print ('IFG detected!')
  85. form = IFGForm(data)
  86. elif choice in ('BIB', 'SOFT', 'ELIT'):
  87. print ('one of the famous three detected!')
  88. for (k,v) in TYPE_CHOICES:
  89. if k == choice:
  90. break
  91. form = LibraryForm(data)
  92. form.fields['library'].label = v
  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 ugly code. how can we copy this without explicit writing?
  109. # i found no way to access the ModelForm.Meta.exclude-tupel
  110. form.realname = data['realname']
  111. form.username = data['username']
  112. form.email = data['email']
  113. # write type of form in some cases
  114. if data['choice'] in ('BIB', 'ELIT', 'SOFT'):
  115. form.type = data['choice']
  116. form.save()
  117. # add some data to context for mail templates
  118. data['pk'] = form.pk
  119. data['urlprefix'] = URLPREFIX
  120. # we need to send the following mails here:
  121. context = { 'data': data }
  122. try:
  123. # - mail with entered data to the Volunteer
  124. mail_template = get_template('input/ifg_volunteer_mail.txt')
  125. send_mail(
  126. 'form filled',
  127. mail_template.render(context),
  128. IF_EMAIL,
  129. [form.email],
  130. fail_silently=False,
  131. )
  132. # - mail to IF with link to accept/decline
  133. mail_template = get_template('input/if_mail.txt')
  134. send_mail(
  135. 'form filled',
  136. mail_template.render(context),
  137. IF_EMAIL,
  138. [IF_EMAIL],
  139. fail_silently=False,
  140. )
  141. except BadHeaderError:
  142. return HttpResponse('Invalid header found.')
  143. return done(self.request)