flask-server/kinolist/database.py

107 lines
3.2 KiB
Python
Raw Normal View History

2024-10-08 13:20:00 -04:00
import sqlite3
import click
from flask import current_app, g
from os import getenv
def init_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)
@click.command("init-db")
def init_db_command():
db = get_db()
with current_app.open_resource("schema.sql") as f:
db.executescript(f.read().decode("utf-8"))
click.echo("You successfully initialized the database.")
def get_db():
try:
return g.db
except:
g.db = sqlite3.connect(
current_app.config["DATABASE"],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
db = g.pop("db", None)
if db is not None:
db.close()
def new_nb():
res = get_db().execute(
"SELECT number FROM movies ORDER BY number DESC"
).fetchone()
return 1 if res == None else 1 if res["number"] == None else res["number"] + 1
def move_movie(dest, src):
db = get_db()
if dest == "end":
num = new_nb()
else:
num = int(db.execute("SELECT number from movies WHERE id = ?", (dest, )).fetchone()["number"])
movies = db.execute("SELECT id FROM movies WHERE number >= ? ORDER BY number", (num, )).fetchall()
nb = num + 1
for movie in movies:
db.execute("UPDATE movies SET number = ? WHERE id = ?", (nb, movie["id"]))
nb += 1
db.execute("UPDATE movies SET number = ? WHERE id = ?", (num, src))
db.commit()
def get_movies():
return get_db().execute("SELECT id, title, year, poster, plot, watched, watchDate, score FROM movies ORDER BY number").fetchall()
def get_movies_to_watch():
return get_db().execute("SELECT id, title, year, poster, plot, watched, watchDate, score FROM movies WHERE watched = 0 ORDER BY number").fetchall()
def get_watched_movies():
return get_db().execute("SELECT id, title, year, poster, plot, watched, watchDate, score FROM movies WHERE watched = 1 ORDER BY watchDate DESC").fetchall()
def new_movie(movie):
db = get_db()
movie["id"] = db.execute("INSERT INTO movies (title, year, poster, plot, number) VALUES (?,?,?,?,?) RETURNING id",
(movie["title"], movie["year"], movie["poster"], movie["plot"], new_nb())).fetchone()["id"]
db.commit()
return movie
def new_blank_movie():
db = get_db()
id = db.execute("INSERT INTO movies DEFAULT VALUES RETURNING id").fetchone()["id"]
nb = new_nb()
db.execute("UPDATE movies SET number = ? WHERE id = ?", (nb, id))
db.commit()
return {"id": id, "nb": nb}
def delete_movie(id):
db = get_db()
db.execute("DELETE FROM movies WHERE id = ?", (id, ))
db.commit()
def update(id, field, value):
db = get_db()
db.execute("UPDATE movies SET " + field + " = ? WHERE id = ?", (value, id))
db.commit()
def update_title(id,title):
update(id, "title", title)
def update_year(id,year):
update(id, "year", year)
def update_poster(id,poster):
update(id, "poster", poster)
def update_plot(id, plot):
update(id, "plot", plot)
def update_watched(id, watched):
update(id, "watched", watched)
def update_watchDate(id, watchDate):
update(id, "watchDate", watchDate)
def update_score(id, score):
update(id, "score", score)