Turning a dictionary into a Tuple for Django flat_choices

Here is my pre-defined code called "Systems.py" as you can see I create a dictionary at the end of this to link display names to classes. This code is part of the meat-and-potatoes of my web app and instead of creating a model for these I want them to be defined in the actual code. I don't want to access the database every time I need to reference these and I also want this code to be portable in the means that you can use it in a shell as well as the web app.

class OperatingSystem(object):
        ESCALATE_COMMAND=''
        PAGINATES       =False
        VERSION         =''
        PROMPTLINE      =''



class CiscoIOS(OperatingSystem):
        '''cisco ios'''
        PROMPTLINE      = r'[-\w]+[>#]'
        GET_CONFIG      ='show running-config'
        PAGINATES       =True
        VERSION         ='show version'
        DISABLE_PAGINATION = 'terminal length 0'
        ESCALATE_COMMAND='enable'

class CiscoWebNS(OperatingSystem):
        '''cisco webns css 11500'''
        PROMPTLINE      ="#"
        GET_CONFIG      ='show running config'
        PAGINATES       =True
        DISABLE_PAGINATION = 'terminal length 65000'

class AppleOSX(OperatingSystem):
        '''apple OSX defaults'''
        PROMPTLINE      = r'[-\w]+[$#]'
        VERSION         ="uname -a"
        PRIVILEGE       ="sudo su"

class OpenBSD(OperatingSystem):
        '''OpenBSD defaults'''
        PROMPTLINE      = r'[$#]'
        VERSION         ="uname -a"
        PRIVILEGE       ="sudo su"


class SecureComputingSidewinder(OperatingSystem):
        '''sidewinder configs'''
        PROMPTLINE      ='{}'
        PRIVILEGE       ='srole'
        GET_CONFIG      ="cf acl query"

class ArubaOS(OperatingSystem):
        '''aruba configs'''
        PROMPTLINE      ='#'
        PAGINATES       =True
        DISABLE_PAGINATION = 'terminal length 0'
        GET_CONFIG      ="show run"

OperatingSystems = {
        'IOS': CiscoIOS,
        'WebNS': CiscoWebNS,
        'OSX': AppleOSX,
        'SOS': SecureComputingSidewinder,
        'AOS': ArubaOS,
        'OBSD': OpenBSD,
        }

In my django app I have a model like so:

os_list = ((k,k) for k in Systems.OperatingSystems.iterkeys())

class Node(models.Model):
        name = models.CharField(max_length=100)
        ip   = models.IPAddressField(null=True, blank=True)
        description = models.TextField(null=True, blank=True)
        os   = models.CharField(choices=os_list,max_length=6, null=True, blank=True)
        transport = models.CharField(choices=transports, max_length=10)
        credentials = models.ForeignKey(Credential, blank=True, null=True)
        escalation_credentials = models.ForeignKey(Credential, blank=True, null=True, help_text="root or enable passwords if needed", related_name="enable_password" )

        def get_ip(self):
           return self.ip
        def __unicode__(self):
           return self.name + " - " + str(self.ip)

Meaningful snippet:

from ncm.management.commands import Systems
os_list = ((k,k) for k in Systems.OperatingSystems.iterkeys())

This iterates over my "OperatingSystems" dictionary's keys and constructs a tuple of (key,key) so that I can use it as a flat_choice for my os field.

Tagged as dictionary , django flat_choices , tuple
Written by Andrew Konkol on July 26th, 2012

0 Comments

Log in with Twitter, Google, Facebook, LinkedIn to leave a comment.