|
|
# 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()
|