exampletest file and topics db

master
grgr 2 years ago
parent b889eb5304
commit b0be03850c

@ -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 = ?',

@ -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()
# ♥ ♥ ♥ ♥

@ -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()

Binary file not shown.

@ -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)
)

@ -1,7 +1,7 @@
{% extends 'base.html' %}
{% block content %}
<h1>{% block title %} Create a New Item {% endblock %}</h1>
<h1>{% block category_name %} Create a New Item {% endblock %}</h1>
<form method="post">
<div class="form-group">
@ -16,13 +16,13 @@
<select class="form-control" name="cat">
<option value="New category" selected>New category</option>
{% for cat in categories %}
{% if cat['title'] == request.form['cat'] %}
{% if cat['category_name'] == request.form['cat'] %}
<option value="{{ request.form['cat'] }}" selected>
{{ request.form['cat'] }}
</option>
{% else %}
<option value="{{ cat['title'] }}">
{{ cat['title'] }}
<option value="{{ cat['category_name'] }}">
{{ cat['category_name'] }}
</option>
{% endif %}
{% endfor %}

@ -16,19 +16,19 @@
<label for="cat">List</label>
<select class="form-control" name="cat">
{% for cat in categories %}
{% if cat['title'] == request.form['cat'] %}
{% if cat['category_name'] == request.form['cat'] %}
<option value="{{ request.form['cat'] }}" selected>
{{ request.form['cat'] }}
</option>
{% elif cat['title'] == todo['title'] %}
<option value="{{ todo['title'] }}" selected>
{{ todo['title'] }}
{% elif cat['category_name'] == todo['category_name'] %}
<option value="{{ todo['category_name'] }}" selected>
{{ todo['category_name'] }}
</option>
{% else %}
<option value="{{ cat['title'] }}">
{{ cat['title'] }}
<option value="{{ cat['category_name'] }}">
{{ cat['category_name'] }}
</option>
{% endif %}
{% endfor %}

@ -11,7 +11,19 @@
<div class="card">
<div class="card-header">
<h6>{{ category }}</h6>
<!-- this part has to be ckecked!!! -->
{% if card['topics'] %}
{% for topic in card['topics'] %}
<h6 class="badge badge-primary">
{{ topic['content'] }}
</h6>
{% endfor %}
{% endif %}
<!-- till here -->
</div>
<div class="list-group-card">{{ card['content'] }}</div>

Loading…
Cancel
Save