@ -9,6 +9,12 @@ from flask import Flask, render_template, url_for, request, redirect
# ----- functions ----- #
def get_db_connection ( ) :
conn = sqlite3 . connect ( ' library.db ' )
conn . row_factory = sqlite3 . Row
return conn
# Added row_factory attribute to the sqlite connection, in this way you can have name-based access to columns; this means that the database connection will return rows that behave like regular Python dictionaries.
# ----- FLASK ----- #
@ -37,22 +43,31 @@ app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/soupboat/library')
@app.route ( " / " , methods = [ ' GET ' , ' POST ' ] )
def home ( ) :
conn = get_db_connection ( )
todos = conn . execute ( ' SELECT c.content, cat.title FROM cards c JOIN categories cat \
ON c . category_id = cat . id ORDER BY c . created ' ).fetchall()
categories = { }
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 ' ) )
# for each category and group of cards for each cat in groupby() grouper object
for k , g in groupby ( todos , key = lambda t : t [ ' title ' ] ) :
categories [ k ] = list ( g )
# 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 ' ) )
return render_template ( ' home.html ' , categories= categories )
@app.route ( " /add " , methods = [ ' GET ' , ' POST ' ] )
def add_new_pag e( ) :
def creat e( ) :
# 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 ' :