2020-12-21 19:26:12 +01:00
|
|
|
"""This is the main flask library page"""
|
2020-12-11 16:54:09 +01:00
|
|
|
|
|
|
|
|
2021-02-14 22:11:58 +01:00
|
|
|
import datetime
|
2023-07-14 21:16:30 +02:00
|
|
|
import json
|
2023-07-14 12:54:43 +02:00
|
|
|
import os
|
2024-03-31 00:30:40 +01:00
|
|
|
from datetime import timedelta
|
2023-07-14 12:54:43 +02:00
|
|
|
|
2021-04-17 21:15:45 +02:00
|
|
|
import bcrypt
|
2024-03-31 00:30:40 +01:00
|
|
|
from flask import (
|
|
|
|
Blueprint,
|
|
|
|
redirect,
|
|
|
|
render_template,
|
|
|
|
request,
|
|
|
|
session,
|
|
|
|
url_for,
|
2023-12-03 15:32:54 +01:00
|
|
|
)
|
2024-03-31 00:30:40 +01:00
|
|
|
from flask_login import current_user, login_required, logout_user
|
|
|
|
from flask_wtf.csrf import CSRFError, CSRFProtect
|
2023-12-03 14:19:53 +01:00
|
|
|
from icalendar import Calendar
|
|
|
|
from PIL import Image
|
|
|
|
from requests import get
|
|
|
|
from werkzeug.utils import secure_filename
|
2020-12-11 16:54:09 +01:00
|
|
|
|
2024-03-31 00:30:40 +01:00
|
|
|
from app import create_app, login_manager
|
2024-03-31 11:33:03 +02:00
|
|
|
from application.search import search
|
2024-03-31 00:30:40 +01:00
|
|
|
from application.csvparser import CsvParser
|
|
|
|
from application.forms.borrowform import BorrowForm
|
2024-03-31 12:25:53 +02:00
|
|
|
from application.forms.image_uploadform import ImageUploadForm
|
|
|
|
from application.forms.pdf_uploadform import PdfUploadForm
|
2024-03-31 11:33:03 +02:00
|
|
|
from application.forms.publicationform import PublicationForm
|
2024-03-31 00:30:40 +01:00
|
|
|
from application.models.usermodel import User
|
|
|
|
from application.user.forgotpassword import ForgotPassword
|
|
|
|
from application.user.loginuser import LoginUser
|
|
|
|
from application.user.registeruser import RegisterUser
|
|
|
|
from application.user.resetpassword import ResetPassword
|
2024-03-31 11:33:03 +02:00
|
|
|
|
2024-03-30 12:00:47 +01:00
|
|
|
|
2023-07-14 12:54:43 +02:00
|
|
|
APP = create_app()
|
2021-03-07 19:55:47 +01:00
|
|
|
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"]
|
|
|
|
)
|
2020-12-11 16:54:09 +01:00
|
|
|
|
2023-12-01 15:58:33 +01:00
|
|
|
|
2023-12-03 15:32:54 +01:00
|
|
|
@APP.before_request
|
|
|
|
def session_handler():
|
|
|
|
session.permanent = True
|
|
|
|
APP.permanent_session_lifetime = timedelta(minutes=30)
|
|
|
|
|
|
|
|
|
2020-12-11 16:54:09 +01:00
|
|
|
@APP.route("/")
|
|
|
|
def index():
|
2023-07-14 12:54:43 +02:00
|
|
|
"""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",
|
|
|
|
publications=publicatons,
|
|
|
|
pubtypes=pubtypes,
|
2020-12-29 22:56:28 +01:00
|
|
|
pubyears=pubyears,
|
|
|
|
publicenses=publicenses,
|
2020-12-21 18:58:46 +01:00
|
|
|
)
|
|
|
|
return template
|
2020-12-11 16:54:09 +01:00
|
|
|
|
|
|
|
|
2021-04-05 21:42:43 +02:00
|
|
|
@APP.route("/upload", methods=["GET", "POST"])
|
2023-12-03 15:32:54 +01:00
|
|
|
@login_required
|
2021-03-07 19:55:47 +01:00
|
|
|
def upload():
|
2021-04-18 00:07:16 +02:00
|
|
|
"""Upload route, a page to upload a book to the csv"""
|
2024-03-31 11:33:03 +02:00
|
|
|
publicationform = PublicationForm()
|
2022-04-18 21:27:35 +02:00
|
|
|
if request.method == "POST":
|
2024-03-31 11:33:03 +02:00
|
|
|
if publicationform.validate_on_submit():
|
|
|
|
id = csvparser.writepublication(publicationform)
|
|
|
|
saveimage(publicationform.image.data, id)
|
2021-04-05 21:42:43 +02:00
|
|
|
return redirect(str(id), code=303)
|
|
|
|
else:
|
2024-03-31 11:33:03 +02:00
|
|
|
return render_template("upload.html", publicationform=publicationform)
|
|
|
|
return render_template("upload.html", publicationform=publicationform)
|
2021-03-07 19:55:47 +01:00
|
|
|
|
|
|
|
|
2021-04-17 21:15:45 +02:00
|
|
|
@APP.route("/<publicationID>", methods=["GET", "POST"])
|
2020-12-21 18:41:55 +01:00
|
|
|
def show_book(publicationID):
|
2021-04-18 00:07:16 +02:00
|
|
|
"""route for a single publication, shows full info and allows borrowing"""
|
2023-11-29 23:07:19 +01:00
|
|
|
fullpublication = csvparser.getfullpublication(publicationID)
|
2021-04-17 21:15:45 +02:00
|
|
|
borrowform = BorrowForm()
|
2024-03-31 12:25:53 +02:00
|
|
|
image_uploadform = ImageUploadForm()
|
|
|
|
pdf_uploadform = PdfUploadForm()
|
2022-04-18 21:27:35 +02:00
|
|
|
if request.method == "POST":
|
2024-03-30 18:22:53 +01:00
|
|
|
if borrowform.validate_on_submit():
|
2021-04-17 21:15:45 +02:00
|
|
|
editborrowedby(publicationID, borrowform.borrowed.data)
|
|
|
|
fullpublication["Borrowed"] = borrowform.borrowed.data
|
2024-03-31 12:25:53 +02:00
|
|
|
if image_uploadform.validate_on_submit():
|
|
|
|
saveimage(image_uploadform.image.data, fullpublication.id)
|
|
|
|
|
2022-04-18 21:27:35 +02:00
|
|
|
# return a full publication with or without form errors
|
|
|
|
return render_template(
|
|
|
|
"publication.html",
|
|
|
|
fullpublication=fullpublication,
|
|
|
|
publicationID=publicationID,
|
|
|
|
borrowform=borrowform,
|
2024-03-31 12:25:53 +02:00
|
|
|
image_uploadform=image_uploadform,
|
|
|
|
pdf_uploadform=pdf_uploadform,
|
2022-04-18 21:27:35 +02:00
|
|
|
)
|
2020-12-11 16:54:09 +01:00
|
|
|
|
|
|
|
|
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)
|
2023-07-14 12:54:43 +02:00
|
|
|
|
|
|
|
|
2021-04-17 23:34:28 +02:00
|
|
|
def saveimage(image, id):
|
2021-04-18 00:07:16 +02:00
|
|
|
"""helper function that can save images"""
|
2022-04-18 21:27:35 +02:00
|
|
|
image.save(os.path.join(APP.config["UPLOAD_FOLDER"], image.filename))
|
2021-04-18 11:00:11 +02:00
|
|
|
orig_image = Image.open(
|
2024-03-30 18:09:04 +01:00
|
|
|
os.path.join(APP.confmailig["UPLOAD_FOLDER"], image.filename)
|
2022-04-18 21:27:35 +02:00
|
|
|
)
|
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)
|
2021-04-17 23:34:28 +02:00
|
|
|
filename = secure_filename("image-{0}.jpg".format(id))
|
2021-04-18 11:15:32 +02:00
|
|
|
resized_image.save(os.path.join("static/images/", filename), "JPEG")
|
2022-04-18 21:27:35 +02:00
|
|
|
os.remove(os.path.join(APP.config["UPLOAD_FOLDER"], image.filename))
|
2021-04-17 23:34:28 +02:00
|
|
|
|
2021-02-14 22:11:58 +01:00
|
|
|
|
2023-12-03 15:32:54 +01:00
|
|
|
@APP.route("/logout")
|
|
|
|
@login_required
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
|
|
|
return redirect(url_for("index"))
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/login", methods=["GET", "POST"])
|
|
|
|
def login():
|
|
|
|
return LoginUser()
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/register", methods=["GET", "POST"])
|
|
|
|
def register():
|
|
|
|
return RegisterUser()
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/forgotpassword", methods=["GET", "POST"])
|
|
|
|
def forgotpassword():
|
2024-03-30 18:09:04 +01:00
|
|
|
return ForgotPassword()
|
2023-12-03 15:32:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/resetpassword/<path>", methods=["GET", "POST"])
|
|
|
|
def resetpassword(path):
|
|
|
|
return ResetPassword(path)
|
|
|
|
|
|
|
|
|
|
|
|
@APP.errorhandler(CSRFError)
|
|
|
|
def handle_csrf_error(e):
|
|
|
|
return render_template("csrf_error.html", reason=e.description), 400
|
|
|
|
|
|
|
|
|
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(user_id):
|
|
|
|
return User.query.get(int(user_id))
|
|
|
|
|
|
|
|
|
|
|
|
@APP.errorhandler(CSRFError)
|
|
|
|
def handle_csrf_error(e):
|
|
|
|
return render_template("csrf_error.html", reason=e.description), 400
|
2021-04-17 21:15:45 +02:00
|
|
|
|
|
|
|
|
2020-12-11 16:54:09 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
APP.debug = True
|
|
|
|
APP.run(port=5000)
|