create new card with new topic

master
grgr 2 years ago
parent b0be03850c
commit b093c8b5d3

Binary file not shown.

@ -3,6 +3,7 @@
from itertools import groupby # to handle complex iterations from itertools import groupby # to handle complex iterations
import os import os
from pydoc_data.topics import topics
import sqlite3 import sqlite3
from webbrowser import get from webbrowser import get
@ -37,25 +38,25 @@ class PrefixMiddleware(object):
app = Flask(__name__) 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 # 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("/") @ app.route("/")
def home(): def home():
conn=get_db_connection() conn = get_db_connection()
todos=conn.execute('SELECT c.id, c.content, cat.category_name \ todos = conn.execute('SELECT c.id, c.content, cat.category_name \
FROM cards c JOIN categories cat \ FROM cards c JOIN categories cat \
ON c.category_id = cat.id ORDER BY cat.category_name').fetchall() ON c.category_id = cat.id ORDER BY cat.category_name').fetchall()
categories={} categories = {}
topics={} #hint for later to fetch all the topics ehhe 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 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']): for k, g in groupby(todos, key=lambda t: t['category_name']):
cards=[] cards = []
for card in g: for card in g:
topics = conn.execute('SELECT t.id, t.content FROM topics t \ topics = conn.execute('SELECT t.id, t.content FROM topics t \
@ -64,21 +65,17 @@ def home():
WHERE t_c.card_id = ?', WHERE t_c.card_id = ?',
(card['id'],)).fetchall() (card['id'],)).fetchall()
card=dict(card) card = dict(card)
card['topics'] = topics card['topics'] = topics
print('card is:', card) print('card is:', card)
cards.append(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(?) for cat, cards in categories.items(): # ♥ .items is a build in attribute of the dictionary(?)
print(cat) print(cat)
for card in cards: for card in cards:
topicss=[t['content'] for t in card['topics']] topicss = [t['content'] for t in card['topics']]
print(' ', card['content']) print(' ', card['content'])
print(' ', list(topicss)) print(' ', list(topicss))
@ -88,14 +85,17 @@ def home():
@ app.route("/add/", methods=['GET', 'POST']) @ app.route("/add/", methods=['GET', 'POST'])
def create(): def create():
conn=get_db_connection() conn = get_db_connection()
if request.method == 'POST': if request.method == 'POST':
content=request.form['content'] content = request.form['content']
category_name=request.form['cat'] 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_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 a new category_category_name is created add it to the table of categories
if category_name == 'New category' and new_category: if category_name == 'New category' and new_category:
conn.execute('INSERT INTO categories (category_name) VALUES (?)', conn.execute('INSERT INTO categories (category_name) VALUES (?)',
@ -104,44 +104,59 @@ def create():
# update category_name to refer to the newly added category # update category_name to refer to the newly added category
category_name = new_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: if not content:
flash('plz write a content!') flash('plz write a content!')
return redirect(url_for('home')) 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'] (category_name,)).fetchone()['id']
conn.execute('INSERT INTO cards (content, category_id) VALUES (?,?)', conn.execute('INSERT INTO cards (content, category_id) VALUES (?,?)',
(content, cat_id)) (content, cat_id))
conn.commit() conn.commit()
conn.close() conn.close()
return redirect(url_for('home')) 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() conn.close()
return render_template('create.html', categories=categories) return render_template('create.html', categories=categories, topics=topics)
@ app.route('/<int:id>/edit/', methods=('GET', 'POST')) @ app.route('/<int:id>/edit/', methods=('GET', 'POST'))
def edit(id): 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 \ FROM cards c JOIN categories cat \
ON c.category_id = cat.id WHERE c.id = ?', (id,)).fetchone() 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': if request.method == 'POST':
content=request.form['content'] content = request.form['content']
category_name=request.form['cat'] category_name = request.form['cat']
if not content: if not content:
flash('plz insert any content!') flash('plz insert any content!')
return redirect(url_for('home')) 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'] (category_name,)).fetchone()['id']
conn.execute('UPDATE cards SET content = ?, category_id = ? \ conn.execute('UPDATE cards SET content = ?, category_id = ? \
@ -156,7 +171,7 @@ def edit(id):
@ app.route('/<int:id>/delete/', methods=('POST',)) @ app.route('/<int:id>/delete/', methods=('POST',))
def delete(id): def delete(id):
conn=get_db_connection() conn = get_db_connection()
conn.execute('DELETE FROM cards WHERE id = ?', (id,)) conn.execute('DELETE FROM cards WHERE id = ?', (id,))
conn.commit() conn.commit()
conn.close() conn.close()

Binary file not shown.

@ -4,6 +4,8 @@
<h1>{% block category_name %} Create a New Item {% endblock %}</h1> <h1>{% block category_name %} Create a New Item {% endblock %}</h1>
<form method="post"> <form method="post">
<!-- {# content #} -->
<div class="form-group"> <div class="form-group">
<label for="content">Content</label> <label for="content">Content</label>
<input type="text" name="content" <input type="text" name="content"
@ -11,6 +13,8 @@
value="{{ request.form['content'] }}"></input> value="{{ request.form['content'] }}"></input>
</div> </div>
<!-- {# category #} -->
<div class="form-group"> <div class="form-group">
<label for="cat">category/type</label> <label for="cat">category/type</label>
<select class="form-control" name="cat"> <select class="form-control" name="cat">
@ -34,6 +38,36 @@
placeholder="New category name" class="form-control" placeholder="New category name" class="form-control"
value="{{ request.form['new_category'] }}"></input> value="{{ request.form['new_category'] }}"></input>
</div> </div>
<!-- {# topic #} -->
<div class="form-group">
<label for="topic_tag">topic_tag</label>
<select class="form-control" name="topic_tag">
<option value="{{ request.form['topic_tag'] }}" selected> --- </option>
<option value="New topic">New topic</option>
{% for topic in topics %}
{% if topic['content'] == request.form['topic_tag'] %}
<option value="{{ request.form['topic_tag'] }}" selected>
{{ request.form['topic_tag'] }}
</option>
{% else %}
<option value="{{ topic['content'] }}">
{{ topic['content'] }}
</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="new_topic">New topic</label>
<input type="text" name="new_topic"
placeholder="New topic name" class="form-control"
value="{{ request.form['new_topic'] }}"></input>
</div>
<div class="form-group"> <div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button> <button type="submit" class="btn btn-primary">Submit</button>
</div> </div>

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

Loading…
Cancel
Save