55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
|
|
from flask import Blueprint, abort, flash, redirect, render_template
|
|
from sqlalchemy import select, func
|
|
|
|
from ..models import Page, PageTag, db
|
|
from .. import app_config
|
|
|
|
bp = Blueprint('home', __name__)
|
|
|
|
@bp.route('/')
|
|
def homepage():
|
|
page_limit = app_config.default_items_per_page
|
|
new_notes = db.paginate(select(Page).order_by(Page.touched.desc()).limit(page_limit))
|
|
return render_template('home.html',
|
|
new_notes = new_notes)
|
|
|
|
@bp.route('/p/most_recent/')
|
|
def view_most_recent():
|
|
general_query = db.paginate(select(Page).order_by(Page.touched.desc()))
|
|
return render_template('listrecent.html', notes=general_query)
|
|
|
|
@bp.route('/p/random/')
|
|
def view_random():
|
|
page = None
|
|
page_count = db.session.execute(select(func.count()).select_from(Page)).scalar()
|
|
if page_count < 4:
|
|
flash('Too few pages in this site.')
|
|
abort(404)
|
|
while not page:
|
|
page = db.session.execute(select(Page).order_by(func.random())).scalar()
|
|
return redirect(page.get_url())
|
|
|
|
@bp.route('/p/leaderboard/')
|
|
def page_leaderboard():
|
|
headers = {
|
|
'Cache-Control': 'max-age=180, stale-while-revalidate=1800'
|
|
}
|
|
|
|
pages = []
|
|
## TODO make it a query
|
|
pages_q = db.session.execute(select(Page)).scalars()
|
|
for p in pages_q:
|
|
p_latest = p.latest()
|
|
score = (p_latest.length >> 10) + len(p.forward_links) + len(p.back_links)
|
|
## TODO make it a namedtuple
|
|
pages.append((p, score, len(p.back_links.count), len(p.forward_links), p_latest.length))
|
|
pages.sort(key = lambda x: (x[1], x[2], x[4], x[3]), reverse = True)
|
|
|
|
return render_template('leaderboard.html', pages=pages), headers
|
|
|
|
@bp.route('/tags/<slug:tag>/')
|
|
def listtag(tag):
|
|
general_query = select(Page).join(PageTag, PageTag.page_id == Page.id).where(PageTag.name == tag).order_by(Page.touched.desc())
|
|
return render_template('listtag.html', tagname=tag, tagged_notes=db.paginate(general_query))
|
|
|