new file: .env.example
new file: .gitignore new file: Dockerfile new file: README.md new file: app.py new file: app/__init__.py new file: app/game.py new file: app/models.py new file: app/routes.py new file: app/seed.py new file: data/movies.json new file: docker-compose.yml new file: requirements.txt new file: static/css/style.css new file: static/js/game.js new file: static/js/player.js new file: templates/index.html
This commit is contained in:
89
app/seed.py
Normal file
89
app/seed.py
Normal file
@@ -0,0 +1,89 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
from . import db
|
||||
from .models import Genre, Movie, Platform, Studio, Universe
|
||||
|
||||
# Feste Referenzlisten (die moeglichen Werte fuer Plattform/Genre/Studio/Universum)
|
||||
PLATFORMS = [
|
||||
"Netflix", "Disney+", "Prime Video", "Max", "Apple TV+", "Paramount+",
|
||||
"Blu-ray", "DVD", "4K UHD", "Cinema"
|
||||
]
|
||||
|
||||
GENRES = [
|
||||
"Action", "Abenteuer", "Animation", "Komödie", "Krimi", "Drama", "Fantasy",
|
||||
"Horror", "Mystery", "Romantik", "Sci-Fi", "Thriller", "Western", "Musical", "Krieg", "Biografie"
|
||||
]
|
||||
|
||||
STUDIOS = [
|
||||
"Warner Bros", "Disney", "Universal", "Paramount", "Sony Pictures",
|
||||
"20th Century", "MGM", "Lionsgate", "A24", "DreamWorks", "New Line",
|
||||
"Orion", "Neon", "CJ Entertainment", "Show East", "Dimension", "Netflix"
|
||||
]
|
||||
|
||||
UNIVERSES = [
|
||||
"MCU", "Star Wars", "DC", "Wizarding World", "Fast & Furious", "Herr der Ringe", "Spider-Man"
|
||||
]
|
||||
|
||||
|
||||
def _get_or_create(model, name):
|
||||
if not name:
|
||||
return None
|
||||
obj = model.query.filter_by(name=name).first()
|
||||
if obj is None:
|
||||
obj = model(name=name)
|
||||
db.session.add(obj)
|
||||
db.session.flush()
|
||||
return obj
|
||||
|
||||
|
||||
def seed_all():
|
||||
"""Idempotent: legt Referenzlisten und Filme an, ohne bestehende zu duplizieren."""
|
||||
# Referenzlisten
|
||||
for name in PLATFORMS:
|
||||
_get_or_create(Platform, name)
|
||||
for name in GENRES:
|
||||
_get_or_create(Genre, name)
|
||||
for name in STUDIOS:
|
||||
_get_or_create(Studio, name)
|
||||
for name in UNIVERSES:
|
||||
_get_or_create(Universe, name)
|
||||
db.session.commit()
|
||||
|
||||
# Filme aus JSON laden
|
||||
_seed_movies_from_json()
|
||||
|
||||
|
||||
def _seed_movies_from_json():
|
||||
data_path = os.path.join(
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "data", "movies.json"
|
||||
)
|
||||
if not os.path.exists(data_path):
|
||||
return
|
||||
|
||||
with open(data_path, "r", encoding="utf-8") as f:
|
||||
movies = json.load(f)
|
||||
|
||||
# Nur hinzufuegen, wenn noch kein Film in der DB ist (idempotent ueber den ganzen Datensatz)
|
||||
if Movie.query.count() > 0:
|
||||
return
|
||||
|
||||
for entry in movies:
|
||||
movie = Movie(
|
||||
title=entry["title"],
|
||||
year=entry["year"],
|
||||
fsk=entry.get("fsk"),
|
||||
director=entry.get("director"),
|
||||
budget=entry.get("budget"),
|
||||
studio=_get_or_create(Studio, entry.get("studio")),
|
||||
universe=_get_or_create(Universe, entry.get("universe")),
|
||||
)
|
||||
db.session.add(movie)
|
||||
db.session.flush()
|
||||
|
||||
for gname in entry.get("genres", []):
|
||||
movie.genres.append(_get_or_create(Genre, gname))
|
||||
for pname in entry.get("platforms", []):
|
||||
movie.platforms.append(_get_or_create(Platform, pname))
|
||||
|
||||
db.session.commit()
|
||||
Reference in New Issue
Block a user