Child: [48e073] (diff)

Download this file

custom_tags.py    30 lines (25 with data), 1.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from django import template
from analysis.models import Instance, Question, Answer, Page
register = template.Library()
@register.simple_tag
def instance_score_question(instance_id, question_id, *args, **kwargs):
instance = Instance.objects.get(pk=instance_id)
question = Question.objects.get(pk=question_id)
try:
answer = Answer.objects.get(question=question, instance=instance)
choice = question.choice_set.get(order=answer.value_integer)
return "(" + str(answer.score) + ") " + choice.text
except:
return "<font color=red>not answered</font>"
@register.simple_tag
def instance_score_page(instance_id, page_id, *args, **kwargs):
instance = Instance.objects.get(pk=instance_id)
page = Page.objects.get(pk=page_id)
score = 0
for question in page.question_set.all():
try:
answer = Answer.objects.get(question=question, instance=instance)
score += answer.score
except:
pass
return "(" + str(score) + ") <strong>" + instance.name + "</strong>"