|
@@ -24,13 +24,13 @@ from django.contrib.admin.util import Ne
|
|
|
from django.core.exceptions import ValidationError
|
|
|
from django.core.urlresolvers import reverse
|
|
|
from django.db import models
|
|
|
from django.db.models.query_utils import Q
|
|
|
|
|
|
# Application imports.
|
|
|
from .utils import list_formatter_callback
|
|
|
from .utils import list_formatter_callback, get_distinct_colors
|
|
|
|
|
|
|
|
|
class SearchManager(models.Manager):
|
|
|
"""
|
|
|
Custom model manager that implements search for model instances that contain
|
|
|
a specific string (search term) in fields "name" or "description".
|
|
@@ -142,12 +142,56 @@ class Project(RelatedCollectorMixin, mod
|
|
|
"""
|
|
|
Return absolute URL for viewing a single project.
|
|
|
"""
|
|
|
|
|
|
return reverse("project", kwargs={'pk': self.pk})
|
|
|
|
|
|
def get_project_communications_summary(self):
|
|
|
"""
|
|
|
Creates a list of dictionaries where each dictionary provides summary of
|
|
|
a communication. Each dictionary will contain the following keys:
|
|
|
|
|
|
- source - Source of the communication.
|
|
|
- source_color - Color that is associated with the source of
|
|
|
communication. The color will match with color used in the project
|
|
|
communications diagram.
|
|
|
- destination - Destination of the communication.
|
|
|
- destination_color - Color that is associated with the destination of
|
|
|
communication. The color will match with color used in the project
|
|
|
communications diagram.
|
|
|
- protocol - Protocol used for communication.
|
|
|
- port - Port used for communication.
|
|
|
|
|
|
Returns:
|
|
|
List of dictionaries, where each dictionary describes a single communication.
|
|
|
"""
|
|
|
|
|
|
# Obtain the list of colors.
|
|
|
colors = get_distinct_colors(self.entity_set.count())
|
|
|
|
|
|
# Fetch ID's of each end entity.
|
|
|
entity_ids = self.entity_set.values_list("pk", flat=True).order_by("pk")
|
|
|
|
|
|
# Map the entity ID's to generated colors.
|
|
|
entity_colors = dict(zip(entity_ids, colors))
|
|
|
|
|
|
# Set-up an empty list where the resulting data will be stored.
|
|
|
communications = []
|
|
|
|
|
|
# Process each communication, and add the information to result.
|
|
|
for communication in Communication.objects.filter(source__entity__project=self).select_related().order_by("source__entity__pk"):
|
|
|
communications.append({"source": communication.source.entity.name,
|
|
|
"source_color": entity_colors[communication.source.entity.pk],
|
|
|
"destination": communication.destination.entity.name,
|
|
|
"destination_color": entity_colors[communication.destination.entity.pk],
|
|
|
"protocol": communication.protocol,
|
|
|
"port": communication.port,})
|
|
|
|
|
|
# Finally return the result.
|
|
|
return communications
|
|
|
|
|
|
|
|
|
class Location(RelatedCollectorMixin, models.Model):
|
|
|
"""
|
|
|
Implements a model with information about location. Locations can further be
|
|
|
assigned to entities, letting the user group different servers and equipment
|
|
|
based on location.
|