populate m_n
This commit is contained in:
parent
cc76955a08
commit
c6e9c9cab4
@ -285,7 +285,7 @@ def populate_static(app):
|
||||
for row in reader:
|
||||
row_updates = dict()
|
||||
for key, value in row.items():
|
||||
if value.startswith(SENSITIVE_PREFIX):
|
||||
if value and value.startswith(SENSITIVE_PREFIX):
|
||||
sensitive_key = SENSITIVE_PREFIX.join(value.split(SENSITIVE_PREFIX)[1:])
|
||||
sensitive_value = getattr(sensitive, sensitive_key)
|
||||
row_updates[key] = sensitive_value
|
||||
@ -299,6 +299,39 @@ def populate_static(app):
|
||||
role = Role.query.filter_by(name=role_name).first()
|
||||
user_datastore.add_role_to_user(user, role)
|
||||
else:
|
||||
instance = model(**row)
|
||||
instance = create_model(model, row)
|
||||
db.session.add(instance)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def filter_m_n(serialized_args):
|
||||
m_ns, to_delete = {}, []
|
||||
for k in serialized_args:
|
||||
if k.startswith('_m_n_'):
|
||||
m_n_name = k.split('_m_n_')[1]
|
||||
m_ns[m_n_name] = serialized_args[k]
|
||||
to_delete.append(k)
|
||||
return m_ns, to_delete
|
||||
|
||||
|
||||
def update_m_ns(instance, m_ns):
|
||||
from oshipka.webapp.views import webapp_models
|
||||
instance = instance
|
||||
for key, ids in m_ns.items():
|
||||
child_rel = getattr(instance, "_m_n_table_{}".format(key))
|
||||
child_table = getattr(webapp_models, child_rel)
|
||||
children = db.session.query(child_table).filter(child_table.id.in_(ids)).all()
|
||||
setattr(instance, key, children)
|
||||
|
||||
|
||||
def create_model(model, serialized_args):
|
||||
m_ns, to_delete = filter_m_n(serialized_args)
|
||||
for key in to_delete:
|
||||
del serialized_args[key]
|
||||
instance = model()
|
||||
for k, v in serialized_args.items():
|
||||
setattr(instance, k, v)
|
||||
for key, ids in m_ns.items():
|
||||
m_ns[key] = ids.split(',')
|
||||
update_m_ns(instance, m_ns)
|
||||
return instance
|
||||
|
@ -6,7 +6,7 @@ import inflect
|
||||
from flask import flash, render_template, redirect, request, url_for, jsonify
|
||||
from flask_security import login_required, roles_required
|
||||
|
||||
from oshipka.persistance import db
|
||||
from oshipka.persistance import db, filter_m_n, update_m_ns
|
||||
from oshipka.util.strings import camel_case_to_snake_case
|
||||
|
||||
webapp_models = importlib.import_module("webapp.models")
|
||||
@ -74,44 +74,21 @@ def default_search_func(vc):
|
||||
vc.instances = vc.model_view.model.search_query("{q}".format(q=q)).all()
|
||||
|
||||
|
||||
def _filter_m_n(vc):
|
||||
m_ns, to_delete = {}, []
|
||||
for k in vc.serialized_args:
|
||||
if k.startswith('_m_n_'):
|
||||
m_n_name = k.split('_m_n_')[1]
|
||||
m_ns[m_n_name] = vc.serialized_args[k]
|
||||
to_delete.append(k)
|
||||
def default_update_func(vc):
|
||||
m_ns, to_delete = filter_m_n(vc.serialized_args)
|
||||
for key in to_delete:
|
||||
del vc.serialized_args[key]
|
||||
return m_ns
|
||||
|
||||
|
||||
def _update_m_ns(vc, m_ns):
|
||||
instance = vc.instance
|
||||
for key, ids in m_ns.items():
|
||||
child_rel = getattr(vc.instance, "_m_n_table_{}".format(key))
|
||||
child_table = getattr(webapp_models, child_rel)
|
||||
children = db.session.query(child_table).filter(child_table.id.in_(ids)).all()
|
||||
setattr(instance, key, children)
|
||||
vc.instance = vc.instances[0]
|
||||
for k, v in vc.serialized_args.items():
|
||||
setattr(vc.instance, k, v)
|
||||
update_m_ns(vc.instance, m_ns)
|
||||
db.session.add(vc.instance)
|
||||
|
||||
|
||||
def default_create_func(vc):
|
||||
m_ns = _filter_m_n(vc)
|
||||
instance = vc.instances or vc.model_view.model(**vc.serialized_args)
|
||||
vc.instance = instance
|
||||
vc.instances = instance
|
||||
_update_m_ns(vc, m_ns)
|
||||
db.session.add(instance)
|
||||
|
||||
|
||||
def default_update_func(vc):
|
||||
m_ns = _filter_m_n(vc)
|
||||
instance = vc.instances[0]
|
||||
vc.instance = instance
|
||||
for k, v in vc.serialized_args.items():
|
||||
setattr(instance, k, v)
|
||||
_update_m_ns(vc, m_ns)
|
||||
db.session.add(instance)
|
||||
instance = vc.instances or vc.model_view.model()
|
||||
vc.instances = [instance]
|
||||
default_update_func(vc)
|
||||
|
||||
|
||||
def default_delete_func(vc):
|
||||
|
Loading…
Reference in New Issue
Block a user