def generate_template_model_from_YAML_file(file_path,name=None): yaml_file = open(file_path, "r") yaml_string = yaml.dump(yaml.load(yaml_file.read())) yaml_file.close() #Hash the content of the yaml file/dump produced by PyYAML (equivalent) basename = os.path.splitext(os.path.basename(file_path))[0] yaml_hash = hash_content(yaml_string) yaml_python = yaml.load(yaml_string) model_content = generate_template_model_from_YAML(yaml_python) if name is None or name == "" : name = basename model_name = name else : model_name = name.replace(' ','_') + '_' + yaml_hash model_name = normalize_string(model_name) model_hash = hash_content(model_content) model_content = model_content.format(classname=model_name) model = Template( yaml_hash=yaml_hash, model_hash=model_hash, name=name, model_name=model_name, yaml=yaml.dump(yaml_python), json=json.dumps(yaml_python), model=model_content) return model
def generate_template_model_from_YAML_with_name(yaml_string,name=None): yaml_hash = hash_content(yaml_string) yaml_python = yaml.load(yaml_string) model_content = generate_template_model_from_YAML(yaml_python) model_hash = hash_content(model_content) if name is None or name == "" : name = model_hash model_name = name else : model_name = name.replace(' ','_') + '_' + yaml_hash model_name = normalize_string(model_name) model_content = model_content.format(classname=model_name) model = Template( yaml_hash=yaml_hash, model_hash=model_hash, name=name, model_name=model_name, yaml=yaml.dump(yaml_python), json=json.dumps(yaml_python), model=model_content) return model
def sync_model() : # For every Template in the db, we check that the model file is still there, # and if its content wasn't tampered with (we check the hash) : # if one of those two is true, then we re-write the model file content, # with the content saved in the Template for model in Template.objects.all() : model_path = '{0}/{1}.py'.format( nodesk_template.models.__path__[0], model.model_name) if os.path.isfile(model_path) is False : with open(model_path, "w+") as model_file: model_hash = hash_content(model_file.read()) if model.model_hash != model_hash: model_file.seek(0) model_file.write(model.model) model_file.truncate() reload(nodesk_template.models) call_command('syncdb', interactive=False)