# HG changeset patch # User Branko Majic # Date 2013-03-24 13:32:48 # Node ID e8434f637f9f5abca261e69e64647095ed113690 # Parent b037d11ccc3729a7bb39a9e4e7381ad552f10413 Removed the line stripper middleware since it interfered with regular template lookups. Added some custom code to remove all blank lines from iptables rendering instead. diff --git a/conntrackt/stripper.py b/conntrackt/stripper.py deleted file mode 100644 --- a/conntrackt/stripper.py +++ /dev/null @@ -1,80 +0,0 @@ -# This was taken from https://github.com/ldiqual/django-linestripper/ - -from django.conf import settings -from django.template.loaders import app_directories - -import re - -# To be set in settings.py -STRIPPER_TAG = getattr(settings, 'STRIPPER_TAG', '__STRIPPER_TAG__') -STRIPPER_CLEAR_LINE = getattr(settings, 'STRIPPER_CLEAR_LINE', False) -STRIPPER_ENABLED = getattr(settings, 'STRIPPER_ENABLED', True) -STRIPPER_ALLOWED_CONTENT_TYPES = getattr(settings, 'STRIPPER_ALLOWED_CONTENT_TYPES', ( - 'text', - 'xml' -)) - -# Finders -FIND_BLANK_LINE = r'\n(\s*)\n' -FIND_START_BLANK_LINE = r'^(\s*)\n' -FIND_TAG = STRIPPER_TAG - -# Replacers -REPLACE_WITH_TAG = '\n'+ STRIPPER_TAG +'\n' if STRIPPER_CLEAR_LINE else r'\n\1'+ STRIPPER_TAG +'\n' -REPLACE_START_WITH_TAG = STRIPPER_TAG +'\n' if STRIPPER_CLEAR_LINE else r'\n\1'+ STRIPPER_TAG +'\n' - -# Deleters -DELETE_BLANK_LINE = '\n' -DELETE_START_BLANK_LINE = '' -DELETE_TAG = '' - -''' -This is called AFTER the template generation. -It suppresses blank lines and deletes STRIPPER_TAG -''' -class StripperMiddleware(object): - def process_response(self, request, response): - if not STRIPPER_ENABLED: - return response - - # Checks if the content type is allowed - allowed = False - for type in STRIPPER_ALLOWED_CONTENT_TYPES: - if type in response['Content-Type']: - allowed = True - break - - # If the content type is not allowed, untag and return - if not allowed: - response.content = response.content.replace(STRIPPER_TAG, DELETE_TAG) - return response - - # Suppress a blank line at the beginning of the document - response.content = re.sub(FIND_START_BLANK_LINE, DELETE_START_BLANK_LINE, response.content) - # Suppress blank lines - response.content = re.sub(FIND_BLANK_LINE, DELETE_BLANK_LINE, response.content) - # Delete STRIPPER_TAG - # response.content = re.sub(FIND_TAG, DELETE_TAG, response.content) - response.content = response.content.replace(STRIPPER_TAG, DELETE_TAG) - return response - -''' -This is called BEFORE the template generation. -It adds STRIPPER_TAG to blank lines so they aren't removed in the middleware -''' -class Loader(app_directories.Loader): - is_usable = True - - def process_content(self, content): - if not STRIPPER_ENABLED: - return content - - content = re.sub(FIND_BLANK_LINE, REPLACE_WITH_TAG, content) - content = re.sub(FIND_START_BLANK_LINE, REPLACE_WITH_TAG, content) - return content - - def load_template(self, template_name, template_dirs=None): - source, origin = self.load_template_source(template_name, template_dirs) - if not STRIPPER_ENABLED: - return source, origin - return self.process_content(source), origin diff --git a/conntrackt/templates/conntrackt/entity_iptables.html b/conntrackt/templates/conntrackt/entity_iptables.html --- a/conntrackt/templates/conntrackt/entity_iptables.html +++ b/conntrackt/templates/conntrackt/entity_iptables.html @@ -8,6 +8,8 @@ {% if communication.description %} # {{communication.description}} + {% else %} +# Communications without desecription. {% endif %} {% endifchanged %} {% iptables communication %} diff --git a/conntrackt/views.py b/conntrackt/views.py --- a/conntrackt/views.py +++ b/conntrackt/views.py @@ -1,6 +1,7 @@ # For generating ZIP files. from StringIO import StringIO from zipfile import ZipFile, ZIP_DEFLATED +import re # Django-specific imports. from django.http import HttpResponse @@ -99,6 +100,11 @@ class IptablesView(DetailView): # browser to download the file with suggested filename. response['Content-Disposition']="attachment; filename=%s-iptables.conf" % self.object.name.lower().replace(" ", "_") + # Render the response, and remove the blank lines from the template. + response.render() + response.content = re.sub('^\s*\n', '', response.content) + response.content = re.sub('\n\s*\n', '\n', response.content) + # Return the modified response. return response diff --git a/projtest/projtest/settings.py b/projtest/projtest/settings.py --- a/projtest/projtest/settings.py +++ b/projtest/projtest/settings.py @@ -81,7 +81,6 @@ SECRET_KEY = '%s-x^wskhxu#5%o)0ck71g7o@7 # List of callables that know how to import templates from various sources. TEMPLATE_LOADERS = ( - 'conntrackt.stripper.Loader', 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', # 'django.template.loaders.eggs.Loader', @@ -95,7 +94,6 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.messages.middleware.MessageMiddleware', # Uncomment the next line for simple clickjacking protection: # 'django.middleware.clickjacking.XFrameOptionsMiddleware', - 'conntrackt.stripper.StripperMiddleware' ) ROOT_URLCONF = 'projtest.urls'