Changeset - 47eeeca43a84
[Not reviewed]
default
0 1 1
Branko Majic (branko) - 11 years ago 2013-06-23 18:48:21
branko@majic.rs
Added uniqueness constraint for communications. Added utility methods for fetching entity's incoming and outgoing communications.
2 files changed with 93 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conntrackt/migrations/0002_auto__add_unique_communication_source_destination_protocol_port.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8 -*-
 
import datetime
 
from south.db import db
 
from south.v2 import SchemaMigration
 
from django.db import models
 

	
 

	
 
class Migration(SchemaMigration):
 

	
 
    def forwards(self, orm):
 
        # Adding unique constraint on 'Communication', fields ['source', 'destination', 'protocol', 'port']
 
        db.create_unique(u'conntrackt_communication', ['source_id', 'destination_id', 'protocol', 'port'])
 

	
 

	
 
    def backwards(self, orm):
 
        # Removing unique constraint on 'Communication', fields ['source', 'destination', 'protocol', 'port']
 
        db.delete_unique(u'conntrackt_communication', ['source_id', 'destination_id', 'protocol', 'port'])
 

	
 

	
 
    models = {
 
        u'conntrackt.communication': {
 
            'Meta': {'unique_together': "(('source', 'destination', 'protocol', 'port'),)", 'object_name': 'Communication'},
 
            'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
 
            'destination': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'destination_set'", 'to': u"orm['conntrackt.Interface']"}),
 
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
 
            'port': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
 
            'protocol': ('django.db.models.fields.CharField', [], {'max_length': '10'}),
 
            'source': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'source_set'", 'to': u"orm['conntrackt.Interface']"})
 
        },
 
        u'conntrackt.entity': {
 
            'Meta': {'object_name': 'Entity'},
 
            'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
 
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
 
            'location': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['conntrackt.Location']"}),
 
            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
 
            'project': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['conntrackt.Project']"})
 
        },
 
        u'conntrackt.interface': {
 
            'Meta': {'object_name': 'Interface'},
 
            'address': ('django.db.models.fields.IPAddressField', [], {'max_length': '15'}),
 
            'description': ('django.db.models.fields.TextField', [], {'default': "'Main network interface.'", 'blank': 'True'}),
 
            'entity': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['conntrackt.Entity']"}),
 
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
 
            'name': ('django.db.models.fields.CharField', [], {'default': "'eth0'", 'max_length': '100'}),
 
            'netmask': ('django.db.models.fields.IPAddressField', [], {'default': "'255.255.255.255'", 'max_length': '15'})
 
        },
 
        u'conntrackt.location': {
 
            'Meta': {'object_name': 'Location'},
 
            'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
 
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
 
            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
 
        },
 
        u'conntrackt.project': {
 
            'Meta': {'object_name': 'Project'},
 
            'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
 
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
 
            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
 
        }
 
    }
 

	
 
    complete_apps = ['conntrackt']
 
\ No newline at end of file
conntrackt/models.py
Show inline comments
 
@@ -133,6 +133,34 @@ class Entity(models.Model):
 

	
 
        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.
 
        """
 

	
 
        communications = []
 

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

	
 
        return communications
 

	
 

	
 
class Interface(models.Model):
 
    """
 
@@ -218,6 +246,10 @@ class Communication(models.Model):
 
    port = models.IntegerField(default=0)
 
    description = models.TextField(blank=True)
 

	
 
    class Meta:
 
        # Enfoce uniqueness of communication.
 
        unique_together = ("source", "destination", "protocol", "port")
 

	
 
    def __unicode__(self):
 
        """
 
        Returns:
0 comments (0 inline, 0 general)