Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevents extra constraint records from getting saved to the database #4778

Merged
merged 3 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon. (semi)

};

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