-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (105 loc) · 4.47 KB
/
main.py
File metadata and controls
133 lines (105 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
import os
import sqlite3
from dotenv import load_dotenv
from flask import Flask, g, jsonify, redirect, render_template, request, url_for
from article_manager import ArticleManager
app = Flask(__name__)
# Load environment variables from .env file (if present)
load_dotenv()
# Require FLASK_SECRET_KEY for session security (no insecure fallback)
_secret = os.environ.get("FLASK_SECRET_KEY")
if not _secret:
raise RuntimeError("FLASK_SECRET_KEY is required. Set it in your environment or .env")
app.secret_key = _secret
# Configure the database path.
# In production (e.g., Render), point this at a persistent disk mount.
app.config["DATABASE"] = os.environ.get("DATABASE_PATH", "lychee.db")
def get_db():
"""Connects to the database, creates a new connection if not already connected."""
if 'db' not in g:
g.db = sqlite3.connect(app.config['DATABASE'])
return g.db
@app.teardown_appcontext
def close_connection(exception):
"""Closes the database connection at the end of the request."""
db = g.pop('db', None)
if db is not None:
db.close()
@app.route('/')
def home():
# Ensure tables exist (safe no-op if already created)
db = get_db()
ArticleManager(db).create_tables()
return render_template('index.html')
@app.route('/submit_article', methods=['GET', 'POST'])
def submit_article():
db = get_db() # Get the database connection for this request
article_manager = ArticleManager(db)
if request.method == 'POST':
title = request.form['title']
content = request.form['body'] # Using 'body' to match the textarea name in create_article.html
category = request.form['category']
article_manager.submit_article_for_vote(title, content, category)
return redirect(url_for('home'))
categories = article_manager.get_all_categories()
return render_template('submit_article.html', categories=categories)
@app.route('/search', methods=['GET', 'POST'])
def search():
db = get_db() # Get the database connection for this request
article_manager = ArticleManager(db)
if request.method == 'POST':
query = request.form['query']
results = article_manager.search_articles(query)
return render_template('search_results.html', results=results)
return render_template('search.html')
@app.route('/articles') # JSON response
def get_all_articles_json():
db = get_db()
article_manager = ArticleManager(db)
articles = article_manager.get_articles_json()
return jsonify(articles)
@app.route('/categories')
def view_all_categories():
db = get_db() # Use the active database connection
article_manager = ArticleManager(db) # Pass the connection to ArticleManager
categories = article_manager.get_categories_json() # Fetch categories
return render_template('view_all_categories.html', categories=categories) # Render the template
@app.route('/vote/<int:article_id>', methods=['POST'])
def vote_on_article(article_id: int):
db = get_db()
vote_str = request.form.get('vote')
# Map approve/reject to 1/0
vote_val = 1 if vote_str == 'approve' else 0
article_manager = ArticleManager(db)
article_manager.vote_on_article(article_id, vote_val)
return redirect(url_for('home'))
@app.route('/pending_articles')
def view_pending_articles():
db = get_db()
article_manager = ArticleManager(db)
pending_articles = article_manager.view_pending_articles()
return render_template('view_pending_articles.html', pending_articles=pending_articles)
@app.route('/view_article/<int:article_id>')
def view_article(article_id):
db = get_db()
article_manager = ArticleManager(db)
article = article_manager.get_article_by_id(article_id)
return render_template('view_article.html', article=article)
@app.route('/view_all_articles')
def view_all_articles_page():
db = get_db()
article_manager = ArticleManager(db)
articles = article_manager.get_all_articles()
print("Fetched articles:", articles) # Debugging output
return render_template('view_all_articles.html', articles=articles)
@app.route('/test')
def test():
return render_template('test.html')
if __name__ == '__main__':
with app.app_context():
print(app.url_map) # This will show all routes and their names
# Allow overriding port via PORT env var (default: 8000)
port = int(os.environ.get('PORT', 8000))
print(f"Starting server on port {port}...")
app.run(host='0.0.0.0', port=port, debug=True, threaded=True)