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.

103 lines
3.1 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 .forms import ProjectForm, VolunteerForm, LibraryForm, IFGForm
  8. from .models import Project
  9. def project(request):
  10. # return HttpResponse("Hello, world. You're at the input form")
  11. # ProjectFormSet = modelformset_factory(Project, fields='__all__')
  12. if request.method == 'POST':
  13. print('POST detected')
  14. form = ProjectForm(request.POST, request.FILES)
  15. if form.is_valid():
  16. print('form valid')
  17. form.save()
  18. # do something.
  19. else:
  20. print("form not valid")
  21. else:
  22. print
  23. form = ProjectForm()
  24. return render(request, 'input/project.html', {'form': form})
  25. def accreditation(request):
  26. pass
  27. def travel(request):
  28. pass
  29. def certificate(request):
  30. pass
  31. def done(request):
  32. return HttpResponse("Your data is save now.")
  33. def extern(request):
  34. return HttpResponse("The world out there is large and dangerous")
  35. class ExternView(CookieWizardView):
  36. template_name = "input/extern.html"
  37. form_list = [VolunteerForm, LibraryForm]
  38. # def process_step(self, form):
  39. # if form.cleaned_data.get('choice') == 'IFG':
  40. # print ('IFG detected!')
  41. # self.form_list = [VolunteerForm, IFGForm]
  42. # print('leaving process_step()')
  43. # return self.get_form_step_data(form)
  44. def get_form(self, step=None, data=None, files=None):
  45. if step is None:
  46. step = self.steps.current
  47. print ("get_form() step " + step)
  48. if step == '1':
  49. prev_data = self.get_cleaned_data_for_step('0')
  50. if prev_data.get('choice') == 'IFG':
  51. print ('IFG detected!')
  52. form = IFGForm(data)
  53. else:
  54. form = LibraryForm(data)
  55. else:
  56. form = super().get_form(step, data, files)
  57. return form
  58. def done(self, form_list, **kwargs):
  59. print('ExternView.done() reached')
  60. # gather data from all forms
  61. data = {}
  62. for form in form_list:
  63. data = {**data, **form.cleaned_data}
  64. print(data)
  65. # write data to database
  66. form = form.save(commit=False)
  67. # this is ugly code. how can we copy this without explicit writing?
  68. # i found no way to access the ModelForm.Meta.exclude-tupel
  69. form.realname = data['realname']
  70. form.username = data['username']
  71. form.email = data['email']
  72. form.save()
  73. # we need to send the following mails here:
  74. # - mail with entered data to the Volunteer
  75. send_mail(
  76. 'form filled',
  77. 'you are such a great form filler!',
  78. settings.IF_EMAIL,
  79. [form.email],
  80. fail_silently=False,
  81. )
  82. # - mail to IF with link to accept/decline
  83. return done(self.request)
  84. # return render(self.request, 'saved', {
  85. # 'form_data': [form.cleaned_data for form in form_list],
  86. # })