Skip to content

Commit

Permalink
Prevents extra constraint records from getting saved to the database.…
Browse files Browse the repository at this point in the history
… re #4375
  • Loading branch information
chiatt committed Apr 16, 2019
1 parent 9dd2dbc commit bfc17c4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 48 deletions.
21 changes: 11 additions & 10 deletions arches/app/media/js/models/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ define([
this.set('component_id', this.component_id);
this.set('config', {});
this.set('constraints', this.constraints);

this.cardComponentLookup = cardComponentLookup;
this.configKeys = ko.observableArray();
this.disposables = [];
Expand Down Expand Up @@ -126,16 +125,18 @@ define([

this.setConstraints = function(arr) {
var self = this;
arr.forEach(function(constraint){
var constraintViewModel = new CardConstraintsViewModel({
constraint: koMapping.fromJS(constraint),
widgets: self.widgets()
if (arr) {
arr.forEach(function(constraint){
var constraintViewModel = new CardConstraintsViewModel({
constraint: koMapping.fromJS(constraint),
widgets: self.widgets()
});
constraintViewModel.constraint.nodes.subscribe(function(){
self.toJSON();
}, self);
self.constraints.push(constraintViewModel);
});
constraintViewModel.constraint.nodes.subscribe(function(){
self.toJSON();
}, self);
self.constraints.push(constraintViewModel);
});
}
};

this.sourceData = attributes;
Expand Down
13 changes: 11 additions & 2 deletions arches/app/media/js/viewmodels/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ define([
'models/card-widget',
'arches',
'require',
'uuid',
'utils/dispose',
'viewmodels/tile'
], function($, _, ko, koMapping, CardModel, CardWidgetModel, arches, require, dispose) {
], function($, _, ko, koMapping, CardModel, CardWidgetModel, arches, require, uuid, dispose) {
/**
* A viewmodel used for generic cards
*
Expand Down Expand Up @@ -71,6 +72,12 @@ define([
var multiselect = params.multiselect || false;
var isWritable = params.card.is_writable || false;
var selection;
var emptyConstraint = [{
uniquetoallinstances: false,
nodes:[],
cardid: self.cardid,
constraintid: uuid.generate()
}];
if (params.multiselect) {
selection = params.selection || ko.observableArray([]);
} else {
Expand All @@ -83,7 +90,8 @@ define([
data: _.extend(params.card, {
widgets: params.cardwidgets,
nodes: params.graphModel.get('nodes'),
nodegroup: nodegroup
nodegroup: nodegroup,
constraints: params.constraints
}),
datatypelookup: params.graphModel.get('datatypelookup'),
});
Expand Down Expand Up @@ -134,6 +142,7 @@ define([
parentCard: params.parentCard,
expanded: ko.observable(false),
perms: perms,
constraints: params.constraints || emptyConstraint,
permsLiteral: permsLiteral,
scrollTo: ko.pureComputed(function() {
return scrollTo() === this;
Expand Down
38 changes: 19 additions & 19 deletions arches/app/media/js/views/graph/graph-designer/card-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ define([
'viewmodels/card',
'models/card-widget',
'arches',
'uuid',
'graph-designer-data',
'bindings/sortable',
'bindings/scrollTo',
'widgets',
'card-components'
], function($, _, ko, CardViewModel, CardWidgetModel, arches, data) {
], function($, _, ko, CardViewModel, CardWidgetModel, arches, uuid, data) {
var CardTreeViewModel = function(params) {
var self = this;
var filter = ko.observable('');
Expand All @@ -26,23 +27,16 @@ define([
var scrollTo = ko.observable();
var cachedFlatTree;
var cardList = data.cards;
data.cards.forEach(function(card){
var cardConstraints = [];
data.constraints.forEach(function(constraint){
if (card.cardid === constraint.card_id) {
cardConstraints.push(constraint);
}
});
if (cardConstraints.length === 0) {
cardConstraints.push({
uniquetoallinstances: false,
nodes:[],
cardid: undefined,
constraintid: undefined
});
}
card.constraints = cardConstraints;
});

var getBlankConstraint = function(card){
return [{
uniquetoallinstances: false,
nodes: [],
cardid: card.cardid,
constraintid: uuid.generate()
}]
};

this.flattenTree = function(parents, flatList) {
_.each(ko.unwrap(parents), function(parent) {
flatList.push(parent);
Expand Down Expand Up @@ -170,6 +164,10 @@ define([
});
return !nodegroup || !ko.unwrap(nodegroup.parentnodegroup_id);
}).map(function(card) {
var constraints = data.constraints.filter(function(ct){return ct.card_id === card.cardid;});
if (constraints.length === 0) {
constraints = getBlankConstraint(card);
}
return new CardViewModel({
card: card,
graphModel: params.graphModel,
Expand All @@ -178,6 +176,7 @@ define([
displayname: ko.observable(),
handlers: {},
cards: data.cards,
constraints: constraints,
tiles: [],
selection: selection,
hover: hover,
Expand Down Expand Up @@ -317,7 +316,8 @@ define([
userisreviewer: true,
perms: ko.observableArray(),
permsLiteral: ko.observableArray(),
parentCard: parent
parentCard: parent,
constraints: getBlankConstraint(data.card)
});
parentcards.push(newCardViewModel);

Expand Down
32 changes: 15 additions & 17 deletions arches/app/models/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,27 @@ def add_nodeconstraints(nodeids, constraint_model):
constraintid = constraint.get('constraintid', None)
unique_to_all = constraint.get('uniquetoallinstances', False)
nodeids = constraint.get('nodes', [])
if constraintid is None:
try:
constraint_model = models.ConstraintModel.objects.get(pk=constraintid)
constraint_model.uniquetoallinstances = unique_to_all
current_nodeids = {str(i.nodeid) for i in constraint_model.nodes.all()}
future_nodeids = set(nodeids)
nodes_to_remove = current_nodeids - future_nodeids
nodes_to_add = future_nodeids - current_nodeids
add_nodeconstraints(nodes_to_add, constraint_model)
models.ConstraintXNode.objects.filter(
Q(constraint=constraint_model) &
Q(node__in=nodes_to_remove)
).delete()
constraint_model.save()
except ObjectDoesNotExist as e:
constraint_model = models.ConstraintModel()
constraint_model.card = self
constraint_model.constraintid = constraintid
constraint_model.uniquetoallinstances = unique_to_all
constraint_model.save()
add_nodeconstraints(nodeids, constraint_model)
constraint_model.save()
else:
try:
constraint_model = models.ConstraintModel.objects.get(pk=constraintid)
constraint_model.uniquetoallinstances = unique_to_all
current_nodeids = {str(i.nodeid) for i in constraint_model.nodes.all()}
future_nodeids = set(nodeids)
nodes_to_remove = current_nodeids - future_nodeids
nodes_to_add = future_nodeids - current_nodeids
add_nodeconstraints(nodes_to_add, constraint_model)
models.ConstraintXNode.objects.filter(
Q(constraint=constraint_model) &
Q(node__in=nodes_to_remove)
).delete()
constraint_model.save()
except ObjectDoesNotExist as e:
print e
constraint_models.append(constraint_model)
self.constraints = constraint_models

Expand Down

0 comments on commit bfc17c4

Please sign in to comment.