logic move out
This commit is contained in:
86
app.py
86
app.py
@@ -1,6 +1,6 @@
|
||||
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
|
||||
from flask_logic.logic import get_enriched_post, get_comments_for_post, save_comment
|
||||
|
||||
# 🌟 IMPORT THE content from separate files.
|
||||
from content.posts import BLOG_POSTS
|
||||
@@ -16,15 +16,6 @@ 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):
|
||||
@@ -43,35 +34,66 @@ def post_comment(post_id):
|
||||
# --- Routes ---
|
||||
|
||||
|
||||
def calculate_pagination(posts, posts_per_page, page):
|
||||
"""
|
||||
Calculate pagination parameters for blog posts.
|
||||
|
||||
Args:
|
||||
posts (list): Full list of blog posts
|
||||
posts_per_page (int): Number of posts per page
|
||||
page (int): Current page number (1-indexed)
|
||||
|
||||
Returns:
|
||||
dict: Pagination data including:
|
||||
- posts_to_show: Posts for current page
|
||||
- prev_url: Previous page URL or None
|
||||
- next_url: Next page URL or None
|
||||
- current_page: Current page number
|
||||
- total_pages: Total number of pages
|
||||
- total_posts: Total number of posts
|
||||
"""
|
||||
# Sort posts by ID newest first
|
||||
sorted_posts = sorted(posts, key=lambda x: x['id'], reverse=True)
|
||||
|
||||
total_posts = len(sorted_posts)
|
||||
total_pages = math.ceil(total_posts / posts_per_page)
|
||||
|
||||
# Clamp page to valid range
|
||||
page = max(1, min(page, total_pages))
|
||||
|
||||
start = (page - 1) * posts_per_page
|
||||
end = start + posts_per_page
|
||||
|
||||
posts_to_show = sorted_posts[start:end]
|
||||
|
||||
prev_url = url_for('home', page=page - 1) if page > 1 else None
|
||||
next_url = url_for('home', page=page + 1) if end < total_posts else None
|
||||
|
||||
return {
|
||||
'posts_to_show': posts_to_show,
|
||||
'prev_url': prev_url,
|
||||
'next_url': next_url,
|
||||
'current_page': page,
|
||||
'total_pages': total_pages,
|
||||
'total_posts': total_posts
|
||||
}
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
"""Home page with paginated blog posts."""
|
||||
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
|
||||
pagination = calculate_pagination(BLOG_POSTS, POSTS_PER_PAGE, page)
|
||||
|
||||
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)
|
||||
|
||||
posts=pagination['posts_to_show'],
|
||||
prev_url=pagination['prev_url'],
|
||||
next_url=pagination['next_url'],
|
||||
current_page=pagination['current_page'],
|
||||
total_pages=pagination['total_pages'],
|
||||
blog_title=blog_title,
|
||||
total_posts=pagination['total_posts'])
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user