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.

159 lines
5.2 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. from .models import Project, TYPE_CHOICES, Library
  12. from .settings import URLPREFIX, IF_EMAIL
  13. def authorize(request, choice, pk):
  14. '''If IF grant a support they click a link in a mail which leads here'''
  15. # TODO: write a timestamp which is needed to determine time of next mail
  16. if choice in ('BIB', 'ELIT', 'SOFT'):
  17. Library.set_granted(pk,True)
  18. return HttpResponse(f"AUTHORIZED! choice: {choice}, pk: {pk}")
  19. else:
  20. return HttpResponse('ERROR! UNKNWON CHOICE TYPE!')
  21. def deny(request, choice, pk):
  22. '''If IF denies a support they click a link in a mail which leads here'''
  23. if choice in ('BIB', 'ELIT', 'SOFT'):
  24. Library.set_granted(pk,False)
  25. return HttpResponse(f"DENIED! choice: {choice}, pk: {pk}")
  26. else:
  27. return HttpResponse('ERROR! UNKNWON CHOICE TYPE!')
  28. def project(request):
  29. # return HttpResponse("Hello, world. You're at the input form")
  30. # ProjectFormSet = modelformset_factory(Project, fields='__all__')
  31. if request.method == 'POST':
  32. print('POST detected')
  33. form = ProjectForm(request.POST, request.FILES)
  34. if form.is_valid():
  35. print('form valid')
  36. form.save()
  37. # do something.
  38. else:
  39. print("form not valid")
  40. else:
  41. print
  42. form = ProjectForm()
  43. return render(request, 'input/project.html', {'form': form})
  44. def accreditation(request):
  45. pass
  46. def travel(request):
  47. pass
  48. def certificate(request):
  49. pass
  50. def done(request):
  51. return HttpResponse("Your data is save now.")
  52. def extern(request):
  53. return HttpResponse("The world out there is large and dangerous")
  54. class ExternView(CookieWizardView):
  55. template_name = "input/extern.html"
  56. form_list = [VolunteerForm, LibraryForm]
  57. # def process_step(self, form):
  58. # if form.cleaned_data.get('choice') == 'IFG':
  59. # print ('IFG detected!')
  60. # self.form_list = [VolunteerForm, IFGForm]
  61. # print('leaving process_step()')
  62. # return self.get_form_step_data(form)
  63. def get_form(self, step=None, data=None, files=None):
  64. if step is None:
  65. step = self.steps.current
  66. print ("get_form() step " + step)
  67. if step == '1':
  68. prev_data = self.get_cleaned_data_for_step('0')
  69. choice = prev_data.get('choice')
  70. if choice == 'IFG':
  71. print ('IFG detected!')
  72. form = IFGForm(data)
  73. elif choice in ('BIB', 'SOFT', 'ELIT'):
  74. print ('one of the famous three detected!')
  75. for (k,v) in TYPE_CHOICES:
  76. if k == choice:
  77. break
  78. form = LibraryForm(data)
  79. form.fields['library'].label = v
  80. else:
  81. print('ERROR! UNKNOWN FORMTYPE!')
  82. else:
  83. form = super().get_form(step, data, files)
  84. return form
  85. def done(self, form_list, **kwargs):
  86. print('ExternView.done() reached')
  87. # gather data from all forms
  88. data = {}
  89. for form in form_list:
  90. data = {**data, **form.cleaned_data}
  91. print(data)
  92. # write data to database
  93. form = form.save(commit=False)
  94. # we have to copy the data from the first form here
  95. # this is ugly code. how can we copy this without explicit writing?
  96. # i found no way to access the ModelForm.Meta.exclude-tupel
  97. form.realname = data['realname']
  98. form.username = data['username']
  99. form.email = data['email']
  100. # write type of form in some cases
  101. if data['choice'] in ('BIB', 'ELIT', 'SOFT'):
  102. form.type = data['choice']
  103. form.save()
  104. # add some data to context for mail templates
  105. data['pk'] = form.pk
  106. data['urlprefix'] = URLPREFIX
  107. # we need to send the following mails here:
  108. context = { 'data': data }
  109. try:
  110. # - mail with entered data to the Volunteer
  111. mail_template = get_template('input/ifg_volunteer_mail.txt')
  112. send_mail(
  113. 'form filled',
  114. mail_template.render(context),
  115. IF_EMAIL,
  116. [form.email],
  117. fail_silently=False,
  118. )
  119. # - mail to IF with link to accept/decline
  120. mail_template = get_template('input/if_mail.txt')
  121. send_mail(
  122. 'form filled',
  123. mail_template.render(context),
  124. IF_EMAIL,
  125. [IF_EMAIL],
  126. fail_silently=False,
  127. )
  128. except BadHeaderError:
  129. return HttpResponse('Invalid header found.')
  130. return done(self.request)
  131. # return render(self.request, 'saved', {
  132. # 'form_data': [form.cleaned_data for form in form_list],
  133. # })