Changeset - 353630bdfef6
[Not reviewed]
default
0 1 0
Branko Majic (branko) - 12 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
 
# -*- coding: utf-8 -*-
 
#
 
# Copyright (C) 2013 Branko Majic
 
#
 
# This file is part of Django Conntrackt.
 
#
 
# Django Conntrackt is free software: you can redistribute it and/or modify it
 
# under the terms of the GNU General Public License as published by the Free
 
# Software Foundation, either version 3 of the License, or (at your option) any
 
# later version.
 
#
 
# Django Conntrackt is distributed in the hope that it will be useful, but
 
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
# details.
 
#
 
# You should have received a copy of the GNU General Public License along with
 
# 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.
 

	
 
        Arguments:
 
          search_term - String to search the name and description for.
 

	
 
        Returns:
 
          Query set with model instances that matched the search.
 
        """
 

	
 
        return self.filter(Q(name__icontains=search_term) | Q(description__icontains=search_term))
 

	
 

	
 
class RelatedCollectorMixin(object):
 
    """
 
    Implements model mixin for easily obtainning related items of a model
 
    instance.
 

	
 
    The mixin can be used for obtaining all model objects that are directly or
 
    indirectly linked to the calling model object (through foreign key
 
    relationships).
 

	
 
    This mixin is very useful in delete views for warning the user about all of
 
    the related items that will be deleted if the calling item is deleted as
 
    well.
 
    """
 

	
 
    def get_dependant_objects(self):
 
        """
 
        Creates a list of model objects that depend (reference), both directly
 
        and indirectly, on calling model object. The calling model object is
 
        included as the first element of the list. This method call can be used
 
        in order to obtain a list of model objects that would get deleted in
 
        case the calling model object gets deleted.
 

	
 
        Returns:
 
          Nested list of model objects that depend (reference) calling model
 
          object.
 
        """
 

	
 
        collector = NestedObjects(using='default')
 

	
 
        collector.collect([self])
 

	
 
        return collector.nested()
 

	
 
    def get_dependant_objects_representation(self):
 
        """
 
        Creates a nested list of object representations that depend (reference),
 
        both directly and indirectly, calling model object. This method call can
 
        be used in order to obtain a list of string representations of model
 
        objects that would get deleted in case the calling model object gets
 
        deleted.
 

	
 
        The resulting nested list can be shown to the user for
 
        warning/notification purposes using the unordered_list template tag.
 

	
 
        Each non-list element will be a string generated using the
 
        conntrackt.utils.list_formatter_callback function.
 

	
 
        Returns:
 
          Nested list of representations of model objects that depend
 
          (reference) calling model object.
 
        """
 

	
 
        collector = NestedObjects(using='default')
 

	
 
        collector.collect([self])
 

	
 
        return collector.nested(list_formatter_callback)
 

	
 

	
 
class Project(RelatedCollectorMixin, models.Model):
 
    """
 
    Implements a model with information about a project. A project has some
 
    basic settings, and mainly serves the purpose of grouping entities for
 
    easier handling and administration.
 

	
 
    Fields:
 

	
 
      name - String denoting the project name.
 
      description - Free-form description of the project.
 
    """
 

	
 
    name = models.CharField(max_length=100, unique=True)
 
    description = models.TextField(blank=True)
 
    objects = SearchManager()
 
    deletion_collect_models = ["Entity", "Interface"]
 

	
 
    class Meta:
 
        permissions = (("view", "Can view information"),)
 

	
 
    def __unicode__(self):
 
        """
 
        Returns:
 
          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:
 

	
 
      - Main site
 
      - Backup site
 
      - Disaster recovery site
 
      - Belgrade
 
      - Stockholm
 

	
 
    Fields:
 

	
 
      name - String denoting the location name.
 
      description - Free-form description of a location.
 
    """
 

	
 
    name = models.CharField(max_length=100, unique=True)
 
    description = models.TextField(blank=True)
 

	
 
    def __unicode__(self):
 
        """
 
        Returns:
 
          String representation of a location.
 
        """
 

	
 
        return self.name
 

	
 

	
 
class Entity(RelatedCollectorMixin, models.Model):
 
    """
 
    Models an entity in a project. An entity can be a server, router, or any
 
    other piece of networking equipment that has its own IP address.
 

	
 
    Entities can also be used for representing subnets etc. This is useful when
 
    the communication restrictions need to be applied across a subnet.
 

	
 
    Entities are tied to specific projects and locations.
 

	
 
    Fields:
 

	
 
      name - String denoting the entity name.
 
      description - Free-form description of an entity.
 
      project - Foreign key pointing to the project to which the entity
 
      belongs.
 
      location - Foreign key pointing to the location at which the entity is
 
      located.
 
    """
 

	
 
    name = models.CharField(max_length=100)
 
    description = models.TextField(blank=True)
 
    project = models.ForeignKey(Project)
 
    location = models.ForeignKey(Location)
 
    objects = SearchManager()
 

	
 
    class Meta:
 
        # Fix the plural form used by Django.
 
        verbose_name_plural = 'entities'
 
        # Enforce uniqueness of entity name in a project.
 
        unique_together = ("name", "project")
 

	
 
    def __unicode__(self):
 
        """
 
        Returns:
 
          String representation of an entity. This identifier contains name of
 
          entity, its project name, and location name.
 
        """
 

	
 
        return "%s (%s - %s)" % (self.name, self.project, self.location)
 

	
 
    def incoming_communications(self):
 
        """
 
        Returns:
 
          List of incoming communications for an entity.
 
        """
 

	
 
        communications = []
 

	
 
        for interface in self.interface_set.all():
 
            for communication in interface.destination_set.all():
 
                communications.append(communication)
 

	
 
        return communications
 

	
 
    def outgoing_communications(self):
 
        """
 
        Returns:
 
          List of outgoing communications for an entity.
0 comments (0 inline, 0 general)