update to include more model stuff
This commit is contained in:
parent
6c45fca97e
commit
c4bb8db789
@ -1 +0,0 @@
|
||||
from oshipka.persistance import db
|
@ -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() {
|
||||
|
@ -1,3 +1,4 @@
|
||||
autopep8==1.5.3
|
||||
Babel==2.8.0
|
||||
bcrypt==3.1.7
|
||||
blinker==1.4
|
||||
|
4
vm_gen/templates/_model_searchable_header_py
Normal file
4
vm_gen/templates/_model_searchable_header_py
Normal file
@ -0,0 +1,4 @@
|
||||
[%- if searchable %]
|
||||
__searchable__ = [ [%- for s in searchable %]'[[s]]', [%- endfor %]]
|
||||
|
||||
[% endif %]
|
@ -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 ]]"))
|
||||
[%- 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 %]
|
||||
)
|
@ -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 %]
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user