Changeset - 353630bdfef6
[Not reviewed]
default
0 1 0
Branko Majic (branko) - 10 years ago 2013-11-09 13:50:30
branko@majic.rs
CONNT-17: Added helper method for the Project model for obtaining a summary of communcations that can be then easily output in a table.
1 file changed with 45 insertions and 1 deletions:
0 comments (0 inline, 0 general)
conntrackt/models.py
Show inline comments
 
@@ -18,25 +18,25 @@
 
# Django Conntrackt.  If not, see <http://www.gnu.org/licenses/>.
 
#
 

	
 

	
 
# Django imports.
 
from django.contrib.admin.util import NestedObjects
 
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".
 
    """
 

	
 
    def search(self, search_term):
 
        """
 
        Performs a search for model instances that contain the provided search
 
        term in fields "name" or "description". The search is case-insensitive.
 
@@ -136,24 +136,68 @@ class Project(RelatedCollectorMixin, mod
 
          String representation of a project.
 
        """
 

	
 
        return self.name
 

	
 
    def get_absolute_url(self):
 
        """
 
        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.
 

	
 
    Locations are not tied to specific project, and they do not have to be
 
    actual physical locations. Such generic locations are therefore reusable
 
    accross multiple projects.
 

	
 
    For example, locations can be:
0 comments (0 inline, 0 general)