예제 #1
0
 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'
예제 #2
0
 def topic_handler(self):
     if local.data["action"] == "list":
         forum = (
             local.session.query(Forum)
             .filter(Forum.access_level >= local.access_level)
             .filter(Forum.id == local.data["forum"])
             .first()
         )
         if forum is None:
             return "Not found"
         noAnswer = "noAnswer" in local.data and local.data["noAnswer"]
         local.resp["title"] = forum.title
         local.resp["description"] = forum.description
         query = (
             local.session.query(Topic)
             .filter(Topic.forum_id == forum.id)
             .order_by(desc(Topic.sticky), desc(Topic.timestamp))
         )
         topics, local.resp["num"] = self.sliced_query(query)
         local.resp["numUnsolved"] = (
             local.session.query(Topic).filter(Topic.forum_id == forum.id).filter(Topic.solved == False).count()
         )
         local.resp["topics"] = []
         for t in topics:
             if noAnswer and t.solved is True:
                 continue
             topic = dict()
             topic["id"] = t.id
             topic["status"] = t.status
             topic["title"] = t.title
             topic["timestamp"] = make_timestamp(t.creation_timestamp)
             topic["posts"] = t.npost
             topic["views"] = t.nview
             topic["author_username"] = t.author.username
             topic["sticky"] = t.sticky
             topic["solved"] = t.solved
             topic["lastpost"] = {"username": t.last_writer.username, "timestamp": make_timestamp(t.timestamp)}
             local.resp["topics"].append(topic)
     elif local.data["action"] == "new":
         return "Not anymore"
         if local.user is None:
             return "Unauthorized"
         forum = (
             local.session.query(Forum)
             .filter(Forum.access_level >= local.access_level)
             .filter(Forum.id == local.data["forum"])
             .first()
         )
         if forum is None:
             return "Not found"
         if local.data["title"] is None or len(local.data["title"]) < 4:
             return "forum.title_short"
         if local.data["text"] is None or len(local.data["text"]) < 4:
             return "post.text_short"
         if local.data["sticky"] is None or (local.data["sticky"] != False and local.data["sticky"] != True):
             raise KeyError
         topic = Topic(
             status="open",
             title=local.data["title"],
             timestamp=make_datetime(),
             creation_timestamp=make_datetime(),
             solved=False,
         )
         topic.forum = forum
         topic.last_writer = local.user
         topic.author = local.user
         topic.npost = 1
         topic.sticky = local.data["sticky"]
         post = Post(text=local.data["text"], timestamp=make_datetime())
         post.author = local.user
         post.topic = topic
         post.forum = forum
         local.session.add(topic)
         local.session.add(post)
         forum.ntopic = len(forum.topics)
         forum.npost = local.session.query(Post).filter(Post.forum_id == forum.id).count()
         local.session.commit()
     else:
         raise KeyError
예제 #3
0
 def topic_handler(self):
     if local.data['action'] == 'list':
         forum = local.session.query(Forum)\
             .filter(Forum.access_level >= local.access_level)\
             .filter(Forum.id == local.data['forum']).first()
         if forum is None:
             return 'Not found'
         noAnswer = 'noAnswer' in local.data and local.data['noAnswer']
         local.resp['title'] = forum.title
         local.resp['description'] = forum.description
         query = local.session.query(Topic)\
             .filter(Topic.forum_id == forum.id)\
             .order_by(desc(Topic.timestamp))
         topics, local.resp['num'] = self.sliced_query(query)
         local.resp['numUnanswered'] = local.session.query(Topic)\
             .filter(Topic.forum_id == forum.id)\
             .filter(Topic.answered == False).count()
         local.resp['topics'] = []
         for t in topics:
             if noAnswer and t.answered is True:
                 continue
             topic = dict()
             topic['id'] = t.id
             topic['status'] = t.status
             topic['title'] = t.title
             topic['timestamp'] = make_timestamp(t.creation_timestamp)
             topic['posts'] = t.npost
             topic['views'] = t.nview
             topic['author_username'] = t.author.username
             topic['lastpost'] = {
                 'username':  t.last_writer.username,
                 'timestamp': make_timestamp(t.timestamp)
             }
             local.resp['topics'].append(topic)
     elif local.data['action'] == 'new':
         if local.user is None:
             return 'Unauthorized'
         forum = local.session.query(Forum)\
             .filter(Forum.access_level >= local.access_level)\
             .filter(Forum.id == local.data['forum']).first()
         if forum is None:
             return 'Not found'
         if local.data['title'] is None or len(local.data['title']) < 4:
             return "forum.title_short"
         if local.data['text'] is None or len(local.data['text']) < 4:
             return "post.text_short"
         topic = Topic(status='open',
                       title=local.data['title'],
                       timestamp=make_datetime(),
                       creation_timestamp=make_datetime(),
                       answered=False)
         topic.forum = forum
         topic.last_writer = local.user
         topic.author = local.user
         topic.npost = 1
         post = Post(text=local.data['text'],
                     timestamp=make_datetime())
         post.author = local.user
         post.topic = topic
         post.forum = forum
         local.session.add(topic)
         local.session.add(post)
         forum.ntopic = len(forum.topics)
         forum.npost = local.session.query(Post)\
             .filter(Post.forum_id == forum.id).count()
         local.session.commit()
     else:
         return 'Bad request'
예제 #4
0
 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"