# THE LIBRARY # import os import sqlite3 from flask import Flask, render_template, url_for, request, redirect # ----- functions ----- # # create table def createNewTable(tab_name): ''' input types str, and list of strings''' try: connection = sqlite3.connect('library.db') cursor = connection.cursor() print(f'connected to library.db') table = f'''CREATE TABLE {tab_name} (author, title, description) ''' cursor.execute(table) print(f'table {tab_name} created successfully') cursor.close() except sqlite3.Error as error: print("Failed to create a new table", error) finally: if connection: connection.close() print("The Sqlite connection is closed") def getAllTables(db_name): '''input type str, returns a list''' tables = [] try: connection = sqlite3.connect(db_name) cursor = connection.cursor() print("connected to" + db_name) sql_query = """SELECT name FROM sqlite_master WHERE type='table';""" cursor.execute(sql_query) print("list of tables:") tables_available=cursor.fetchall() for table in tables_available: print(table) except sqlite3.Error as error: print("Failed to execute the above query", error) finally: if connection: connection.close() print("the sqlite connection is closed") return tables #probably need to make this more general and put add book inside the routes' functions # add entry for the table "reading list" def add_book(author_n, book_title, text): db = sqlite3.connect('library.db') cursor = db.cursor() cursor.execute("INSERT INTO reading_list VALUES (?,?,?)", (author_n, book_title, text)) db.commit() print(f"added {book_title} to the reading_list db") db.close() def getAllRows(db): '''input type string, get all the rows of a db''' rows_list = [] try: connection = sqlite3.connect(db) cursor = connection.cursor() print("Connected to reading_list") sqlite_select_query = """SELECT * from reading_list""" cursor.execute(sqlite_select_query) records = cursor.fetchall() print("Total rows are: ", len(records)) print("Printing each row") print("\n") # f.e. the following is very specific of the library.db for row in records: row = {"author": row[0], "title": row[1], "description": row[2]} rows_list.append(row) print(row) cursor.close() except sqlite3.Error as error: print("Failed to read data from table", error) finally: if connection: connection.close() print("The Sqlite connection is closed") return rows_list # ----- 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("/") def home(): getAllTables('./library.db') return render_template('home.html', reading_list=getAllRows('library.db')) # def reading_list(): # rows = getAllRows('library.db') #it's type list, you can try to print it # reading_list = [] # for entry in rows: # entry = {"author": entry["author"], # "title": entry["title"], # "description": entry["description"]} # reading_list.append(entry) # return reading_list @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') add_new_author() return redirect(url_for('home')) return render_template('add_new.html') # so instead of having a function for everything there will be a unique function to create the category with relative table (it will probably be an augmented "create_table()" function # def add_new_book(): # 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') # add_new_author() # return redirect(url_for('home')) # return render_template('add_book.html') def add_new_author(): print('testetest this function is working') # if request.method == 'POST': # table # the list of books doesn't have to coincide with the list of authors, but when you add a new book that has a new author the function will automacally add a new author so. shall it be in the home page?? (no mettiamo tutto nell'add new page con una funzione che aggiunge ogni elemento del form nella tabella corrispondente, se non esiste, aggiunge una nuova categoria) app.run(port=3148) #------------------- # TODO: # # - ⭐ create flask app # - ⭐ create symple db with readings # - ⭐ add the form input to db # - ⭐ fetch all rows # - ⭐ getallrows returns a list to be iterated with jinja # - ⭐ and visualize them in the homepage (