def post (topic_str): """ Creates a new post. Is called by the javascript event callback for #dopost :param topic_id: topic to which the posts belongs :rtype: json """ topic_id = int(topic_str.split("-")[0]) topic = Topic.query.get(topic_id) if not topic: return "nosuchtopic", 400 check_ban(topic.board_id) if not current_user.may_post_in(topic.board): return "forbidden", 403 if topic.closed: return "forbidden", 403 if not current_user.is_anonymous(): topic_with_user_context = TopicWrapper(topic) topic_with_user_context.follow() text = request.json["text"] post = Post(current_user.id, text, topic_id, request.remote_addr) has_posted_at_least_once = Post.query.filter(Post.topic_id == topic_id, Post.user_id == current_user.id).first() if not has_posted_at_least_once: topic.poster_count = Topic.poster_count + 1 topic.post_count = Topic.post_count + 1 db.session.add(post) db.session.commit() for referenced_post_id in request.json["referencedPostIDs"]: reference = PostReference(post.id, referenced_post_id) db.session.add(reference) db.session.commit() event_dispatcher = EventDispatcher() event = Event("post_created", post) event_dispatcher.dispatch(event) event = Event("post_id_created", post.id) event_dispatcher.dispatch(event) post = PostWrapper(post) post.read() event = Event("post_read", post) event_dispatcher.dispatch(event) event = Event("post_id_read", post.id) event_dispatcher.dispatch(event) return jsonify(postId = post.id)
def start_topic (board_str): """ Creates a new topic. Is called by the javascript event callback for #docreate :param board_id: board in which the new topic should be started :rtype: json """ board_id = int(board_str.split("-")[0]) check_ban(board_id) board = Board.query.get(board_id) if not board: return "boardnotfound", 404 if not current_user.may_post_in(board): return "forbidden", 403 title = request.json["title"] text = request.json["text"] topic = Topic(board_id, title) db.session.add(topic) db.session.commit() event_dispatcher = EventDispatcher() event = Event("topic_created", topic) event_dispatcher.dispatch(event) event = Event("topic_id_created", topic.id) event_dispatcher.dispatch(event) if not current_user.is_anonymous(): wrapper = TopicWrapper(topic) wrapper.follow() post = Post(current_user.id, text, topic.id, request.remote_addr) db.session.add(post) db.session.commit() event = Event("post_created", post) event_dispatcher.dispatch(event) event = Event("post_id_created", post.id) event_dispatcher.dispatch(event) if not current_user.is_anonymous(): post = PostWrapper(post) post.read() event = Event("post_read", post) event_dispatcher.dispatch(event) cache = TopicCache() cache.refresh(board_id) return jsonify(topicId = topic.id)