def routine(user: User, board: Board): """ Create new Thread linked to specified board, from POST JSON data. :param user: Requesting User object :param board: Board object :return: New thread inside specified board """ try: # Check thread JSON arguments self.check_args() self.validate_args() # Process anon, image and construct new entity anon = str_to_bool(self.args['anon']) image = self.media_processing() thread = Thread(anon, self.args['title'], self.args['text'], image, board.id, user.id) # Add new Thread table to database uchan.add_to_db(thread) # Add new ThreadUser link thread = user.get_last_thread() uchan.add_to_db(ThreadUser(thread.id, user.id)) return responses.successful(201, JSONRepresentation.thread(thread, user)) except ValueError as msg: return responses.client_error(400, '{}'.format(msg)) except KeyError as key: return responses.client_error(400, 'Invalid parameter: {}'.format(key))
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 routine(user: User, board: Board): """ Returns Board's threads list, in JSON representation. :param user: Requesting User object :param board: Board object :return: Board's threads list """ return responses.successful(200, [JSONRepresentation.thread(thread, user) for thread in board.get_threads(page)])
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 get(self, id=None): """ GET method implementation for University API resource. :param id: University ID :return: Universities list (if id is None), else an University object (if id is not None); JSON representation. University with ID == 1 is not accessible (401 Unauthorized, if requested); University with ID not in the database will result in 404 Not Found. """ if id is None: return responses.successful(200, [JSONRepresentation.university(uni) for uni in self.university_list() if uni.id != 1]) elif id == 1: return responses.client_error(401, 'Cannot request this university') else: university = self.university_id(id) if university is None: return responses.client_error(404, 'University not found') else: return responses.successful(200, JSONRepresentation.university(university))
def post(self): """ POST method implementation for API Session resource entity. :return: JSON response (201 Created, 400 Bad Request, 409 Conflict [Database IntegrityError]) """ try: self.check_args() self.validate_args() session, user = self.register_session(request) uchan.add_to_db(session) return responses.successful(201, {'token': session.token, 'user': JSONRepresentation.me(user)}) except ValueError as msg: # Arguments validation error return responses.client_error(400, '{}'.format(msg)) except IntegrityError as msg: # Database integrity error return responses.client_error(409, 'Session error, check your JSON or contact server manteiner' .format(msg))
def requests_routine(user: User): return responses.successful(200, [JSONRepresentation.chatrequest(req) for req in user.get_requests() if req.accepted is False])
def routine(user: User): return responses.successful(200, [JSONRepresentation.thread(thread, user) for thread in user.get_threads(page)])
def routine(user: User): return responses.successful(200, JSONRepresentation.me(user))