thanks Ross - very interesting.

Is there a pattern that the Web2py framework implements out of the
box? is the use of modules encouraged by the design of the framework?

On Feb 28, 2:44 pm, Ross Peoples <ross.peop...@gmail.com> wrote:
> I don't know how anyone else handles this, but I have been setting up things
> like global settings in modules like this:
>
> class GlobalSettings:
>     @staticmethod
>     def get_settings(db, cache:
>         return cache.ram('globalsettings', lambda:
> GlobalSettings._get_settings_from_db(db), time_expire=None)
>
>     @staticmethod
>     def _get_settings_from_db(db):
>         settings = Storage()
>         for row in db().select(db.global_settings.ALL):
>             settings[row.name] = row.value
>         return settings
>
>     @staticmethod
>     def save_settings(db, cache, settings):
>         #code to write settings to db
>         cache.ram.clear('globalsettings')
>
> Then, assuming I store a bunch of objects into a 'core.py' module, then I
> create a new model called 'z_import_modules.py' that I use to initialize the
> modules. The name is important so that the database and tables get
> initialized before the modules do. In the 'z_import_modules.py' file, all I
> have to do is import the 'core.py' file:
>
> core = local_import('core')
>
> My method turns the whole GlobalSettings class into a static object, so that
> whenever I need access to the settings object from another module or any
> controller, all I have to do is this:
>
> settings = core.GlobalSettings.get_settings(db, cache)
>
> It pulls the settings from the cache.ram (obviously, you can use whatever
> method you want). It stores the settings for the life of the application,
> and when settings are changed, clears the ram of all settings.
>
> Hope that helps to at least answer part of you question. I have no idea if
> this is the "right" way to do this, but it works for me. If someone has a
> better way or finds a flaw in my implementation, I would love to hear it. I
> am new to web2py and just started working with modules last week.

Reply via email to