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.

167 lines
5.8 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. form = LibraryForm(data)
  89. form.fields['library'].label = TYPE_CHOICES[choice]
  90. else:
  91. raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice}')
  92. else:
  93. form = super().get_form(step, data, files)
  94. return form
  95. def done(self, form_list, **kwargs):
  96. print('ExternView.done() reached')
  97. # gather data from all forms
  98. data = {}
  99. for form in form_list:
  100. data = {**data, **form.cleaned_data}
  101. print(data)
  102. # write data to database
  103. form = form.save(commit=False)
  104. # we have to copy the data from the first form here
  105. # this is ugly code. how can we copy this without explicit writing?
  106. # i found no way to access the ModelForm.Meta.exclude-tupel
  107. form.realname = data['realname']
  108. form.username = data['username']
  109. form.email = data['email']
  110. # write type of form in some cases
  111. if data['choice'] in ('BIB', 'ELIT', 'SOFT'):
  112. form.type = data['choice']
  113. form.save()
  114. # add some data to context for mail templates
  115. data['pk'] = form.pk
  116. data['urlprefix'] = URLPREFIX
  117. # we need to send the following mails here:
  118. context = { 'data': data }
  119. try:
  120. # - mail with entered data to the Volunteer
  121. mail_template = get_template('input/ifg_volunteer_mail.txt')
  122. send_mail(
  123. 'form filled',
  124. mail_template.render(context),
  125. IF_EMAIL,
  126. [form.email],
  127. fail_silently=False,
  128. )
  129. # - mail to IF with link to accept/decline
  130. mail_template = get_template('input/if_mail.txt')
  131. send_mail(
  132. 'form filled',
  133. mail_template.render(context),
  134. IF_EMAIL,
  135. [IF_EMAIL],
  136. fail_silently=False,
  137. )
  138. except BadHeaderError:
  139. return HttpResponse('Invalid header found.')
  140. return done(self.request)