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
 

	
 

	
 
@@ -15,7 +16,7 @@ class Project(models.Model):
 
      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:
 
@@ -29,6 +30,13 @@ class Project(models.Model):
 

	
 
        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):
 
    """
 
@@ -54,7 +62,7 @@ class Location(models.Model):
 
      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):
 
@@ -92,15 +100,11 @@ class Entity(models.Model):
 
    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):
 
        """
 
@@ -139,6 +143,13 @@ class Entity(models.Model):
 

	
 
        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):
 
    """
 
@@ -166,6 +177,10 @@ class Interface(models.Model):
 
    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:
 
@@ -225,7 +240,7 @@ class Communication(models.Model):
 
    description = models.TextField(blank=True)
 

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

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