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.

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