1
|
|
|
"""Import seats from JSON lines. |
2
|
|
|
|
3
|
|
|
:Copyright: 2014-2023 Jochen Kupperschmidt |
4
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
from pathlib import Path |
8
|
|
|
|
9
|
|
|
import click |
10
|
|
|
from flask.cli import with_appcontext |
11
|
|
|
|
12
|
|
|
from ...services.seating import seat_import_service, seating_area_service |
13
|
|
|
from ...services.ticketing import ticket_category_service |
14
|
|
|
from ...typing import PartyID |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
@click.command() |
18
|
|
|
@click.argument('party_id') |
19
|
|
|
@click.argument( |
20
|
|
|
'data_file', type=click.Path(exists=True, dir_okay=False, path_type=Path) |
21
|
|
|
) |
22
|
|
|
@with_appcontext |
23
|
|
|
def import_seats(party_id: PartyID, data_file: Path) -> None: |
24
|
|
|
"""Import seats.""" |
25
|
|
|
areas = seating_area_service.get_areas_for_party(party_id) |
26
|
|
|
area_ids_by_title = {area.title: area.id for area in areas} |
27
|
|
|
|
28
|
|
|
categories = ticket_category_service.get_categories_for_party(party_id) |
29
|
|
|
category_ids_by_title = { |
30
|
|
|
category.title: category.id for category in categories |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
with data_file.open() as f: |
34
|
|
|
lines = seat_import_service.parse_lines(f) |
35
|
|
|
for line_number, line in enumerate(lines, start=1): |
36
|
|
|
try: |
37
|
|
|
seat_to_import = seat_import_service.parse_seat_json(line) |
38
|
|
|
seat = seat_import_service.import_seat( |
39
|
|
|
seat_to_import, area_ids_by_title, category_ids_by_title |
40
|
|
|
) |
41
|
|
|
click.secho( |
42
|
|
|
f'[line {line_number}] Imported seat ' |
43
|
|
|
f'(area="{seat_to_import.area_title}", x={seat.coord_x}, y={seat.coord_y}, category="{seat_to_import.category_title}").', |
44
|
|
|
fg='green', |
45
|
|
|
) |
46
|
|
|
except Exception as e: |
47
|
|
|
click.secho( |
48
|
|
|
f'[line {line_number}] Could not import seat: {e}', fg='red' |
49
|
|
|
) |
50
|
|
|
|