diff --git a/bootstrap/webapp/models.py b/bootstrap/webapp/models.py deleted file mode 100644 index 3739d55..0000000 --- a/bootstrap/webapp/models.py +++ /dev/null @@ -1 +0,0 @@ -from oshipka.persistance import db \ No newline at end of file diff --git a/oshipka.sh b/oshipka.sh index a3a3ef5..e6b4708 100755 --- a/oshipka.sh +++ b/oshipka.sh @@ -110,9 +110,8 @@ run_in_prod() { model() { shift - MODEL_NAME=$1 source venv/bin/activate - python "${OSHIPKA_PATH}/vm_gen/vm_gen.py" "${MODEL_NAME}" "`pwd`" + python "${OSHIPKA_PATH}/vm_gen/vm_gen.py" "`pwd`" } command_main() { diff --git a/requirements.txt b/requirements.txt index f683eb0..b1a0504 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +autopep8==1.5.3 Babel==2.8.0 bcrypt==3.1.7 blinker==1.4 diff --git a/vm_gen/templates/_model_searchable_header_py b/vm_gen/templates/_model_searchable_header_py new file mode 100644 index 0000000..e1730f2 --- /dev/null +++ b/vm_gen/templates/_model_searchable_header_py @@ -0,0 +1,4 @@ + [%- if searchable %] + __searchable__ = [ [%- for s in searchable %]'[[s]]', [%- endfor %]] + + [% endif %] \ No newline at end of file diff --git a/vm_gen/templates/_relationship_py b/vm_gen/templates/_relationship_py index 18427d6..e1dd0c5 100644 --- a/vm_gen/templates/_relationship_py +++ b/vm_gen/templates/_relationship_py @@ -1,4 +1,9 @@ - - [[ column.name ]]_id = db.Column(db.Integer, db.ForeignKey('[[ column.name ]].id')) - [[ column.name ]] = db.relationship('[[ column.name|snake_to_camel ]]', backref=db.backref("[[ name|camel_to_snake|pluralize ]]")) \ No newline at end of file + [%- if not column.secondary %] + [[ column.name ]]_id = db.Column(db.Integer, db.ForeignKey('[%- if column.to %][[ column.to|camel_to_snake ]][%- else %][[ column.name ]][% endif %].id')) + [%- endif %] + [[ column.name ]] = db.relationship('[%- if column.to %][[ column.to ]][%- else %][[ column.name|snake_to_camel ]][% endif %]', + [%- if column.secondary %]secondary=[[ column.secondary ]], [%- endif %] + backref=db.backref("[%- if column.backref %][[ column.backref ]][%- else %][[ name|camel_to_snake|pluralize ]][%- endif %]"), + [%- if column.foreign_keys %]foreign_keys=[ [[ column.foreign_keys ]]_id],[%- endif %] + ) \ No newline at end of file diff --git a/vm_gen/templates/model_py b/vm_gen/templates/model_py index 9946a14..49f73fc 100644 --- a/vm_gen/templates/model_py +++ b/vm_gen/templates/model_py @@ -1,17 +1,34 @@ +from oshipka.persistance import db, ModelController + [%- if _choice_types %] from sqlalchemy_utils import ChoiceType [%- endif %] -from oshipka.persistance import db, ModelController +[%- if _secondaries %] +from webapp.models import [% for secondary in _secondaries %][[ secondary ]][%- if not loop.last %], [% endif %][% endfor %] +[%- endif %] +[%- if imports %] + [%- for import in imports %] +[[ import ]] + [%- endfor %] +[%- endif %] class [[ name ]](db.Model, ModelController): [%- include "_model_choice_header_py" %] + [%- include "_model_searchable_header_py" %] [%- for column in columns %] [%- if column._type == 'relationship' %] [%- include "_relationship_py" %] [%- else %] - [[ column.name ]] = db.Column([[ column._type ]][%- if column.default %], default="[[ column.default ]]"[%- endif %]) + [[ column.name ]] = db.Column([[ column._type ]], + [%- if column.default %]default="[[ column.default ]]",[%- endif %] + [%- if column.index %]index=True,[%- endif %]) [%- endif %] [%- endfor %] + + [%- if extra_code %] + + [[ extra_code ]] + [%- endif %] diff --git a/vm_gen/vm_gen.py b/vm_gen/vm_gen.py index caeaac5..56f1526 100644 --- a/vm_gen/vm_gen.py +++ b/vm_gen/vm_gen.py @@ -2,12 +2,15 @@ import os import shutil import sys +import autopep8 import inflect import yaml from jinja2 import Environment, FileSystemLoader, select_autoescape from util.strings import snake_case_to_camel_case, camel_case_to_snake_case +pep_options = {'max_line_length': 120} + def _process_choice(column): column_name = column.get('name', '') @@ -23,13 +26,19 @@ def _process_choice(column): def enrich_view_model(view_model): columns = [] for column in view_model.get('columns', {}): + column_name = column.get('name') column_type = column.get('type') if column_type in ['text', 'long_text', ]: _column_type = 'db.UnicodeText' - elif column_type in ['bool', ]: + elif column_type in ['bool', ] or column_name.startswith('is_'): _column_type = 'db.Boolean' elif column_type in ['relationship', ]: _column_type = 'relationship' + secondary = column.get('secondary') + if secondary: + if '_secondaries' not in view_model: + view_model['_secondaries'] = [] + view_model['_secondaries'].append(secondary) elif column_type in ['choice', ]: if '_choice_types' not in view_model: view_model['_choice_types'] = [] @@ -46,11 +55,11 @@ def enrich_view_model(view_model): def process_model(view_model): template = env.get_template('model_py') - rv = template.render(**view_model) + model = autopep8.fix_code(template.render(**view_model), options=pep_options) _model_name = view_model.get('name') filename = "{}.py".format(camel_case_to_snake_case(_model_name.split('.yaml')[0])) with open(os.path.join(MODELS_PATH, filename), 'w+') as f: - f.write(rv) + f.write(model) def process_html_templates(view_model): @@ -66,8 +75,9 @@ def process_html_templates(view_model): f.write(rv) -def main(view_model_name): - view_model_names = os.listdir(VIEW_MODELS_PATH) if not view_model_name else ["{}.yaml".format(view_model_name)] +def main(): + view_model_names = os.listdir(VIEW_MODELS_PATH) + all_imports = ['from oshipka.persistance import db'] for view_model_name in view_model_names: with open(os.path.join(VIEW_MODELS_PATH, view_model_name), 'r') as stream: try: @@ -75,21 +85,28 @@ def main(view_model_name): for view_model in view_models: view_model = enrich_view_model(view_model) process_model(view_model) + view_model_name = view_model.get('name', '') + all_imports.append('from webapp.models.{} import {}'.format( + camel_case_to_snake_case(view_model_name), view_model_name)) process_html_templates(view_model) except yaml.YAMLError as e: breakpoint() + all_imports = autopep8.fix_code('\n'.join(all_imports), options=pep_options) + with open(os.path.join(MODELS_PATH, "__init__.py"), 'w+') as f: + f.write(all_imports) if __name__ == "__main__": - model_name = sys.argv[1] - basepath = sys.argv[2] + basepath = sys.argv[1] oshipka_path = os.environ.get('OSHIPKA_PATH') VM_TEMPLATES_PATH = os.path.join(oshipka_path, "vm_gen", "templates") WEBAPP_PATH = os.path.join(basepath, "webapp") VIEW_MODELS_PATH = os.path.join(WEBAPP_PATH, "view_models") - MODELS_PATH = os.path.join(WEBAPP_PATH, "models_gen") + MODELS_PATH = os.path.join(WEBAPP_PATH, "models") + if not os.path.exists(MODELS_PATH): + os.makedirs(MODELS_PATH) HTML_TEMPLATES_PATH = os.path.join(WEBAPP_PATH, "templates_gen") env = Environment( @@ -105,4 +122,4 @@ if __name__ == "__main__": p = inflect.engine() env.filters['pluralize'] = p.plural - main(model_name) + main()