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.

189 lines
6.5 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 project(request):
  32. # # return HttpResponse("Hello, world. You're at the input form")
  33. # # ProjectFormSet = modelformset_factory(Project, fields='__all__')
  34. # if request.method == 'POST':
  35. # print('POST detected')
  36. # form = ProjectForm(request.POST, request.FILES)
  37. # if form.is_valid():
  38. # print('form valid')
  39. # form.save()
  40. #
  41. # # do something.
  42. # else:
  43. # print("form not valid")
  44. # else:
  45. # print
  46. # form = ProjectForm()
  47. # return render(request, 'input/project.html', {'form': form})
  48. def done(request):
  49. return HttpResponse("Your data is save now.")
  50. class InternView(CookieWizardView):
  51. '''This View is for WMDE-employees only'''
  52. template_name = 'input/extern.html'
  53. form_list = [InternForm, ProjectForm]
  54. def get_form(self, step=None, data=None, files=None):
  55. if step is None:
  56. step = self.steps.current
  57. print ("get_form() step " + step)
  58. if step == '1':
  59. prev_data = self.get_cleaned_data_for_step('0')
  60. choice = prev_data.get('choice')
  61. if choice == 'HON':
  62. print ('Ehrenamtsbescheinigung detected!')
  63. form = HonoraryCertificateForm(data)
  64. elif choice == 'PRO':
  65. print ('Projektsteckbrief erreicht!')
  66. form = ProjectForm(data)
  67. else:
  68. raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice}')
  69. else:
  70. form = super().get_form(step, data, files)
  71. return form
  72. def done(self, form_list, **kwargs):
  73. print('ExternView.done() reached')
  74. # gather data from all forms
  75. data = {}
  76. for form in form_list:
  77. data = {**data, **form.cleaned_data}
  78. print(data)
  79. # write data to database
  80. form = form.save(commit=False)
  81. # we have to copy the data from the first form here
  82. # this is ugly code. how can we copy this without explicit writing?
  83. # i found no way to access the ModelForm.Meta.exclude-tupel
  84. form.realname = data['realname']
  85. form.username = data['username']
  86. form.email = data['email']
  87. form.save()
  88. return done(self.request)
  89. class ExternView(CookieWizardView):
  90. '''This View is for Volunteers'''
  91. template_name = "input/extern.html"
  92. form_list = [VolunteerForm, LibraryForm]
  93. def get_form(self, step=None, data=None, files=None):
  94. if step is None:
  95. step = self.steps.current
  96. print ("get_form() step " + step)
  97. if step == '1':
  98. prev_data = self.get_cleaned_data_for_step('0')
  99. choice = prev_data.get('choice')
  100. if choice == 'IFG':
  101. print ('IFG detected!')
  102. form = IFGForm(data)
  103. elif choice in ('BIB', 'SOFT', 'ELIT'):
  104. print ('one of the famous three detected!')
  105. for (k,v) in TYPE_CHOICES:
  106. if k == choice:
  107. break
  108. form = LibraryForm(data)
  109. form.fields['library'].label = v
  110. else:
  111. raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice}')
  112. else:
  113. form = super().get_form(step, data, files)
  114. return form
  115. def done(self, form_list, **kwargs):
  116. print('ExternView.done() reached')
  117. # gather data from all forms
  118. data = {}
  119. for form in form_list:
  120. data = {**data, **form.cleaned_data}
  121. print(data)
  122. # write data to database
  123. form = form.save(commit=False)
  124. # we have to copy the data from the first form here
  125. # this is ugly code. how can we copy this without explicit writing?
  126. # i found no way to access the ModelForm.Meta.exclude-tupel
  127. form.realname = data['realname']
  128. form.username = data['username']
  129. form.email = data['email']
  130. # write type of form in some cases
  131. if data['choice'] in ('BIB', 'ELIT', 'SOFT'):
  132. form.type = data['choice']
  133. form.save()
  134. # add some data to context for mail templates
  135. data['pk'] = form.pk
  136. data['urlprefix'] = URLPREFIX
  137. # we need to send the following mails here:
  138. context = { 'data': data }
  139. try:
  140. # - mail with entered data to the Volunteer
  141. mail_template = get_template('input/ifg_volunteer_mail.txt')
  142. send_mail(
  143. 'form filled',
  144. mail_template.render(context),
  145. IF_EMAIL,
  146. [form.email],
  147. fail_silently=False,
  148. )
  149. # - mail to IF with link to accept/decline
  150. mail_template = get_template('input/if_mail.txt')
  151. send_mail(
  152. 'form filled',
  153. mail_template.render(context),
  154. IF_EMAIL,
  155. [IF_EMAIL],
  156. fail_silently=False,
  157. )
  158. except BadHeaderError:
  159. return HttpResponse('Invalid header found.')
  160. return done(self.request)