Runtime settings and configuration for Django sites
Project description
An application for managing site configuration through normal Django forms, and thus through the admin site.
Yay, I’ve made another app to abuse contrib.admin.
Release |
Status |
---|---|
master |
Installation
Add the package to your INSTALLED_APPS in your settings:
INSTALLED_APPS = ( # ... 'stagesetting', # ... )
Add a STAGESETTINGS dictionary to your project’s settings:
STAGESETTINGS = { 'SETTING_NAME': ['path.to.Form.Class'], 'ANOTHER_SETTING_NAME': ['path.to.Form.Class', { 'initial': 'data', 'is': 'optionl', }], }
The setting collection name is the dictionary key, so must be unique. The dictionary value should be a list or tuple containing:
A simple configuration form might look like:
from django.core.exceptions import ValidationError from django.forms import Form, DateField class DateForm(Form): start = DateField() end = DateField() def clean(self): cd = self.cleaned_data if 'start' in cd and 'end' in cd and cd['start'] > cd['end']: raise ValidationError("Start date cannot be after end date") return cd
As you can see, it really is just a normal Form. Internally, this form’s cleaned_data will be converted into JSON before being saved to the database. It will get re-converted to proper Python values when pulled out of the database, by going through the given Form class’s validation again, including converting to rich values like model instances.
Usage in code
The best way to access the settings in your views is to include stagesetting.middleware.ApplyRuntimeSettings in your MIDDLEWARE_CLASSES which will ensure there is a request.stagesettings variable which can be used like so:
def myview(request): how_many_form_data = request.stagesetting.LIST_PER_PAGE allow_empty_form_data = request.stagesetting['ALLOW_EMPTY']
each setting will be a dictionary of the Form values, either the default ones or those changed in the database.
Usage in templates
If you’ve already got request in your template, obviously you can continue to use request.stagesettings if the middleware is wired up.
If you don’t have request, or you’re not using the middleware, stagesetting.context_processors.runtime_settings provides a STAGESETTING template variable which contains the exact same data.
Usage outside of a request
If you don’t have the middleware, or are in a part of the code which doesn’t have a request, you can use the wrapper object directly:
from stagesetting.models import RuntimeSettingWrapper def my_signal_handler(sender, instance, **kwargs): live_settings = RuntimeSettingWrapper() data = live_settings.LIST_PER_PAGE
Try to keep a single RuntimeSettingWrapper around for as long as possible, rather than creating a new instance everywhere, as the object must fetch the available settings from the database the first time it needs them. It caches them for it’s lifetime thereafter.
License
django-stagesetting 0.2.0 is available under the terms of the Simplified BSD License (alternatively known as the FreeBSD License, or the 2-clause License):
Copyright (c) 2015, Keryn Knight All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project.
Change log
0.2.0
Initial release.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-stagesetting-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ea6833ca49f841c9a7291660d45fa8016b77c3192b22180edb12221139acf50 |
|
MD5 | 13a75568d6df141fc44028dece5a1dd2 |
|
BLAKE2b-256 | e7a7cc393d276b3ccdbae1380ffaab2561ada969353dc433ded2b71ae6018ee6 |
Hashes for django_stagesetting-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f736979d6fc50b3eb9dd5cecf52a77b8a15437a0c58190628609100e3e49141 |
|
MD5 | 290b9c9f2a947b84c9606e21b30451aa |
|
BLAKE2b-256 | 0e8583b85125904aeade16d107917f6745d0c547cc7b8705277b6db40bc9b405 |