edit, delete functions

master
grgr 2 years ago
parent c5594f6da0
commit c0237ef77f

Binary file not shown.

@ -5,6 +5,7 @@ from itertools import groupby # to handle complex iterations
import os
import sqlite3
from webbrowser import get
from flask import Flask, render_template, url_for, request, redirect, flash
@ -44,27 +45,30 @@ app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/soupboat/library')
@app.route("/")
def home():
conn = get_db_connection()
todos = conn.execute('SELECT c.content, cat.title FROM cards c JOIN categories cat \
todos = conn.execute('SELECT c.id, c.content, cat.title \
FROM cards c JOIN categories cat \
ON c.category_id = cat.id ORDER BY cat.title').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['title']):
categories[k] = list(g)
print(categories[k])
cards = []
for card in g:
card = dict(card)
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:
print(' ', card['content'])
# 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'))
conn.close()
return render_template('home.html', categories=categories)
@ -80,20 +84,59 @@ def create():
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 title = (?);',
(cat_title,)).fetchone()['id']
conn.execute('INSERT INTO cards (content, category_id) VALUES (?,?)',
(content, cat_title))
(content, cat_id))
conn.commit()
conn.close()
return redirect(url_for('home'))
categories = conn.execute('SELECT title FROM categories;').fetchall()
conn.close()
return render_template('create.html', categories=categories)
@app.route('/<int:id>/edit/', methods=('GET', 'POST'))
def edit(id):
conn = get_db_connection()
todo = conn.execute('SELECT c.id, c.category_id, c.content, cat.title \
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()
if request.method == 'POST':
content = request.form['content']
cat_title = 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']
conn.execute('UPDATE cards SET content = ?, category_id = ? \
WHERE id = ?',
(content, cat_id, id))
conn.commit()
conn.close()
return redirect(url_for('home'))
return render_template('edit.html', todo=todo, categories=categories)
@app.route('/<int:id>/delete/', methods=('POST',))
def delete(id):
conn = get_db_connection()
conn.execute('DELETE FROM cards WHERE id = ?', (id,))
conn.commit()
conn.close()
return redirect(url_for('home'))
# app.run(port=3148)

Binary file not shown.

@ -12,8 +12,8 @@
</div>
<div class="form-group">
<label for="category">category/type</label>
<select class="form-control" name="category">
<label for="cat">category/type</label>
<select class="form-control" name="cat">
{% for cat in categories %}
{% if cat['title'] == request.form['cat'] %}
<option value="{{ request.form['cat'] }}" selected>

@ -0,0 +1,41 @@
{% extends 'base.html' %}
{% block content %}
<h1>{% block title %} Edit an Item {% endblock %}</h1>
<form method="post">
<div class="form-group">
<label for="content">Content</label>
<input type="text" name="content"
placeholder="Todo content" class="form-control"
value="{{ todo['content'] or request.form['content'] }}"></input>
</div>
<div class="form-group">
<label for="cat">List</label>
<select class="form-control" name="cat">
{% for cat in categories %}
{% if cat['title'] == 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'] }}
</option>
{% else %}
<option value="{{ cat['title'] }}">
{{ cat['title'] }}
</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
{% endblock %}

@ -8,8 +8,28 @@
<h3>{{ category }}</h3>
</div>
<ul class="list-group list-group-flush">
{% for card in cards %}
<li class="list-group-item">{{ card['content'] }}</li>
<li class="list-group-card">{{ card['content'] }}</li>
<div class="row">
<div class="col-12 col-md-3">
<a class="btn btn-warning btn-sm"
href="{{ url_for('edit', id=card['id']) }}">Edit</a>
</div>
<div class="col-12 col-md-3">
<form action="{{ url_for('delete', id=card['id']) }}"
method="POST">
<input type="submit" value="Delete"
class="btn btn-danger btn-sm">
</form>
</div>
</div>
<hr>
{% endfor %}
</ul>
</div>

Loading…
Cancel
Save