a fresh start :)
This commit is contained in:
@@ -1,147 +0,0 @@
|
||||
from flask import (
|
||||
Blueprint, render_template, request, redirect, url_for, flash, session,
|
||||
abort, current_app
|
||||
)
|
||||
from .users import login_required, mod_only, get_active_user, is_logged_in
|
||||
from ..models import Users, Topics, Threads, Subscriptions
|
||||
from ..constants import InfoboxKind
|
||||
from ..lib.render_atom import render_atom_template
|
||||
from slugify import slugify
|
||||
from app import cache
|
||||
import time
|
||||
import math
|
||||
|
||||
bp = Blueprint("topics", __name__, url_prefix = "/topics/")
|
||||
|
||||
|
||||
@bp.get("/")
|
||||
def all_topics():
|
||||
return render_template("topics/topics.html", topic_list = Topics.get_list(), active_threads = Topics.get_active_threads())
|
||||
|
||||
|
||||
@bp.get("/create")
|
||||
@login_required
|
||||
@mod_only(".all_topics")
|
||||
def create():
|
||||
return render_template("topics/create.html")
|
||||
|
||||
|
||||
@bp.post("/create")
|
||||
@login_required
|
||||
@mod_only(".all_topics")
|
||||
def create_post():
|
||||
topic_name = request.form['name'].strip()
|
||||
now = int(time.time())
|
||||
slug = f"{slugify(topic_name)}-{now}"
|
||||
|
||||
topic_count = Topics.count()
|
||||
topic = Topics.create({
|
||||
"name": topic_name,
|
||||
"description": request.form['description'],
|
||||
"slug": slug,
|
||||
"sort_order": topic_count + 1,
|
||||
})
|
||||
|
||||
flash("Topic created.", InfoboxKind.INFO)
|
||||
return redirect(url_for("topics.topic", slug = slug))
|
||||
|
||||
|
||||
@bp.get("/<slug>")
|
||||
def topic(slug):
|
||||
THREADS_PER_PAGE = 10
|
||||
target_topic = Topics.find({
|
||||
"slug": slug
|
||||
})
|
||||
if not target_topic:
|
||||
abort(404)
|
||||
return
|
||||
|
||||
threads_count = Threads.count({
|
||||
"topic_id": target_topic.id
|
||||
})
|
||||
|
||||
sort_by = session.get('sort_by', default="activity")
|
||||
|
||||
page_count = max(math.ceil(threads_count / THREADS_PER_PAGE), 1)
|
||||
page = max(1, min(int(request.args.get('page', default=1)), page_count))
|
||||
|
||||
threads_list = target_topic.get_threads(THREADS_PER_PAGE, page, sort_by)
|
||||
subscriptions = {}
|
||||
if is_logged_in():
|
||||
for thread in threads_list:
|
||||
subscription = Subscriptions.find({
|
||||
'user_id': get_active_user().id,
|
||||
'thread_id': thread['id'],
|
||||
})
|
||||
if subscription:
|
||||
subscriptions[thread['id']] = subscription.get_unread_count()
|
||||
|
||||
return render_template(
|
||||
"topics/topic.html",
|
||||
threads_list = threads_list,
|
||||
subscriptions = subscriptions,
|
||||
topic = target_topic,
|
||||
current_page = page,
|
||||
page_count = page_count,
|
||||
__feedlink = url_for('.topic_atom', slug=slug, _external=True),
|
||||
__feedtitle = f'latest threads in {target_topic.name}',
|
||||
)
|
||||
|
||||
|
||||
@bp.get('/<slug>/feed.atom')
|
||||
@cache.cached(timeout=10 * 60, unless=lambda: current_app.config.get('DEBUG', False))
|
||||
def topic_atom(slug):
|
||||
target_topic = Topics.find({
|
||||
"slug": slug
|
||||
})
|
||||
if not target_topic:
|
||||
abort(404) # TODO throw an atom friendly 404
|
||||
return
|
||||
|
||||
threads_list = target_topic.get_threads_with_op_rss()
|
||||
|
||||
return render_atom_template('topics/topic.atom', threads_list=threads_list, target_topic=target_topic)
|
||||
|
||||
|
||||
@bp.get("/<slug>/edit")
|
||||
@login_required
|
||||
@mod_only(".topic", slug = lambda slug: slug)
|
||||
def edit(slug):
|
||||
topic = Topics.find({"slug": slug})
|
||||
if not topic:
|
||||
abort(404)
|
||||
return
|
||||
return render_template("topics/edit.html", topic=topic)
|
||||
|
||||
|
||||
@bp.post("/<slug>/edit")
|
||||
@login_required
|
||||
@mod_only(".topic", slug = lambda slug: slug)
|
||||
def edit_post(slug):
|
||||
topic = Topics.find({"slug": slug})
|
||||
if not topic:
|
||||
abort(404)
|
||||
return
|
||||
|
||||
topic.update({
|
||||
"name": request.form.get('name', default = topic.name).strip(),
|
||||
"description": request.form.get('description', default = topic.description),
|
||||
"is_locked": int(request.form.get("is_locked", default = topic.is_locked)),
|
||||
})
|
||||
|
||||
return redirect(url_for("topics.topic", slug=slug))
|
||||
|
||||
|
||||
@bp.post("/<slug>/delete")
|
||||
@login_required
|
||||
@mod_only(".topic", slug = lambda slug: slug)
|
||||
def delete(slug):
|
||||
topic = Topics.find({"slug": slug})
|
||||
if not topic:
|
||||
abort(404)
|
||||
return
|
||||
|
||||
topic.delete()
|
||||
|
||||
flash("Topic deleted.", InfoboxKind.INFO)
|
||||
return redirect(url_for("topics.all_topics"))
|
||||
Reference in New Issue
Block a user