Compare commits

...

6 commits

2 changed files with 88 additions and 17 deletions

0
main.py Normal file → Executable file
View file

105
parse.py
View file

@ -1,23 +1,50 @@
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, 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']
@ -25,6 +52,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']:
@ -37,12 +65,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.
@ -54,30 +81,74 @@ 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"
ret += "|}\n\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"
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
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"* <pre>{wish[1]}</pre>\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 ""
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():
ret += f"|| {prefix(variant[0])} {variant[1]} || {count}\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]}<br/>\n"
ret = ret[:-6]
ret += f"\n|-\n"
ret += "|}\n"
return ret