Files
flask_blog/app.py
2026-03-15 13:50:42 +01:00

102 lines
3.0 KiB
Python

from flask import Flask, render_template, request, redirect, url_for
from content.posts import BLOG_POSTS
from content.logic import get_enriched_post, get_comments_for_post, save_comment
# 🌟 IMPORT THE content from separate files.
from content.posts import BLOG_POSTS
from content.about_text import ABOUT, TITLE
app = Flask(__name__)
import csv
import datetime
import os, math
COMMENT_FILE = 'content/comments.csv'
MAX_FILE_SIZE_BYTES = 5 * 1024 * 1024 # 5 Megabytes
POSTS_PER_PAGE = 5 # posts per page limit here
"""
import os
from flask import send_from_directory
@app.route('/content/images/<path:filename>')
def custom_static(filename):
# This serves files from your private submodule folder
return send_from_directory('content/images', filename)
"""
@app.route('/post/<int:post_id>/comment', methods=['POST'])
def post_comment(post_id):
# Honeypot stays in app.py (it's a web-security concern)
if request.form.get('honeypot'):
return redirect(url_for('post_detail', post_id=post_id))
author = request.form.get('author')
content = request.form.get('content')
# Just call the logic function
success = save_comment(post_id, author, content)
# Optional: You could flash a message if success is False
return redirect(url_for('post_detail', post_id=post_id))
# --- Routes ---
@app.route('/')
def home():
page = request.args.get('page', 1, type=int)
blog_title = TITLE
# 2. Sort posts by ID newest first
all_posts = sorted(BLOG_POSTS, key=lambda x: x['id'], reverse=True)
# Calculate totals
total_posts = len(all_posts)
total_pages = math.ceil(total_posts / POSTS_PER_PAGE)
# 3. Calculate start and end indices
start = (page - 1) * POSTS_PER_PAGE
end = start + POSTS_PER_PAGE
# 4. Slice the list for the current page
posts_to_show = all_posts[start:end]
# 5. Determine if there is a next or previous page
prev_url = url_for('home', page=page - 1) if page > 1 else None
next_url = url_for('home', page=page + 1) if end < len(all_posts) else None
return render_template('index.html',
posts=posts_to_show,
prev_url=prev_url,
next_url=next_url,
current_page=page,
blog_title=blog_title,
total_pages=total_pages)
@app.route('/about')
def about():
about_txt = ABOUT
"""Renders the About page."""
return render_template('about.html', about_txt = about_txt, blog_title = TITLE)
@app.route('/post/<int:post_id>')
def post_detail(post_id):
# One call to get the data + the extra logic (templates/timelines)
post = get_enriched_post(post_id, BLOG_POSTS)
if not post:
return "Post not found", 404
comments = get_comments_for_post(post_id)
return render_template('post_detail.html', post=post, comments=comments, blog_title = TITLE)
if __name__ == '__main__':
app.run(debug=True)