From a567739eaf4aa05615593e15373a1161b10339fe Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Sun, 7 Jun 2020 20:29:51 +0200 Subject: [PATCH] m_n --- oshipka/persistance/__init__.py | 18 ++------ oshipka/webapp/views.py | 66 +++++++----------------------- shared/__init__.py | 0 shared/shared.py | 18 ++++++++ vm_gen/templates/_hooks.py | 17 ++++++-- vm_gen/templates/_model_m_n_py | 5 ++- vm_gen/templates/_model_py | 4 -- vm_gen/templates/_relationship_py | 2 +- vm_gen/templates/html/_create.html | 2 +- vm_gen/templates/html/table.html | 8 ++++ vm_gen/templates/model_py | 6 +-- vm_gen/templates/routes_py | 13 +++--- vm_gen/vm_gen.py | 20 +++++++-- 13 files changed, 89 insertions(+), 90 deletions(-) create mode 100644 shared/__init__.py create mode 100644 shared/shared.py create mode 100644 vm_gen/templates/html/table.html diff --git a/oshipka/persistance/__init__.py b/oshipka/persistance/__init__.py index 5a2c618..8985883 100644 --- a/oshipka/persistance/__init__.py +++ b/oshipka/persistance/__init__.py @@ -8,9 +8,8 @@ from json import JSONEncoder from uuid import uuid4 from config import SQLALCHEMY_DATABASE_URI, MAKEDIRS, DATABASE_FILE, SEARCH_INDEX_PATH, STATIC_DATA_DIR, basepath -from flask_migrate import Migrate, Manager, MigrateCommand +from flask_migrate import Migrate from flask_migrate import upgrade as migrate_upgrade -from flask_migrate import migrate as migrate_migrate from flask_migrate import init as migrate_init from flask_security import RoleMixin, UserMixin from flask_security import Security, SQLAlchemyUserDatastore @@ -24,6 +23,7 @@ from sqlalchemy_utils import Choice from tww.lib import solve_query, resolve_timezone, dt_tz_translation, time_ago from whooshalchemy import IndexService from oshipka.util.strings import camel_case_to_snake_case +from vm_gen.vm_gen import order_from_process_order db = SQLAlchemy() migrate = Migrate() @@ -265,19 +265,7 @@ def init_db(app): def populate_static(app): with app.app_context(): models = import_module("webapp.models") - model_names = set([f.split('.csv')[0] for f in os.listdir(STATIC_DATA_DIR) if f.endswith(".csv")]) - process_order_file = os.path.join(STATIC_DATA_DIR, "_process_order") - ordered_model_names = [] - # process first ordered if exists - if os.path.exists(process_order_file): - with open(process_order_file) as f: - for line in f.readlines(): - line = line.strip() - if line: - ordered_model_names.append(line) - model_names.remove(line) - for model_name in model_names: - ordered_model_names.append(model_name) + ordered_model_names = order_from_process_order('csv', STATIC_DATA_DIR) for model_name in ordered_model_names: model = getattr(models, model_name) with open(os.path.join(STATIC_DATA_DIR, "{}.csv".format(model_name))) as f: diff --git a/oshipka/webapp/views.py b/oshipka/webapp/views.py index 6d71576..a17c706 100644 --- a/oshipka/webapp/views.py +++ b/oshipka/webapp/views.py @@ -180,8 +180,11 @@ class ModelView(object): self.model = model p = inflect.engine() - - self.model_name = camel_case_to_snake_case(model.__name__) + if hasattr(model, "__name__"): + _model_name = getattr(model, "__name__") + else: + _model_name = model.name + self.model_name = camel_case_to_snake_case(_model_name) self.model_name_pl = p.plural(self.model_name) MODEL_VIEWS[self.model_name] = self @@ -198,56 +201,17 @@ class ModelView(object): self.app.add_url_rule(rule=api_url, endpoint=api_endpoint, view_func=view_func(self, **kwargs), **url_args) - def register_get(self, **kwargs): + def register_verb(self, verb, methods=None, per_item=False, **kwargs): + if not methods: + methods = ["GET"] + rule = '/{}'.format(self.model_name_pl) + if per_item: + rule += '/' + rule += '/{}'.format(verb) url_args = dict( - rule='/{}//get'.format(self.model_name_pl), - methods=["GET"], - endpoint='get_{}'.format(self.model_name), - view_func=create_view, - ) - self._register_rule(url_args, **kwargs) - - def register_list(self, **kwargs): - url_args = dict( - rule='/{}/list'.format(self.model_name_pl), - methods=["GET"], - endpoint='list_{}'.format(self.model_name), - view_func=create_view, - ) - self._register_rule(url_args, **kwargs) - - def register_create(self, **kwargs): - url_args = dict( - rule='/{}/create'.format(self.model_name_pl), - methods=["GET", "POST"], - endpoint='create_{}'.format(self.model_name), - view_func=create_view, - ) - self._register_rule(url_args, **kwargs) - - def register_update(self, **kwargs): - url_args = dict( - rule='/{}//update'.format(self.model_name_pl), - methods=["GET", "POST"], - endpoint='update_{}'.format(self.model_name), - view_func=create_view, - ) - self._register_rule(url_args, **kwargs) - - def register_delete(self, **kwargs): - url_args = dict( - rule='/{}//delete'.format(self.model_name_pl), - methods=["GET", "POST"], - endpoint='delete_{}'.format(self.model_name), - view_func=create_view, - ) - self._register_rule(url_args, **kwargs) - - def register_search(self, **kwargs): - url_args = dict( - rule='/{}/search'.format(self.model_name_pl), - methods=["GET"], - endpoint='search_{}'.format(self.model_name), + rule=rule, + methods=methods, + endpoint='{}_{}'.format(verb, self.model_name), view_func=create_view, ) self._register_rule(url_args, **kwargs) diff --git a/shared/__init__.py b/shared/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shared/shared.py b/shared/shared.py new file mode 100644 index 0000000..1542f78 --- /dev/null +++ b/shared/shared.py @@ -0,0 +1,18 @@ +import os + + +def order_from_process_order(file_ext, directory): + model_names = set([f.split('.{}'.format(file_ext))[0] for f in os.listdir(directory) if f.endswith(".{}".format(file_ext))]) + process_order_file = os.path.join(directory, "_process_order") + ordered_model_names = [] + # process first ordered if exists + if os.path.exists(process_order_file): + with open(process_order_file) as f: + for line in f.readlines(): + line = line.strip() + if line: + ordered_model_names.append(line) + model_names.remove(line) + for model_name in model_names: + ordered_model_names.append(model_name) + return ordered_model_names \ No newline at end of file diff --git a/vm_gen/templates/_hooks.py b/vm_gen/templates/_hooks.py index 09a6e32..164439a 100644 --- a/vm_gen/templates/_hooks.py +++ b/vm_gen/templates/_hooks.py @@ -6,14 +6,18 @@ def get_template(vc): vc.template = "{}/get.html".format(vc.model_view.model_name) -def search_template(vc): - vc.template = "{}/search.html".format(vc.model_view.model_name) - - def list_template(vc): vc.template = "{}/list.html".format(vc.model_view.model_name) +def table_template(vc): + vc.template = "{}/table.html".format(vc.model_view.model_name) + + +def search_template(vc): + vc.template = "{}/search.html".format(vc.model_view.model_name) + + def create_template(vc): vc.template = "{}/create.html".format(vc.model_view.model_name) @@ -36,6 +40,11 @@ list_view_context = ViewContext( template_func=list_template, ) +table_view_context = ViewContext( + filter_func=default_list_func, + template_func=table_template, +) + search_view_context = ViewContext( filter_func=default_search_func, template_func=list_template, diff --git a/vm_gen/templates/_model_m_n_py b/vm_gen/templates/_model_m_n_py index 9db24ca..04626b4 100644 --- a/vm_gen/templates/_model_m_n_py +++ b/vm_gen/templates/_model_m_n_py @@ -1,5 +1,6 @@ -[[ name ]] = db.Table('[[ name ]]', -[%- for column in columns %] + +[[ secondary.name ]] = db.Table('[[ secondary.name|camel_to_snake ]]', +[%- for column in secondary.columns %] db.Column('[[ column.name ]]_id', db.Integer(), db.ForeignKey('[[ column.name ]].id')), [%- endfor %] ) \ No newline at end of file diff --git a/vm_gen/templates/_model_py b/vm_gen/templates/_model_py index f635d56..4a8bc04 100644 --- a/vm_gen/templates/_model_py +++ b/vm_gen/templates/_model_py @@ -2,10 +2,6 @@ from sqlalchemy_utils import ChoiceType [%- endif %] -[%- if _secondaries %] -from webapp.models import [% for secondary in _secondaries %][[ secondary ]][%- if not loop.last %], [% endif %][% endfor %] -[%- endif %] - class [[ name ]](db.Model, ModelController): [%- include "_model_choice_header_py" %] [%- include "_model_searchable_header_py" %] diff --git a/vm_gen/templates/_relationship_py b/vm_gen/templates/_relationship_py index e1dd0c5..6af0c44 100644 --- a/vm_gen/templates/_relationship_py +++ b/vm_gen/templates/_relationship_py @@ -2,7 +2,7 @@ [%- 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 %]', + [[ column.name|pluralize if column.secondary else 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 %] diff --git a/vm_gen/templates/html/_create.html b/vm_gen/templates/html/_create.html index e249a9b..c3211f9 100644 --- a/vm_gen/templates/html/_create.html +++ b/vm_gen/templates/html/_create.html @@ -3,7 +3,7 @@ [%- for column in columns %] : [%- if column.type in ['relationship'] %] -