Package: djangoplicity-livesettings
AboutDjango-Livesettings is a project split from theSatchmo Project.  It provides the ability to configure settings via an admin interface, rather than by editing ‘settings.py’. In addition, livesettings allows you to set sane defaults so that your site can be perfectly functional without any changes. Livesettings uses caching to make sure this has minimal impact on your site’s performance.Finally, if you wish to lock down your site and disable the settings, you can export your livesettings and store them in your ‘settings.py’. This allows you have flexibility in deciding how various users interact with your app.Livesettings supports several types of input choices:BooleanDecimalDurationFloatIntegerPositive Integer (non negative)StringLong stringMultiple stringsLong multiple stringsModule valuesPasswordLivesettings has been used for many years in the satchmo project and is considered stable and production ready.InstallationRequirementsPython2.5+, 2.6+ or 2.7+Django1.4+ or 1.5+Django-KeyedcacheInstalling LivesettingsAfter the dependencies have been installed, you can install the latest livesettings, using:pip install djangoplicity-livesettingsAdd livesettings to your installed apps in ‘settings.py’:INSTALLED_APPS = (…
# Uncomment the next line to enable the admin:
‘django.contrib.admin’,
‘livesettings’,
‘myapp’
…)It is high recommended to configure a global cache (likeMemcachedCache) for
multiprocess servers! Otherwise the processes would not be notified about new
values with the defaultLocMemCache. The default configuration is safe for
a debug server (manage.py runserver).Add it to your ‘urls.py’:urlpatterns = patterns(‘’,…
# Uncomment the next line to enable the admin:
url(r’^admin/’, include(admin.site.urls)),
url(r’^settings/’, include(‘livesettings.urls’)),
…)Execute a syncdb to create the required tables:python manage.py syncdbUsageAn example project is in the directory ‘test-project’.
It’s beginning isidentical to the following description and is a useful example for integrating livesettings into your app.Creating Config.pyIn order to use livesettings, you will need to create a ‘config.py’ in your django application.
For this example, we will create a ‘config.py’ file in the ‘test-project/localsite’ directory.Example: “For this specific app, we want to allow an admin user to control how many images are displayed on the front page of our site.”
We will create the following ‘config.py’:from livesettings import config_register, ConfigurationGroup, PositiveIntegerValue, MultipleStringValue
from django.utils.translation import ugettext_lazy as _# First, setup a grup to hold all our possible configs
MYAPP_GROUP = ConfigurationGroup(
‘MyApp’,               # key: internal name of the group to be created
_(‘My App Settings’),  # name: verbose name which can be automatically translated
ordering=0             # ordering: order of group in the list (default is 1)
)# Now, add our number of images to display value
# If a user doesn’t enter a value, default to 5
config_register(PositiveIntegerValue(
MYAPP_GROUP,           # group: object of ConfigurationGroup created above
‘NUM_IMAGES’,      # key:   internal name of the configuration value to be created
description = _(‘Number of images to display’),              # label for the value
help_text = _(“How many images to display on front page.”),  # help text
default = 5        # value used if it have not been modified by the user interface
))# Another example of allowing the user to select from several values
config_register(MultipleStringValue(
MYAPP_GROUP,
‘MEASUREMENT_SYSTEM’,
description=_(“Measurement System”),
help_text=_(“Default measurement system to use.”),
choices=[(‘metric’,_(‘Metric’)),
(‘imperial’,_(‘Imperial’))],
default=”imperial”
))In order to activate this file, add the following line to your ‘models.py’:import configYou can now see the results of your work by running the dev server and going tosettingspython manage.py runserverDislayed values can be limited to a configuration group by the url. For example
we want to do experiments with configuration groupMyApponly:group settings::
whereMyAppis the key name of the displayed group.More examples for all implemented types of ..Values can be found in
‘test-project/localsite/config.py’:
including configuration groups which are enabled or disabled based on modules selected in the form.
You can review examples by:cd test-project
python manage.py runserverand browse<http://127.0.0.1:8000/settings/>.Accessing your value in a viewNow that you have been able to set a value and allow a user to change it, the next step is to access it from a view.In a ‘views.py’, you can use the config_value function to get access to the value. Here is a very simple view that passes the value to your template:from django.shortcuts import render_to_response
from livesettings import config_value

def index(request):
    image_count = config_value('MyApp','NUM_IMAGES')
    # Note, the measurement_system will return a list of selected values
    # in this case, we use the first one
    measurement_system = config_value('MyApp','MEASUREMENT_SYSTEM')
    return render_to_response('myapp/index.html',
                            {'image_count': image_count,
                            'measurement_system': measurement_system[0]})Using the value in your ‘index.html’ is straightforward:<p>Test page</p>
<p>You want to show {{image_count}} pictures and use the {{measurement_system}} system.</p>Security and Restricting Access to LivesettingsIn order to give non-superusers access to the /settings/ views, open Django Admin Auth screen
and give the user or to its group the permissionlivesettings|setting|Can change settting.
The same permission is needed to view the form and submit.
Permissions for insert or delete and any permissions for “long setting” are ignored.NoteSuperusers will have access to this setting without enabling any specific permissions.NoteBecause of the security significance of livesettings, all views in livesettings support CSRF regardless of whether or not the
CsrfViewMiddleware is enabled or disabled.If you want to save a sensitive information to livesettings on production site (e.g. a password for logging into other web service)
it is recommended not to grant permissions to livesettings to users which are logging in everyday.
The most secure method is to export the settings and disable web access to livesettings as described below.
Exporting settings itself is allowed only by the superuser.Password values should be declared byPasswordValue(… render_value=False)that replaces password characters by asterisks in the browser. (Though hidden
to a human observer, password is still accessible by attacker’s javascripts or
by connection eavesdropping.)Exporting SettingsSettings can be exported by thehttp://127.0.0.1:8000/settings/export/. After exporting the file, the entire
output can be manually copied and pasted to ‘settings.py’ in order to deploy configuration to more sites
or to entirely prevent further changes and reading by web browser.
If you restrict DB access to the settings, all of the livesettings_* tables will be unused.Here is a simple example of what the extract will look like:LIVESETTINGS_OPTIONS = \
{   1: {   'DB': False,
           'SETTINGS': {   u'MyApp': {   u'DECIMAL_TEST': u'34.0923443',
                                         u'MEASUREMENT_SYSTEM': u'["metric"]',
                                         u'STRING_TEST': u'Orange'}}}}In order to restrict or enable DB access, use the following line in your settings:'DB': True,    # or FalseIf you have multiple sites, they can be manually combined in the file as well,
where “1:” is to be repeatedly replaced by site id.Exporting settings requires to be a superuser in Django.NotesIf you use logging with the level DEBUG in your application, prevent increasing of logging level of keyedcache by configuring it in settings.py:import logging
logging.getLogger('keyedcache').setLevel(logging.INFO)Next StepsThe rest of the various livesettings types can be used in a similar manner. You can review thesatchmo codefor more advanced examples.