Skip to content

Commit

Permalink
small code changes; add required files for PyPi; write on README
Browse files Browse the repository at this point in the history
  • Loading branch information
Torsten Engelbrecht committed Apr 15, 2011
1 parent f765808 commit 685c146
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 5 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2011 Sterno.Ru and contributors.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include LICENSE
include MANIFEST
include README
16 changes: 16 additions & 0 deletions PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Name: django-minify
Version: 0.1
Summary: Application to handle settings within Django
Home-page:
Author: Sterno.Ru
Author-email: [email protected]
License: UNKNOWN
Download-URL:
Description: UNKNOWN
Platform: UNKNOWN
Classifier: Environment :: Web Environment
Classifier: Programming Language :: Python
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Topic :: Software Development :: Libraries :: Python Modules
203 changes: 203 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
.. _Python: http://www.python.org/
.. _Django: http://www.djangoproject.com/
.. _staticfiles app: http://headjs.com/

=======================
django-dynamicsettings
=======================

1. Requirements
:::::::::::::::::::::::::::::::::

At the moment *django-dynamic-settings* requires Python_ >2.5 and
Django_ >1.0 to run. If you use Django <1.3 you need to add the
predecessor for django.contrib.staticfiles app: https://github.com/jezdez/django-staticfiles

2. Installation
:::::::::::::::::::::::::::::::::

Simply run:
::

python setup.py install



You can also obtain django-dynamic-settings via:

::

pip install django-dynamic-settings

or

::

easy_install django-dynamic-settings



3. Usage
:::::::::::::::::::::::::::::::::

3.1. Setup
------------------------------

If you installed *django-dynamic-settings* you can already
use it like you are used to python modules. That lets you
make advantage of the internal caching for settings.

If you want to do more (save settings in the database,
check settings in the admin, run tests) you need to do the
following:

1.put it into your ``INSTALLED_APPS`` setting. Make sure
``django.contrib.admin`` (and requirements) is appearing in your
``INSTALLED_APPS`` setting as well, since its used by
*django-dynamic-settings*:

::

INSTALLED_APPS = (
...
'django.contrib.sessions',
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.staticfiles', #django >1.3, for django <1.3 use 'staticfiles'
...
'dynamicsettings',
...
)


2. Add a url handler to handle the admin views of
*django-dynamic-settings*. This is built-in into the standard
django admin, so you could add something like this:

::

urlpatterns = patterns('',
...
url(r'^admin/settings/', include('dynamicsettings.urls')),
url(r'^admin/', include(admin.site.urls)),
...
)


3. Add settings:

If you have another app which has settings you want to include or
your custom app which is using *django-dynamic-settings* has
some custom settings you can include these modules to add them
to be handled by *django-dynamic-settings*. For example:

::

DYNAMICSETTINGS_INCLUDE_MODULES = ['myapp.app_settings', 'globals']


In this case ``'myapp.app_settings'`` would be the string
representation of ``myapp.app_settings`` module which should
be available via your PYTHONPATH. The same goes for the
``'globals'`` module.
Now you can view the value of these settings within your
admin interface as well.

If you also want to edit settings and save them in the database
you have to add the names of the settings which can be edited.
For example:

::

DYNAMICSETTINGS_INCLUDE_SETTINGS = ['MY_SETTING', 'DEFAULT_CACHE_DURATION']


This would make ``MY_SETTING`` and ``DEFAULT_CACHE_DURATION``
editable in the database.
There is one thing you need to keep in mind when making
settings editable via admin:

**App's which are not using django-dynamic-settings are not
affected by the changes. That includes for example Django itself.
It doesn't make much sense to change a setting in the database
via admin which is used internally by Django. It will simply have
no effect.**

So the project or app which will make usage of *django-dynamic-settings*
great way of retrieving and setting the settings in the database
should use *django-dynamic-settings* instead of the usual way of
retrieving settings via ``from django.conf import settings``. The
the next section for usage examples.


3.2. Using django-dynamic-settings
------------------------------------------------------------


*django-dynamic-settings* is used in a very similiar way you
usually would use your settings. Instead of using:

::

from django.conf import settings


use

::

from dynamicsettings import settings


After that you can use settings as you are used to. Examples:

::

login_redirect_url = settings.LOGIN_REDIRECT_URL
my_custom_setting = settings.MY_SETTING


Additionally you have access to the following methods:

``settings.get(key, default)``: Retrieve a setting for a
particular name (``key``) or return the ``default``
(``None`` if emitted). Works like the python built-in
``dict.get()`` method Example usage:

::

login_redirect_url = settings.get('LOGIN_REDIRECT_URL', '/')
my_custom_setting = settings.get('MY_SETTING)


``settings.set(key, value, type)``: This is setting a setting
specified by ``key`` directly in the database without using the
admin interface. ``value`` is the new value of the setting and
``type`` the python type. If ``type`` is not explicitly given
it will try to resolve the type from the given `value``. Returns
the new value if setting was successful. Raises
KeyError if the setting is not allowed to be changed due to
not defining it in ``DYNAMICSETTINGS_INCLUDE_SETTINGS``.
Examples:

::

login_redirect_url = settings.set('LOGIN_REDIRECT_URL', '/home/')
my_custom_setting = settings.set('MY_SETTING', 73, 'int')




3.3. Complex usage
------------------------------


3.4. Additional settings
------------------------------







12 changes: 7 additions & 5 deletions dynamicsettings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ def __init__(self):
if self._settings is None:
self._get_settings()

def get(self, key, alternative=None):
return self._settings.get(key, alternative)
def get(self, key, default=None):
return self._settings.get(key, default)

def set(self, key, value, value_type):
def set(self, key, value, value_type=None):
if not value_type:
value_type = type(value).__name__
if self.can_change(key):
dynamic_setting, is_new = models.Settings.objects.get_or_create(key=key)
dynamic_setting.value = base64.b64encode(pickle.dumps(value))
dynamic_setting.type = value_type
dynamic_setting.save()
#refresh the cache
self._get_settings()
return True
return False
return dynamic_setting.value
raise KeyError('Setting "%s" can not be set in the database. If you want to change the setting add it to DYNAMICSETTINGS_INCLUDE_SETTINGS.' % key)

def reset(self, key):
if self.can_change(key):
Expand Down
21 changes: 21 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from setuptools import setup, find_packages

setup(
name='django-dynamic-settings',
version='0.1',
description='Application to handle settings within Django.',
author='Sterno.Ru',
author_email='[email protected]',
url='',
download_url='',
packages=find_packages(),
include_package_data=True,
classifiers=[
'Environment :: Web Environment',
"Programming Language :: Python",
'Framework :: Django',
'Intended Audience :: Developers',
'Operating System :: OS Independent',
'Topic :: Software Development :: Libraries :: Python Modules'
],
)

0 comments on commit 685c146

Please sign in to comment.