def add_model(): """Add a new network model """ # parse the json request new_model = Model.from_dict(connexion.request.form) try: # parse the attached xml files req_files = connexion.request.files.getlist("files") files = [] for f in req_files: # Validate xml input filestr = f.stream.read() ElementTree.fromstring(filestr) f.stream.seek(0) files.append(f.stream) except ElementTree.ParseError: return Error(code=422, message="Invalid XML files"), 422 # create cimpy objects try: cimpy_data = cimpy.cim_import(files, new_model.version) except Exception: return Error(code=422, message="Invalid CIM files"), 422 new_id = model_db.put_model(new_model, cimpy_data, files) # Return the model as `ModelReply` return ModelReply.from_model(new_model, new_id)
def get_model(id_): """Get a network model This is only useful at the moment to get the name of the model :param id: Model id :type id: int :rtype: Model """ try: return ModelReply.from_model(model_db.get_model(id_), id_) except KeyError: return Error(code=404, message="Model not found"), 404
def get_model(id_): """Get a network model This is only useful at the moment to get the name of the model :param id: Model id :type id: int :rtype: Model """ try: record = model_db.get_record(id_) # Temporary Hack: manipulate the stored json so that it can be deserialized into a model again model_str = record.model.replace("\n", "").replace("'", '"') model = json.loads(model_str, object_hook=lambda d: SimpleNamespace(**d)) return ModelReply.from_model(model, id_) except KeyError: return Error(code=404, message="Model not found"), 404