This is a reusable plain version the varia library website. You can host your own website of books using just a simple csv file
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.1 KiB

"""This is the main flask library page"""
12 months ago
from datetime import timedelta
import datetime
import json
import os
import bcrypt
12 months ago
from app import create_app, login_manager
12 months ago
from application.csvparser import CsvParser
8 months ago
from application.user.loginuser import LoginUser
from application.user.registeruser import RegisterUser
from application.user.forgotpassword import ForgotPassword
from application.user.resetpassword import ResetPassword
from application.models.usermodel import User
8 months ago
from flask import Blueprint, redirect, render_template, request, session, url_for
12 months ago
from flask_wtf.csrf import CSRFProtect, CSRFError
from flask_login import (
logout_user,
login_required,
current_user,
)
from application.forms.borrowform import BorrowForm
from application.forms.uploadform import PublicationForm
12 months ago
from icalendar import Calendar
from PIL import Image
from requests import get
from search import search
12 months ago
from werkzeug.utils import secure_filename
8 months ago
APP = create_app()
csrf = CSRFProtect()
csrf.init_app(APP)
files = Blueprint("files", __name__, static_folder="files")
APP.register_blueprint(files)
csvparser = CsvParser(
APP.config["LIBRARY_FILENAME"], APP.config["IMAGE_FOLDER"]
)
12 months ago
@APP.before_request
def session_handler():
session.permanent = True
APP.permanent_session_lifetime = timedelta(minutes=30)
@APP.route("/")
def index():
"""Main route, shows all the books and you can filter them
12 months ago
based on year, type"""
pubtypes = csvparser.gettypes()
pubyears = csvparser.getyears()
publicenses = csvparser.getlicenses()
publicatons = csvparser.getpublications()
template = render_template(
"index.html",
title=APP.config["TITLE"],
publications=publicatons,
pubtypes=pubtypes,
pubyears=pubyears,
publicenses=publicenses,
)
return template
@APP.route("/upload", methods=["GET", "POST"])
12 months ago
@login_required
def upload():
"""Upload route, a page to upload a book to the csv"""
uploadform = PublicationForm()
if request.method == "POST":
12 months ago
if uploadform.validate_on_submit():
id = csvparser.writepublication(uploadform)
saveimage(uploadform.image.data, id)
return redirect(str(id), code=303)
else:
return render_template("upload.html", uploadform=uploadform)
12 months ago
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"""
fullpublication = csvparser.getfullpublication(publicationID)
borrowform = BorrowForm()
if request.method == "POST":
if borrowform.validate_on_submit():
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",
title=APP.config["TITLE"],
fullpublication=fullpublication,
publicationID=publicationID,
borrowform=borrowform,
)
@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))
orig_image = Image.open(
os.path.join(APP.confmailig["UPLOAD_FOLDER"], image.filename)
)
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))
12 months ago
@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():
return ForgotPassword()
12 months ago
@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
if __name__ == "__main__":
APP.debug = True
APP.run(port=5000)