def routine(user: User): # Retrieve post post = self.get_post(id) if post is None: # Post does not exist return responses.client_error(404, 'Post does not exist') if not user.admin and post.author != user.id: # Post cannot be deleted from requesting user return responses.client_error(401, 'User cannot delete this post') image = post.image is not None # If post has image thread = ThreadAPI.get_thread(post.thread) # Retrieve thread parent if thread is None: # Thread is None, IMPOSSIBLE raise AssertionError('Thread cannot be None') # Delete post from database and decrement replies uchan.delete_from_db(post, False) thread.decr_replies(image) uchan.commit() return '', 204
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 get(self, token: str): """ GET method implementation for API Activation resource entity. :param token: Activation token from URL :return: JSON Response (200 OK, 404 Not Found, 409 Conflict) """ user = self.retrieve_user_by_token(token) if user is None: return responses.client_error(404, 'Invalid token') if user.activated: return responses.client_error(409, 'User already activated') # Activate user and add it to boards user.activated = True self.add_to_general_boards(user) self.add_to_university_board(user) uchan.commit() return responses.successful(200, 'User {} activated'.format(user.nickname))
#!flask/bin/python from sys import argv from server import uchan from server.models import User, Board, UserBoard help = """ Usage: update_boards.py [memo board] [name board] """ if __name__ == '__main__' and len(argv) == 3: memo = argv[1] name = argv[2] uchan.add_to_db(Board(memo, name, 1)) board = Board.query.order_by(Board.id.desc()).first() for user in User.query.all(): uchan.add_to_db(UserBoard(user.id, board.id), False) uchan.commit() else: print(help)