File diff e8434f637f9f → 90fac7c6abb9
conntrackt/utils.py
Show inline comments
 
new file 100644
 
# Standard library imports.
 
import re
 

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

	
 

	
 
def generate_entity_iptables(entity):
 
    """
 
    Generates full iptables rules for the supplied entity. The generated rules
 
    can be fed directly to the iptables-restore utility.
 

	
 
    Arguments:
 

	
 
        entity - An Entity instance for which the iptables rules should be
 
        generated.
 

	
 
    Returns:
 

	
 
        String containing the iptables rules for entity.
 
    """
 

	
 
    # Render the iptables template.
 
    template = loader.get_template('conntrackt/entity_iptables.html')
 
    context = Context({'entity': entity})
 
    content = template.render(context)
 

	
 
    # Remove the blank lines.
 
    content = re.sub('^\s*\n', '', content)
 
    content = re.sub('\n\s*\n', '\n', content)
 

	
 
    return content