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.

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