# HG changeset patch # User Branko Majic # Date 2013-04-14 11:37:14 # Node ID f397b9db81830564708ff036ef51138b512a30e5 # Parent 9296d8c23a54efe6f5e64a5c7c899f1cb300fe9e Styling updates to reflect 2SoD recommendations. diff --git a/conntrackt/admin.py b/conntrackt/admin.py --- a/conntrackt/admin.py +++ b/conntrackt/admin.py @@ -1,12 +1,10 @@ -# 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) diff --git a/conntrackt/models.py b/conntrackt/models.py --- a/conntrackt/models.py +++ b/conntrackt/models.py @@ -1,8 +1,8 @@ -# 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): """ diff --git a/conntrackt/templatetags/conntrackt_tags.py b/conntrackt/templatetags/conntrackt_tags.py --- a/conntrackt/templatetags/conntrackt_tags.py +++ b/conntrackt/templatetags/conntrackt_tags.py @@ -1,8 +1,8 @@ -# 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 diff --git a/conntrackt/urls.py b/conntrackt/urls.py --- a/conntrackt/urls.py +++ b/conntrackt/urls.py @@ -1,22 +1,21 @@ -# 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\d+)/$', ProjectView.as_view(), - name = 'project'), + name='project'), # View for showing information about an entity. url(r'^entity/(?P\d+)/$', EntityView.as_view(), - name = 'entity'), + name='entity'), # View for rendering iptables rules for a specific entity. url(r'^entity/(?P\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\d+)/location/(?P\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"), ) diff --git a/conntrackt/utils.py b/conntrackt/utils.py --- a/conntrackt/utils.py +++ b/conntrackt/utils.py @@ -1,7 +1,7 @@ # Standard library imports. import re -# Django-specific imports. +# Django imports. from django.template import Context, loader diff --git a/conntrackt/views.py b/conntrackt/views.py --- a/conntrackt/views.py +++ b/conntrackt/views.py @@ -1,13 +1,13 @@ -# 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()