1
|
|
|
""" |
2
|
|
|
byceps.services.seating.seat_import_service |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2014-2023 Jochen Kupperschmidt |
6
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
import json |
10
|
|
|
from io import TextIOBase |
11
|
|
|
from typing import Iterator, Optional |
12
|
|
|
|
13
|
|
|
from pydantic import BaseModel, ValidationError |
14
|
|
|
|
15
|
|
|
from ..ticketing.models.ticket import TicketCategoryID |
16
|
|
|
|
17
|
|
|
from .models import Seat, SeatingAreaID |
18
|
|
|
from . import seat_service |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class SeatToImport(BaseModel): |
22
|
|
|
area_title: str |
23
|
|
|
coord_x: int |
24
|
|
|
coord_y: int |
25
|
|
|
rotation: Optional[int] = None |
26
|
|
|
category_title: str |
27
|
|
|
label: Optional[str] = None |
28
|
|
|
type_: Optional[str] = None |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def parse_lines(lines: TextIOBase) -> Iterator[str]: |
32
|
|
|
for line in lines: |
33
|
|
|
yield line.strip() |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def parse_seat_json(json_data: str) -> SeatToImport: |
37
|
|
|
data_dict = json.loads(json_data) |
38
|
|
|
|
39
|
|
|
try: |
40
|
|
|
return SeatToImport.parse_obj(data_dict) |
41
|
|
|
except ValidationError as e: |
42
|
|
|
raise Exception(str(e)) |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def import_seat( |
46
|
|
|
seat_to_import: SeatToImport, |
47
|
|
|
area_ids_by_title: dict[str, SeatingAreaID], |
48
|
|
|
category_ids_by_title: dict[str, TicketCategoryID], |
49
|
|
|
) -> Seat: |
50
|
|
|
area_id = area_ids_by_title[seat_to_import.area_title] |
51
|
|
|
category_id = category_ids_by_title[seat_to_import.category_title] |
52
|
|
|
|
53
|
|
|
return seat_service.create_seat( |
54
|
|
|
area_id, |
55
|
|
|
seat_to_import.coord_x, |
56
|
|
|
seat_to_import.coord_y, |
57
|
|
|
category_id, |
58
|
|
|
rotation=seat_to_import.rotation, |
59
|
|
|
label=seat_to_import.label, |
60
|
|
|
type_=seat_to_import.type_, |
61
|
|
|
) |
62
|
|
|
|