From b093c8b5d38c404319ff09fbfa2bf51b61e832f1 Mon Sep 17 00:00:00 2001 From: grgr Date: Sun, 9 Oct 2022 12:39:53 +0200 Subject: [PATCH] create new card with new topic --- __pycache__/app.cpython-310.pyc | Bin 4041 -> 4994 bytes app.py | 75 +++++++++++++++++++------------- library.db | Bin 24576 -> 24576 bytes templates/create.html | 34 +++++++++++++++ templates/home.html | 8 ++-- 5 files changed, 83 insertions(+), 34 deletions(-) diff --git a/__pycache__/app.cpython-310.pyc b/__pycache__/app.cpython-310.pyc index 880b87264bbf7d76fc2508e3e5dc037431bd6cda..6833017e7cf6f4a62d8e60ff07bb7e04b5e0487e 100644 GIT binary patch literal 4994 zcmbVQUvt~W5yu_=5Tq!IvTVgl8XKKSQnO=A>C~B~j^aPD=|pWpc_byBV4T5FJcxn} z64C*xA`JK>cluI4^czUizBJS6Q$IsL1YY~pKK0qt#{KPqlt{U8X9CV07k7J$y}jMv zE)Y#mS2SGT{PCmC9~Lz2?{qVM8MwKN7XK5S&;$#$9&;HZnupxw_}4?dXSl|2%ydnR z8KKoHxg|AbhIX&)mU|Vq(wlOpdR4dDn|7yrHMfR&OO&2!?##!UutoW)Cdz*ONOx!b zvVY--xpSi8&+S|kQ_Gq^3+j@nis@(EpYtyHbI0?dcEZGrs6W-f`x@rw#jLm>=DJ*5 ze8$BkF|TGWtC`otWi`4WURR?FYV-~912y`lxS~d17kpi7T>S_3t2J0&>dI(;ck{3X z+Df9`pc8}AyMEHJa`R!>j-TW+((emjdWqlL4cm#Im-c1oZACIKNq=wOkCWV%z6hk> zNpf=wYqmQiesnPqI@*1-cphD%9W%jB*Z|ZH&oocZG$_SeX(bFcWm_IDip_?Rm*TxJ zNc^AXrB2lEL%OT4MF;Mbj1Ii5b|;DCA(S;}@=uzqLGts){acSek9`?G_GQp%KaOd4 zQ9F4&6nsN$K5p;sF76)Yv)H63Hoc+jAnMPMf)=_8vzftU9puDLaIbc97yt1UbiU>? zp}|c`f(!jAb9G?|69;Yx3p=v&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R zM?1b65Js5_cxXHHhVtOVecD#rT1{!|fd=21w&81m(IxQKnGPH!N}%_Qz}O{3*&6fB zK<|=(2I_*cl?!Ok+Eu_W2XV}{?(2cs1-n28Sxm+NPi77)7-#F!wXNAe>BV*xqWCtm z0?J}Lh>E@3`evoLRJ?vSUp?!vGeE0e!{-ej*ouGFclisZN(=Wl#z#2+zUUkk9VN8I($q8DlDa^F(Xjd$S$I(^mGIuP}?*h%?m!@gP8fI zZ!xP&x?}}yG!_dedLUfP9#-0#l*R)RXlwM1NJ`I^kRC+n^g|?eNiV*O!}ZD}!IFW_ zQWEv_vbYHOi_=Kn0}>?~e7n5Y_s6ZoGT%n{5rxOC!mZ_%;+?zsTg!KEzmftr`AG?W zA^+LcrB911`N_+LR&Mj7dMm#z9G~Ya`8{+eWG7#o*_$(mbb7tix?W=K@|vVWUX<(y6!c&cn4+END%8z!H`2ITNTBiC*}UgzmHx9emhK#;^C1q z`k5_(U2_(QL2YWopxJ7I8gR#HFDy-Q3X+Jz)q|ELdz-Hg ztqO0u$%Rl+rLWa1&4)q*KKyN9q=Q*ShPS%GtG@h&h!Y^$`$xgySaWKTwcV^a<;vz} zq{D9!wvq@T%>6w_72^)j4_&8i` Kr(hjVk^cbVc-iFu diff --git a/app.py b/app.py index e328949..7f2438c 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,7 @@ from itertools import groupby # to handle complex iterations import os +from pydoc_data.topics import topics import sqlite3 from webbrowser import get @@ -37,48 +38,44 @@ class PrefixMiddleware(object): app = Flask(__name__) -app.config['SECRET KEY']='this should be a secret random string' +app.config['SECRET KEY'] = 'this should be a secret random string' # register the middleware to prefix all the requests with our base_url -app.wsgi_app=PrefixMiddleware(app.wsgi_app, prefix='/soupboat/library') +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.category_name \ + 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={} - topics={} #hint for later to fetch all the topics ehhe + 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['category_name']): - cards=[] + 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['id'],)).fetchall() - card=dict(card) + card = dict(card) card['topics'] = topics print('card is:', card) cards.append(card) - categories[k]=list(cards) - - - - + 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']] + topicss = [t['content'] for t in card['topics']] print(' ', card['content']) print(' ', list(topicss)) @@ -88,60 +85,78 @@ def home(): @ app.route("/add/", methods=['GET', 'POST']) def create(): - conn=get_db_connection() + conn = get_db_connection() if request.method == 'POST': - content=request.form['content'] - category_name=request.form['cat'] + content = request.form['content'] + category_name = request.form['cat'] + # can this be a category? o be general? + topic_tag = request.form['topic_tag'] - #create a new category: + # create a new category and topic: new_category = request.form['new_category'] + new_topic = request.form['new_topic'] # 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,)) + (new_category,)) conn.commit() # update category_name to refer to the newly added category category_name = new_category + if topic_tag == 'New topic' and new_topic: + conn.execute('INSERT INTO topics (content) VALUES (?)', + (new_topic,)) + conn.commit() + topic_tag = new_topic + + topic_id = conn.execute('SELECT id FROM topics WHERE content = (?);', + (topic_tag,)).fetchone()['id'] + conn.execute('INSERT INTO topic_cards (topic_id, card_id) VALUES (?,?)', + (topic_id, cat_id)) if not content: flash('plz write a content!') return redirect(url_for('home')) - cat_id=conn.execute('SELECT id FROM categories WHERE category_name = (?);', + 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 category_name FROM categories;').fetchall() + categories = conn.execute( + 'SELECT category_name FROM categories;').fetchall() + topics = conn.execute('SELECT content FROM topics;').fetchall() conn.close() - return render_template('create.html', categories=categories) + return render_template('create.html', categories=categories, topics=topics) @ app.route('//edit/', methods=('GET', 'POST')) def edit(id): - conn=get_db_connection() + conn = get_db_connection() - todo=conn.execute('SELECT c.id, c.category_id, c.content, cat.category_name \ + 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 category_name FROM categories;').fetchall() + categories = conn.execute( + 'SELECT category_name FROM categories;').fetchall() if request.method == 'POST': - content=request.form['content'] - category_name=request.form['cat'] + content = request.form['content'] + 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 category_name = (?);', + cat_id = conn.execute('SELECT id FROM categories WHERE category_name = (?);', (category_name,)).fetchone()['id'] conn.execute('UPDATE cards SET content = ?, category_id = ? \ @@ -156,7 +171,7 @@ def edit(id): @ app.route('//delete/', methods=('POST',)) def delete(id): - conn=get_db_connection() + conn = get_db_connection() conn.execute('DELETE FROM cards WHERE id = ?', (id,)) conn.commit() conn.close() diff --git a/library.db b/library.db index 9aeb6f9dd8e6286c1e7484c6fc2d8351d31d3d9c..43fb888ef799c8bbf02fbef0359d7a2afdfec7d5 100644 GIT binary patch delta 209 zcmZoTz}Rqrae_4Cl!-FVtWy~D{A)L+bn7d#@-Z?iGA1XMq^9Q=Wu_Lh@-i?mu<~ax z@IT-`%)f}gl|N&%pgm9eFjp+QM%afvhw7D;m} zBSVnn=2k}b*jpX3BFk@w9U^I4T!D47)UVceNYLP;6eqKpx ZUWr0^W=V!ZUcN#}enDpP<{gZOcXYzbnDBr@G&wgGA1XMq^9Q=Wu_K0^D;0nF!L{B r;D5k>n19h`L4hg!lNZ@1@v!g*G4S8!U(4Uf9|RP2;@=$PzfJ%EM@tyi diff --git a/templates/create.html b/templates/create.html index 7b56ea4..d2192a0 100644 --- a/templates/create.html +++ b/templates/create.html @@ -4,6 +4,8 @@

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

+ +
+ +
+ + + {% for topic in topics %} + {% if topic['content'] == request.form['topic_tag'] %} + + {% else %} + + {% endif %} + {% endfor %} + +
+
+ + +
+ + +
diff --git a/templates/home.html b/templates/home.html index 2f417c5..398263c 100644 --- a/templates/home.html +++ b/templates/home.html @@ -10,17 +10,17 @@ {% for card in cards %}
-
{{ category }}
+
{{ category }}
- + {% if card['topics'] %} {% for topic in card['topics'] %} -
+
{{ topic['content'] }}
{% endfor %} {% endif %} - +