def post(self): resp = dict(RESPONSE) args = self.parser.parse_args() user_identity = get_jwt_identity() current_user = User.query.get(user_identity) if not current_user: resp['error'] = True resp['msg'] = f'User {user_identity} does\'t exists' resp['data'] = None return resp, 404 exists_name = db.session.query(User.id).filter_by(name_lower=args['name'].lower()).scalar() is not None if exists_name: resp['error'] = True resp['msg'] = f'Notebook with {args["name"]} name exists' resp['data'] = None return resp, 401 notebook = Notebook(name=args['name']) notebook.user = current_user notebook.user_id = current_user.id db.session.add(notebook) db.session.commit() resp['data'] = marshal(notebook, notebook_fields) return resp, 200
def fill(db): user = User(name='user', email='*****@*****.**') user.set_password('pass') notebook = Notebook(name='First notebook') notebook.user = user notebook.user_id = user.id note = Note(name='First note', description='Note\'s description') note.notebook = notebook note.notebook_id = notebook.id note.user = user note.user_id = user.id db.session.add(user) db.session.add(note) db.session.add(notebook) db.session.commit()