Skip to content

Commit

Permalink
fix dates in project_answers_tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Dec 6, 2016
1 parent 7c17c5b commit d673b02
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
19 changes: 16 additions & 3 deletions apps/projects/templates/projects/project_answers_tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ <h3>{{ subsection.title}}</h3>
{% for set in question.sets %}

{% if question.is_collection %}
<p><em>{{ set.id }}:</em> {{ set.answers | join:', ' }}</p>
<p><em>{{ set.id }}:</em></>
<ul>
{% for answer in set.answers %}
<li>{{ answer }}</li>
{% endfor %}
</ul>
{% else %}
<p><em>{{ set.id }}:</em> {{ set.answers.0 }}</p>
{% endif %}
Expand All @@ -35,7 +40,11 @@ <h3>{{ subsection.title}}</h3>
<p><strong>{{ question.text }}</strong></p>

{% if question.is_collection %}
<p>{{ question.answers | join:', ' }}</p>
<ul>
{% for answer in question.answers %}
<li>{{ answer }}</li>
{% endfor %}
</ul>
{% else %}
<p>{{ question.answers.0 }}</p>
{% endif %}
Expand All @@ -49,7 +58,11 @@ <h3>{{ subsection.title}}</h3>
<p><strong>{{ entity.text }}</strong></p>

{% if entity.is_collection %}
<p>{{ entity.answers | join:', ' }}</p>
<ul>
{% for answer in entity.answers %}
<li>{{ answer }}</li>
{% endfor %}
</ul>
{% else %}
<p>{{ entity.answers.0 }}</p>
{% endif %}
Expand Down
63 changes: 30 additions & 33 deletions apps/projects/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import iso8601

from django.utils.translation import ugettext_lazy as _


def get_answers_tree(project, snapshot=None):

values = {}
valuesets = {}
yesno = {
'1': _('yes'),
'0': _('no')
}

# loop over all values of this snapshot
for value in project.values.filter(snapshot=snapshot):
Expand Down Expand Up @@ -55,13 +53,7 @@ def get_answers_tree(project, snapshot=None):
for value in values[catalog_question.attribute_entity.id]:

if value.set_index == set_index:
if value.option:
answers.append(value.option.text)
elif value.text:
if catalog_question.widget_type == 'yesno':
answers.append(yesno[value.text])
else:
answers.append(value.text)
answers.append(get_answer(value, catalog_question.attribute_entity.attribute))

if answers:
sets.append({
Expand All @@ -73,16 +65,16 @@ def get_answers_tree(project, snapshot=None):
questions.append({
'sets': sets,
'text': catalog_question.text,
'attribute': catalog_question.attribute_entity.label,
'attribute': catalog_question.attribute_entity.attribute,
'is_collection': catalog_question.attribute_entity.is_collection or catalog_question.widget_type == 'checkbox'
})

if questions:
entities.append({
'questions': questions,
'attribute': catalog_entity.attribute_entity.label,
'attribute': catalog_entity.attribute_entity,
'is_set': True,
'is_collection': True
'is_collection': True,
})

else:
Expand All @@ -94,27 +86,20 @@ def get_answers_tree(project, snapshot=None):

answers = []
for value in values[catalog_question.attribute_entity.id]:

if value.option:
answers.append(value.option.text)
elif value.text:
if catalog_question.widget_type == 'yesno':
answers.append(yesno[value.text])
else:
answers.append(value.text)
answers.append(get_answer(value, catalog_question.attribute_entity.attribute))

if answers:
questions.append({
'text': catalog_question.text,
'attribute': catalog_question.attribute_entity.label,
'attribute': catalog_question.attribute_entity.attribute,
'answers': answers,
'is_collection': catalog_question.attribute_entity.is_collection or catalog_question.widget_type == 'checkbox'
})

if questions:
entities.append({
'questions': questions,
'attribute': catalog_entity.attribute_entity.label,
'attribute': catalog_entity.attribute_entity,
'is_set': True,
'is_collection': False
})
Expand All @@ -126,19 +111,12 @@ def get_answers_tree(project, snapshot=None):

answers = []
for value in values[catalog_entity.attribute_entity.id]:

if value.option:
answers.append(value.option.text)
elif value.text:
if catalog_entity.question.widget_type == 'yesno':
answers.append(yesno[value.text])
else:
answers.append(value.text)
answers.append(get_answer(value, catalog_entity.attribute_entity.attribute))

if answers:
entities.append({
'text': catalog_entity.question.text,
'attribute': catalog_entity.attribute_entity.label,
'attribute': catalog_entity.attribute_entity.attribute,
'answers': answers,
'is_set': False,
'is_collection': catalog_entity.attribute_entity.is_collection or catalog_entity.question.widget_type == 'checkbox'
Expand All @@ -157,3 +135,22 @@ def get_answers_tree(project, snapshot=None):
})

return {'sections': sections}


def get_answer(value, attribute):

if value.option:
return value.option.text

elif value.text:
if attribute.value_type == 'datetime':
return iso8601.parse_date(value.text).date()

elif attribute.value_type == 'boolian':
if bool(value.text):
return _('yes')
else:
return _('no')

else:
return value.text

0 comments on commit d673b02

Please sign in to comment.