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.

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