implement csv export facility and use it for Rahmenprogrammwuensche
This commit is contained in:
parent
5df2f840b2
commit
d54ab30523
3 changed files with 53 additions and 3 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -4,3 +4,5 @@ __pycache__/
|
|||
.idea/
|
||||
# JSON export (contains private data)
|
||||
koma89_pretixdata.json
|
||||
# generated tables (contain private data)
|
||||
*.csv
|
||||
|
|
9
main.py
9
main.py
|
@ -2,7 +2,7 @@
|
|||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from parse import parse, wiki_stats
|
||||
from parse import parse, rahmprog_stats, wiki_stats
|
||||
|
||||
|
||||
def print_wiki_stats(filename: str) -> None:
|
||||
|
@ -12,5 +12,10 @@ def print_wiki_stats(filename: str) -> None:
|
|||
print(wiki_stats(*parse(path)))
|
||||
|
||||
|
||||
FILENAME = "koma89_pretixdata.json"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print_wiki_stats("koma89_pretixdata.json")
|
||||
#print_wiki_stats(FILENAME)
|
||||
with open("rahmprogramm_wahlen.csv", "w") as f:
|
||||
f.write(rahmprog_stats(FILENAME))
|
||||
|
|
45
parse.py
45
parse.py
|
@ -1,7 +1,7 @@
|
|||
import json
|
||||
from pathlib import Path
|
||||
from pprint import pprint
|
||||
from typing import Dict, Tuple, Union # I have just python3.8 here :(
|
||||
from typing import Collection, Dict, List, Tuple, Union # I have just python3.8 here :(
|
||||
|
||||
|
||||
# ids of the questions which represent a size of a merch product
|
||||
|
@ -28,6 +28,49 @@ BOOLEAN_IDS = (
|
|||
)
|
||||
# where are U from?
|
||||
UNI_ID = 300
|
||||
# name ids
|
||||
NICKNAME_ID = 298
|
||||
|
||||
|
||||
|
||||
def query_to_csv(filename: Union[str, Path], relevant_items: Collection[int], columns: List[str]) -> str:
|
||||
"""Parse a JSON export from pretix and query some columns from some orders.
|
||||
|
||||
:param filename: filename of the JSON export to parse.
|
||||
:returns: CSV string with one line per order, containing the requested columns.
|
||||
"""
|
||||
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}
|
||||
|
||||
head = ("Typ", "Name") + tuple(questions[col] for col in columns)
|
||||
res = [head]
|
||||
|
||||
for order in data['orders']:
|
||||
if order['status'] == 'c': # skip cancelled orders
|
||||
continue
|
||||
for position in order['positions']:
|
||||
item_id = position['item']
|
||||
attendee_name = position.get('attendee_name')
|
||||
if item_id not in relevant_items:
|
||||
continue
|
||||
quest_ans = {a['question'] : a['answer'] for a in position['answers']}
|
||||
res.append((items[item_id], attendee_name) + tuple(quest_ans[col] for col in columns))
|
||||
|
||||
# hacky sanity check that we do not have to escape csv
|
||||
for line in res:
|
||||
if any('"' in ans for ans in line):
|
||||
raise RuntimeError(f"Illegal CSV char, escape needed in line {line}")
|
||||
|
||||
return "\n".join( ('"' + '","'.join(line) + '"') for line in res)
|
||||
|
||||
def rahmprog_stats(filename) -> str:
|
||||
return query_to_csv(filename, (HUMAN_ID,), (NICKNAME_ID, RAHMERST_ID, RAHMZWEIT_ID))
|
||||
|
||||
|
||||
def parse(filename: Union[str, Path]) -> Tuple[
|
||||
|
|
Loading…
Reference in a new issue