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.

107 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
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
  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
  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. if prev_data.get('choice') == 'IFG':
  53. print ('IFG detected!')
  54. form = IFGForm(data)
  55. else:
  56. form = LibraryForm(data)
  57. else:
  58. form = super().get_form(step, data, files)
  59. return form
  60. def done(self, form_list, **kwargs):
  61. print('ExternView.done() reached')
  62. # gather data from all forms
  63. data = {}
  64. for form in form_list:
  65. data = {**data, **form.cleaned_data}
  66. print(data)
  67. # write data to database
  68. form = form.save(commit=False)
  69. # this is ugly code. how can we copy this without explicit writing?
  70. # i found no way to access the ModelForm.Meta.exclude-tupel
  71. form.realname = data['realname']
  72. form.username = data['username']
  73. form.email = data['email']
  74. form.save()
  75. # we need to send the following mails here:
  76. # - mail with entered data to the Volunteer
  77. mail_template = get_template('input/ifg_volunteer_mail.txt')
  78. context = { 'data': data }
  79. send_mail(
  80. 'form filled',
  81. mail_template.render(context),
  82. settings.IF_EMAIL,
  83. [form.email],
  84. fail_silently=False,
  85. )
  86. # - mail to IF with link to accept/decline
  87. return done(self.request)
  88. # return render(self.request, 'saved', {
  89. # 'form_data': [form.cleaned_data for form in form_list],
  90. # })