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 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 getSingleObjectAttribute(userId, attributeId): if not isValidUser(userId): return None try: return ObjectAttributes.query.filter( ObjectAttributes.id == attributeId).one_or_none() except Exception: return None
def addObjectAttributes(userId, groupId, objectAttributes): if ObjectAttributes.query.filter( ObjectAttributes.objectId == groupId).filter( ObjectAttributes.name == objectAttributes.name).first(): print("This attribute already exists!") return if isValidUser(userId) and isValidRouteGroup(groupId): db_session.add(objectAttributes) db_session.commit()
def addRoute(userId, routeGroup): if isValidUser(userId): # if isValidRoute(routeGroup.objectName): # print ('in error', routeGroup.objectName) # generalLogger.error("Error: RouteGroup '" + routeGroup.objectName + "' already added to userID '" + str(userId) + "'") # return # print('IN DB', routeGroup) db_session.add(routeGroup) db_session.commit() generalLogger.info("RouteGroup '" + routeGroup.objectName + "' added to userID '" + str(userId) + "'")
def checkValidUser(): try: data = request.get_json() routeRequestLogger.info(request) if not checkParam(data, 'userId'): return jsonify(**{'message': 'Bad params'}), ErrorCode_BadParams if not isValidUser(int(data['userId'])): return jsonify(**{'message': 'Invalid User'}), ErrorCode_NotFound return jsonify(**{'id': int(data['userId'])}), ErrorCode_Success except: routeRequestLogger.exception(request) return jsonify(**{"message": "Unexpected Error"}), ErrorCode_ServerError
def getRoute(userId, queries): if not isValidUser(userId): return None attr = val = "" try: filter_list = [] for attr, val in queries.items(): filter_list.append(getattr(RouteGroup, attr) == str(val)) route_list = RouteGroup.query.filter(*filter_list).all() return route_list if len(route_list) > 1 else route_list[0] except Exception as e: print("An exception occurred with the following details:\n{}".format(str(e))) print("Attribute: {}\tValue: {}\n".format(attr, val)) return None
def getObjectAttributes(): try: data = request.get_json() routeRequestLogger.info(request) if not checkParam(data, 'userId') or \ not checkParam(data, 'objectId') or \ not isValidUser(int(data['userId'])): return jsonify(**{'message': 'Bad params'}), ErrorCode_BadParams object_attrs = retrieveObjectAttributes(int(data['userId']), int(data['objectId'])) attributes = [] for obj in object_attrs: attributes.append(dict(obj)) return jsonify(**{'attributes': attributes}), ErrorCode_Success except: routeRequestLogger.exception(request) return jsonify( **{"message": "Unexpected Error"}), ErrorCode_ServerError
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 retrieveObjectAttributes(userId, groupId): if not isValidUser(userId) or not isValidRouteGroup(groupId): return [] return ObjectAttributes.query.filter(ObjectAttributes.userId == userId).\ filter(ObjectAttributes.objectId == groupId).all()
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
def getUserRoutes(userId): if not isValidUser(userId): return [] return RouteGroup.query.filter(RouteGroup.userId == userId).all()
def getCurrentAPI(userId): if not isValidUser(userId): return [] return APIGroup.query.filter(APIGroup.userId == userId).first()
def getUserAPIs(userId): if not isValidUser(userId): return [] return APIGroup.query.filter(APIGroup.userId == userId).all()
def updateAPI(userId, apiGroup): if isValidUser(userId): api_row = APIGroup.query.filter( APIGroup.userId == userId).one_or_none() api_row.name = apiGroup.name db_session.commit()
def addAPI(userId, apiGroup): if isValidUser(userId): db_session.add(apiGroup) db_session.commit()