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.

212 lines
7.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. from datetime import date
  2. from smtplib import SMTPException
  3. from django.shortcuts import render
  4. from django.forms import modelformset_factory
  5. from django.http import HttpResponse
  6. from formtools.wizard.views import CookieWizardView
  7. from django.core.mail import send_mail, BadHeaderError
  8. from django.conf import settings
  9. from django.template.loader import get_template
  10. from django.template import Context
  11. from django.contrib.auth.decorators import login_required
  12. from django.contrib.auth.mixins import LoginRequiredMixin
  13. from .forms import ProjectForm, ExternForm, LibraryForm, IFGForm, LiteratureForm,\
  14. HonoraryCertificateForm, InternForm, TravelForm, EmailForm,\
  15. ListForm, BusinessCardForm, INTERN_CHOICES
  16. from .models import Project, TYPE_CHOICES, Library, Literature
  17. from .settings import IF_EMAIL
  18. def auth_deny(choice,pk,auth):
  19. if choice in ('BIB', 'ELIT', 'SOFT'):
  20. Library.set_granted(pk,auth)
  21. if choice == 'LIT':
  22. Literature.set_granted(pk,auth)
  23. if choice == 'IFG':
  24. IFG.set_granted(pk,auth)
  25. else:
  26. return HttpResponse(f'ERROR! UNKNWON CHOICE TYPE! {choice}')
  27. return False
  28. @login_required
  29. def authorize(request, choice, pk):
  30. '''If IF grant a support they click a link in a mail which leads here.
  31. We write the granted field in the database here and set a timestamp.'''
  32. ret = auth_deny(choice, pk, True)
  33. if ret:
  34. return ret
  35. else:
  36. return HttpResponse(f"AUTHORIZED! choice: {choice}, pk: {pk}")
  37. @login_required
  38. def deny(request, choice, pk):
  39. '''If IF denies a support they click a link in a mail which leads here
  40. We write the granted field in the database here.'''
  41. ret = auth_deny(choice, pk, False)
  42. if ret:
  43. return ret
  44. else:
  45. return HttpResponse(f"DENIED! choice: {choice}, pk: {pk}")
  46. def done(request):
  47. return HttpResponse("Your data is save now.")
  48. class InternView(LoginRequiredMixin, CookieWizardView):
  49. '''This View is for WMDE-employees only'''
  50. template_name = 'input/extern.html'
  51. form_list = [InternForm, ProjectForm]
  52. def get_form(self, step=None, data=None, files=None):
  53. '''this function determines which part of the multipart form is
  54. displayed next'''
  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. print(f'choice detection: {INTERN_CHOICES[choice]}')
  62. if choice == 'HON':
  63. form = HonoraryCertificateForm(data)
  64. elif choice == 'PRO':
  65. form = ProjectForm(data)
  66. elif choice == 'TRAV':
  67. form = TravelForm(data)
  68. else:
  69. raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice} in InternView')
  70. else:
  71. form = super().get_form(step, data, files)
  72. return form
  73. def done(self, form_list, **kwargs):
  74. print('InternView.done() reached')
  75. # gather data from all forms
  76. data = {}
  77. for form in form_list:
  78. data = {**data, **form.cleaned_data}
  79. print(data)
  80. # write data to database
  81. form = form.save(commit=False)
  82. # we have to copy the data from the first form here
  83. # this is ugly code. how can we copy this without explicit writing?
  84. # i found no way to access the ModelForm.Meta.exclude-tupel
  85. form.realname = data['realname']
  86. # form.username = data['username']
  87. form.email = data['email']
  88. form.granted = True
  89. form.granted_date = date.today()
  90. form.save()
  91. return done(self.request)
  92. class ExternView(CookieWizardView):
  93. '''This View is for Volunteers'''
  94. template_name = "input/extern.html"
  95. form_list = [ExternForm, LibraryForm]
  96. def get_form(self, step=None, data=None, files=None):
  97. '''this function determines which part of the multipart form is
  98. displayed next'''
  99. if step is None:
  100. step = self.steps.current
  101. print ("get_form() step " + step)
  102. if step == '1':
  103. prev_data = self.get_cleaned_data_for_step('0')
  104. choice = prev_data.get('choice')
  105. print(f'choice detection in ExternView: {TYPE_CHOICES[choice]}')
  106. if choice == 'IFG':
  107. form = IFGForm(data)
  108. elif choice in ('BIB', 'SOFT', 'ELIT'):
  109. form = LibraryForm(data)
  110. form.fields['library'].label = TYPE_CHOICES[choice]
  111. elif choice == 'MAIL':
  112. form = EmailForm(data)
  113. elif choice == 'LIT':
  114. form = LiteratureForm(data)
  115. elif choice == 'VIS':
  116. form = BusinessCardForm(data)
  117. elif choice == 'LIST':
  118. form = ListForm(data)
  119. else:
  120. raise RuntimeError(f'ERROR! UNKNOWN FORMTYPE {choice} in ExternView')
  121. self.choice = choice
  122. else:
  123. form = super().get_form(step, data, files)
  124. return form
  125. def get_context_data(self, **kwargs):
  126. context = super().get_context_data(**kwargs)
  127. if hasattr(self, 'choice'):
  128. context["choice"] = TYPE_CHOICES[self.choice]
  129. return context
  130. def done(self, form_list, **kwargs):
  131. print('ExternView.done() reached')
  132. # gather data from all forms
  133. data = {}
  134. for form in form_list:
  135. data = {**data, **form.cleaned_data}
  136. print(data)
  137. # write data to database
  138. modell = form.save(commit=False)
  139. # we have to copy the data from the first form here
  140. # this is a bit ugly code. can we copy this without explicit writing?
  141. modell.realname = data['realname']
  142. # form.username = data['username']
  143. modell.email = data['email']
  144. # write type of form in some cases
  145. if data['choice'] in ('BIB', 'ELIT', 'SOFT'):
  146. modell.type = data['choice']
  147. form.save()
  148. # add some data to context for mail templates
  149. data['pk'] = modell.pk
  150. data['urlprefix'] = settings.URLPREFIX
  151. data['grant'] = ('LIT', 'SOFT', 'ELIT', 'BIB', 'IFG')
  152. data['DOMAIN'] = ('MAIL', 'LIST')
  153. data['typestring'] = TYPE_CHOICES[data['choice']]
  154. # we need to send the following mails here:
  155. context = { 'data': data }
  156. try:
  157. # - mail with entered data to the Volunteer
  158. mail_template = get_template('input/ifg_volunteer_mail.txt')
  159. send_mail(
  160. 'Formular ausgefüllt',
  161. mail_template.render(context),
  162. IF_EMAIL,
  163. [data['email']],
  164. fail_silently=False)
  165. # - mail to IF with link to accept/decline
  166. mail_template = get_template('input/if_mail.txt')
  167. send_mail(
  168. 'Formular ausgefüllt',
  169. mail_template.render(context),
  170. IF_EMAIL,
  171. [IF_EMAIL],
  172. fail_silently=False)
  173. # raise SMTPException("testing pupose only")
  174. except BadHeaderError:
  175. modell.delete()
  176. return HttpResponse('Invalid header found. Data not saved!')
  177. except SMTPException:
  178. modell.delete()
  179. return HttpResponse('Error in sending mails (propably wrong adress?). Data not saved!')
  180. return done(self.request)