def edit_post(forum_slug, thread_id, thread_slug, post_id): url_forum = Forum.query.filter_by(slug=forum_slug).first() url_thread = Thread.query.get(thread_id) post = Post.query.get(post_id) thread = post.thread if not post: abort(404) if not post.can_be_edited_by(g.user): abort(403) if request.method == 'GET' and (url_forum != thread.forum or thread_slug != thread.slug): return redirect(post.edit_url, code=301) posts_before = Post.query.\ filter(Post.thread == thread, Post.id < post.id).\ count() edits_thread = (posts_before == 0) ajax = ('ajax' in request.values) cur_version = post.current_version form_cls = ThreadForm if edits_thread else PostForm form = form_cls(obj=post, content=cur_version.content, title=thread.title, subtitle=thread.subtitle) if 'getQuickEditAjaxForm' in request.args: html = render_template('inline_edit.html', post=post, is_thread=edits_thread, form=form, url=post.edit_url) return jsonify(form_html=html) if form.validate_on_submit(): if cur_version.content != form.content.data: version = PostVersion( content=form.content.data, post=post, creator=g.user, created_at=datetime.datetime.now(), created_ip=request.remote_addr) db.session.add(version) post.version_count += 1 post.current_version = version else: # Return the current version, as we're quick editing version = cur_version if edits_thread: thread.title = form.title.data thread.subtitle = form.subtitle.data thread.slug = slugify(thread.title) db.session.commit() if not ajax: flash('Your post has been updated.') return redirect(post.url, code=303) else: return jsonify( was_edited=True, post_html=escape(parse_text(version.content)) ) if not ajax: return render_template('post.html', form=form, is_thread=edits_thread, is_edit=True, thread=thread, forum=thread.forum, url=post.edit_url) else: return jsonify(was_edited=False, errors=jsonify_errors(form))
def post_reply(thread_id, thread_slug, forum_slug=None, is_private=False): if not is_private: url_forum = Forum.query.filter_by(slug=forum_slug).first() thread = Thread.query.get(thread_id) if not thread: abort(404) if not thread.can_be_replied_to_by(g.user): abort(403) if request.method == 'GET': url_valid = True if not is_private: if url_forum != thread.forum: url_valid = False if thread_slug != thread.slug: url_valid = False if not url_valid: return redirect(thread.reply_url, code=301) ajax = ('ajax' in request.values) form = PostForm() if form.validate_on_submit(): if is_private: post_number = -1 else: post_number = g.user.post_count + 1 post = Post( thread=thread, creator=g.user, created_at=datetime.datetime.now(), created_ip=request.remote_addr, version_count=1, number=post_number) db.session.add(post) version = PostVersion( content=form.content.data, post=post, creator=g.user, created_at=datetime.datetime.now(), created_ip=request.remote_addr) db.session.add(version) post.current_version = version thread.post_count += 1 if not is_private: g.user.post_count += 1 thread.forum.post_count += 1 thread.update_last_post(post) # This is ugly. Deal with notifications. u_table = pm_thread_users if is_private else thread_followers notify_join = db.and_( Notification.type == Notification.FOLLOWED_THREAD, u_table.c.user_id == Notification.recipient_id, u_table.c.thread_id == Notification.thread_id ) notify_which = db.session.query(u_table.c.user_id, Notification.id).\ filter(u_table.c.thread_id == thread.id).\ filter(u_table.c.user_id != g.user.id).\ outerjoin(Notification, notify_join) add_new = [] update_existing = [] for uid, nid in notify_which: if nid: update_existing.append(nid) else: add_new.append({ 'recipient_id': uid, 'type': Notification.FOLLOWED_THREAD, 'thread_id': thread.id }) if update_existing: Notification.query.\ filter(Notification.id.in_(update_existing)).\ update({'count': Notification.count + 1}, synchronize_session=False) if add_new: db.session.execute(Notification.__table__.insert(), add_new) db.session.commit() if not ajax: flash('Your reply has been posted.') return redirect(post.url, code=303) else: return jsonify( was_posted=True, post_id=post.id, post_html=render_template('post_box.html', post=post, postNumber=thread.post_count) ) if not ajax: return render_template('post.html', form=form, thread=thread, forum=thread.forum, url=thread.reply_url) else: return jsonify(was_posted=False, errors=jsonify_errors(form))