Skip to content

Commit

Permalink
Merge pull request #3283 from szymi-/cluster-relations
Browse files Browse the repository at this point in the history
display related clusters
  • Loading branch information
szymi- committed Jul 25, 2018
2 parents 6731489 + cabf852 commit 446fa7d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ <h2>{% trans "Physical hosts" %}</h2>
{% endfor %}
</table>
{% endif %}
{% if rel_obj_type == "clusters" %}
<h2>{% trans "Clusters" %}</h2>
<table>
<tr>
<th>{% trans "Name" %}</th>
<th>{% trans "Type" %}</th>
</tr>
{% for cluster in objects %}
<tr>
<td><a href="{{ cluster.get_absolute_url }}">{{ cluster.name }}</a></td>
<td>{{ cluster.type }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
{% endfor %}

{% else %}
Expand Down
14 changes: 13 additions & 1 deletion src/ralph/data_center/tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.core.urlresolvers import reverse
from django.test import TestCase

from ralph.data_center.models import DataCenterAsset
from ralph.data_center.models import BaseObjectCluster, Cluster, DataCenterAsset
from ralph.data_center.tests.factories import (
ClusterFactory,
DataCenterAssetFactory,
Expand Down Expand Up @@ -319,3 +319,15 @@ def test_should_add_physical_hosts_to_dictionary(self):
self.assertEqual(4, len(related_objects['physical_hosts']))
self.assertEqual(1, len(content_type))
self.assertEqual(physical_server, content_type.pop())

def test_should_add_clusters_to_dictionary(self):
cluster = ContentType.objects.get_for_model(Cluster)
self.view.object.clusters.add(
BaseObjectCluster(cluster=ClusterFactory())
)
related_objects = {}
self.view._add_clusters(related_objects)
content_type = related_objects['clusters'][0].content_type

self.assertEqual(1, len(related_objects['clusters']))
self.assertEqual(cluster, content_type)
8 changes: 8 additions & 0 deletions src/ralph/data_center/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ def _add_physical_hosts(self, related_objects):
if physical_hosts:
related_objects['physical_hosts'] = physical_hosts

def _add_clusters(self, related_objects):
clusters = [base_object.cluster
for base_object in list(self.object.clusters.all())]

if clusters:
related_objects['clusters'] = clusters

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

related_objects = {}
self._add_cloud_hosts(related_objects)
self._add_virtual_hosts(related_objects)
self._add_physical_hosts(related_objects)
self._add_clusters(related_objects)
context['related_objects'] = related_objects

return context

0 comments on commit 446fa7d

Please sign in to comment.