Changeset 14130 for trunk


Ignore:
Timestamp:
03/15/12 15:54:39 (12 years ago)
Author:
ramonb
Message:

templatetags/cmts_extras.py:

  • added stringfilter's: arpanize, base_net, ip_last_digit
  • see #4
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sara_cmt/sara_cmt/cluster/templatetags/cmts_extras.py

    r12020 r14130  
    1 import os
    2 import re
     1import os, re, string
    32
    43# Inspired by Django tips on:
     
    3534    return NoBlankLinesNode(nodelist)
    3635
     36@stringfilter
     37def arpanize(value):
     38    """
     39        Converts a IP (range) to reversed DNS style arpa notation
     40
     41        Usage:
     42            {{{ <variable>|arpanize }}}
     43
     44        I.e.:
     45            {% assign broadcast = '192.168.1.0' %}
     46            {{{ broastcast|arpanize }}}
     47        Results in output:
     48            1.168.192.in-addr.arpa
     49    """
     50    ip_blocks = value.split('.')
     51
     52    reverse_block = [ ip_blocks[2], ip_blocks[1], ip_blocks[0], 'in-addr.arpa' ]
     53
     54    return string.join( reverse_block, '.' )
     55
     56register.filter( 'arpanize', arpanize )
     57
     58@stringfilter
     59def base_net(value):
     60    """
     61        Converts a IP (range) to it's first 3 octects
     62
     63        Usage:
     64            {{{ <variable>|base_net }}}
     65
     66        I.e.:
     67            {% assign broadcast = '192.168.1.0' %}
     68            {{{ broastcast|base_net }}}
     69        Results in output:
     70            192.168.1
     71    """
     72    ip_blocks = value.split('.')
     73
     74    return string.join( ip_blocks[:3], '.' )
     75
     76register.filter( 'base_net', base_net )
     77
     78@stringfilter
     79def ip_last_digit(value):
     80    """
     81        Converts a IP (range) to it's last octect
     82
     83        Usage:
     84            {{{ <variable>|ip_last_digit }}}
     85
     86        I.e.:
     87            {% assign myip = '192.168.1.123' %}
     88            {{{ myip|ip_last_digit }}}
     89        Results in output:
     90            123
     91    """
     92    ip_blocks = value.split('.')
     93
     94    return ip_blocks[3]
     95
     96register.filter( 'ip_last_digit', ip_last_digit )
    3797
    3898class MetaNode(template.Node):
Note: See TracChangeset for help on using the changeset viewer.