Skip to content

Commit

Permalink
Merge pull request #4724 from archesproject/4375_unique_tiles
Browse files Browse the repository at this point in the history
Supports import of graphs with card constraints
  • Loading branch information
chiatt authored Mar 28, 2019
2 parents 9d86274 + 2bff017 commit ef9a0f2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
5 changes: 3 additions & 2 deletions arches/app/models/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def update_constraints(self, constraints):

def add_nodeconstraints(nodeids, constraint_model):
for nodeid in nodeids:
print('adding nodes', nodeid)
node_constraint = models.ConstraintXNode()
node_constraint.node = models.Node.objects.get(pk=nodeid)
node_constraint.constraint = constraint_model
node_constraint.save()

constraint_models = []
for constraint in constraints:
constraintid = constraint.get('constraintid', None)
unique_to_all = constraint.get('uniquetoallinstances', False)
Expand Down Expand Up @@ -69,7 +69,8 @@ def add_nodeconstraints(nodeids, constraint_model):
constraint_model.save()
except ObjectDoesNotExist as e:
print e
self.constraints = constraint_model
constraint_models.append(constraint_model)
self.constraints = constraint_models

def __init__(self, *args, **kwargs):
"""
Expand Down
30 changes: 30 additions & 0 deletions arches/app/models/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def __init__(self, *args, **kwargs):
self.widgets = {}
self._nodegroups_to_delete = []
self._functions = []
self._card_constraints = []
self._constraints_x_nodes = []

if args:
if isinstance(args[0], dict):
Expand Down Expand Up @@ -236,6 +238,20 @@ def add_edge(self, edge):
self.edges[edge.pk] = edge
return edge

def add_card_contraint(self, constraint, card):
unique_to_all = constraint.get('uniquetoallinstances', False)
constraint_model = models.ConstraintModel()
constraint_model.constraintid = constraint.get('constraintid', None)
constraint_model.uniquetoallinstances = constraint.get('uniquetoallinstances', False)
constraint_model.card = card
self._card_constraints.append(constraint_model)
for nodeid in constraint.get('nodes', []):
constraint_x_node = {
'constraint': constraint_model,
'node': nodeid
}
self._constraints_x_nodes.append(constraint_x_node)

def add_card(self, card):
"""
Adds a card to this graph
Expand All @@ -260,6 +276,9 @@ def add_card(self, card):
card.component_id = cardobj.get('component_id', uuid.UUID('f05e4d3a-53c1-11e8-b0ea-784f435179ea'))
card.nodegroup_id = uuid.UUID(str(cardobj.get('nodegroup_id', '')))
card.nodegroup = self.get_or_create_nodegroup(nodegroupid=card.nodegroup_id)
constraints = cardobj.get('constraints', '')
for constraint in constraints:
self.add_card_contraint(constraint, card)

card.graph = self

Expand Down Expand Up @@ -340,6 +359,15 @@ def save(self, validate=True):
for card in self.cards.itervalues():
card.save()

for constraint in self._card_constraints:
constraint.save()

for constraint_x_node in self._constraints_x_nodes:
node_constraint = models.ConstraintXNode()
node_constraint.node = models.Node.objects.get(pk=constraint_x_node['node'])
node_constraint.constraint = constraint_x_node['constraint']
node_constraint.save()

for widget in self.widgets.itervalues():
widget.save()

Expand Down Expand Up @@ -1162,6 +1190,8 @@ def get_cards(self):
card.description = self.nodes[card.nodegroup_id].description
card_dict = JSONSerializer().serializeToPython(card)
card_dict['is_editable'] = is_editable
card_constraints = card.constraintmodel_set.all()
card_dict['constraints'] = JSONSerializer().serializeToPython(card_constraints)
cards.append(card_dict)

return cards
Expand Down
2 changes: 1 addition & 1 deletion arches/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
url(r'^rdm/concepts/(?P<conceptid>%s|())$' % uuid_regex, api.Concepts.as_view(), name='concepts'),
url(r'^plugins/(?P<pluginid>%s)$' % uuid_regex, PluginView.as_view(), name='plugins'),
url(r'^plugins/(?P<slug>[-\w]+)$', PluginView.as_view(), name='plugins'),
url(r'^cards/(?P<nodegroupid>%s|())$' % uuid_regex, api.Card.as_view(), name='card'),
url(r'^cards/(?P<nodegroupid>%s|())$' % uuid_regex, api.Card.as_view(), name='api_card'),

# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
Expand Down

0 comments on commit ef9a0f2

Please sign in to comment.