def post_handler(self): if local.data['action'] == 'list': topic = local.session.query(Topic)\ .filter(Topic.id == local.data['topic']).first() if topic is None or topic.forum.access_level < local.access_level: return 'Not found' topic.nview += 1 local.session.commit() query = local.session.query(Post)\ .filter(Post.topic_id == topic.id)\ .order_by(Post.timestamp) posts, local.resp['num'] = self.sliced_query(query) local.resp['title'] = topic.title local.resp['forumId'] = topic.forum.id local.resp['forumTitle'] = topic.forum.title local.resp['posts'] = [] for p in posts: post = dict() post['id'] = p.id post['text'] = p.text post['timestamp'] = make_timestamp(p.timestamp) post['author'] = self.get_user_info(p.author) local.resp['posts'].append(post) elif local.data['action'] == 'new': if local.user is None: return 'Unauthorized' topic = local.session.query(Topic)\ .filter(Topic.id == local.data['topic']).first() if topic is None or topic.forum.access_level < local.access_level: return 'Not found' if local.data['text'] is None or len(local.data['text']) < 4: return "post.text_short" post = Post(text=local.data['text'], timestamp=make_datetime()) post.author = local.user post.topic = topic post.forum = topic.forum topic.timestamp = post.timestamp topic.answered = True topic.last_writer = local.user local.session.add(post) topic.forum.npost = local.session.query(Post)\ .filter(Post.forum_id == topic.forum.id).count() topic.npost = local.session.query(Post)\ .filter(Post.topic_id == topic.id).count() local.session.commit() elif local.data['action'] == 'delete': if local.user is None: return 'Unauthorized' post = local.session.query(Post)\ .filter(Post.id == local.data['id']).first() if post is None: return 'Not found' if post.author != local.user and local.user.access_level > 2: return 'Unauthorized' forum = post.topic.forum if post.topic.posts[0] == post: local.session.delete(post.topic) local.resp['success'] = 2 else: local.session.delete(post) post.topic.npost = local.session.query(Post)\ .filter(Post.topic_id == post.topic.id).count() forum.npost = local.session.query(Post)\ .filter(Post.forum_id == forum.id).count() forum.ntopic = local.session.query(Topic)\ .filter(Topic.forum_id == forum.id).count() local.session.commit() elif local.data['action'] == 'edit': if local.user is None: return 'Unauthorized' post = local.session.query(Post)\ .filter(Post.id == local.data['id']).first() if post is None: return 'Not found' if post.author != local.user and local.user.access_level > 2: return 'Unauthorized' if local.data['text'] is None or len(local.data['text']) < 4: return 'post.text_short' post.text = local.data['text'] local.session.commit() else: return 'Bad request'
def post_handler(self): if local.data["action"] == "list": topic = local.session.query(Topic).filter(Topic.id == local.data["topic"]).first() if topic is None or topic.forum.access_level < local.access_level: return "Not found" topic.nview += 1 local.session.commit() query = local.session.query(Post).filter(Post.topic_id == topic.id).order_by(Post.timestamp) posts, local.resp["num"] = self.sliced_query(query) local.resp["title"] = topic.title local.resp["forumId"] = topic.forum.id local.resp["forumTitle"] = topic.forum.title local.resp["posts"] = [] for p in posts: post = dict() post["id"] = p.id post["text"] = p.text post["timestamp"] = make_timestamp(p.timestamp) post["author"] = self.get_user_info(p.author) local.resp["posts"].append(post) elif local.data["action"] == "new": return "Not anymore" if local.user is None: return "Unauthorized" topic = local.session.query(Topic).filter(Topic.id == local.data["topic"]).first() if topic is None or topic.forum.access_level < local.access_level: return "Not found" if local.data["text"] is None or len(local.data["text"]) < 4: return "Text is too short" post = Post(text=local.data["text"], timestamp=make_datetime()) post.author = local.user post.topic = topic post.forum = topic.forum topic.timestamp = post.timestamp topic.last_writer = local.user local.session.add(post) topic.forum.npost = local.session.query(Post).filter(Post.forum_id == topic.forum.id).count() topic.npost = local.session.query(Post).filter(Post.topic_id == topic.id).count() local.session.commit() elif local.data["action"] == "delete": return "Not anymore" if local.user is None: return "Unauthorized" post = local.session.query(Post).filter(Post.id == local.data["id"]).first() if post is None: return "Not found" if post.author != local.user and local.user.access_level > 2: return "Unauthorized" forum = post.topic.forum if post.topic.posts[0] == post: local.session.delete(post.topic) local.resp["success"] = 2 else: local.session.delete(post) post.topic.npost = local.session.query(Post).filter(Post.topic_id == post.topic.id).count() forum.npost = local.session.query(Post).filter(Post.forum_id == forum.id).count() forum.ntopic = local.session.query(Topic).filter(Topic.forum_id == forum.id).count() local.session.commit() elif local.data["action"] == "edit": return "Not anymore" if local.user is None: return "Unauthorized" post = local.session.query(Post).filter(Post.id == local.data["id"]).first() if post is None: return "Not found" if post.author != local.user and local.user.access_level > 2: return "Unauthorized" if local.data["text"] is None or len(local.data["text"]) < 4: return "Text is too short" post.text = local.data["text"] local.session.commit() else: return "Bad request"