def routine(user: User, thread: Thread): """ Creates new Post table object and returns it as JSON representation. Image posting is totally optional (but if chosen, it needs both 'image' and 'image_name' fields. :param user: User object :param thread: Thread object :return: New Post Object as JSON object """ try: self.check_args() self.validate_args() anon = str_to_bool(self.args['anon']) image = self.media_processing() if self.args['image'] is not None and \ self.args['image_name'] is not None else None post = Post((user.id == thread.author), anon, self.args['text'], thread.id, user.id, thread.board, self.args['reply'], image) # Add new Post table to database uchan.add_to_db(post) # Add new ThreadUser link if thread.get_authid(user.id) is None: uchan.add_to_db(ThreadUser(thread.id, user.id)) # Increments thread counter thread.incr_replies((image is not None)) uchan.commit() return responses.successful(201, JSONRepresentation.post(thread.get_last_post(), thread, user)) except ValueError as msg: return responses.client_error(400, '{}'.format(msg))
def thread_author(thread: Thread, user=None): """ Thread author JSON representation, from Thread object. :param thread: Thread object :param user: Requesting User object :return: Thread author object JSON representation """ if user is None or user.id != thread.author: # If requesting user is None or different from Thread Original Poster, request OP User object user = User.query.get(thread.author) if user is None: # IMPOSSIBLE - User cannot be None at this point raise AssertionError('Author has to be present') elif thread.anon: # If Anonymous is set, returns gender and authid # TODO: implement chat requests return {'gender': user.get_gender(), 'authid': thread.get_author_authid(), 'chat': get_request(thread, user)} else: # TODO: implement chat requests return { 'nickname': user.nickname, 'university': University.query.get(user.university).name, 'gender': user.gender, 'chat': get_request(thread, user) }
def routine(user: User, thread: Thread): """ Returns Thread's Posts list in JSON object representation. :param user: Requesting User object :param thread: Thread ID :return: Thread's Posts list in JSON """ return responses.successful(200, [JSONRepresentation.post(post, thread, user) for post in thread.get_posts(page)])
def thread(thread: Thread, user: User): """ Thread Object representation from Thread Database Object. :param thread: Thread Database Object :param user: Requesting User object :return: Thread Object JSON representation """ return { 'id': thread.id, 'board': thread.board, 'anon': not user.admin and thread.anon, 'title': thread.title, 'text': thread.text, 'image': thread.get_image(), 'posted': str(thread.posted), 'replies': thread.replies, 'images': thread.images, 'delete': user.admin or (thread.author == user.id), 'author': thread_author(thread, user) }