diff --git a/conntrackt/utils.py b/conntrackt/utils.py --- a/conntrackt/utils.py +++ b/conntrackt/utils.py @@ -29,10 +29,12 @@ import pydot # Django imports. from django.template import Context, loader +from django.utils.html import format_html +from django.utils.text import capfirst # Application imports. import iptables -from .models import Communication +import models def generate_entity_iptables(entity): @@ -187,7 +189,7 @@ def generate_project_diagram(project): graph.add_subgraph(cluster) # Get all project communications. - communications = Communication.objects.filter(source__entity__project=project) + communications = models.Communication.objects.filter(source__entity__project=project) # Add the edges (lines) representing communications, drawing them with same # colour as the source node/entity. @@ -201,3 +203,27 @@ def generate_project_diagram(project): graph.add_edge(edge) return graph + + +def list_formatter_callback(obj): + """ + Creates model object representation in format: + + MODEL_NAME: OBJECT_REPRESENTATION + + If passed object has a callable get_absolute_url method, the + instance representation will be surrouned by an HTML anchor + () where target is set to value of the get_absolute_url() + method call. + + Arguments: + obj - Model object whose representation should be returned. + + Returns: + String represenation of passed model object. + """ + + try: + return format_html('{0}: {2}', capfirst(obj._meta.verbose_name), obj.get_absolute_url(), str(obj)) + except AttributeError: + return format_html('{0}: {1}', capfirst(obj._meta.verbose_name), str(obj))