From 474631ff96cae0bc1a526dc37f77e317f7af0505 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 28 Mar 2020 21:26:58 +0100 Subject: feat: create model file --- src/frest/manage/utils.py | 48 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/frest/manage/utils.py b/src/frest/manage/utils.py index 8ff6dcb..47adeb1 100644 --- a/src/frest/manage/utils.py +++ b/src/frest/manage/utils.py @@ -98,21 +98,58 @@ def create_model_cli(name): "\n\n", ) fields = [] - logging("Create field: (Y/n)") + logging("Create field? (Y/n): ") answer = inputsr() while answer in ["y", "", "yes"]: field = create_field(fields) fields.append(field) - logging("Create new field: (Y/n)") + logging("Create new field? (Y/n): ") answer = inputsr() - return fields + fields_string = "" + init_fields_s = "" + for field in fields: + field_string = f"{field['name']} = db.Column(" + if field["type"] == "int": + field_string += "db.Integer" + elif field["type"] == "str": + field_string += f"db.String({field['size']})" + elif field["type"] == "text": + field_string += "db.Text" + elif field["type"] == "datetime": + field_string += "db.Datetime" + elif field["type"] == "float": + field_string += "db.Float" + else: + field_string += "db.Boolean" + + field_string += f", nullable={field['nullable']})\n\t".replace("\t", " " * 4) + + fields_string += field_string + init_fields_s += f"self.{field['name']} = kwargs.get('{field['name']}')\n\t\t".replace( + "'", '"' + ).replace( + "\t", " " * 4 + ) + + with open("templates/models.txt") as f: + modeltext = "".join(f.readlines()) + + modeltext = modeltext.replace("%%NAME%%", name.capitalize()) + modeltext = modeltext.replace("%%name%%", name) + modeltext = modeltext.replace("%%params_model%%", fields_string[:-5]) + modeltext = modeltext.replace("%%params_model_init%%", init_fields_s[:-9]) + + modelpath = f"scheme/{name}/models.py" + logging_arg("Create {}... ", modelpath) + with open(modelpath, "w") as f: + f.write(modeltext) def create_app(name): - name = name.lower().replace('-', '_') + name = name.lower().replace("-", "_") if name.isdigit() or name[0].isdigit(): logging("Name cannot be a number o starts with a number", 2) @@ -135,6 +172,5 @@ def create_app(name): logging("OK", 3, "\n") logging_arg("Create model for {}...\n", name) - fields = create_model_cli(name) - print(fields) + create_model_cli(name) logging("OK", 3, "\n") -- cgit v1.2.3-18-g5258