csv-library-website/library/page.py

129 lines
4.0 KiB
Python
Raw Normal View History

"""This is the main flask library page"""
2021-02-14 22:11:58 +01:00
import datetime
2023-07-14 21:16:30 +02:00
import json
import os
import bcrypt
2023-11-29 23:40:12 +01:00
from flask import redirect, render_template, request, Blueprint
from flask_wtf.csrf import CSRFProtect
from icalendar import Calendar
2021-04-18 11:00:11 +02:00
from PIL import Image
from requests import get
from werkzeug.utils import secure_filename
from app import create_app
2023-11-26 18:38:33 +01:00
from application.csvparser import CsvParser
2023-11-26 17:52:21 +01:00
from forms.borrowform import BorrowForm
from forms.uploadform import PublicationForm
2023-07-14 21:16:30 +02:00
from search import search
APP = create_app()
csrf = CSRFProtect()
csrf.init_app(APP)
2023-11-29 23:40:12 +01:00
files = Blueprint("files", __name__, static_folder="files")
APP.register_blueprint(files)
2023-11-29 23:07:19 +01:00
csvparser = CsvParser(
APP.config["LIBRARY_FILENAME"], APP.config["IMAGE_FOLDER"]
)
2023-12-01 15:58:33 +01:00
@APP.route("/")
def index():
"""Main route, shows all the books and you can filter them
2023-11-26 18:38:33 +01:00
based on year, type"""
2023-11-26 17:52:21 +01:00
pubtypes = csvparser.gettypes()
pubyears = csvparser.getyears()
publicenses = csvparser.getlicenses()
publicatons = csvparser.getpublications()
2020-12-21 18:58:46 +01:00
template = render_template(
"index.html",
title=APP.config["TITLE"],
2020-12-21 18:58:46 +01:00
publications=publicatons,
pubtypes=pubtypes,
pubyears=pubyears,
publicenses=publicenses,
2020-12-21 18:58:46 +01:00
)
return template
2021-04-05 21:42:43 +02:00
@APP.route("/upload", methods=["GET", "POST"])
def upload():
"""Upload route, a page to upload a book to the csv"""
uploadform = PublicationForm()
if request.method == "POST":
if uploadform.validate_on_submit() and checksecret(
uploadform.secret.data
):
2023-11-26 17:52:21 +01:00
id = csvparser.writepublication(uploadform)
saveimage(uploadform.image.data, id)
2021-04-05 21:42:43 +02:00
return redirect(str(id), code=303)
else:
return render_template("upload.html", uploadform=uploadform)
2023-11-26 18:38:33 +01:00
return render_template(
"upload.html", title=APP.config["TITLE"], uploadform=uploadform
)
@APP.route("/<publicationID>", methods=["GET", "POST"])
def show_book(publicationID):
"""route for a single publication, shows full info and allows borrowing"""
2023-11-29 23:07:19 +01:00
fullpublication = csvparser.getfullpublication(publicationID)
borrowform = BorrowForm()
if request.method == "POST":
if borrowform.validate_on_submit() and checksecret(
borrowform.secret.data
):
editborrowedby(publicationID, borrowform.borrowed.data)
fullpublication["Borrowed"] = borrowform.borrowed.data
return render_template(
"publication.html",
fullpublication=fullpublication,
publicationID=publicationID,
borrowform=borrowform,
)
# return a full publication with or without form errors
return render_template(
"publication.html",
fullpublication=fullpublication,
publicationID=publicationID,
borrowform=borrowform,
)
2023-07-14 21:16:30 +02:00
@APP.route("/search/<search_query>", methods=["GET"])
def searchbooks(search_query):
print(f"Searched for {search_query}")
search_results = search(search_query)
return json.dumps(search_results)
def saveimage(image, id):
"""helper function that can save images"""
image.save(os.path.join(APP.config["UPLOAD_FOLDER"], image.filename))
2021-04-18 11:00:11 +02:00
orig_image = Image.open(
os.path.join(APP.config["UPLOAD_FOLDER"], image.filename)
)
2021-04-18 11:00:11 +02:00
new_width = 640
new_height = int(new_width * orig_image.height / orig_image.width)
resized_image = orig_image.resize((new_width, new_height), Image.ANTIALIAS)
filename = secure_filename("image-{0}.jpg".format(id))
resized_image.save(os.path.join("static/images/", filename), "JPEG")
os.remove(os.path.join(APP.config["UPLOAD_FOLDER"], image.filename))
2021-02-14 22:11:58 +01:00
def checksecret(secret):
"""small simple check to a secret, library group members can upload"""
with open("secret") as f:
secrethash = f.readline().rstrip()
if bcrypt.checkpw(secret.encode("utf-8"), secrethash.encode("utf-8")):
return True
else:
return False
if __name__ == "__main__":
APP.debug = True
APP.run(port=5000)