def node_add(): if request.json: if ("node" in request.json) and ('name' not in request.json.get('node') or 'description' not in request.json.get('node')): return make_response(400, 6, "name and description json fields required") else: node_json = request.json.get('node') node_description = node_json['description'] node_name = node_json['name'] else: return make_response(400, 6, "Invalid request data") node = Node(name=node_name) node.ownerId = g.user.id node.description = node_description db_node = Node.query.filter(Node.name == node_name, Node.ownerId == g.user.id).first() if db_node: if request.method == 'PUT': node_remove(db_node.id) else: return make_response(409, 1, str("user " + g.user.username + ' already have node ' + node_name)) try: db.session.add(node) db.session.commit() except exc.SQLAlchemyError: return make_response(500, 2, "Database exception") return jsonify({'hypermedia': { 'self': { 'resource': str('/api/node/' + str(node.id)), 'method': 'GET' }, 'replace': { 'resource': str('/api/node/' + str(node.id)), 'method': 'PUT' }, 'delete': { 'resource': str('/api/node/delete/' + str(node.id)), 'method': 'DELETE' } }, 'message': 'Created.'}), 201