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.

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