From 40ebc31f5e4f6ab33c669b768fbea805a5d1afa5 Mon Sep 17 00:00:00 2001 From: grgr Date: Thu, 29 Sep 2022 19:31:49 +0200 Subject: [PATCH] home html template and home funciton in library.py --- library.py | 31 +++++++++++++++++++++++-------- templates/base.html | 13 +++++++++++++ templates/home.html | 21 +++++++++++++++++++-- 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 templates/base.html diff --git a/library.py b/library.py index 0398cdc..71fee29 100644 --- a/library.py +++ b/library.py @@ -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_page(): +def create(): # 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': diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..3d8c5b8 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,13 @@ + + + + + library + + + + + + {% block content %} {% endblock %} + + diff --git a/templates/home.html b/templates/home.html index 86a47dd..0deac9a 100644 --- a/templates/home.html +++ b/templates/home.html @@ -1,4 +1,21 @@ - +{% extends 'base.html' %} + +{% block content %} +

{% block title %} Welcome to FlaskTodo {% endblock %}

+ {% for category, cards in categories.cards() %} +
+
+

{{ category }}

+
+ +
+ {% endfor %} +{% endblock %} +