add posts route
This commit is contained in:
parent
3da3054587
commit
76da1c3e61
@ -77,12 +77,14 @@ def create_app():
|
||||
from app.routes.users import bp as users_bp
|
||||
from app.routes.mod import bp as mod_bp
|
||||
from app.routes.api import bp as api_bp
|
||||
from app.routes.posts import bp as posts_bp
|
||||
app.register_blueprint(app_bp)
|
||||
app.register_blueprint(topics_bp)
|
||||
app.register_blueprint(threads_bp)
|
||||
app.register_blueprint(users_bp)
|
||||
app.register_blueprint(mod_bp)
|
||||
app.register_blueprint(api_bp)
|
||||
app.register_blueprint(posts_bp)
|
||||
|
||||
app.config['SESSION_COOKIE_SECURE'] = True
|
||||
|
||||
|
@ -1,9 +1,14 @@
|
||||
from flask import Blueprint, redirect, url_for
|
||||
from flask import (
|
||||
Blueprint, redirect, url_for, flash, render_template, request
|
||||
)
|
||||
from .users import login_required, get_active_user
|
||||
from ..lib.babycode import babycode_to_html
|
||||
from ..constants import InfoboxKind
|
||||
from ..db import db
|
||||
from ..models import Posts, PostHistory
|
||||
from ..models import Posts, PostHistory, Threads
|
||||
|
||||
bp = Blueprint("posts", __name__, url_prefix = "/post")
|
||||
|
||||
bp = Blueprint("posts", __name__, url_prefix = "/posts")
|
||||
|
||||
def create_post(thread_id, user_id, content, markup_language="babycode"):
|
||||
with db.transaction():
|
||||
@ -24,3 +29,71 @@ def create_post(thread_id, user_id, content, markup_language="babycode"):
|
||||
post.update({"current_revision_id": revision.id})
|
||||
return post
|
||||
|
||||
|
||||
def update_post(post_id, new_content, markup_language='babycode'):
|
||||
with db.transaction():
|
||||
post = Posts.find({'id': post_id})
|
||||
new_revision = PostHistory.create({
|
||||
'post_id': post.id,
|
||||
'content': babycode_to_html(new_content),
|
||||
'is_initial_revision': False,
|
||||
'original_markup': new_content,
|
||||
'markup_language': markup_language,
|
||||
})
|
||||
|
||||
post.update({'current_revision_id': new_revision.id})
|
||||
|
||||
|
||||
@bp.post("/<post_id>/delete")
|
||||
@login_required
|
||||
def delete(post_id):
|
||||
post = Posts.find({'id': post_id})
|
||||
thread = Threads.find({'id': post.thread_id})
|
||||
user = get_active_user()
|
||||
|
||||
if user.is_mod() or post.user_id == user.id:
|
||||
post.delete()
|
||||
flash("Post deleted.", InfoboxKind.INFO)
|
||||
|
||||
return redirect(url_for('threads.thread', slug=thread.slug))
|
||||
|
||||
|
||||
@bp.get("/<post_id>/edit")
|
||||
@login_required
|
||||
def edit(post_id):
|
||||
user = get_active_user()
|
||||
q = f"{Posts.FULL_POSTS_QUERY} WHERE posts.id = ?"
|
||||
editing_post = db.fetch_one(q, post_id)
|
||||
if not editing_post:
|
||||
return redirect(url_for('topics.all_topics'))
|
||||
if editing_post['user_id'] != user.id:
|
||||
return redirect(url_for('topics.all_topics'))
|
||||
|
||||
thread = Threads.find({'id': editing_post['thread_id']})
|
||||
|
||||
thread_predicate = f'{Posts.FULL_POSTS_QUERY} WHERE posts.thread_id = ?'
|
||||
|
||||
context_prev_q = f'{thread_predicate} AND posts.created_at < ? ORDER BY posts.created_at DESC LIMIT 2'
|
||||
context_next_q = f'{thread_predicate} AND posts.created_at > ? ORDER BY posts.created_at ASC LIMIT 2'
|
||||
prev_context = db.query(context_prev_q, thread.id, editing_post['created_at'])
|
||||
next_context = db.query(context_next_q, thread.id, editing_post['created_at'])
|
||||
|
||||
return render_template('posts/edit.html',
|
||||
editing_post = editing_post,
|
||||
thread = thread,
|
||||
prev_context = prev_context,
|
||||
next_context = next_context,
|
||||
)
|
||||
|
||||
|
||||
@bp.post("/<post_id>/edit")
|
||||
@login_required
|
||||
def edit_form(post_id):
|
||||
user = get_active_user()
|
||||
post = Posts.find({'id': post_id})
|
||||
if post.user_id != user.id:
|
||||
return redirect(url_for('topics.all_topics'))
|
||||
|
||||
update_post(post.id, request.form.get('new_content', default=''))
|
||||
thread = Threads.find({'id': post.thread_id})
|
||||
return redirect(url_for('threads.thread', slug=thread.slug, after=post.id, _anchor=f'post-{post.id}'))
|
||||
|
@ -123,7 +123,7 @@
|
||||
{% set show_edit = (active_user.id | string) == (post['user_id'] | string) and (not post['thread_is_locked'] or active_user.is_mod()) and not no_reply %}
|
||||
{% endif %}
|
||||
{% if show_edit %}
|
||||
<a class="linkbutton" href="#TODO">Edit</a>
|
||||
<a class="linkbutton" href="{{ url_for('posts.edit', post_id=post.id, _anchor='babycode-content') }}">Edit</a>
|
||||
{% endif %}
|
||||
|
||||
{% set show_reply = true %}
|
||||
|
18
app/templates/posts/edit.html
Normal file
18
app/templates/posts/edit.html
Normal file
@ -0,0 +1,18 @@
|
||||
{% from 'common/macros.html' import full_post %}
|
||||
{% extends 'base.html' %}
|
||||
{% block title %}editing a post{% endblock %}
|
||||
{% block content %}
|
||||
{% for post in prev_context | reverse %}
|
||||
{{ full_post(post=post, no_reply=true, active_user=active_user) }}
|
||||
{% endfor %}
|
||||
<span class="context-explain">
|
||||
<span>↑↑↑</span><i>Context</i><span>↑↑↑</span>
|
||||
</span>
|
||||
{{ full_post(post=editing_post, editing=true, no_reply=true, active_user=active_user) }}
|
||||
<span class="context-explain">
|
||||
<span>↓↓↓</span><i>Context</i><span>↓↓↓</span>
|
||||
</span>
|
||||
{% for post in next_context %}
|
||||
{{ full_post(post=post, no_reply=true, active_user=active_user) }}
|
||||
{% endfor %}
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user