1. Osiloke Emoekpere
  2. django-tupperware

Source

django-tupperware /

Filename Size Date modified Message
tupperware
34.3 KB
Added license
3.2 KB
added a setup.py
1.1 KB
added a setup.py

== What is Tupperware? ==

Tupperware lets you move settings out of the settings.py file and into the database. It allows you to have different settings for each site and automatically updates your settings object for each request.

== How to install ==

  1. Make sure you have the dependencies

  2. Put the "tupperware" directory somewhere in your python path

    OR at the command line, run: $ python setup.py install

  3. Add tupperware to your installed apps (in the settings.py file)

    INSTALLED_APPS = (

    ... other django apps...

    'tupperware',

    )

  4. Add tupperware to the beginning of your MIDDLEWARE_CLASSES setting (settings.py):

    MIDDLEWARE_CLASSES = ( 'tupperware.middleware.SiteSettings', ... other middleware...

    )

  5. It is highly reccommended that you use caching (otherwise you'll be

    hitting the database for all your settings on every request). If you're using a cache you need to use the tupperware cache wrapper.

    In your settings.py:

    # Your cache backend of choice (used to be the CACHE_BACKEND setting) CUSTOM_CACHE_BACKEND = 'file:///tmp/djangocache'

    # The tupperware cache wrapper CACHE_BACKEND = 'tupperware.cache_wrapper://'

  6. create the necessary tables in the database. At the command line:

    $ python manage.py syncdb

== Gotchas ==

Be careful of overwriting the database settings. The settings are loaded from the database. It probably won't work.

There are a few settings that you can't overwrite, mostly pertaining to the current site.

  • special cases -

    settings.SITE_ID - (read only) don't try to override this setting. it

    is loaded from site.id

    settings.SITE_NAME - loaded from (and written to) site.name

    settings.DEFAULT_FROM_EMAIL - (read only) returns noreply@{{ site.domain }}

    you can remove the getter from tupperware.managers to make it act like a normal setting

    settings.CONTACT_EMAIL - (read only) returns feedback@{{ site.domain }}

    you can remove the getter from tupperware.managers to make it act like a normal setting

== Using Settings from Tupperware ==

Tupperware monkey-patches django's settings object. Just keep using django settings like you always did.

from django.conf import settings

# This value is pulled from database. If it is not found, it uses the # settings.py file like normal.

print(settings.SOME_SETTING)

== Changing settings on the fly ==

Tupperware adds an "update" method to the settings object. It works just like the "update" method you see on python's built in dict object.

from django.conf import settings

# changes the value of settings.SOME_SETTING (stores it in the database) # the setting will be changed for the current site only. settings.update({

"SOME_SETTING": "NEW VALUE"

})

To change settings for a different site than the current site:

from django.contrib.sites.models import Site from tupperware.models import SiteSetting

site = Site.objects.get(id=site_id) SiteSetting.objects.update({

"SOME_SETTING": "NEW VALUE"

}, site=site)