Ticket #18434: view.txt

File view.txt, 5.4 KB (added by sbaechler, 3 years ago)

view for ticket

Line 
1The view
2
3@login_required
4def fragebogen(request, code):
5    visitorgroup = get_object_or_none(VisitorGroup, code=code)
6    if not visitorgroup:
7        return HttpResponseRedirect('/fragebogen/?q=invalid')
8    user = request.user
9    FragebogenFormSet = modelformset_factory(FragebogenAnswer, form=FragebogenForm, fields=("answer",), extra=0)
10    FragebogenFormSetOptional = modelformset_factory(FragebogenAnswer, form=FragebogenOptionalAnswerForm, fields=("answer",), extra=0)
11    daten_online_quesion = Question.objects.get(id=91)
12    answers = FragebogenAnswer.objects.filter(user=user)
13    #logging.debug('Answers: %s \n' % answers)
14    if not answers:
15        singleanswer_questions = Question.objects.select_related('choice').filter(category__gte=100, handler__in=[30,31])
16        for question in singleanswer_questions:
17            answer=FragebogenAnswer()
18            answer.question = question
19            answer.user = user
20            answer.visitorgroup = visitorgroup
21            answer.save()
22        answers = FragebogenAnswer.objects.filter(user=user)
23    if request.method == 'POST':
24        internet_formset = FragebogenFormSet(request.POST, queryset=answers.filter(question__category=100).order_by('-question__ordering'), prefix='internet')
25        games_formset = FragebogenFormSet(request.POST, queryset=answers.filter(question__category=110).order_by('question__ordering'), prefix='games')
26        communities_formset = FragebogenFormSetOptional(request.POST, queryset=answers.filter(question__category=120, question__handler__in=[30,31]).order_by('question__ordering'), prefix='communities')
27        communities_frage_form = FragebogenMultipleAnswersForm(request.POST, question=daten_online_quesion)
28        handy_formset = FragebogenFormSet(request.POST, queryset=answers.filter(question__category=130).order_by('question__ordering'), prefix='handy')
29        interessenform = InteressenForm(request.POST)
30
31        if internet_formset.is_valid() and games_formset.is_valid()\
32          and communities_formset.is_valid() and handy_formset.is_valid()\
33          and interessenform.is_valid() and communities_frage_form.is_valid():
34           
35            internet_formset.save()
36            games_formset.save()
37            communities_formset.save()
38            handy_formset.save()
39            processMultipleChoiceForm(request, form=communities_frage_form, visitorgroup=visitorgroup) #Verwaltet Datenbankeinträge
40            weg = interesse(interessenform)
41            UserProfile.objects.filter(user=request.user).update(weg=weg)
42
43        return HttpResponseRedirect('/fragebogen/danke/')
44    else:
45       
46        internet_formset = FragebogenFormSet(queryset=answers.filter(question__category=100).order_by('-question__ordering'), prefix='internet')
47        games_formset = FragebogenFormSet(queryset=answers.filter(question__category=110).order_by('question__ordering'), prefix='games')
48        communities_formset = FragebogenFormSetOptional(queryset=answers.filter(question__category=120, question__handler__in=[30,31]).order_by('question__ordering'), prefix='communities')
49        communities_frage_form = FragebogenMultipleAnswersForm(question=daten_online_quesion)
50        handy_formset = FragebogenFormSet(queryset=answers.filter(question__category=130).order_by('question__ordering'), prefix='handy')
51        interessenform = InteressenForm()
52
53
54
55The Diff:
56
57if request.method == 'POST':
58-        internet_formset = FragebogenFormSet(request.POST, prefix='internet')
59-        games_formset = FragebogenFormSet(request.POST, prefix='games')
60-        communities_formset = FragebogenFormSetOptional(request.POST, prefix='communities')
61+        internet_formset = FragebogenFormSet(request.POST, queryset=answers.filter(question__category=100).order_by('-question__ordering'), prefix='internet')
62+        games_formset = FragebogenFormSet(request.POST, queryset=answers.filter(question__category=110).order_by('question__ordering'), prefix='games')
63+        communities_formset = FragebogenFormSetOptional(request.POST, queryset=answers.filter(question__category=120, question__handler__in=[30,31]).order_by('question__ordering'), prefix='communities')
64         communities_frage_form = FragebogenMultipleAnswersForm(request.POST, question=daten_online_quesion)
65-        handy_formset = FragebogenFormSet(request.POST, prefix='handy')
66+        handy_formset = FragebogenFormSet(request.POST, queryset=answers.filter(question__category=130).order_by('question__ordering'), prefix='handy')
67         interessenform = InteressenForm(request.POST)
68+
69         if internet_formset.is_valid() and games_formset.is_valid()\
70           and communities_formset.is_valid() and handy_formset.is_valid()\
71           and interessenform.is_valid() and communities_frage_form.is_valid():
72@@ -121,12 +133,13 @@ def fragebogen(request, code):
73             games_formset.save()
74             communities_formset.save()
75             handy_formset.save()
76-            #processMultipleChoiceForm(request, form=communities_frage_form, visitorgroup=visitorgroup) #Verwaltet Datenbankeinträge
77+            processMultipleChoiceForm(request, form=communities_frage_form, visitorgroup=visitorgroup) #Verwaltet Datenbankeinträge
78             weg = interesse(interessenform)
79             UserProfile.objects.filter(user=request.user).update(weg=weg)
80-            return HttpResponseRedirect('/fragebogen/danke/')
81+
82+        return HttpResponseRedirect('/fragebogen/danke/')
Back to Top