def updateObjectAttribute(): try: data = request.get_json() routeRequestLogger.info(request) if data is None: return jsonify( **{'message': 'No data found'}), ErrorCode_ServerError if not checkParam(data, 'userId') or not checkParam(data, 'attributeId') or not\ checkParams(data['updatedObjectAttribute'], *ObjectAttributes.params): return jsonify(**{'message': 'Bad params'}), ErrorCode_BadParams user = retrieveUser(data['userId']) if user is None: return jsonify( **{'message': 'No data found'}), ErrorCode_ServerError if not isValidUser(user.id): return jsonify(**{'message': 'Bad Params'}), ErrorCode_BadParams if updateObjectAttributes(user.id, data['attributeId'], **data['updatedObjectAttribute']): return jsonify( **{'attributeId': data['attributeId']}), ErrorCode_Success return jsonify(**{'message': 'Update failed'}), ErrorCode_ServerError except: routeRequestLogger.exception(request) return jsonify( **{"message": "Unexpected Error"}), ErrorCode_ServerError
def addNewRouteGroup(): try: data = request.get_json() routeRequestLogger.info(request) if not checkParams(data, *RouteGroup.params): generalLogger.error("data sent to " + request.path + " does not have correct params. Expected " + RouteGroup.params + " but received " + data) return jsonify(**{"message": "Bad params"}), ErrorCode_BadParams if data['objectName'] == '': generalLogger.error("route group name is empty. cannot be empty") return jsonify(**{"message": "Bad params"}), ErrorCode_BadParams user = retrieveUser(data['userId']) newRouteGroup = RouteGroup(data['userId'], data["objectName"], data["apiId"], data["isCRUD"], data['isReadMultiple'], data['isReadAllObjects'], data['isSingleRoute']) addRoute(user.id, newRouteGroup) return jsonify(**{"objectId": newRouteGroup.id}), ErrorCode_Success except: routeRequestLogger.exception(request) return jsonify( **{"message": "Unexpected Error"}), ErrorCode_ServerError
def deleteRouteGroup(): try: data = request.get_json() routeRequestLogger.info(request) if data is None: return jsonify( **{'message': 'No data found'}), ErrorCode_ServerError if not checkParam(data, 'userId') or not checkParam(data, 'objectId'): return jsonify(**{'message': 'Bad params'}), ErrorCode_BadParams user = retrieveUser(data['userId']) if user is None: return jsonify( **{'message': 'No data found'}), ErrorCode_ServerError if not isValidUser(user.id): return jsonify(**{'message': 'Bad Params'}), ErrorCode_BadParams deleteRoute(user.id, data['objectId']) return jsonify(**{}), ErrorCode_Success except: routeRequestLogger.exception(request) return jsonify( **{"message": "Unexpected Error"}), ErrorCode_ServerError
def addObjectAttribute(): try: data = request.get_json() routeRequestLogger.info(request) if not checkParams(data, *['userId', 'objectId', 'attributes']): return jsonify(**{'message': 'Bad params'}), ErrorCode_BadParams attributeIds = [] user = retrieveUser(data['userId']) route = getRoute(user.id, {"id": data['objectId']}) for row in data['attributes']: if not checkParams(row, *ObjectAttributes.params): return jsonify( **{'message': 'Bad params'}), ErrorCode_BadParams if row['name'] == '': return jsonify( **{'message': 'Bad params'}), ErrorCode_BadParams routeRequestLogger.info(request_method=POST, url=request.path, request_body=data, client_ip=request.remote_addr) if not checkParams(data, *['userId', 'objectId', 'attributes']): return jsonify( **{'message': 'Bad params'}), ErrorCode_BadParams newObjectAttributes = ObjectAttributes( data['userId'], data['objectId'], row['name'], row['type'], row['description'], row['isUnique'], row['isEncrypted'], row['isNullable'], row['generationMethod']) addObjectAttributes(user.id, route.id, newObjectAttributes) attributeIds += [newObjectAttributes.id] return jsonify(**{'attributeIds': attributeIds}), ErrorCode_Success except: routeRequestLogger.exception(request)
def viewAvailableRouteGroups(): try: data = request.get_json() routeRequestLogger.info(request) if data is None: return jsonify(**{}), ErrorCode_ServerError if not checkParam(data, 'userId'): user = gimmeNewUser() else: user = retrieveUser(data['userId']) if not isValidUser(user.id): return jsonify(**{'message': 'Bad Params'}), ErrorCode_BadParams # routes = [{"id":route.id, "objectName" : route.objectName, "apiId": route.apiId} for route in getUserRoutes(user.id)] routes = [] for route in getUserRoutes(user.id): attributes = retrieveObjectAttributes(data['userId'], route.id) new_list = {} for attr in attributes: new_list[attr.id] = dict(attr) routes.append({ "id": route.id, "objectName": route.objectName, "apiId": route.apiId, "attributes": new_list }) return jsonify(**{"groups": routes}), ErrorCode_Success except: routeRequestLogger.exception(request) return jsonify( **{"message": "Unexpected Error"}), ErrorCode_ServerError
def updateMultipleObjectAttributes(): try: data = request.get_json() routeRequestLogger.info(request) if data is None: return jsonify( **{'message': 'No data found'}), ErrorCode_ServerError if not checkParam(data, 'userId') or not checkParam( data, 'objectId') or not checkParam( data, 'attributes') or not checkParam(data, "deletedAttr"): return jsonify(**{'message': 'Bad params'}), ErrorCode_BadParams user = retrieveUser(data['userId']) attrToDelete = data['deletedAttr'] for i in attrToDelete: deleteObjectAttributes(user.id, int(i)) if user is None: return jsonify( **{'message': 'No data found'}), ErrorCode_ServerError if not isValidUser(user.id): return jsonify(**{'message': 'Bad Params'}), ErrorCode_BadParams allDBAttributes = retrieveObjectAttributes(user.id, data['objectId']) for dbAttributes in allDBAttributes: deleteObjectAttributes(user.id, dbAttributes.id) allAttributes = data['attributes'] for attrId in allAttributes: modifiedAttrDict = allAttributes[attrId] if (modifiedAttrDict["name"] == "" or modifiedAttrDict["type"] == ""): continue modifiedAttrDict['description'] = "" modifiedAttrDict['isUnique'] = True modifiedAttrDict['isEncrypted'] = True modifiedAttrDict['isNullable'] = True modifiedAttrDict['generationMethod'] = "" if isValidAttribute(modifiedAttrDict["name"], data['objectId'], user.id): try: if str(attrId) in attrToDelete: deleteObjectAttributes(user.id, attrId) else: updateObjectAttributes(user.id, attrId, modifiedAttrDict) except Exception as e: print(e) else: try: newObjectAttributes = ObjectAttributes( user.id, data['objectId'], modifiedAttrDict['name'], modifiedAttrDict['type'], modifiedAttrDict['description'], modifiedAttrDict['isUnique'], modifiedAttrDict['isEncrypted'], modifiedAttrDict['isNullable'], modifiedAttrDict['generationMethod']) addObjectAttributes(user.id, data['objectId'], newObjectAttributes) except Exception as e: print(e) # if updateObjectAttributes(user.id, data['attributeId'], **data['updatedObjectAttribute']): # return jsonify(**{'attributeId': data['attributeId']}), ErrorCode_Success # return jsonify(**{'message': 'Update failed'}), ErrorCode_ServerError return jsonify(**{'message': 'Update success'}), ErrorCode_Success except: routeRequestLogger.exception(request) return jsonify( **{"message": "Unexpected Error"}), ErrorCode_ServerError