Passed
Push — main ( d3b156...cc10ee )
by Jochen
04:18
created

byceps.cli.commands.import_seats.import_seats()   A

Complexity

Conditions 4

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 24
nop 2
dl 0
loc 32
ccs 0
cts 18
cp 0
crap 20
rs 9.304
c 0
b 0
f 0
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