forked from skypro-008/coursework_3_source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_fixtures.py
30 lines (21 loc) · 866 Bytes
/
load_fixtures.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from contextlib import suppress
from typing import Any, Dict, List, Type
from sqlalchemy.exc import IntegrityError
from project.config import config
from project.models import Genre, Director, Movie
from project.server import create_app
from project.setup.db import db, models
from project.utils import read_json
def load_data(data: List[Dict[str, Any]], model: Type[models.Base]) -> None:
for item in data:
item['id'] = item.pop('pk')
db.session.add(model(**item))
if __name__ == '__main__':
fixtures: Dict[str, List[Dict[str, Any]]] = read_json("fixtures.json")
app = create_app(config)
with app.app_context():
load_data(fixtures['movies'], Movie)
load_data(fixtures['directors'], Director)
load_data(fixtures['genres'], Genre)
with suppress(IntegrityError):
db.session.commit()