-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
149 lines (120 loc) · 4.7 KB
/
app.py
File metadata and controls
149 lines (120 loc) · 4.7 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from flask import Flask, render_template, url_for, request, redirect, jsonify
import secrets
from extensions import db
from flask_migrate import Migrate
from models import Post, Comment, Event
from datetime import datetime
from sqlalchemy import func
import os
def create_app():
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv(
'DATABASE_URL', 'sqlite:///site.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', secrets.token_hex(16))
# Security configurations
app.config['SESSION_COOKIE_SECURE'] = os.environ.get('FLASK_ENV') == 'production'
app.config['SESSION_COOKIE_HTTPONLY'] = True
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
db.init_app(app)
migrate = Migrate(app, db)
# Initialize the database if needed
with app.app_context():
try:
# Try to query the database
Event.query.first()
except Exception as e:
print(f"Database initialization needed: {e}")
db.create_all()
print("Database tables created successfully!")
# Import and register admin blueprint
from admin import admin
app.register_blueprint(admin)
@app.context_processor
def inject_year():
return {'year': datetime.utcnow().year}
@app.route('/')
def home():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/api/events')
def get_events():
try:
events = Event.query.all()
events_data = [
{
"title": event.title,
"start": event.event_date.strftime('%Y-%m-%d'),
"url": event.link
}
for event in events
]
return jsonify(events_data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/events')
def events():
featured_events = Event.query.filter_by(
is_featured=True).order_by(Event.event_date).all()
all_events = Event.query.order_by(Event.event_date).all()
return render_template('events.html', featured_events=featured_events, events=all_events)
@app.route("/blogs")
def blogs():
posts = Post.query.all()
featured_post = secrets.choice(posts) if posts else None
return render_template("blogs.html", posts=posts, featured_post=featured_post)
@app.route("/post/<int:post_id>")
def post(post_id):
post = Post.query.get_or_404(post_id)
comments = Comment.query.filter_by(post_id=post.id).all()
prev_post = Post.query.filter(
Post.id < post_id).order_by(Post.id.desc()).first()
next_post = Post.query.filter(
Post.id > post_id).order_by(Post.id.asc()).first()
related_posts = []
if post.category:
related_posts = Post.query.filter(
Post.category == post.category,
Post.id != post.id
).order_by(Post.date_posted.desc()).limit(3).all()
recommended_posts = []
if post.category:
recommended_posts = Post.query.filter(
Post.category != post.category,
Post.id != post.id
).order_by(func.random()).limit(3).all()
if not recommended_posts:
recommended_posts = Post.query.filter(
Post.id != post.id
).order_by(func.random()).limit(3).all()
return render_template("post.html", post=post, comments=comments,
prev_post=prev_post, next_post=next_post,
related_posts=related_posts,
recommended_posts=recommended_posts)
@app.route('/add_comment/<int:post_id>', methods=['POST'])
def add_comment(post_id):
username = request.form.get('username')
body = request.form.get('content')
comment = Comment(
username=username,
body=body,
post_id=post_id,
date_posted=datetime.utcnow()
)
db.session.add(comment)
db.session.commit()
return redirect(url_for('post', post_id=post_id))
return app
app = create_app()
if __name__ == "__main__":
from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
# Use environment variables for host and port configuration
host = os.environ.get('FLASK_HOST', '127.0.0.1')
port = int(os.environ.get('FLASK_PORT', 5000))
app.run(host=host, port=port)