36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
from . import main
|
|
from flask import render_template, abort
|
|
from models import Article, Page
|
|
|
|
|
|
@main.route('/')
|
|
def index():
|
|
featured_article = Article.query.order_by(Article.created_at.desc()).first()
|
|
latest_articles = Article.query.order_by(Article.created_at.desc()).limit(4).all()
|
|
root_pages = Page.query.filter_by(parent_id=None).order_by(Page.order).all()
|
|
return render_template('index.html',
|
|
featured_article=featured_article,
|
|
latest_articles=latest_articles,
|
|
root_pages=root_pages)
|
|
|
|
|
|
@main.route('/article/<int:id>')
|
|
def article_detail(id):
|
|
article = Article.query.get_or_404(id)
|
|
root_pages = Page.query.filter_by(parent_id=None).order_by(Page.order).all()
|
|
return render_template('article.html', article=article, root_pages=root_pages)
|
|
|
|
|
|
@main.route('/page/<path:url_path>')
|
|
def page_detail(url_path):
|
|
# 查找未隐藏的页面
|
|
page = Page.query.filter_by(url_path=url_path, is_hidden=False).first()
|
|
if not page:
|
|
abort(404)
|
|
|
|
# 如果不是叶子节点(有子页面的页面),返回 404
|
|
if not page.is_leaf():
|
|
abort(404)
|
|
|
|
root_pages = Page.query.filter_by(parent_id=None).order_by(Page.order).all()
|
|
return render_template('page.html', page=page, root_pages=root_pages) |