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.

205 lines
7.1 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. else:
  122. form = super().get_form(step, data, files)
  123. return form
  124. def done(self, form_list, **kwargs):
  125. print('ExternView.done() reached')
  126. # gather data from all forms
  127. data = {}
  128. for form in form_list:
  129. data = {**data, **form.cleaned_data}
  130. print(data)
  131. # write data to database
  132. modell = form.save(commit=False)
  133. # we have to copy the data from the first form here
  134. # this is a bit ugly code. can we copy this without explicit writing?
  135. modell.realname = data['realname']
  136. # form.username = data['username']
  137. modell.email = data['email']
  138. # write type of form in some cases
  139. if data['choice'] in ('BIB', 'ELIT', 'SOFT'):
  140. modell.type = data['choice']
  141. form.save()
  142. # add some data to context for mail templates
  143. data['pk'] = modell.pk
  144. data['urlprefix'] = settings.URLPREFIX
  145. data['grant'] = ('LIT', 'SOFT', 'ELIT', 'BIB', 'IFG')
  146. data['DOMAIN'] = ('MAIL', 'LIST')
  147. data['typestring'] = TYPE_CHOICES[data['choice']]
  148. # we need to send the following mails here:
  149. context = { 'data': data }
  150. try:
  151. # - mail with entered data to the Volunteer
  152. mail_template = get_template('input/ifg_volunteer_mail.txt')
  153. send_mail(
  154. 'Formular ausgefüllt',
  155. mail_template.render(context),
  156. IF_EMAIL,
  157. [data['email']],
  158. fail_silently=False)
  159. # - mail to IF with link to accept/decline
  160. mail_template = get_template('input/if_mail.txt')
  161. send_mail(
  162. 'Formular ausgefüllt',
  163. mail_template.render(context),
  164. IF_EMAIL,
  165. [IF_EMAIL],
  166. fail_silently=False)
  167. # raise SMTPException("testing pupose only")
  168. except BadHeaderError:
  169. modell.delete()
  170. return HttpResponse('Invalid header found. Data not saved!')
  171. except SMTPException:
  172. modell.delete()
  173. return HttpResponse('Error in sending mails (propably wrong adress?). Data not saved!')
  174. return done(self.request)