def post(self): request_data = request.get_json() try: name = request_data['name'] text = request_data['text'] if not isinstance(auth.current_user(), User): raise Exception owner_id = auth.current_user().id except KeyError: return Message.value_error() except Exception: return Message.creation_error() if note_schema.validate(data=request_data, session=session): return Message.creation_error() note = Note( name=name, text=text, owner_id=owner_id) if note is None: return Message.creation_error() session.add(note) session.commit() return Message.successful('created', 201)
def put(self, note_id): request_data = request.get_json() note = session.query(Note).get(note_id) if note is None: return Message.instance_not_exist() is_moderator = False for moderator in note.users: if moderator.id == auth.current_user().id: is_moderator = True if note.owner_id != auth.current_user().id and not is_moderator: return Message.auth_failed() if 'name' in request_data: note.name = request_data['name'] if 'text' in request_data: note.text = request_data['text'] if 'tag_id' in request_data: tag_id = request_data['tag_id'] tag = session.query(Tag).get(tag_id) note.tags.append(tag) session.commit() return Message.successful('updated')
def put(self, note_id, user_id): note = session.query(Note).get(note_id) if note is None: return Message.instance_not_exist() user = session.query(User).get(user_id) if user is None: return Message.instance_not_exist() is_moderator = False for moderator in note.users: if moderator.id == auth.current_user().id: is_moderator = True if note.owner_id != auth.current_user().id and not is_moderator: return Message.auth_failed() max_moderators = 5 if note.number_of_moderators >= max_moderators: return Message.message('Can not add a moderator', 400) note.number_of_moderators += 1 note.users.append(user) session.commit() return Message.successful('add moderator to note')
def delete(self, note_id): note = session.query(Note).get(note_id) if note is None: return Message.instance_not_exist() if note.owner_id != auth.current_user().id: return Message.auth_failed() session.delete(note) session.commit() return Message.successful('deleted')
def delete(self, user_id): user = session.query(User).get(user_id) if user is None: return Message.instance_not_exist() if user.email != auth.current_user().email: return Message.auth_failed() session.delete(user) session.commit() return Message.successful('deleted')
def put(self, note_id, tag_id): note = session.query(Note).get(note_id) if note is None: return Message.instance_not_exist() if note.owner_id != auth.current_user().id: return Message.auth_failed() tag = session.query(Tag).get(tag_id) if tag is None: return Message.instance_not_exist() note.tags.append(tag) session.commit() return Message.successful('add tag to note')
def put(self, user_id): request_data = request.get_json() user = session.query(User).get(user_id) if user is None: return Message.instance_not_exist() if user.email != auth.current_user().email: return Message.auth_failed() if 'username' in request_data: user.username = request_data['username'] if 'email' in request_data: user.email = request_data['email'] if 'password' in request_data: user.password = request_data["password"] session.commit() return Message.successful('updated')
def index(): return "Hello, {}!".format(auth.current_user())