show beautiful titles for some questions

This commit is contained in:
Johannes 2023-10-06 18:48:50 +02:00
parent d5c7c669d6
commit 34ccc61835

View file

@ -1,5 +1,6 @@
import json
from pathlib import Path
from pprint import pprint
from typing import Dict, Tuple, Union # I have just python3.8 here :(
@ -30,17 +31,19 @@ def parse(filename: Union[str, Path]) -> Tuple[
Dict[int, str],
Dict[int, int],
Dict[int, str],
Dict[int, Dict[Tuple[int, str], int]]
Dict[int, Dict[Tuple[int, str], int]],
Dict[int, str]
]:
"""Parse a JSON export from pretix.
:param filename: filename of the JSON export to parse.
:returns: Four mappings:
:returns: Five mappings:
* item_id to item name
* item_id to order count of this item
* question_id to question identifier
* question_id to stats for this question, which identifies an answer by its item_id and actual answer.
The stats map this identifier to number of occurrences
* question_id to question text (the actual question shown to the user)
"""
with open(filename, 'r', encoding='utf8') as f:
data = json.load(f)['event']
@ -48,6 +51,7 @@ def parse(filename: Union[str, Path]) -> Tuple[
items = {i['id']: i['name'] for i in data['items']}
count_items = {id_: 0 for id_ in items}
questions = {q['id']: q['identifier'] for q in data['questions']}
quest_titles = {q['id']: q['question'] for q in data['questions']}
quest_answers = {id_: {} for id_ in questions}
for order in data['orders']:
@ -60,11 +64,11 @@ def parse(filename: Union[str, Path]) -> Tuple[
quest_answers[quest_id][answer_key] = (
quest_answers[quest_id].get(answer_key, 0) + 1
)
return items, count_items, questions, quest_answers
return items, count_items, questions, quest_answers, quest_titles
def wiki_stats(items: Dict[int, str], count_items: Dict[int, int], questions: Dict[int, str],
quest_answers: Dict[int, Dict[Tuple[int, str], int]]) -> str:
quest_answers: Dict[int, Dict[Tuple[int, str], int]], quest_titles: Dict[int, str]) -> str:
"""Generate stats for the Wiki page.
Input parameters are output of the parse() function.
@ -126,7 +130,7 @@ def wiki_stats(items: Dict[int, str], count_items: Dict[int, int], questions: Di
for answer, count in quest_answers[field].items():
if answer[1] in ("False", ""):
continue
ret += f"|| {questions[field]}: || {answer[1]} || {count}\n|-\n"
ret += f"|| {quest_titles[field]}: || {answer[1]} || {count}\n|-\n"
ret += "|}\n\n"
def prefix(id_):