From d7aa685e22693955671d1fd1b675b71e249e0d1c Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Tue, 2 Jul 2019 22:25:07 +0200 Subject: [PATCH] Tweak `Question` display --- app.py | 25 ++++++++++++++++++++++--- templates/utils.html | 11 +++++------ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 7489735..1dfb157 100644 --- a/app.py +++ b/app.py @@ -442,9 +442,28 @@ def is_img(filename): @app.template_filter() -def get_answer_count(choice, meta): - print(choice, meta) - return meta.get("question_answers", {}).get(_answer_key(choice), 0) +def get_answer_count(choice, obj, meta): + count_from_meta = meta.get("question_answers", {}).get(_answer_key(choice), 0) + print(count_from_meta) + print(choice, obj, meta) + if count_from_meta: + return count_from_meta + for option in obj.get("oneOf", obj.get("anyOf", [])): + if option.get("name") == choice: + return option.get("replies", {}).get("totalItems", 0) + + +@app.template_filter() +def get_total_answers_count(obj, meta): + cached = meta.get("question_replies", 0) + if cached: + return cached + cnt = 0 + print("OKI", obj) + for choice in obj.get("anyOf", obj.get("oneOf", [])): + print(choice) + cnt += choice.get("replies", {}).get("totalItems", 0) + return cnt def add_response_headers(headers={}): diff --git a/templates/utils.html b/templates/utils.html index ed0933e..56c5204 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -76,14 +76,13 @@ {% elif obj | has_type('Question') %} {{ obj.content | clean | safe }} - {% if obj.id | is_from_outbox or (meta.question_replies and (obj.closed or meta.voted_for)) %} + {% if obj.id | is_from_outbox or obj.closed or meta.voted_for %}

{% if real_end_time | gtnow %} - Ended {{ obj.endTime | format_timeago }} with {{ total_votes }} vote{% if total_votes | gtone %}s{% endif %}. + Ended {{ real_end_time | format_timeago }} with {{ total_votes }} vote{% if total_votes | gtone %}s{% endif %}. {% else %} - Ends {{ obj.endTime | format_timeago }} ({{ total_votes }} vote{% if total_votes | gtone %}s{% endif %} as of now). + Ends {{ real_end_time | format_timeago }} ({{ total_votes }} vote{% if total_votes | gtone %}s{% endif %} as of now). {% endif %}

{% else %}