From c0b1668f04df4b83fdeebc5178c7bc41fd9a6864 Mon Sep 17 00:00:00 2001 From: duongban Date: Sun, 23 May 2021 20:05:19 +0700 Subject: [PATCH] Implement method select tag and add tag to db --- flash_cards.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/flash_cards.py b/flash_cards.py index 6d34a44..dfca21b 100644 --- a/flash_cards.py +++ b/flash_cards.py @@ -49,10 +49,10 @@ def close_db(error): # Uncomment and use this to initialize database, then comment it # You can rerun it to pave the database and start over -# @app.route('/initdb') -# def initdb(): -# init_db() -# return 'Initialized the database.' +@app.route('/initdb') +def initdb(): + init_db() + return 'Initialized the database.' @app.route('/') @@ -276,6 +276,31 @@ def logout(): flash("You've logged out") return redirect(url_for('index')) +@app.route('/tags') +def tags(): + if not session.get('logged_in'): + return redirect(url_for('login')) + db = get_db() + query = ''' + SELECT id, tagName + FROM tags + ORDER BY id DESC + ''' + cur = db.execute(query) + tags = cur.fetchall() + return render_template('tags.html', tags=tags, filter_name="all") + +@app.route('/addTag', methods=['POST']) +def add_tag(): + if not session.get('logged_in'): + return redirect(url_for('login')) + db = get_db() + db.execute('INSERT INTO tags (tagName) VALUES (?)', + [request.form['tagName']]) + db.commit() + flash('New tag was successfully added.') + return redirect(url_for('tags')) + if __name__ == '__main__': app.run(host='0.0.0.0')