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.

76 lines
2.5 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 intern(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 done(request):
  24. return HttpResponse("Your data is save now.")
  25. def extern(request):
  26. return HttpResponse("The world out there is large and dangerous")
  27. class ExternView(CookieWizardView):
  28. template_name = "input/extern.html"
  29. form_list = [VolunteerForm, LibraryForm]
  30. # def process_step(self, form):
  31. # if form.cleaned_data.get('choice') == 'IFG':
  32. # print ('IFG detected!')
  33. # self.form_list = [VolunteerForm, IFGForm]
  34. # print('leaving process_step()')
  35. # return self.get_form_step_data(form)
  36. def get_form(self, step=None, data=None, files=None):
  37. if step:
  38. print ("get_form() step " + step)
  39. if step == '1':
  40. print('iiiooopppp')
  41. data = self.get_cleaned_data_for_step('0')
  42. print (data)
  43. if data.get('choice') == 'IFG':
  44. print ('IFG detected!')
  45. form = super().get_form(step, data, files)
  46. return form
  47. def done(self, form_list, **kwargs):
  48. print('ExternView.done() reached')
  49. # gather data from all forms
  50. data = {}
  51. for form in form_list:
  52. data = {**data, **form.cleaned_data}
  53. print(data)
  54. # write data to database
  55. form = form.save(commit=False)
  56. # this is ugly code. how can we copy this without explicit writing?
  57. # i found no way to access the ModelForm.Meta.exclude-array
  58. form.realname = data['realname']
  59. form.username = data['username']
  60. form.email = data['email']
  61. form.save()
  62. return done(self.request)
  63. # return render(self.request, 'saved', {
  64. # 'form_data': [form.cleaned_data for form in form_list],
  65. # })