def handleMethod(clazz, method, model, id=None): result = None status = False message = None code = None entityById = None if id is not None: entityById = DBUtil.findById(clazz, id) if method == 'POST': s, model = DBUtil.insert(model) status = s result = model code = 201 if not status: code = 500 message = "Entity not created [{}]".format(model) elif method == 'PUT': if entityById is not None: GenericHelperService.compareWithCurrent(entityById, model) status = True result = entityById else: status = False result = None if id is None: message = "PUT method [PATH param ID is missing]" code = 500 else: message = "Entity not found" code = 404 elif method == 'GET': if id is not None: result = DBUtil.findById(clazz, id) else: result = DBUtil.findAll(clazz) if result is None: status = False code = 404 message = "Entity not found" else: status = True elif method == 'DELETE': if entityById is not None: entityById.active = False DBUtil.commit() status = False code = 200 message = "Successfully deleted" else: status = False code = 404 message = "Entity not found" result = None return status, result, message, code
def execute(id, args, header): try: token = header.get(AUTH_TOKEN) status = TaskExecutionService.checkToken(token) if status is not None: return status except: return Utils.JsonMessage("Internal server error", 500) task = DBUtil.findById(Task, id) if task is None: return Utils.JsonMessage("Task ID[{}] does not exists!".format(id), 404) robotID = args.get(ROBOT_ID) simulatedTime = args.get(SIMULATED_TIME) simulatedStatus = args.get(SIMULATED_STATUS) if robotID is None: Utils.JsonMessage("robot_id parameter is missing", 500) else: robot = DBUtil.findById(Robot, robotID) if robot is None: return Utils.JsonMessage("Robot ID[{}] does not exists!".format(robotID), 404) startTime = dt.now() d = startTime.date() t = startTime.time() ts = startTime.timestamp() execution = TaskExecution.create(int(id), int(robotID), d, t, str(int(ts))) if simulatedTime is not None: endTime = startTime + timedelta(seconds=int(simulatedTime)) else: endTime = startTime + timedelta(seconds=30) duration = endTime - startTime s = True if simulatedStatus is not None: s = bool(int(simulatedStatus)) execution.setEnd(endTime.time(), duration.seconds, s) status, model = DBUtil.insert(execution) if status: print(model) return Utils.JsonMessage("Task ID[{}] executed by robot ID[{}]!".format(id, robotID), 200) else: return Utils.JsonMessage("Task ID[{}] not executed!", 500)
def deleteUserById(id): user = DBUtil.findById(User, id) if user.username == 'admin': flash("Admin cannot be deleted!") else: status = DBUtil.delete(user) if status: flash("User successfully deleted!", "success") else: flash("Error occurred!", "error")
def updateUser(user): update = DBUtil.findById(User, user.id) update.require_change = user.require_change try: db.session.commit() return True except Exception as e: print(e) db.session.rollback() return False
def getUserById(id): user = DBUtil.findById(User, id) return user
def getModelById(clazz, id): e = DBUtil.findById(clazz, id) return e
def findAppById(id): app = DBUtil.findById(Application, id) return app