Changeset - a5941ab6ea3b
[Not reviewed]
default
0 8 0
Branko Majic (branko) - 11 years ago 2013-06-30 20:18:36
branko@majic.rs
Style fixes (PEP8).
8 files changed with 16 insertions and 24 deletions:
0 comments (0 inline, 0 general)
conntrackt/models.py
Show inline comments
 
@@ -262,7 +262,6 @@ class Communication(models.Model):
 

	
 
        This provides ability to let all of the other fields of a communication
 
        instance to be editable.
 
        """
 

	
 
        return "Edit"
 

	
conntrackt/templatetags/conntrackt_tags.py
Show inline comments
 
@@ -44,13 +44,13 @@ def html_link(text, view, *args, **kwarg
 
    # Iterate over keyword arguments, and if we support them, just assign them
 
    # to the context as is. Raise an exception if a keyword is unsupported.
 
    for key, value in kwargs.items():
 
        if key in ("class", "title", "id", "get"):
 
            context[key] = value
 
        else:
 
            raise template.TemplateSyntaxError("Unknown argument for 'advhtml_link' tag: %r" % key )
 
            raise template.TemplateSyntaxError("Unknown argument for 'advhtml_link' tag: %r" % key)
 

	
 
    return context
 

	
 

	
 
@register.simple_tag
 
def iptables(communication):
 
@@ -86,14 +86,14 @@ def active_link(context, url_name, retur
 
      request.
 
    """
 

	
 
    matches = current_url_equals(context, url_name, **kwargs)
 

	
 
    return return_value if matches else ''
 
 
 
 
 

	
 

	
 
def current_url_equals(context, url_name, **kwargs):
 
    """
 
    Helper function for checking if the specified URL corresponds to the current
 
    request path in the context.
 

	
 
    Arguments:
 
@@ -121,7 +121,6 @@ def current_url_equals(context, url_name
 
            kwarg = kwargs.get(key)
 
            resolved_kwarg = resolved.kwargs.get(key)
 
            if not resolved_kwarg or kwarg != resolved_kwarg:
 
                return False
 

	
 
    return matches
 

	
conntrackt/tests/test_models.py
Show inline comments
 
@@ -137,7 +137,6 @@ class CommunicationTest(TestCase):
 

	
 
        comm = Communication.objects.get(pk=1)
 

	
 
        expected = "Test Entity 2 -> Test Entity 1 (TCP:22)"
 

	
 
        self.assertEqual(expected, str(comm))
 

	
conntrackt/tests/test_utils.py
Show inline comments
 
@@ -44,7 +44,6 @@ COMMIT
 
:INPUT ACCEPT [0:0]
 
:OUTPUT ACCEPT [0:0]
 
:POSTROUTING ACCEPT [0:0]
 
COMMIT
 
"""
 
        self.assertEqual(generated, expected)
 

	
conntrackt/tests/test_views.py
Show inline comments
 
@@ -72,13 +72,13 @@ class IndexViewTest(ViewTest):
 
        Tests if projects are shown or not.
 
        """
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("index"))
 
        
 

	
 
        self.assertQuerysetEqual(response.context["projects"], ["<Project: Test Project 1>", "<Project: Test Project 2>"])
 
        self.assertContains(response, "Test Project 1")
 
        self.assertContains(response, "Test Project 2")
 

	
 

	
 
class ProjectViewTest(ViewTest):
 
@@ -128,13 +128,13 @@ class ProjectViewTest(ViewTest):
 
        self.assertContains(response, "Test Project 1")
 
        self.assertContains(response, "Test Location 1")
 
        self.assertContains(response, "Test Location 2")
 

	
 

	
 
class EntityView(ViewTest):
 
    
 

	
 
    def test_permission_denied(self):
 
        """
 
        Tests if permission will be denied for client without sufficient privileges.
 
        """
 

	
 
        self.client.login(username="noperms", password="noperms")
 
@@ -236,13 +236,13 @@ class EntityIptablesTest(ViewTest):
 

	
 
        response = self.client.get(reverse("entity_iptables", args=(1,)))
 

	
 
        self.assertContains(response, ":INPUT")
 
        self.assertContains(response, ":OUTPUT")
 
        self.assertContains(response, ":FORWARD")
 
        
 

	
 

	
 
class ProjectIptablesTest(ViewTest):
 

	
 
    def test_permission_denied(self):
 
        """
 
        Tests if permission will be denied for client without sufficient privileges.
 
@@ -272,24 +272,23 @@ class ProjectIptablesTest(ViewTest):
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("project_iptables", args=(200,)))
 
        self.assertEqual(response.status_code, 404)
 

	
 
        response = self.client.get(reverse("project_location_iptables", args=(200,1)))
 
        response = self.client.get(reverse("project_location_iptables", args=(200, 1)))
 
        self.assertEqual(response.status_code, 404)
 

	
 

	
 
    def test_invalid_location(self):
 
        """
 
        Tests if a 404 is returned if invalid location is specified.
 
        """
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("project_location_iptables", args=(1,200)))
 
        response = self.client.get(reverse("project_location_iptables", args=(1, 200)))
 
        self.assertEqual(response.status_code, 404)
 

	
 
    def test_content_type(self):
 
        """
 
        Test if correct content type is being returned by the response.
 
        """
 
@@ -307,13 +306,13 @@ class ProjectIptablesTest(ViewTest):
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("project_iptables", args=(1,)))
 
        self.assertEqual(response['Content-Disposition'], 'attachment; filename="test_project_1-iptables.zip"')
 

	
 
        response = self.client.get(reverse("project_location_iptables", args=(1,1)))
 
        response = self.client.get(reverse("project_location_iptables", args=(1, 1)))
 
        self.assertEqual(response['Content-Disposition'], 'attachment; filename="test_project_1-test_location_1-iptables.zip"')
 

	
 
    def test_project_entities_show(self):
 
        """
 
        Test if the project's iptables are being shown or not.
 
        """
 
@@ -325,13 +324,13 @@ class ProjectIptablesTest(ViewTest):
 
        buff = StringIO(response.content)
 

	
 
        zipped_iptables = ZipFile(buff, "r", ZIP_DEFLATED)
 
        expected_zip_files = ["test_entity_1-iptables.conf",
 
                              "test_entity_2-iptables.conf",
 
                              "test_entity_3-iptables.conf",
 
                              "test_subnet-iptables.conf" ]
 
                              "test_subnet-iptables.conf"]
 

	
 
        self.assertEqual(len(zipped_iptables.namelist()), 4)
 
        self.assertEqual(zipped_iptables.namelist(), expected_zip_files)
 

	
 
        for filename in expected_zip_files:
 
            iptables_file = zipped_iptables.read(filename)
 
@@ -345,13 +344,13 @@ class ProjectIptablesTest(ViewTest):
 
        """
 
        Test if the project location's iptables are being shown or not.
 
        """
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("project_location_iptables", args=(1,1)))
 
        response = self.client.get(reverse("project_location_iptables", args=(1, 1)))
 

	
 
        buff = StringIO(response.content)
 

	
 
        zipped_iptables = ZipFile(buff, "r", ZIP_DEFLATED)
 
        expected_zip_files = ["test_entity_1-iptables.conf",
 
                              "test_entity_2-iptables.conf"]
 
@@ -363,7 +362,6 @@ class ProjectIptablesTest(ViewTest):
 
            iptables_file = zipped_iptables.read(filename)
 
            self.assertIn(":INPUT", iptables_file)
 
            self.assertIn(":OUTPUT", iptables_file)
 
            self.assertIn(":FORWARD", iptables_file)
 

	
 
        zipped_iptables.close()
 

	
conntrackt/urls.py
Show inline comments
 
@@ -23,7 +23,6 @@ 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"),
 
)
 

	
conntrackt/utils.py
Show inline comments
 
@@ -49,7 +49,6 @@ def generate_entity_iptables(entity):
 
    filter.add_chain(iptables.Chain("FORWARD", "DROP"))
 

	
 
    # Construct the iptables file using the two tables.
 
    content = "%s%s" % (filter, nat)
 

	
 
    return content
 

	
conntrackt/views.py
Show inline comments
 
@@ -12,12 +12,13 @@ from django.views.generic import Templat
 
from braces.views import MultiplePermissionsRequiredMixin
 

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

	
 

	
 
class IndexView(MultiplePermissionsRequiredMixin, TemplateView):
 
    """
 
    Custom view used for rendering the index page.
 
    """
 

	
 
    template_name = 'conntrackt/index.html'
 
@@ -125,13 +126,13 @@ class EntityView(MultiplePermissionsRequ
 
        # Add the rendered iptables rules to the context.
 
        context['entity_iptables'] = generate_entity_iptables(self.object)
 

	
 
        return context
 

	
 

	
 
@permission_required("conntrackt.view", raise_exception = True)
 
@permission_required("conntrackt.view", raise_exception=True)
 
def entity_iptables(request, pk):
 
    """
 
    Custom view that returns response containing iptables rules generated for an
 
    entity.
 

	
 
    Makes sure to set the Content-Disposition of a response in order to
 
@@ -150,21 +151,21 @@ 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)
 
    content = generate_entity_iptables(entity)
 
    response = HttpResponse(content, mimetype='text/plain')
 
    
 

	
 
    # Add the Content-Disposition information for the browser, telling the
 
    # browser to download the file with suggested filename.
 
    response['Content-Disposition']="attachment; filename=%s-iptables.conf" % entity.name.lower().replace(" ", "_")
 
    response['Content-Disposition'] = "attachment; filename=%s-iptables.conf" % entity.name.lower().replace(" ", "_")
 

	
 
    return response
 

	
 

	
 
@permission_required("conntrackt.view", raise_exception = True)
 
@permission_required("conntrackt.view", raise_exception=True)
 
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.
 

	
 
    Arguments:
 
@@ -225,7 +226,6 @@ def project_iptables(request, project_id
 
    # Set the Content-Disposition so the browser would know it should download
 
    # the archive, and suggest the filename.
 
    response['Content-Disposition'] = 'attachment; filename="%s"' % filename
 

	
 
    # Finally return the response object.
 
    return response
 

	
0 comments (0 inline, 0 general)