# THE LIBRARY # from itertools import groupby # to handle complex iterations import os import sqlite3 from flask import Flask, render_template, url_for, request, redirect # ----- functions ----- # # ----- FLASK ----- # class PrefixMiddleware(object): def __init__(self, app, prefix=""): self.app = app self.prefix = prefix def __call__(self, environ, start_response): if environ["PATH_INFO"].startswith(self.prefix): environ["PATH_INFO"] = environ["PATH_INFO"][len(self.prefix):] environ["SCRIPT_NAME"] = self.prefix return self.app(environ, start_response) else: start_response("404", [("Content-Type", "text/plain")]) return ["This url does not belong to the app.".encode()] app = Flask(__name__) # register the middleware to prefix all the requests with our base_url app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/soupboat/library') @app.route("/", methods=['GET', 'POST']) def home(): if request.method == 'POST': title = request.form.get('title') author = request.form.get('author') description = request.form.get('description') add_book(author, title, description) return redirect(url_for('home')) return render_template('home.html', reading_list=getAllRows('library.db')) @app.route("/add", methods=['GET', 'POST']) def add_new_page(): # the goal here is to choose from a list of parameters what you want to add # 1- ogni blocco del form dovrebbe essere una tabella a parte if request.method == 'POST': title = request.form.get('title') author = request.form.get('author') description = request.form.get('description') add_book(author, title, description) # if author: # return url_for('add_new_author') return redirect(url_for('home')) return render_template('add_new.html') app.run(port=3148) # TODO: # - list the cards # - put its category inside