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 e4cc553
Show file tree
Hide file tree
Showing 3 changed files with 41 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_component


@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_component('widget', "%s" % self.id, self.tab.workspace.wiringStatus)
self.tab.workspace.save() # This also invalidates the workspace cache

super(IWidget, self).delete(*args, **kwargs)
14 changes: 14 additions & 0 deletions src/wirecloud/platform/tests/selenium_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ def test_remove_widget_from_workspace(self):
self.login(username="user_with_workspaces", next="/user_with_workspaces/Workspace")
self.find_widget(title="Test 1").remove()

def test_remove_widget_from_workspace_with_behaviours(self):
self.login(username="user_with_workspaces", next="/user_with_workspaces/WorkspaceBehaviours")

widget = self.find_widget(title="Test 1")
widget_id = widget.id
widget.remove()

self.reload()
self.wait_wirecloud_ready()

with self.wiring_view as wiring:
self.assertIsNone(wiring.find_draggable_component('widget', id=widget_id))
self.assertEqual(len(wiring.find_connections()), 1)

def test_remove_tab_from_workspace(self):
self.login(username='user_with_workspaces', next='/user_with_workspaces/Pending Events')
self.find_tab(title="Tab 1").remove()
Expand Down
45 changes: 25 additions & 20 deletions src/wirecloud/platform/wiring/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,43 @@
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_component(component_type, component_id, wiring_status):

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

def check_endpoint(endpoint):
return endpoint['type'] == component_type and endpoint['id'] == component_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_component(connection):

def remove_related_iwidget_connections(wiring, iwidget):
def check_endpoint(endpoint):
c_type, c_id, e_name = tuple(endpoint.split('/'))
return c_type == component_type and c_id == component_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_from_description(description, has_component):

if 'visualdescription' in wiring:
if 'connections' in wiring['visualdescription']:
removed_visual_connections = []
if 'components' in description and component_id in description['components'][component_type]:
del description['components'][component_type][component_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_component(c)]:
description['connections'].remove(connection)

for connection in removed_visual_connections:
wiring['visualdescription']['connections'].remove(connection)
if component_type == 'operator' and component_id in description['operators']:
del description['operators'][component_id]

for connection in removed_connections:
wiring['connections'].remove(connection)
remove_from_description(wiring_status, has_model_component)

if 'visualdescription' in wiring_status:
remove_from_description(wiring_status['visualdescription'], has_view_component)

for behaviour in wiring_status['visualdescription']['behaviours']:
remove_from_description(behaviour, has_view_component)

return wiring_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 e4cc553

Please sign in to comment.