Skip to content

Commit

Permalink
Update wiring before remove widgets. Fix Wirecloud#248
Browse files Browse the repository at this point in the history
  • Loading branch information
jpajuelo committed Oct 27, 2016
1 parent 9068da3 commit c4e6de3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/wirecloud/platform/iwidget/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from django.utils.translation import ugettext as _

from wirecloud.commons.fields import JSONField
from wirecloud.platform.wiring.utils import remove_related_iwidget_connections
from wirecloud.platform.wiring.utils import remove_widget_from_wiring_status


@python_2_unicode_compatible
Expand Down Expand Up @@ -71,7 +71,7 @@ def save(self, *args, **kwargs):
def delete(self, *args, **kwargs):

# Delete IWidget from wiring
remove_related_iwidget_connections(self.tab.workspace.wiringStatus, self)
remove_widget_from_wiring_status("%s" % self.id, self.tab.workspace.wiringStatus)
self.tab.workspace.save() # This also invalidates the workspace cache

super(IWidget, self).delete(*args, **kwargs)
79 changes: 79 additions & 0 deletions src/wirecloud/platform/wiring/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,85 @@ def test_iwidget_removed(self):
workspace = Workspace.objects.get(id=self.workspace_id)
self.assertEqual(workspace.wiringStatus, self.empty_wiring)

def test_remove_widget_from_workspace_with_behaviours(self):

workspace = Workspace.objects.get(id=self.workspace_id)
workspace.wiringStatus = {
'connections': [
{
'source': {
'type': 'widget',
'id': '1',
'endpoint': 'event'
},
'target': {
'type': 'widget',
'id': '2',
'endpoint': 'slot'
},
},
],
'visualdescription': {
'components': {
'widget': {
'1': {},
'2': {}
}
},
'connections': [
{
'sourcename': "widget/1/event",
'targetname': "widget/2/slot"
}
],
'behaviours': [
{
'components': {
'widget': {
'1': {},
'2': {}
}
},
'connections': [
{
'sourcename': "widget/1/event",
'targetname': "widget/2/slot"
}
]
}
]
}
}
workspace.save()

client = Client()
client.login(username='test', password='test')
url = reverse('wirecloud.iwidget_entry', kwargs={'workspace_id': self.workspace_id, 'tab_id': 1, 'iwidget_id': 1})
client.delete(url)

workspace = Workspace.objects.get(id=self.workspace_id)
self.assertEqual(workspace.wiringStatus, {
'connections': [],
'visualdescription': {
'components': {
'widget': {
'2': {}
}
},
'connections': [],
'behaviours': [
{
'components': {
'widget': {
'2': {}
}
},
'connections': []
}
]
}
})


@patch('wirecloud.platform.core.plugins.get_version_hash', new=Mock(return_value='v1'))
@override_settings(DEBUG=False, FORCE_DOMAIN='example.com', FORCE_PROTO='http', WIRECLOUD_PLUGINS=())
Expand Down
42 changes: 22 additions & 20 deletions src/wirecloud/platform/wiring/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,40 @@
from wirecloud.platform.plugins import get_operator_api_extensions


def has_component(endpoint, component_id, component_type):
c_type, c_id, e_name = tuple(endpoint.split('/'))
def remove_widget_from_wiring_status(id, status):

return c_type == component_type and c_id == component_id
def has_model_widget(connection):

def check_endpoint(endpoint):
return endpoint['type'] == 'widget' and ("%s" % endpoint['id']) == id

def is_component(connection, endpoint_type, component_type, component_id):
return connection[endpoint_type]['type'] == component_type and connection[endpoint_type]['id'] == component_id
return check_endpoint(connection['source']) or check_endpoint(connection['target'])

def has_view_widget(connection):

def remove_related_iwidget_connections(wiring, iwidget):
def check_endpoint(endpoint):
c_type, c_id, e_name = tuple(endpoint.split('/'))
return c_type == 'widget' and c_id == id

removed_connections = []
return check_endpoint(connection['sourcename']) or check_endpoint(connection['targetname'])

for i, connection in enumerate(wiring['connections']):
if is_component(connection, 'source', 'widget', iwidget.id) or is_component(connection, 'target', 'widget', iwidget.id):
removed_connections.append(connection)
def remove_references(description, has_widget):

if 'visualdescription' in wiring:
if 'connections' in wiring['visualdescription']:
removed_visual_connections = []
if 'components' in description and id in description['components']['widget']:
del description['components']['widget'][id]

for connection in wiring['visualdescription']['connections']:
if has_component(connection['sourcename'], iwidget.id, 'widget') or has_component(connection['targetname'], iwidget.id, 'widget'):
removed_visual_connections.append(connection)
for connection in [c for c in description['connections'] if has_widget(c)]:
description['connections'].remove(connection)

for connection in removed_visual_connections:
wiring['visualdescription']['connections'].remove(connection)
remove_references(status, has_model_widget)

for connection in removed_connections:
wiring['connections'].remove(connection)
if 'visualdescription' in status:
remove_references(status['visualdescription'], has_view_widget)

for behaviour in status['visualdescription']['behaviours']:
remove_references(behaviour, has_view_widget)

return status

def get_operator_cache_key(operator, domain, mode):
return '_operator_xhtml/%s/%s/%s?mode=%s' % (operator.cache_version, domain, operator.id, mode)
Expand Down

0 comments on commit c4e6de3

Please sign in to comment.