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.

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