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.

186 lines
6.3 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('ERROR! UNKNWON CHOICE TYPE!')
  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('ERROR! UNKNWON CHOICE TYPE!')
  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 the 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. else:
  65. raise RuntimeError('ERROR! UNKNOWN FORMTYPE!')
  66. else:
  67. form = super().get_form(step, data, files)
  68. return form
  69. def done(self, form_list, **kwargs):
  70. print('ExternView.done() reached')
  71. # gather data from all forms
  72. data = {}
  73. for form in form_list:
  74. data = {**data, **form.cleaned_data}
  75. print(data)
  76. # write data to database
  77. form = form.save(commit=False)
  78. # we have to copy the data from the first form here
  79. # this is ugly code. how can we copy this without explicit writing?
  80. # i found no way to access the ModelForm.Meta.exclude-tupel
  81. form.realname = data['realname']
  82. form.username = data['username']
  83. form.email = data['email']
  84. form.save()
  85. return done(self.request)
  86. class ExternView(CookieWizardView):
  87. '''This View is for Volunteers'''
  88. template_name = "input/extern.html"
  89. form_list = [VolunteerForm, LibraryForm]
  90. def get_form(self, step=None, data=None, files=None):
  91. if step is None:
  92. step = self.steps.current
  93. print ("get_form() step " + step)
  94. if step == '1':
  95. prev_data = self.get_cleaned_data_for_step('0')
  96. choice = prev_data.get('choice')
  97. if choice == 'IFG':
  98. print ('IFG detected!')
  99. form = IFGForm(data)
  100. elif choice in ('BIB', 'SOFT', 'ELIT'):
  101. print ('one of the famous three detected!')
  102. for (k,v) in TYPE_CHOICES:
  103. if k == choice:
  104. break
  105. form = LibraryForm(data)
  106. form.fields['library'].label = v
  107. else:
  108. raise RuntimeError('ERROR! UNKNOWN FORMTYPE!')
  109. else:
  110. form = super().get_form(step, data, files)
  111. return form
  112. def done(self, form_list, **kwargs):
  113. print('ExternView.done() reached')
  114. # gather data from all forms
  115. data = {}
  116. for form in form_list:
  117. data = {**data, **form.cleaned_data}
  118. print(data)
  119. # write data to database
  120. form = form.save(commit=False)
  121. # we have to copy the data from the first form here
  122. # this is ugly code. how can we copy this without explicit writing?
  123. # i found no way to access the ModelForm.Meta.exclude-tupel
  124. form.realname = data['realname']
  125. form.username = data['username']
  126. form.email = data['email']
  127. # write type of form in some cases
  128. if data['choice'] in ('BIB', 'ELIT', 'SOFT'):
  129. form.type = data['choice']
  130. form.save()
  131. # add some data to context for mail templates
  132. data['pk'] = form.pk
  133. data['urlprefix'] = URLPREFIX
  134. # we need to send the following mails here:
  135. context = { 'data': data }
  136. try:
  137. # - mail with entered data to the Volunteer
  138. mail_template = get_template('input/ifg_volunteer_mail.txt')
  139. send_mail(
  140. 'form filled',
  141. mail_template.render(context),
  142. IF_EMAIL,
  143. [form.email],
  144. fail_silently=False,
  145. )
  146. # - mail to IF with link to accept/decline
  147. mail_template = get_template('input/if_mail.txt')
  148. send_mail(
  149. 'form filled',
  150. mail_template.render(context),
  151. IF_EMAIL,
  152. [IF_EMAIL],
  153. fail_silently=False,
  154. )
  155. except BadHeaderError:
  156. return HttpResponse('Invalid header found.')
  157. return done(self.request)