Changeset - f397b9db8183
[Not reviewed]
default
0 6 0
Branko Majic (branko) - 11 years ago 2013-04-14 11:37:14
branko@majic.rs
Styling updates to reflect 2SoD recommendations.
6 files changed with 49 insertions and 44 deletions:
0 comments (0 inline, 0 general)
conntrackt/admin.py
Show inline comments
 
# Import all the models from the application.
 
from conntrackt.models import *
 

	
 
# Import the administrator application.
 
# Django imports.
 
from django.contrib import admin
 
from django.core.urlresolvers import resolve
 

	
 
# Import resolver used for figuring-out the view being called in custom query
 
# calls.
 
from django.core.urlresolvers import resolve
 
# Application imports.
 
from .models import Project, Location, Entity, Interface, Communication
 

	
 

	
 
class InterfaceInline(admin.StackedInline):
 
    """
 
@@ -24,6 +22,7 @@ class InterfaceInline(admin.StackedInlin
 
    model = Interface
 
    extra = 1
 

	
 

	
 
class CommunicationAdmin(admin.ModelAdmin):
 
    """
 
    Modifies the default admin class for the Communication class. The
 
@@ -81,6 +80,7 @@ class CommunicationAdmin(admin.ModelAdmi
 
        # Call the parent's method so it would do any of its magic.
 
        return super(CommunicationAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
 

	
 

	
 
class EntityAdmin(admin.ModelAdmin):
 
    """
 
    This class implements the admin view of the entity instances. It adds some
 
@@ -97,6 +97,7 @@ class EntityAdmin(admin.ModelAdmin):
 
    # Enable filtering based on project and location.
 
    list_filter = ['project', 'location']
 

	
 

	
 
class InterfaceAdmin(admin.ModelAdmin):
 
    """
 
    This class implements the admin view of the interface instances. It allows
 
@@ -112,6 +113,7 @@ class InterfaceAdmin(admin.ModelAdmin):
 
    # Enable filtering based on project and location.
 
    list_filter = ['entity__project', 'entity__location']
 

	
 

	
 
# Register our admin classes.
 
admin.site.register(Project)
 
admin.site.register(Location)
conntrackt/models.py
Show inline comments
 
# Django-specific imports
 
# Django imports.
 
from django.core.exceptions import ValidationError
 
from django.db import models
 

	
 
# Create your models here.
 

	
 
class Project(models.Model):
 
    """
 
    Implements a model with information about a project. A project has some
 
@@ -15,8 +15,8 @@ class Project(models.Model):
 
      description - Free-form description of the project.
 
    """
 

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

	
 
    def __unicode__(self):
 
        """
 
@@ -26,6 +26,7 @@ class Project(models.Model):
 

	
 
        return self.name
 

	
 

	
 
class Location(models.Model):
 
    """
 
    Implements a model with information about location. Locations can further be
 
@@ -50,8 +51,8 @@ class Location(models.Model):
 
      description - Free-form description of a location.
 
    """
 

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

	
 
    def __unicode__(self):
 
        """
 
@@ -61,6 +62,7 @@ class Location(models.Model):
 

	
 
        return self.name
 

	
 

	
 
class Entity(models.Model):
 
    """
 
    Models an entity in a project. An entity can be a server, router, or any
 
@@ -81,8 +83,8 @@ class Entity(models.Model):
 
      located.
 
    """
 

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

	
 
@@ -106,6 +108,7 @@ class Entity(models.Model):
 

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

	
 

	
 
class Interface(models.Model):
 
    """
 
    Models a representation of an interface on an entity. It can be used for
 
@@ -126,11 +129,11 @@ class Interface(models.Model):
 
      denoting the network netmask.
 
    """
 

	
 
    name = models.CharField(max_length = 100, default = 'eth0')
 
    description = models.TextField(blank = True, default = 'Main network interface.')
 
    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')
 
    netmask = models.IPAddressField(default='255.255.255.255')
 

	
 
    def __unicode__(self):
 
        """
 
@@ -145,6 +148,7 @@ class Interface(models.Model):
 
        else:
 
            return '%s (%s/%s)' % (self.entity.name, self.address, self.netmask)
 

	
 

	
 
class Communication(models.Model):
 
    """
 
    Models a representation of allowed network communication. This lets the user
 
@@ -183,11 +187,11 @@ class Communication(models.Model):
 
        ('ICMP', 'ICMP'),
 
        )
 

	
 
    source = models.ForeignKey(Interface, related_name = 'source_set')
 
    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)
 
    source = models.ForeignKey(Interface, related_name='source_set')
 
    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)
 

	
 
    def __unicode__(self):
 
        """
conntrackt/templatetags/conntrackt_tags.py
Show inline comments
 
# Import Django's template library.
 
# Django imports.
 
from django import template
 
# Import for determining the active URL.
 
from django.core import urlresolvers
 

	
 

	
 
# Get an instance of Django's template library.
 
register = template.Library()
 

	
 
@@ -72,7 +72,7 @@ def iptables(communication):
 
    return rule_template % values
 

	
 

	
 
@register.simple_tag(takes_context = True)
 
@register.simple_tag(takes_context=True)
 
def active_link(context, url_name, return_value='active', **kwargs):
 
    """
 
    This template tag can be used to check if the provided URL matches against
conntrackt/urls.py
Show inline comments
 
# Import basic functions for URL pattern processing.
 
# Django imports.
 
from django.conf.urls import patterns, url
 

	
 
# For logging-in the users
 
from django.contrib.auth.views import login, logout
 

	
 
# Import some app-specific views.
 
# Application imports.
 
from .views import IndexView, ProjectView, EntityView, entity_iptables, project_iptables
 

	
 

	
 
urlpatterns = patterns(
 
    'conntrackt.views',
 
    # Homepage/index view.
 
    url(r'^$', IndexView.as_view(), name="index"),
 
    # View for showing information about a project.
 
    url(r'^project/(?P<pk>\d+)/$', ProjectView.as_view(),
 
        name = 'project'),
 
        name='project'),
 
    # View for showing information about an entity.
 
    url(r'^entity/(?P<pk>\d+)/$', EntityView.as_view(),
 
        name = 'entity'),
 
        name='entity'),
 
    # View for rendering iptables rules for a specific entity.
 
    url(r'^entity/(?P<pk>\d+)/iptables/$', entity_iptables, name="entity_iptables"),
 
    # View for rendering zip file with iptables rules for all entities in a project.
 
@@ -24,7 +23,7 @@ urlpatterns = patterns(
 
    # View for rendering zip file with iptables rules for all entities in a project for a specific location.
 
    url(r'^project/(?P<project_id>\d+)/location/(?P<location_id>\d+)/iptables/$', project_iptables, name="project_location_iptables"),
 
    # Views for logging-in/out the users.
 
    url(r'^login/$', login, {'template_name': 'conntrackt/login.html'}, name = "login"),
 
    url(r'^logout/$', logout, name = "logout"),
 
    url(r'^login/$', login, {'template_name': 'conntrackt/login.html'}, name="login"),
 
    url(r'^logout/$', logout, name="logout"),
 
)
 

	
conntrackt/utils.py
Show inline comments
 
# Standard library imports.
 
import re
 

	
 
# Django-specific imports.
 
# Django imports.
 
from django.template import Context, loader
 

	
 

	
conntrackt/views.py
Show inline comments
 
# For generating ZIP files.
 
# Standard library imports.
 
from StringIO import StringIO
 
from zipfile import ZipFile, ZIP_DEFLATED
 

	
 
# Django-specific imports.
 
# Django imports.
 
from django.http import HttpResponse
 
from django.shortcuts import render_to_response, get_object_or_404
 
from django.views.generic import TemplateView, DetailView
 

	
 
# Application-specific imports.
 
# Application imports.
 
from .models import Project, Entity, Location
 
from .utils import generate_entity_iptables
 

	
 
@@ -62,8 +62,8 @@ class ProjectView(DetailView):
 

	
 
        # Add the (location, entities) tuple for each location that has entities
 
        # belonging to the related project.
 
        for location in Location.objects.filter(entity__project = self.object).distinct():
 
            entities = Entity.objects.filter(project = self.object, location = location)
 
        for location in Location.objects.filter(entity__project=self.object).distinct():
 
            entities = Entity.objects.filter(project=self.object, location=location)
 
            location_entities.append((location, entities))
 

	
 
        # Add the (location, entities) tuples to context.
 
@@ -118,7 +118,7 @@ def entity_iptables(request, pk):
 

	
 
    # Fetch the entity, and construct the response with iptables rules as
 
    # content.
 
    entity = get_object_or_404(Entity, pk = pk)
 
    entity = get_object_or_404(Entity, pk=pk)
 
    content = generate_entity_iptables(entity)
 
    response = HttpResponse(content, mimetype='text/plain')
 
    
 
@@ -129,7 +129,7 @@ def entity_iptables(request, pk):
 
    return response
 

	
 

	
 
def project_iptables(request, project_id, location_id = None):
 
def project_iptables(request, project_id, location_id=None):
 
    """
 
    Custom view for obtaining iptables for all entities of a project or project
 
    location in a single ZIP file.
 
@@ -152,7 +152,7 @@ def project_iptables(request, project_id
 
    """
 

	
 
    # Fetch the project.
 
    project = get_object_or_404(Project, pk = project_id)
 
    project = get_object_or_404(Project, pk=project_id)
 

	
 
    # Set-up a string IO object to which we'll write the ZIP file (in-memory).
 
    buff = StringIO()
 
@@ -169,7 +169,7 @@ def project_iptables(request, project_id
 
    # set-up the filename that will be suggested to the browser.
 
    if location_id:
 
        location = get_object_or_404(Location, pk=location_id)
 
        entities = project.entity_set.filter(location = location)
 
        entities = project.entity_set.filter(location=location)
 
        filename = '%s_%s-iptables.zip' % (project.name, location.name)
 
    else:
 
        entities = project.entity_set.all()
0 comments (0 inline, 0 general)