2020-12-21 19:26:12 +01:00
|
|
|
"""This is the main flask library page"""
|
2020-12-11 16:54:09 +01:00
|
|
|
|
|
|
|
|
2021-04-17 23:34:28 +02:00
|
|
|
import os
|
2020-12-11 16:54:09 +01:00
|
|
|
import flask
|
2021-02-14 22:11:58 +01:00
|
|
|
from requests import get
|
|
|
|
from icalendar import Calendar
|
|
|
|
import datetime
|
2021-04-17 21:15:45 +02:00
|
|
|
import bcrypt
|
2021-04-05 21:42:43 +02:00
|
|
|
from flask import (
|
|
|
|
render_template,
|
|
|
|
redirect,
|
|
|
|
request,
|
|
|
|
)
|
2021-04-17 23:34:28 +02:00
|
|
|
from flask_wtf.csrf import CSRFProtect
|
|
|
|
from werkzeug.utils import secure_filename
|
2021-04-18 11:00:11 +02:00
|
|
|
from PIL import Image
|
2021-02-14 22:11:58 +01:00
|
|
|
from rnrfeed.rnrfeeder import getevents, getlatestevent
|
2021-03-07 19:55:47 +01:00
|
|
|
from uploadform import PublicationForm
|
2021-04-17 21:15:45 +02:00
|
|
|
from borrowform import BorrowForm
|
2020-12-29 22:56:28 +01:00
|
|
|
from csvparser.csvparser import (
|
|
|
|
getlicenses,
|
|
|
|
getpublications,
|
|
|
|
gettypes,
|
|
|
|
getyears,
|
|
|
|
getfullpublication,
|
2021-04-05 21:42:43 +02:00
|
|
|
writepublication,
|
2021-04-17 21:15:45 +02:00
|
|
|
editborrowedby,
|
2020-12-29 22:56:28 +01:00
|
|
|
)
|
2021-04-17 23:34:28 +02:00
|
|
|
|
2020-12-11 16:54:09 +01:00
|
|
|
|
2021-03-07 19:55:47 +01:00
|
|
|
csrf = CSRFProtect()
|
2020-12-11 16:54:09 +01:00
|
|
|
APP = flask.Flask(__name__, static_folder="static")
|
2022-04-18 21:27:35 +02:00
|
|
|
APP.config["SECRET_KEY"] = "ty4425hk54a21eee5719b9s9df7sdfklx"
|
|
|
|
APP.config["UPLOAD_FOLDER"] = "tmpupload"
|
2021-03-07 19:55:47 +01:00
|
|
|
csrf.init_app(APP)
|
2020-12-11 16:54:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/")
|
|
|
|
def index():
|
2020-12-21 19:26:12 +01:00
|
|
|
"""Main route, shows all the books and you can filter them, a bit"""
|
2020-12-21 18:41:55 +01:00
|
|
|
pubtypes = gettypes()
|
2020-12-21 18:58:46 +01:00
|
|
|
pubyears = getyears()
|
2020-12-29 22:56:28 +01:00
|
|
|
publicenses = getlicenses()
|
2020-12-21 18:41:55 +01:00
|
|
|
publicatons = 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"])
|
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"""
|
2021-03-07 19:55:47 +01:00
|
|
|
uploadform = PublicationForm()
|
2022-04-18 21:27:35 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
if uploadform.validate_on_submit() and checksecret(
|
|
|
|
uploadform.secret.data
|
|
|
|
):
|
2021-04-05 21:42:43 +02:00
|
|
|
id = writepublication(uploadform)
|
2021-04-17 23:34:28 +02:00
|
|
|
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)
|
|
|
|
print("test")
|
2021-03-07 19:55:47 +01:00
|
|
|
return render_template("upload.html", uploadform=uploadform)
|
|
|
|
|
|
|
|
|
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"""
|
2020-12-29 22:56:28 +01:00
|
|
|
fullpublication = getfullpublication(publicationID)
|
2021-04-17 21:15:45 +02:00
|
|
|
borrowform = BorrowForm()
|
2022-04-18 21:27:35 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
if borrowform.validate_on_submit() and checksecret(
|
|
|
|
borrowform.secret.data
|
|
|
|
):
|
2021-04-17 21:15:45 +02:00
|
|
|
editborrowedby(publicationID, borrowform.borrowed.data)
|
|
|
|
fullpublication["Borrowed"] = borrowform.borrowed.data
|
|
|
|
return render_template(
|
|
|
|
"publication.html",
|
|
|
|
fullpublication=fullpublication,
|
|
|
|
publicationID=publicationID,
|
2022-04-18 21:27:35 +02:00
|
|
|
borrowform=borrowform,
|
|
|
|
)
|
|
|
|
# return a full publication with or without form errors
|
|
|
|
return render_template(
|
|
|
|
"publication.html",
|
|
|
|
fullpublication=fullpublication,
|
|
|
|
publicationID=publicationID,
|
|
|
|
borrowform=borrowform,
|
|
|
|
)
|
2020-12-11 16:54:09 +01:00
|
|
|
|
|
|
|
|
2021-02-06 17:08:07 +01:00
|
|
|
@APP.route("/pastevents")
|
|
|
|
def pastevents():
|
2021-04-18 00:07:16 +02:00
|
|
|
"""show past R&R events and book recommendations"""
|
2021-02-14 19:34:50 +01:00
|
|
|
events = getevents()
|
2021-02-06 17:08:07 +01:00
|
|
|
return render_template("pastevents.html", events=events)
|
2020-12-11 16:54:09 +01:00
|
|
|
|
|
|
|
|
2021-02-14 22:11:58 +01:00
|
|
|
@APP.route("/upcoming")
|
|
|
|
def latestevent():
|
2021-04-18 00:07:16 +02:00
|
|
|
"""show upcoming or latest R&R events and book recommendations"""
|
2021-02-14 22:11:58 +01:00
|
|
|
event = getlatestevent()
|
|
|
|
return render_template("upcomingevent.html", event=event)
|
|
|
|
|
|
|
|
|
|
|
|
@APP.context_processor
|
|
|
|
def upcoming_or_latest():
|
2021-04-18 00:07:16 +02:00
|
|
|
"""determines wether the newest R&R event is upcoming or not"""
|
2021-02-14 22:11:58 +01:00
|
|
|
upcoming = True
|
|
|
|
ics = get("https://varia.zone/events.ics").text
|
|
|
|
gcal = Calendar.from_ical(ics)
|
|
|
|
eventtimes = [
|
2022-04-18 21:27:35 +02:00
|
|
|
c.get("dtstart").dt
|
|
|
|
for c in gcal.walk()
|
|
|
|
if c.name == "VEVENT" and "Read & Repair" in c.get("summary")
|
|
|
|
]
|
2021-02-14 22:11:58 +01:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
eventtimes.sort()
|
|
|
|
eventtimes.reverse()
|
|
|
|
if now > eventtimes[0]:
|
|
|
|
upcoming = False
|
|
|
|
|
|
|
|
return dict(upcoming=upcoming)
|
|
|
|
|
2021-04-18 00:07:16 +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(
|
2022-04-18 21:27:35 +02:00
|
|
|
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)
|
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
|
|
|
|
2021-04-17 21:15:45 +02:00
|
|
|
def checksecret(secret):
|
2021-04-18 00:07:16 +02:00
|
|
|
"""small simple check to a secret, library group members can upload"""
|
2021-04-17 21:15:45 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-12-11 16:54:09 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
APP.debug = True
|
|
|
|
APP.run(port=5000)
|