If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

To use put this class in a model:

class Configure():
    This class implements a configurable set of options
    for use in anything that needs settings that
    are to be stored in the database.
    def __init__(self, auto_define=True, migrate=True, cache=None):
        Initialize configure class.

        Keyword arugments:
        database -- web2py DAL instance
        auto_define -- auto define database tables (default: True)
        migrate -- migrate the database tables (default: True)
        cache -- cache object to use for pulling database settings,
                this is a tuple object consisting of cache object
                and cache timeout. Default No Cache!
                (Cache(r).ram, 1500)
        self.cache = cache
        if auto_define:

    def define_tables(self, migrate=True):
        Defines the database tables needed to function
            Field('value', 'text'),
            Field('description', 'text'),
            Field('created', 'datetime', default=datetime.now(), writable=False, readable=False),
            Field('modified', 'datetime', default=datetime.now(), writable=False, readable=False),

    def _get_settings(self):
        Retreives the settings from the database and
        stores them in a storage dictionary
        settings = Storage()
        rows = db(db.settings.id > 0).select(cache=self.cache)
        for row in rows:
            settings[row.name] = row
        self.settings = settings

    def verify(self, settings):
        Adds the configuration to memory, and assures that the
        configuration exists in the database (DAL for web2py).

        If there are no database entries, it will create the table,
        and fill in the default values, otherwise it will poll
        the database for the information.

        Keyword arguments:
        items -- dictionary of configs to store into the database.
                in the format of
                {'name': 'value'}
        for name, info in settings.iteritems():
            row = db(db.settings.name == name).select().first()
            if not row:
                self.settings[name] = db.settings.insert(name=name, value=info.get('value', None), description=info.get('description', None))

    def read(self, name):
        Returns the value of a settings object

        Keyword arguments:
        name -- setting name
        return self.settings[name].value

    def write(self, name, value):
        Writes a setting to the database

        Keyword arguments:
        name -- setting name
        value -- value for the setting
        self.settings[name].update_record(value=value, modified=datetime.now())

Example use:

configure = Configure()
# initialize values if not exists
    'graph_color': 'value',
    'timezone': '0'
configure.read('graph_colour') # get the current graph colour value
configure.write('graph_colour', '#FF0000') # update the graph colour

Related slices

Comments (2)

  • Login to post

  • 0
    richard 9 years ago
    Where is the updated version? Note that this is an edited version of Configure because I only wanted a single layer of attributes. What did you want me to do regarding the GPLv3 license?

  • 0
    thadeusb 10 years ago
    You probably should update the code with my latest version. It would be nice if you would follow the GPLv3 copyright license by posting this here. Thanks.

Hosting graciously provided by:
Python Anywhere