diff --git a/main.py b/main.py old mode 100755 new mode 100644 diff --git a/parse.py b/parse.py index db2f072..01483a1 100644 --- a/parse.py +++ b/parse.py @@ -1,50 +1,23 @@ 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] + Dict[int, Dict[Tuple[int, str], int]] ]: """Parse a JSON export from pretix. :param filename: filename of the JSON export to parse. - :returns: Five mappings: + :returns: Four 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'] @@ -52,7 +25,6 @@ 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']: @@ -65,11 +37,12 @@ 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, quest_titles + + return items, count_items, questions, quest_answers 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: + quest_answers: Dict[int, Dict[Tuple[int, str], int]]) -> str: """Generate stats for the Wiki page. Input parameters are output of the parse() function. @@ -81,74 +54,30 @@ def wiki_stats(items: Dict[int, str], count_items: Dict[int, int], questions: Di '|-\n') for item_id, item_name in items.items(): ret += f"|| {item_name} || {count_items.get(item_id)}\n|-\n" - ret += "|}\n\n" + ret += "|}\n" + # ids of the questions which represent a size of a merch product + size_ids = (312, 313, 314, 316, 319, 320, 321, 322, 323) ret += ('== Merch-Größenverteilung ==\n' '{|class="wikitable sortable"\n' '! Größe/Eigenschaft !! Wert !! Anzahl\n' '|-\n') - for size_id in SIZE_IDS: + 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" - + ret += "|}\n" + + athene_id = 305 # ID of the question how the logo image is to be named + human_id = 147 # ticket for a human + mascot_id = 148 # ticket for a mascot def prefix(id_): - return "Mensch sagt:" if id_ == HUMAN_ID else "Maskottchen sagt:" if id_ == MASCOT_ID else "" + 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]}
\n" - ret = ret[:-6] - ret += f"\n|-\n" + for variant, count in quest_answers[athene_id].items(): + ret += f"|| {prefix(variant[0])} {variant[1]} || {count}\n|-\n" ret += "|}\n" return ret