flask-server/kinolist/database.py

124 lines
3.9 KiB
Python
Raw Normal View History

2024-10-10 17:44:47 -04:00
#
# Copyright (c) 2024 bartholin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
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, watched) VALUES (?,?,?,?,?, 0) RETURNING id",
2024-10-08 13:20:00 -04:00
(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()
2024-10-13 10:28:34 -04:00
db.execute("UPDATE movies SET number = ?, watched = 0 WHERE id = ?", (nb, id))
2024-10-08 13:20:00 -04:00
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)