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

Fixes issues with properly updating dirty state of node constraints #4726

Merged
merged 3 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
70 changes: 34 additions & 36 deletions arches/app/media/js/models/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ define([
this.disabled = ko.observable();
this.component_id = ko.observable();
this.constraints = ko.observableArray();
this.uniqueConstraints = ko.observableArray();

this.set('cards', this.cards);
this.set('nodes', this.nodes);
Expand Down Expand Up @@ -114,13 +113,31 @@ define([
this._card = ko.observable('{}');

this.dirty = ko.computed(function() {
return JSON.stringify(_.extend(JSON.parse(self._card()), self.toJSON())) !== self._card();
return JSON.stringify(
_.extend(
JSON.parse(self._card()),
self.toJSON())
) !== self._card();
});

this.isContainer = ko.computed(function() {
return !!self.get('cards')().length;
});

this.setConstraints = function(arr) {
var self = this;
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);
});
};

this.sourceData = attributes;
this.parse(attributes);
this.parseNodes.call(this, attributes);
Expand All @@ -145,37 +162,6 @@ define([
this.parseNodes(attributes);
}, this);

this.updateConstraints = function(){
var self = this;
return function(){
var updatedConstraints = self.uniqueConstraints().map(function(c){
return {
nodeIds: ko.unwrap(c.nodeIds),
uniquetoallinstances: ko.unwrap(c.uniqueToAllInstances),
constraintid: c.constraintid,
cardid: c.cardid
};
});
self.constraints(updatedConstraints);
self.set('constraints', updatedConstraints);
self.constraints.valueHasMutated();
};
};

this.setConstraints = function(arr) {
var self = this;
self.uniqueConstraints.removeAll();
arr.forEach(function(constraint){
constraint.widgets = self.widgets();
var constraintViewModel = new CardConstraintsViewModel(constraint);
constraintViewModel.nodeIds.subscribe(self.updateConstraints());
constraintViewModel.uniqueToAllInstances.subscribe(self.updateConstraints());
self.uniqueConstraints.push(constraintViewModel);
});
};

this.setConstraints(this.constraints());

this.disposables.push(componentIdSubscription);
this.disposables.push(cardSubscription);
this.disposables.push(widgetSubscription);
Expand All @@ -198,6 +184,8 @@ define([
*/
parse: function(attributes) {
var self = this;
// console.log(attributes.data.constraints[0].nodes)
// console.log(attributes.data.constraints[0].nodes)
this._attributes = attributes;

_.each(attributes.data, function(value, key) {
Expand Down Expand Up @@ -238,7 +226,13 @@ define([
this.get(key)(value);
break;
case 'constraints':
this.setConstraints ? this.setConstraints(value) : this.get(key)(value);
if (this.constraints().length === 0) {
this.setConstraints(value);
} else {
this.constraints().forEach(function(constraint, i){
constraint.update(value[i]);
});
}
break;
case 'name':
case 'nodegroup_id':
Expand Down Expand Up @@ -320,7 +314,6 @@ define([
widget.label(originalWidgetData.label);
widget.widget_id(originalWidgetData.widget_id);
}
// this.setConstraints(this._attributes.data.constraints);
}, this);
this.parse(this._attributes);
},
Expand All @@ -333,7 +326,12 @@ define([
key !== 'widgets' && key !== 'datatypes' && key !== 'data' && key !== 'helpactive' &&
key !== 'config') {
if (ko.isObservable(this.attributes[key])) {
if (key === 'cards') {
if (key === 'constraints') {
ret[key] = [];
this.attributes[key]().forEach(function(constraint) {
ret[key].push(constraint.toJSON());
}, this);
} else if (key === 'cards') {
ret[key] = [];
this.attributes[key]().forEach(function(card) {
ret[key].push(card.toJSON());
Expand Down
19 changes: 12 additions & 7 deletions arches/app/media/js/viewmodels/card-constraints.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
define([
'knockout',
], function(ko) {
'knockout-mapping'
], function(ko, koMapping) {
var ConstraintViewModel = function(params) {

this.uniqueToAllInstances = ko.observable(params.uniquetoallinstances || false);
this.widgets = params.widgets || [];
this.nodeIds = ko.observableArray(params.nodes);
this.cardid = params.card_id;
this.constraintid = params.constraintid;
this.constraint = params.constraint;
this.getSelect2ConstraintConfig = function(placeholder){
var nodeOptions = this.widgets.map(function(c){return {text: c.label(), id: c.node.nodeid};});
return {
clickBubble: true,
disabled: false,
data: {results: nodeOptions},
value: this.nodeIds,
value: this.constraint.nodes,
multiple: params.multiple || true,
closeOnSelect: false,
placeholder: placeholder || "Select Widgets",
allowClear: true
};
};
this.update = function(val){
this.constraint.nodes(val.nodes);
this.constraint.constraintid(val.constraintid);
this.constraint.uniquetoallinstances(val.uniquetoallinstances);
};
this.toJSON = function(){
return koMapping.toJS(this.constraint);
};

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ define([
if (cardConstraints.length === 0) {
cardConstraints.push({
uniquetoallinstances: false,
nodeIds:[],
nodes:[],
cardid: undefined,
constraintid: undefined
});
Expand Down
2 changes: 1 addition & 1 deletion arches/app/models/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def add_nodeconstraints(nodeids, constraint_model):
for constraint in constraints:
constraintid = constraint.get('constraintid', None)
unique_to_all = constraint.get('uniquetoallinstances', False)
nodeids = constraint.get('nodeIds', [])
nodeids = constraint.get('nodes', [])
if constraintid is None:
constraint_model = models.ConstraintModel()
constraint_model.card = self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@
</div>

<div class="form-divider">
<!--ko if: model.uniqueConstraints().length > 0 -->
<div class="constraint-selection" data-bind="foreach: {data: model.uniqueConstraints(), as: 'constraint'}">
<!--ko if: model.constraints().length > 0 -->
<div class="constraint-selection" data-bind="foreach: {data: model.constraints(), as: 'constraintvm'}">
<div class="dropdown">
<input class="" style="display:inline-block;"
data-bind="select2Query: {
select2Config: getSelect2ConstraintConfig('{% trans "Select Nodes" %}')
}">
</div>
<div data-bind="component: { name: 'views/components/simple-switch', params: {value: constraint.uniqueToAllInstances, config:{label: 'Unique to all resource instances?'}}}"></div>
<div data-bind="component: { name: 'views/components/simple-switch', params: {value: constraintvm.constraint.uniquetoallinstances, config:{label: 'Unique to all resource instances?'}}}"></div>
</div>
<!--/ko-->
</div>
Expand Down
1 change: 0 additions & 1 deletion arches/app/views/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,6 @@ def post(self, request, cardid=None):
card = models.CardModel.objects.get(pk=card_data['id'])
card.sortorder = card_data['sortorder']
card.save()

return JSONResponse(data['cards'])

return HttpResponseNotFound()
Expand Down
38 changes: 20 additions & 18 deletions arches/management/commands/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ def load_apps(package_dir):
try:
app_name = os.path.basename(app)
management.call_command('startapp', '--template', app, app_name)
management.call_command('makemigrations', app_name, interactive=False)
management.call_command('migrate', new_name, interactive=False)
except CommandError as e:
print e

Expand Down Expand Up @@ -627,37 +629,39 @@ def handle_source(source):
if setup_db.lower() in ('t', 'true', 'y', 'yes'):
self.setup_db(settings.PACKAGE_NAME)

print 'loading package_settings.py'
print('loading package_settings.py')
load_package_settings(package_location)
print 'loading preliminary sql'
print('loading preliminary sql')
load_preliminary_sql(package_location)
print 'loading system settings'
print('loading system settings')
load_system_settings(package_location)
print 'loading widgets'
print('loading widgets')
load_widgets(package_location)
print 'loading card components'
print('loading card components')
load_card_components(package_location)
print 'loading plugins'
print('loading plugins')
load_plugins(package_location)
print 'loading reports'
print('loading reports')
load_reports(package_location)
print 'loading functions'
print('loading functions')
load_functions(package_location)
print 'loading datatypes'
print('loading datatypes')
load_datatypes(package_location)
print 'loading concepts'
print('loading concepts')
load_concepts(package_location, overwrite_concepts, stage_concepts)
print 'loading resource models and branches'
print('loading resource models and branches')
load_graphs(package_location)
print 'loading resource to resource constraints'
print('loading resource to resource constraints')
load_resource_to_resource_constraints(package_location)
print 'loading map layers'
print('loading map layers')
load_map_layers(package_location)
print 'loading business data - resource instances and relationships'
print('loading business data - resource instances and relationships')
load_business_data(package_location)
print 'loading resource views'
print('loading resource views')
load_resource_views(package_location)
print 'loading package css'
print('loading package css')
print('loading apps')
load_apps(package_location)
root = settings.APP_ROOT if settings.APP_ROOT is not None else os.path.join(settings.ROOT_DIR, 'app')
css_source = os.path.join(package_location, 'extensions', 'css')
if os.path.exists(css_source):
Expand All @@ -667,8 +671,6 @@ def handle_source(source):
css_files = glob.glob(os.path.join(css_source, '*.css'))
for css_file in css_files:
shutil.copy(css_file, css_dest)
load_apps(package_location)


def update_project_templates(self):
"""
Expand Down