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.

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