Changeset - 9a38c587bd6d
[Not reviewed]
default
0 1 1
Branko Majic (branko) - 11 years ago 2013-07-03 21:19:31
branko@majic.rs
Added a number of name-related unique constraints. Added get_absolute_url methods for relevant models.
2 files changed with 105 insertions and 11 deletions:
0 comments (0 inline, 0 general)
conntrackt/migrations/0003_auto__add_unique_entity_project_name__add_unique_location_name__add_un.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 'Entity', fields ['project', 'name']
 
        db.create_unique(u'conntrackt_entity', ['project_id', 'name'])
 

	
 
        # Adding unique constraint on 'Location', fields ['name']
 
        db.create_unique(u'conntrackt_location', ['name'])
 

	
 
        # Adding unique constraint on 'Interface', fields ['name', 'entity']
 
        db.create_unique(u'conntrackt_interface', ['name', 'entity_id'])
 

	
 
        # Adding unique constraint on 'Project', fields ['name']
 
        db.create_unique(u'conntrackt_project', ['name'])
 

	
 

	
 
    def backwards(self, orm):
 
        # Removing unique constraint on 'Project', fields ['name']
 
        db.delete_unique(u'conntrackt_project', ['name'])
 

	
 
        # Removing unique constraint on 'Interface', fields ['name', 'entity']
 
        db.delete_unique(u'conntrackt_interface', ['name', 'entity_id'])
 

	
 
        # Removing unique constraint on 'Location', fields ['name']
 
        db.delete_unique(u'conntrackt_location', ['name'])
 

	
 
        # Removing unique constraint on 'Entity', fields ['project', 'name']
 
        db.delete_unique(u'conntrackt_entity', ['project_id', 'name'])
 

	
 

	
 
    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': {'unique_together': "(('name', 'project'),)", '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': {'unique_together': "(('name', 'entity'),)", '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', [], {'unique': 'True', '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', [], {'unique': 'True', 'max_length': '100'})
 
        }
 
    }
 

	
 
    complete_apps = ['conntrackt']
 
\ No newline at end of file
conntrackt/models.py
Show inline comments
 
# Django imports.
 
from django.core.exceptions import ValidationError
 
from django.core.urlresolvers import reverse
 
from django.db import models
 

	
 

	
 
class Project(models.Model):
 
    """
 
    Implements a model with information about a project. A project has some
 
@@ -12,13 +13,13 @@ class Project(models.Model):
 
    Fields:
 

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

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

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

	
 
    def __unicode__(self):
 
@@ -26,12 +27,19 @@ class Project(models.Model):
 
        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})
 

	
 

	
 
class Location(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.
 
@@ -51,13 +59,13 @@ class Location(models.Model):
 
    Fields:
 

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

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

	
 
    def __unicode__(self):
 
        """
 
        Returns:
 
          String representation of a location.
 
@@ -89,21 +97,17 @@ class Entity(models.Model):
 
    name = models.CharField(max_length=100)
 
    description = models.TextField(blank=True)
 
    project = models.ForeignKey(Project)
 
    location = models.ForeignKey(Location)
 

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

	
 
        Properties:
 
          verbose_name_plural - Changes the way Django Admin displays the model
 
          name in plural.
 
        """
 

	
 
        verbose_name_plural = 'entities'
 

	
 
    def __unicode__(self):
 
        """
 
        Returns:
 
          String representation of an entity. This identifier contains name of
 
          entity, its project name, and location name.
 
@@ -136,12 +140,19 @@ class Entity(models.Model):
 
        for interface in self.interface_set.all():
 
            for communication in interface.source_set.all():
 
                communications.append(communication)
 

	
 
        return communications
 

	
 
    def get_absolute_url(self):
 
        """
 
        Return absolute URL for viewing a single entity.
 
        """
 

	
 
        return reverse("entity", kwargs={'pk': self.pk})
 

	
 

	
 
class Interface(models.Model):
 
    """
 
    Models a representation of an interface on an entity. It can be used for
 
    representing the subnets as well.
 

	
 
@@ -163,12 +174,16 @@ class Interface(models.Model):
 
    name = models.CharField(max_length=100, default='eth0')
 
    description = models.TextField(blank=True, default='Main network interface.')
 
    entity = models.ForeignKey(Entity)
 
    address = models.IPAddressField()
 
    netmask = models.IPAddressField(default='255.255.255.255')
 

	
 
    class Meta:
 
        # Enforce uniqueness of interface name in an entity.
 
        unique_together = ("name", "entity")
 

	
 
    def __unicode__(self):
 
        """
 
        Returns:
 
          String representation of an interface. In case of single IP this will
 
          simply be the interface name and IP address. In case of subnet it will
 
          include the netmask as well.
 
@@ -222,13 +237,13 @@ class Communication(models.Model):
 
    destination = models.ForeignKey(Interface, related_name='destination_set')
 
    protocol = models.CharField(max_length=10, choices=PROTOCOL_CHOICES)
 
    port = models.IntegerField(default=0)
 
    description = models.TextField(blank=True)
 

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

	
 
    def __unicode__(self):
 
        """
 
        Returns:
 
          String representation of an interface. This involves showing the
0 comments (0 inline, 0 general)