update to include more model stuff

This commit is contained in:
Daniel Tsvetkov 2020-06-03 18:11:37 +02:00
parent 6c45fca97e
commit c4bb8db789
7 changed files with 59 additions and 17 deletions

View File

@ -1 +0,0 @@
from oshipka.persistance import db

View File

@ -110,9 +110,8 @@ run_in_prod() {
model() { model() {
shift shift
MODEL_NAME=$1
source venv/bin/activate 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() { command_main() {

View File

@ -1,3 +1,4 @@
autopep8==1.5.3
Babel==2.8.0 Babel==2.8.0
bcrypt==3.1.7 bcrypt==3.1.7
blinker==1.4 blinker==1.4

View File

@ -0,0 +1,4 @@
[%- if searchable %]
__searchable__ = [ [%- for s in searchable %]'[[s]]', [%- endfor %]]
[% endif %]

View File

@ -1,4 +1,9 @@
[%- if not column.secondary %]
[[ column.name ]]_id = db.Column(db.Integer, db.ForeignKey('[[ column.name ]].id')) [[ column.name ]]_id = db.Column(db.Integer, db.ForeignKey('[%- if column.to %][[ column.to|camel_to_snake ]][%- else %][[ column.name ]][% endif %].id'))
[[ column.name ]] = db.relationship('[[ column.name|snake_to_camel ]]', backref=db.backref("[[ name|camel_to_snake|pluralize ]]")) [%- 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 %]
)

View File

@ -1,17 +1,34 @@
from oshipka.persistance import db, ModelController
[%- if _choice_types %] [%- if _choice_types %]
from sqlalchemy_utils import ChoiceType from sqlalchemy_utils import ChoiceType
[%- endif %] [%- 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): class [[ name ]](db.Model, ModelController):
[%- include "_model_choice_header_py" %] [%- include "_model_choice_header_py" %]
[%- include "_model_searchable_header_py" %]
[%- for column in columns %] [%- for column in columns %]
[%- if column._type == 'relationship' %] [%- if column._type == 'relationship' %]
[%- include "_relationship_py" %] [%- include "_relationship_py" %]
[%- else %] [%- 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 %] [%- endif %]
[%- endfor %] [%- endfor %]
[%- if extra_code %]
[[ extra_code ]]
[%- endif %]

View File

@ -2,12 +2,15 @@ import os
import shutil import shutil
import sys import sys
import autopep8
import inflect import inflect
import yaml import yaml
from jinja2 import Environment, FileSystemLoader, select_autoescape from jinja2 import Environment, FileSystemLoader, select_autoescape
from util.strings import snake_case_to_camel_case, camel_case_to_snake_case from util.strings import snake_case_to_camel_case, camel_case_to_snake_case
pep_options = {'max_line_length': 120}
def _process_choice(column): def _process_choice(column):
column_name = column.get('name', '') column_name = column.get('name', '')
@ -23,13 +26,19 @@ def _process_choice(column):
def enrich_view_model(view_model): def enrich_view_model(view_model):
columns = [] columns = []
for column in view_model.get('columns', {}): for column in view_model.get('columns', {}):
column_name = column.get('name')
column_type = column.get('type') column_type = column.get('type')
if column_type in ['text', 'long_text', ]: if column_type in ['text', 'long_text', ]:
_column_type = 'db.UnicodeText' _column_type = 'db.UnicodeText'
elif column_type in ['bool', ]: elif column_type in ['bool', ] or column_name.startswith('is_'):
_column_type = 'db.Boolean' _column_type = 'db.Boolean'
elif column_type in ['relationship', ]: elif column_type in ['relationship', ]:
_column_type = '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', ]: elif column_type in ['choice', ]:
if '_choice_types' not in view_model: if '_choice_types' not in view_model:
view_model['_choice_types'] = [] view_model['_choice_types'] = []
@ -46,11 +55,11 @@ def enrich_view_model(view_model):
def process_model(view_model): def process_model(view_model):
template = env.get_template('model_py') 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') _model_name = view_model.get('name')
filename = "{}.py".format(camel_case_to_snake_case(_model_name.split('.yaml')[0])) filename = "{}.py".format(camel_case_to_snake_case(_model_name.split('.yaml')[0]))
with open(os.path.join(MODELS_PATH, filename), 'w+') as f: with open(os.path.join(MODELS_PATH, filename), 'w+') as f:
f.write(rv) f.write(model)
def process_html_templates(view_model): def process_html_templates(view_model):
@ -66,8 +75,9 @@ def process_html_templates(view_model):
f.write(rv) f.write(rv)
def main(view_model_name): def main():
view_model_names = os.listdir(VIEW_MODELS_PATH) if not view_model_name else ["{}.yaml".format(view_model_name)] view_model_names = os.listdir(VIEW_MODELS_PATH)
all_imports = ['from oshipka.persistance import db']
for view_model_name in view_model_names: for view_model_name in view_model_names:
with open(os.path.join(VIEW_MODELS_PATH, view_model_name), 'r') as stream: with open(os.path.join(VIEW_MODELS_PATH, view_model_name), 'r') as stream:
try: try:
@ -75,21 +85,28 @@ def main(view_model_name):
for view_model in view_models: for view_model in view_models:
view_model = enrich_view_model(view_model) view_model = enrich_view_model(view_model)
process_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) process_html_templates(view_model)
except yaml.YAMLError as e: except yaml.YAMLError as e:
breakpoint() 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__": if __name__ == "__main__":
model_name = sys.argv[1] basepath = sys.argv[1]
basepath = sys.argv[2]
oshipka_path = os.environ.get('OSHIPKA_PATH') oshipka_path = os.environ.get('OSHIPKA_PATH')
VM_TEMPLATES_PATH = os.path.join(oshipka_path, "vm_gen", "templates") VM_TEMPLATES_PATH = os.path.join(oshipka_path, "vm_gen", "templates")
WEBAPP_PATH = os.path.join(basepath, "webapp") WEBAPP_PATH = os.path.join(basepath, "webapp")
VIEW_MODELS_PATH = os.path.join(WEBAPP_PATH, "view_models") 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") HTML_TEMPLATES_PATH = os.path.join(WEBAPP_PATH, "templates_gen")
env = Environment( env = Environment(
@ -105,4 +122,4 @@ if __name__ == "__main__":
p = inflect.engine() p = inflect.engine()
env.filters['pluralize'] = p.plural env.filters['pluralize'] = p.plural
main(model_name) main()