|
| 1 | +from flask import Flask, render_template, request, redirect, url_for |
| 2 | +from journal_app import add_entry, search_entries |
| 3 | +from datetime import datetime |
| 4 | +import sqlite3 |
| 5 | + |
| 6 | +app = Flask(__name__) |
| 7 | + |
| 8 | +# Route to show the home page |
| 9 | +@app.route('/') |
| 10 | +def index(): |
| 11 | + return render_template('index.html') |
| 12 | + |
| 13 | +# Route to add a new journal entry |
| 14 | +@app.route('/add', methods=['GET', 'POST']) |
| 15 | +def add(): |
| 16 | + if request.method == 'POST': |
| 17 | + mood = request.form['mood'] |
| 18 | + content = request.form['content'] |
| 19 | + tags = request.form['tags'] |
| 20 | + add_entry("user_1", mood, content, tags) |
| 21 | + return redirect(url_for('index')) |
| 22 | + return render_template('add.html') |
| 23 | + |
| 24 | +# Route to search journal entries |
| 25 | + |
| 26 | + |
| 27 | +@app.route('/search', methods=['GET', 'POST']) |
| 28 | +def search(): |
| 29 | + if request.method == 'POST': |
| 30 | + search_input = request.form['search_term'] |
| 31 | + |
| 32 | + # Try to parse the input as a date |
| 33 | + try: |
| 34 | + search_date = datetime.strptime(search_input, '%Y-%m-%d').date() |
| 35 | + results = search_entries("user_1", date=search_date) |
| 36 | + except ValueError: |
| 37 | + # If parsing fails, treat it as a search term |
| 38 | + results = search_entries("user_1", search_term=search_input) |
| 39 | + |
| 40 | + return render_template('search_results.html', results=results) |
| 41 | + |
| 42 | + return render_template('search.html') |
| 43 | + |
| 44 | + |
| 45 | +# Route to list all unique tags |
| 46 | +@app.route('/tags') |
| 47 | +def list_tags(): |
| 48 | + conn = sqlite3.connect('journal.db') |
| 49 | + cursor = conn.cursor() |
| 50 | + |
| 51 | + # Fetch unique tags from all entries |
| 52 | + cursor.execute("SELECT DISTINCT tags FROM journal_entries") |
| 53 | + tags_data = cursor.fetchall() |
| 54 | + conn.close() |
| 55 | + |
| 56 | + # Flatten the tags and remove duplicates |
| 57 | + tags = set() |
| 58 | + for row in tags_data: |
| 59 | + if row[0]: |
| 60 | + tags.update(tag.strip() for tag in row[0].split(',')) |
| 61 | + |
| 62 | + return render_template('tags.html', tags=sorted(tags)) |
| 63 | + |
| 64 | +# Route to show journal entries by tag |
| 65 | +@app.route('/tags/<tag>') |
| 66 | +def entries_by_tag(tag): |
| 67 | + conn = sqlite3.connect('journal.db') |
| 68 | + cursor = conn.cursor() |
| 69 | + |
| 70 | + # Search for entries that contain the selected tag |
| 71 | + cursor.execute("SELECT * FROM journal_entries WHERE tags LIKE ?", ('%' + tag + '%',)) |
| 72 | + results = cursor.fetchall() |
| 73 | + conn.close() |
| 74 | + |
| 75 | + return render_template('search_results.html', results=results) |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + app.run(debug=True, port=8080) |
0 commit comments