flask-server/kinolist/pages.py

127 lines
4.0 KiB
Python
Raw Permalink 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
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)
2024-10-13 04:58:48 -04:00
return filename
2024-10-08 13:20:00 -04:00
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"))