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.

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