import json from pathlib import Path from pprint import pprint from typing import Dict, Tuple, Union # I have just python3.8 here :( # ids of the questions which represent a size of a merch product SIZE_IDS = (312, 313, 314, 316, 319, 320, 321, 322, 323, 324) # ID of wishlist for ewiges Frühstück BREAKFASTWISHES_ID = 309 # Wie heißt die Athenanas? ATHENE_ID = 305 # ID of the question of how the logo image is to be named ATHENE_CUSTOM_ID = 306 # custom naming of the Athenanas HUMAN_ID = 147 # ticket for a human MASCOT_ID = 148 # ticket for a mascot # Rahmenprogramm RAHMERST_ID = 308 RAHMZWEIT_ID = 317 # Yummy! ESSEN_ID = 302 # diverse checkboxes BOOLEAN_IDS = ( 304, # Deutschlandticket 307, # Erstkomatikerin 310, # Workshop-Themen 301, # Mörderspiel 311, # nur Rufname auf Badge ) 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, str] ]: """Parse a JSON export from pretix. :param filename: filename of the JSON export to parse. :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'] 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']: for position in order['positions']: item_id = position['item'] count_items[item_id] += 1 for answer in position['answers']: quest_id = answer['question'] answer_key = (item_id, answer['answer']) quest_answers[quest_id][answer_key] = ( quest_answers[quest_id].get(answer_key, 0) + 1 ) 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]], quest_titles: Dict[int, str]) -> str: """Generate stats for the Wiki page. Input parameters are output of the parse() function. """ ret = "" ret += ('== Anmeldezahlen ==\n' '{|class="wikitable sortable"\n' '! Posten !! Anzahl\n' '|-\n') for item_id, item_name in items.items(): ret += f"|| {item_name} || {count_items.get(item_id)}\n|-\n" ret += "|}\n\n" ret += ('== Merch-Größenverteilung ==\n' '{|class="wikitable sortable"\n' '! Größe/Eigenschaft !! Wert !! Anzahl\n' '|-\n') for size_id in SIZE_IDS: for size, count in quest_answers[size_id].items(): ret += f"|| {questions[size_id]}: || {size[1]} || {count}\n|-\n" ret += "|}\n\n" ret += ('== Rahmenprogramm ==\n' '=== Erstwahlen ===\n' '{|class="wikitable sortable"\n' '! Programmpunkt !! gewählt\n' '|-\n') for program, count in quest_answers[RAHMERST_ID].items(): ret += f"|| {program[1]} || {count}\n|-\n" ret += ('|}\n' '=== Zweitwahlen ===\n' '{|class="wikitable sortable"\n' '! Programmpunkt !! gewählt\n' '|-\n') for program, count in quest_answers[RAHMZWEIT_ID].items(): ret += f"|| {program[1]} || {count}\n|-\n" ret += "|}\n\n" ret += ('== Essen ==\n' '{|class="wikitable sortable"\n' '! Präferenz !! gewählt\n' '|-\n') for pref, count in quest_answers[ESSEN_ID].items(): ret += f"|| {pref[1]} || {count}\n|-\n" ret += ("|}\n" "Unverträglichkeiten/Allergien konnten auch angegeben werden, Antworten beim Pretix-Team erfragen." "\n\n") ret += '== Wünsche an das ewige Frühstück ==\n' for wish in quest_answers[BREAKFASTWISHES_ID]: ret += f"*
{wish[1]}\n" ret += "\n" ret += ('== Checkbox-Abfragen ==\n' '{|class="wikitable sortable"\n' '! Frage !! Antwort !! Anzahl\n' '|-\n') for field in BOOLEAN_IDS: for answer, count in quest_answers[field].items(): if answer[1] in ("False", ""): continue ret += f"|| {quest_titles[field]}: || {answer[1]} || {count}\n|-\n" ret += "|}\n\n" def prefix(id_): return "Mensch sagt:" if id_ == HUMAN_ID else "Maskottchen sagt:" if id_ == MASCOT_ID else "" ret += ('== Wie heißt das Ding? ==\n' '{|class="wikitable sortable"\n' '! Variante !! Stimmen\n' '|-\n') for variant, count in quest_answers[ATHENE_ID].items(): if "Sonstiges" not in variant[1]: ret += f"|| {prefix(variant[0])} {variant[1]} || {count}\n|-\n" else: ret += f"|| {prefix(variant[0])} {variant[1]} || " for custom in quest_answers[ATHENE_CUSTOM_ID]: ret += f"{custom[1]}