flask-server/kinolist/pages.py
2024-10-13 10:58:48 +02:00

110 lines
3.4 KiB
Python

from flask import (
Blueprint,
current_app,
redirect,
render_template,
request,
url_for
)
from flask_login import (
login_required,
login_user,
logout_user
)
from kinolist import (imdb, posters)
from kinolist.login import User
from kinolist.database import (
delete_movie,
get_movies,
get_movies_to_watch,
get_watched_movies,
move_movie,
new_blank_movie,
new_movie,
update_plot,
update_poster,
update_score,
update_title,
update_watchDate,
update_watched,
update_year
)
bp = Blueprint("pages", __name__)
def w():
return current_app.config["IMAGE_WIDTH"]
@bp.route("/")
def to_watch():
return render_template("pages/to_watch.html", movies=get_movies_to_watch(), to_watch=1, w=w())
@bp.route("/watched")
def watched():
return render_template("pages/watched.html", movies=get_watched_movies(), watched=1, w=w())
@bp.route("/manage", methods=("GET", "POST"))
@login_required
def manage():
if request.method == "POST":
if "new" in request.form:
choice = request.form["choice"]
if choice == "title":
movies = imdb.title(request.form["title"])
if movies:
return render_template("_choicelist.html", movies=movies, w=w())
else:
return ""
elif choice == "imdbid":
movie = imdb.imdbid(request.form["imdbid"])
if movie:
return render_template("_line.html", movie=new_movie(movie), manage=1, w=w())
else:
return ""
else:
return render_template("_line.html", movie=new_blank_movie(), manage=1, w=w())
else:
id = request.form["id"]
if "move" in request.form:
move_movie(id, request.form["move"])
if "title" in request.form:
update_title(id, request.form["title"])
elif "year" in request.form:
update_year(id, request.form["year"])
elif "poster" in request.files:
poster = request.files["poster"]
filename = posters.save(poster.filename, poster.read())
update_poster(id, filename)
return filename
elif "plot" in request.form:
update_plot(id, request.form["plot"])
elif "watched" in request.form:
update_watched(id, request.form["watched"])
elif "watchDate" in request.form:
update_watchDate(id, request.form["watchDate"])
elif "score" in request.form:
update_score(id, request.form["score"])
elif "delete" in request.form:
delete_movie(id)
return ""
return render_template("pages/manage.html", movies=get_movies(), manage=1, w=w())
@bp.route("/posters/<filename>")
def send_uploaded_file(filename):
return posters.get(filename)
@bp.route("/login", methods=("GET", "POST"))
def login_page():
if request.method == "POST":
if request.form["password"] == current_app.config["ADMIN_PASSWORD"]:
login_user(User("admin"))
return redirect(url_for("pages.manage"))
return "Bad Login"
return render_template("pages/login.html")
@bp.route('/logout')
def logout():
logout_user()
return redirect(url_for("pages.to_watch"))