Skip to content

Commit

Permalink
update object update
Browse files Browse the repository at this point in the history
Former-commit-id: 03b6a59
  • Loading branch information
afourmy committed Jan 17, 2018
1 parent f2c5db9 commit e047513
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
Binary file modified projects/europe.xls
Binary file not shown.
19 changes: 16 additions & 3 deletions source/objects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,26 @@ def __init__(self, **kwargs):
def get_obj(db, model, **kwargs):
return db.session.query(model).filter_by(**kwargs).first()

def object_factory(db, cls, **kwargs):
#todo if the type is not the same, delete the object and recreate it
def object_factory(db, **kwargs):
obj_type = kwargs['type']
cls = Node if obj_type in node_class else Link
obj = get_obj(db, cls, name=kwargs['name'])
if obj:
for property, value in kwargs.items():
if property in obj.__dict__:
setattr(obj, property, value)
elif obj_type in link_class:
source = get_obj(db, Node, name=kwargs.pop('source'))
destination = get_obj(db, Node, name=kwargs.pop('destination'))
obj = link_class[obj_type](
source_id = source.id,
destination_id = destination.id,
source = source,
destination = destination,
**kwargs
)
else:
node = node_class[kwargs['type']](**kwargs)
db.session.add(node)
obj = object_class[obj_type](**kwargs)
db.session.add(obj)
db.session.commit()
34 changes: 4 additions & 30 deletions source/objects/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def create_objects():
add_node_form = AddNode(request.form)
add_nodes_form = AddNodes(request.form)
add_link_form = AddLink(request.form)
if 'add_node' in request.form:
object_factory(db, Node, **request.form.to_dict())
if 'add_node' in request.form or 'add_link' in request.form:
object_factory(db, **request.form.to_dict())
elif 'add_nodes' in request.form:
filename = request.files['file'].filename
if 'file' in request.files and allowed_file(filename, {'xls', 'xlsx'}):
Expand All @@ -57,37 +57,11 @@ def create_objects():
properties = sheet.row_values(0)
for row_index in range(1, sheet.nrows):
kwargs = dict(zip(properties, sheet.row_values(row_index)))
if obj_type in link_class:
source = get_obj(db, Node, name=kwargs.pop('source'))
destination = get_obj(db, Node, name=kwargs.pop('destination'))
new_link = link_class[obj_type](
source_id = source.id,
destination_id = destination.id,
source = source,
destination = destination,
**kwargs
)
db.session.add(new_link)
else:
kwargs['type'] = obj_type
object_factory(db, Node, **kwargs)
kwargs['type'] = obj_type
object_factory(db, **kwargs)
db.session.commit()
else:
flash('no file submitted')
elif 'add_link' in request.form:
kwargs = request.form.to_dict()
obj_type = kwargs.pop('type')
source = get_obj(db, Node, name=kwargs.pop('source'))
destination = get_obj(db, Node, name=kwargs.pop('destination'))
new_link = link_class[obj_type](
source_id = source.id,
destination_id = destination.id,
source = source,
destination = destination,
**kwargs
)
db.session.add(new_link)
db.session.commit()
all_nodes = Node.visible_choices()
add_link_form.source.choices = add_link_form.destination.choices = all_nodes
return render_template(
Expand Down

0 comments on commit e047513

Please sign in to comment.