From b0be03850c13569e616f3b164d6f258acefcc31d Mon Sep 17 00:00:00 2001 From: grgr Date: Fri, 7 Oct 2022 17:07:26 +0200 Subject: [PATCH] exampletest file and topics db --- app.py | 49 ++++++++++++++++++++++++++---------------- example_test.py | 47 ++++++++++++++++++++++++++++++++++++++++ init_db.py | 16 +++++++++++--- library.db | Bin 20480 -> 24576 bytes schema.sql | 7 +++--- templates/create.html | 8 +++---- templates/edit.html | 12 +++++------ templates/home.html | 12 +++++++++++ 8 files changed, 117 insertions(+), 34 deletions(-) create mode 100644 example_test.py diff --git a/app.py b/app.py index f766e8e..e328949 100644 --- a/app.py +++ b/app.py @@ -46,28 +46,41 @@ app.wsgi_app=PrefixMiddleware(app.wsgi_app, prefix='/soupboat/library') @ app.route("/") def home(): conn=get_db_connection() - todos=conn.execute('SELECT c.id, c.content, cat.title \ + todos=conn.execute('SELECT c.id, c.content, cat.category_name \ FROM cards c JOIN categories cat \ - ON c.category_id = cat.id ORDER BY cat.title').fetchall() + ON c.category_id = cat.id ORDER BY cat.category_name').fetchall() categories={} + topics={} #hint for later to fetch all the topics ehhe # 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']): + for k, g in groupby(todos, key=lambda t: t['category_name']): cards=[] for card in g: + topics = conn.execute('SELECT t.id, t.content FROM topics t \ + JOIN topic_cards t_c \ + ON t.id = t_c.topic_id \ + WHERE t_c.card_id = ?', + (card['id'],)).fetchall() + card=dict(card) + card['topics'] = topics print('card is:', card) cards.append(card) - # print(categories[k]) categories[k]=list(cards) + + + + for cat, cards in categories.items(): # ♥ .items is a build in attribute of the dictionary(?) print(cat) for card in cards: + topicss=[t['content'] for t in card['topics']] print(' ', card['content']) + print(' ', list(topicss)) conn.close() return render_template('home.html', categories=categories) @@ -79,32 +92,32 @@ def create(): if request.method == 'POST': content=request.form['content'] - cat_title=request.form['cat'] + category_name=request.form['cat'] #create a new category: new_category = request.form['new_category'] - # if a new category_title is created add it to the table of categories - if cat_title == 'New category' and new_category: - conn.execute('INSERT INTO categories (title) VALUES (?)', + # if a new category_category_name is created add it to the table of categories + if category_name == 'New category' and new_category: + conn.execute('INSERT INTO categories (category_name) VALUES (?)', (new_category,)) conn.commit() - # update cat_title to refer to the newly added category - cat_title = new_category + # update category_name to refer to the newly added category + category_name = new_category if not content: flash('plz write a content!') return redirect(url_for('home')) - cat_id=conn.execute('SELECT id FROM categories WHERE title = (?);', - (cat_title,)).fetchone()['id'] + cat_id=conn.execute('SELECT id FROM categories WHERE category_name = (?);', + (category_name,)).fetchone()['id'] conn.execute('INSERT INTO cards (content, category_id) VALUES (?,?)', (content, cat_id)) conn.commit() conn.close() return redirect(url_for('home')) - categories=conn.execute('SELECT title FROM categories;').fetchall() + categories=conn.execute('SELECT category_name FROM categories;').fetchall() conn.close() return render_template('create.html', categories=categories) @@ -114,22 +127,22 @@ def create(): def edit(id): conn=get_db_connection() - todo=conn.execute('SELECT c.id, c.category_id, c.content, cat.title \ + todo=conn.execute('SELECT c.id, c.category_id, c.content, cat.category_name \ FROM cards c JOIN categories cat \ ON c.category_id = cat.id WHERE c.id = ?', (id,)).fetchone() - categories=conn.execute('SELECT title FROM categories;').fetchall() + categories=conn.execute('SELECT category_name FROM categories;').fetchall() if request.method == 'POST': content=request.form['content'] - cat_title=request.form['cat'] + category_name=request.form['cat'] if not content: flash('plz insert any content!') return redirect(url_for('home')) - cat_id=conn.execute('SELECT id FROM categories WHERE title = (?);', - (cat_title,)).fetchone()['id'] + cat_id=conn.execute('SELECT id FROM categories WHERE category_name = (?);', + (category_name,)).fetchone()['id'] conn.execute('UPDATE cards SET content = ?, category_id = ? \ WHERE id = ?', diff --git a/example_test.py b/example_test.py new file mode 100644 index 0000000..f04662b --- /dev/null +++ b/example_test.py @@ -0,0 +1,47 @@ +from itertools import groupby +from app import get_db_connection + +conn=get_db_connection() + +todos=conn.execute('SELECT c.id, c.content, cat.category_name \ + FROM cards c JOIN categories cat \ + ON c.category_id = cat.id ORDER BY cat.category_name').fetchall() + +categories={} + + # for each category and group of cards for each cat in groupby() grouper object +for k, g in groupby(todos, key=lambda t: t['category_name']): + cards=[] + + # for each card get the topic it is assigned to + for card in g: + topics = conn.execute('SELECT t.id, t.content FROM topics t \ + JOIN topic_cards t_c \ + ON t.id = t_c.topic_id \ + WHERE t_c.card_id = ?', + (card['id'],)).fetchall() + + #convert the row into a disctionary to add topics + card=dict(card) + card['topics'] = topics + print('card is:', card) + + cards.append(card) + # print(categories[k]) + + categories[k]=list(cards) + + +print('THE GROUP category.items() is :', categories.items()) +for cat, cards in categories.items(): # ♥ .items is a build in attribute of the dictionary(?) + + for card in cards: + topic_names = [t['content'] for t in card['topics']] + print('card:', card['content']) + print(' category:', cat) + print(' topic:', list(topic_names)) + +conn.close() + + +# ♥ ♥ ♥ ♥ \ No newline at end of file diff --git a/init_db.py b/init_db.py index 09ecf1b..d941f4a 100644 --- a/init_db.py +++ b/init_db.py @@ -8,20 +8,30 @@ with open('schema.sql') as f: cur = connection.cursor() -cur.execute("INSERT INTO categories (title) VALUES (?)", ('Reading',)) -cur.execute("INSERT INTO categories (title) VALUES (?)", ('Note',)) -cur.execute("INSERT INTO categories (title) VALUES (?)", ('Question',)) +cur.execute("INSERT INTO categories (category_name) VALUES (?)", ('Reading',)) +cur.execute("INSERT INTO categories (category_name) VALUES (?)", ('Note',)) +cur.execute("INSERT INTO categories (category_name) VALUES (?)", ('Question',)) cur.execute("INSERT INTO cards (category_id, content) VALUES (?,?)", (1, 'Oltre Eboli')) cur.execute("INSERT INTO cards (category_id, content) VALUES (?,?)", (1, 'This is not an Atlas')) +cur.execute("INSERT INTO cards (category_id, content) VALUES (?,?)", + (2, 'This was the note')) +cur.execute("INSERT INTO cards (category_id, content) VALUES (?,?)", + (3, 'is this a question??')) cur.execute("INSERT INTO topics (content) VALUES (?)", ('mapping process',)) cur.execute("INSERT INTO topics (content) VALUES (?)", ('Radical Neutrality',)) cur.execute("INSERT INTO topics (content) VALUES (?)", ('Honeycomb documentation',)) cur.execute("INSERT INTO topics (content) VALUES (?)", ('Metabolic Publishing',)) +# Assign "mapping process" to "oltre eboli" +cur.execute("INSERT INTO topic_cards (topic_id, card_id) VALUES (?, ?)", + (1, 1)) +cur.execute("INSERT INTO topic_cards (topic_id, card_id) VALUES (?, ?)", + (2, 1)) + # close conenction connection.commit() connection.close() diff --git a/library.db b/library.db index c1da01edaf5ad4224e4929b6229ffb0580881e8c..9aeb6f9dd8e6286c1e7484c6fc2d8351d31d3d9c 100644 GIT binary patch delta 785 zcmZ{h&u} z0yht)xc&jAcMpb(M-wkxJvLsv^w?N~Cr{2oYg&Vo%;e?G=lkCG&FsGCc0Y4_iFgko z8AF!E#S5MGCmKDJxo+wE1o{=vrzZ%dIsq{tNfOsJM{|rO7#%)Wh#rSJ zoDh)a=D6-%w*r?q(KVp+MAsa;Wj9_`EzP7@B@b0xuCV=}RKl4IZcvk&o3z19vb)GcwKj-8YfPZ)4&G z9)<{h!>}L61L*C|&iHr{aY0_>L=+50m_~#Ff@3&@0~j#s6U0FjlHA!3aSs-*g5bdd z>?8ONUtpi1@4%WBL?qATv-x}`m(65v;@skLUS7`My0=Z6j$vB~)=s`S0VvxJz06F= zi3yhq)oO;dHMXZ9_y!-K4N5;ATY;U7=tG>mBuvm5P4+(Q(C~|s{)NId!NV8$3ApO9 iEzJ{A@6lWae}mN-i)pejUlVIdW=T%!aXlP|3yt43w9J%Nr{gT3Q*KSeaM^mZlb$Waj5d zGXWKdVo~6iUy>@vh+;ZIcTj3#N@iXZ=K|fWwDB zjDi0)|62Y^{xG1F8-Kkb3o8Sou`CPRI)r=Ea*DIk(o@Tgm_bVHnUItiSXvoaSQ!`> zlqTh57H0tMRY)vRD9K1wNXjotNi8bYVPa+AG&Y56LAWy{BePfmi1PAF6cY0$U-cK` MgiCJz;?E-h0ITzT0RR91 diff --git a/schema.sql b/schema.sql index abcf3b9..a11506e 100644 --- a/schema.sql +++ b/schema.sql @@ -1,12 +1,13 @@ DROP TABLE IF EXISTS categories; -- is a list of categories(lists): type: reading_list, authors, notes, etc it's a type but type is also a keyword in python so better avoiding it DROP TABLE IF EXISTS cards; --the single entry of any category/type DROP TABLE IF EXISTS topics; -- test table weaved on measure for this test +DROP TABLE IF EXISTS topic_cards; -- table creation CREATE TABLE categories ( id INTEGER PRIMARY KEY AUTOINCREMENT, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - title TEXT NOT NULL -- remember to not write the comma here at the end of each table + category_name TEXT NOT NULL -- remember to not write the comma here at the end of each table ); CREATE TABLE cards ( @@ -28,9 +29,9 @@ CREATE TABLE topics ( -- join table cards-topic but will i need also topic-card?? having a table of relations would maybe be a better approach? and a table of actions? (like upload file) CREATE TABLE topic_cards ( - id INTEGER NOT NULL, + id INTEGER PRIMARY KEY AUTOINCREMENT, topic_id INTEGER, card_id INTEGER, - FOREIGN KEY (topic_id) REFERENCES topics(id) + FOREIGN KEY (topic_id) REFERENCES topics(id), FOREIGN KEY (card_id) REFERENCES cards(id) ) \ No newline at end of file diff --git a/templates/create.html b/templates/create.html index a7e0520..7b56ea4 100644 --- a/templates/create.html +++ b/templates/create.html @@ -1,7 +1,7 @@ {% extends 'base.html' %} {% block content %} -

{% block title %} Create a New Item {% endblock %}

+

{% block category_name %} Create a New Item {% endblock %}

@@ -16,13 +16,13 @@ {% for cat in categories %} - {% if cat['title'] == request.form['cat'] %} + {% if cat['category_name'] == request.form['cat'] %} - {% elif cat['title'] == todo['title'] %} - {% else %} - {% endif %} {% endfor %} diff --git a/templates/home.html b/templates/home.html index c3bcfb1..2f417c5 100644 --- a/templates/home.html +++ b/templates/home.html @@ -11,7 +11,19 @@
{{ category }}
+ + + {% if card['topics'] %} + {% for topic in card['topics'] %} +
+ {{ topic['content'] }} +
+ {% endfor %} + {% endif %} +
+ +
{{ card['content'] }}