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.

160 lines
5.1 KiB

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