Skip to content

Commit 41e482d

Browse files
author
Torsten Engelbrecht
committed
update docs
1 parent 685c146 commit 41e482d

File tree

7 files changed

+227
-97
lines changed

7 files changed

+227
-97
lines changed

PKG-INFO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Name: django-minify
22
Version: 0.1
3-
Summary: Application to handle settings within Django
3+
Summary: Application to handle live settings within Django
44
Home-page:
55
Author: Sterno.Ru
66
Author-email: [email protected]

README

Lines changed: 75 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.. _Python: http://www.python.org/
2-
.. _Django: http://www.djangoproject.com/
3-
.. _staticfiles app: http://headjs.com/
4-
51
=======================
62
django-dynamicsettings
73
=======================
@@ -37,12 +33,9 @@ or
3733

3834

3935

40-
3. Usage
36+
3. Setup
4137
:::::::::::::::::::::::::::::::::
4238

43-
3.1. Setup
44-
------------------------------
45-
4639
If you installed *django-dynamic-settings* you can already
4740
use it like you are used to python modules. That lets you
4841
make advantage of the internal caching for settings.
@@ -51,7 +44,7 @@ If you want to do more (save settings in the database,
5144
check settings in the admin, run tests) you need to do the
5245
following:
5346

54-
1.put it into your ``INSTALLED_APPS`` setting. Make sure
47+
**1.** Put ``'dynamicsettings'`` into your ``INSTALLED_APPS``. Make sure
5548
``django.contrib.admin`` (and requirements) is appearing in your
5649
``INSTALLED_APPS`` setting as well, since its used by
5750
*django-dynamic-settings*:
@@ -70,7 +63,7 @@ following:
7063
)
7164

7265

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

@@ -87,9 +80,9 @@ django admin, so you could add something like this:
8780
3. Add settings:
8881

8982
If you have another app which has settings you want to include or
90-
your custom app which is using *django-dynamic-settings* has
91-
some custom settings you can include these modules to add them
92-
to be handled by *django-dynamic-settings*. For example:
83+
your custom app is using *django-dynamic-settings* and has
84+
some custom settings, you can include these modules to be handled
85+
by *django-dynamic-settings*:
9386

9487
::
9588

@@ -114,24 +107,22 @@ For example:
114107

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

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

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

132123

133-
3.2. Using django-dynamic-settings
134-
------------------------------------------------------------
124+
4. Usage
125+
:::::::::::::::::::::::::::::::::
135126

136127

137128
*django-dynamic-settings* is used in a very similiar way you
@@ -156,48 +147,77 @@ After that you can use settings as you are used to. Examples:
156147
login_redirect_url = settings.LOGIN_REDIRECT_URL
157148
my_custom_setting = settings.MY_SETTING
158149

159-
160-
Additionally you have access to the following methods:
161-
162-
``settings.get(key, default)``: Retrieve a setting for a
163-
particular name (``key``) or return the ``default``
164-
(``None`` if emitted). Works like the python built-in
165-
``dict.get()`` method Example usage:
166150

167-
::
168151

169-
login_redirect_url = settings.get('LOGIN_REDIRECT_URL', '/')
170-
my_custom_setting = settings.get('MY_SETTING)
152+
Additionally you have access to the following methods:
171153

154+
- ``settings.get(key, default)``: Retrieve a setting for a
155+
particular name (``key``) or return the ``default``
156+
(``None`` if emitted). Works like the python built-in
157+
``dict.get()`` method Example usage:
158+
159+
::
160+
161+
login_redirect_url = settings.get('LOGIN_REDIRECT_URL', '/')
162+
my_custom_setting = settings.get('MY_SETTING)
163+
164+
165+
- ``settings.set(key, value, type)``: This is setting a setting
166+
specified by ``key`` directly in the database without using the
167+
admin interface. ``value`` is the new value of the setting and
168+
``type`` the python type. If ``type`` is not explicitly given
169+
it will try to resolve the type from the given `value``. Returns
170+
the new value if setting was successful. Raises
171+
KeyError if the setting is not allowed to be changed due to
172+
not defining it in ``DYNAMICSETTINGS_INCLUDE_SETTINGS``.
173+
Examples:
174+
175+
::
176+
177+
login_redirect_url = settings.set('LOGIN_REDIRECT_URL', '/home/')
178+
my_custom_setting = settings.set('MY_SETTING', 73, 'int')
179+
180+
181+
- ``settings.reset(key)``: If you saved a setting in the database you
182+
can reset it (giving the name of the setting) to its original value via
183+
this method. This method returns ``True`` if the reset was successful
184+
and ``False`` if not (setting wasn't saved to the database for example).
185+
Examples:
186+
187+
::
188+
189+
login_redirect_url = settings.set('LOGIN_REDIRECT_URL', '/home/')
190+
settings.reset('LOGIN_REDIRECT_URL')
191+
print settings.LOGIN_REDIRECT_URL
192+
193+
194+
- ``settings.dict(keys)``: Returns a dict representation of the settings.
195+
If ``keys`` is ommitted all settings which are included into *django-dynamic-settings*
196+
are part of the dict. If you just want to retrieve particular settings
197+
you can provide names of the settings within ``keys`` (a list of strings).
198+
199+
- ``settings.is_in_db(key)``: Check if a particular setting given by
200+
its name (``key``) is saved in the db or not. Returns ``True`` if
201+
this is case, ``False`` otherwise.
202+
203+
- ``settings.can_change(key)``: Check if a particular setting given by
204+
its name (``key``) can be changed (and saved in the database). This
205+
returns ``True`` if the setting is provided in ``DYNAMICSETTINGS_INCLUDE_SETTINGS``,
206+
``False`` otherwise.
207+
208+
209+
5. Additional settings
210+
:::::::::::::::::::::::::::::::::
172211

173-
``settings.set(key, value, type)``: This is setting a setting
174-
specified by ``key`` directly in the database without using the
175-
admin interface. ``value`` is the new value of the setting and
176-
``type`` the python type. If ``type`` is not explicitly given
177-
it will try to resolve the type from the given `value``. Returns
178-
the new value if setting was successful. Raises
179-
KeyError if the setting is not allowed to be changed due to
180-
not defining it in ``DYNAMICSETTINGS_INCLUDE_SETTINGS``.
181-
Examples:
212+
Settings within *django-dynamic-settings* are cached in case you
213+
are using Django's cache framework. To define the timeout for the
214+
cached settings you can set ``DYNAMICSETTINGS_CACHE_TIMEOUT``:
182215

183216
::
184217

185-
login_redirect_url = settings.set('LOGIN_REDIRECT_URL', '/home/')
186-
my_custom_setting = settings.set('MY_SETTING', 73, 'int')
218+
DYNAMICSETTINGS_CACHE_TIMEOUT = 60
187219

188220

189221

190-
191-
3.3. Complex usage
192-
------------------------------
193-
194-
195-
3.4. Additional settings
196-
------------------------------
197-
198-
199-
200-
201-
202-
203-
222+
.. _Python: http://www.python.org/
223+
.. _Django: http://www.djangoproject.com/

dynamicsettings/__init__.py

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@
1919
#will not trigger exceptions when syncdb or runserver
2020
#see django.conf.LazySettings
2121
class LazyDynamicSettings(LazyObject):
22+
"""Lazy wrapper for DynamicSettings.
23+
"""
2224
def _setup(self):
2325
self._wrapped = DynamicSettings()
2426

2527

2628
class DynamicSettings(object):
29+
"""A class which is handling the dynamic settings.
30+
It is similiar to Django's own conf.settings Settings
31+
class, but also providing cached settings, setting and
32+
reseting a setting in the database, returning settings
33+
as a dict.
34+
"""
2735

2836
def __init__(self):
2937
self._settings_cache_key = 'dynamicsettings.settings'
@@ -32,9 +40,42 @@ def __init__(self):
3240
self._get_settings()
3341

3442
def get(self, key, default=None):
43+
"""Get a setting value for a partcular key (setting name).
44+
Works similiar to a dict's ``get`` method.
45+
46+
Params:
47+
- ``key``: the name of setting
48+
- ``default`` (optional): a default value if the setting can not be
49+
retrieved, defaults to ``None``
50+
51+
Returns:
52+
- the ``value`` of the setting if exists or the value
53+
specified in ``default``
54+
"""
3555
return self._settings.get(key, default)
3656

3757
def set(self, key, value, value_type=None):
58+
"""Set a new value for a setting in the database. This
59+
is only possible for settings defined in
60+
``app_settings.DYNAMICSETTINGS_INCLUDE_SETTINGS``.
61+
If a setting is set to its original value it will not attempt to
62+
save the "change" in the database (since its no real change).
63+
64+
Params:
65+
- ``key``: the name of the setting
66+
- ``value``: new new value of the setting
67+
- ``value_type`` (optional): tne new type of the value, if omitted it will
68+
try to resolve the type from the value, should be a string
69+
representing the (Python) type of the setting (for examome
70+
'int', 'str', 'list' ...)
71+
72+
Returns:
73+
- the new value of the setting
74+
75+
Raises:
76+
- ``KeyError`` if the setting can not be set or is not allowed
77+
to set
78+
"""
3879
if not value_type:
3980
value_type = type(value).__name__
4081
if self.can_change(key):
@@ -48,6 +89,18 @@ def set(self, key, value, value_type=None):
4889
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)
4990

5091
def reset(self, key):
92+
"""Reset the value of a setting saved in the database.
93+
Does not work for settings which are not saved in the database
94+
of course.
95+
96+
Params:
97+
- ``key``: the name of the setting
98+
99+
Returns:
100+
- a boolean: ``True`` on success, ``False`` if the setting
101+
could not be reset (either not allowed to reset or not saved
102+
in the database)
103+
"""
51104
if self.can_change(key):
52105
try:
53106
dynamic_setting = models.Settings.objects.get(key=key)
@@ -58,6 +111,18 @@ def reset(self, key):
58111
return False
59112

60113
def dict(self, keys=None):
114+
"""Returns a dict representation of the settings
115+
where the key is the name of the setting and the value
116+
is its value.
117+
118+
Params:
119+
- ``keys`` (optional): a list of setting names (as strings)
120+
which should be included in the dict. If ommitted will show
121+
all settings.
122+
123+
Returns:
124+
- a dict representing the settings
125+
"""
61126
if keys is None:
62127
return self._settings
63128
new_dict = {}
@@ -66,8 +131,14 @@ def dict(self, keys=None):
66131
return new_dict
67132

68133
def is_in_db(self, key):
69-
"""
70-
Check if a setting for a given key is saved in the Database
134+
"""Check if a setting for a given key is saved in the Database
135+
136+
Params:
137+
- ``key``: the name of the setting
138+
139+
Returns:
140+
- a boolean: ``True`` if setting is saved in the database,
141+
``False`` otherwise
71142
"""
72143
try:
73144
models.Settings.objects.get(key=key)
@@ -76,13 +147,21 @@ def is_in_db(self, key):
76147
return True
77148

78149
def can_change(self, key):
79-
"""
150+
"""Check if a setting for a given key can be changed in the Database
151+
80152
A setting can be change in the database if:
81153
a) its not in the global settings, but defined in the
82154
settings from DYNAMICSETTINGS_INCLUDE_MODULES
83155
OR
84156
b) its defined in DYNAMICSETTINGS_INCLUDE_SETTINGS,
85157
even it is in the global settings
158+
159+
Params:
160+
- ``key``: the name of the setting
161+
162+
Returns:
163+
- a boolean: ``True`` if setting can be changed in the database,
164+
``False`` otherwise
86165
"""
87166
try:
88167
setting_value = self.__getattr__(key)
@@ -109,16 +188,6 @@ def _combine_settings(self):
109188
if settings_module is not None:
110189
settings_dict = self._filter_settings(settings_module)
111190
all_settings.update(settings_dict)
112-
#third check for settings defined in DYNAMICSETTINGS_INCLUDE_SETTINGS
113-
#and overwrite them
114-
"""
115-
for setting_name in app_settings.DYNAMICSETTINGS_INCLUDE_SETTINGS:
116-
#try to get it from global settings (if its provided in DYNAMICSETTINGS_INCLUDE_MODULES
117-
#it will be loaded already anyway
118-
setting_value = None
119-
settings_dict = {setting_name: django_settings.__getattr__(setting_name)}
120-
all_settings.update(settings_dict)
121-
"""
122191
#and finally check within the db
123192
db_settings_dict = {}
124193
db_settings = models.Settings.objects.all()
@@ -151,3 +220,4 @@ def __getattr__(self, key):
151220

152221

153222
settings = LazyDynamicSettings()
223+

dynamicsettings/app_settings.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
1111
*Notes*:
1212
13-
- ``myapp.app_settings`` should be an available module in myapp (``myapp/app_settings.py``) somewhere in the project dir or PYTHONPATH
14-
- ``some_globals`` should be an available module either in the project dir or somewhere in the PYTHONPATH: ``some_globals.py``
13+
- ``myapp.app_settings`` should be an available module in myapp
14+
(``myapp/app_settings.py``) somewhere in the project dir or
15+
PYTHONPATH
16+
- ``some_globals`` should be an available module either in the
17+
project dir or somewhere in the PYTHONPATH: ``some_globals.py``
1518
1619
1720
**Note**: if a settings module provided in DYNAMICSETTINGS_INCLUDE_MODULES is not
@@ -28,7 +31,8 @@
2831
*Notes*:
2932
3033
- ``MY_SETTING`` or ``DEFAULT_CACHE_DURATION`` should be an available setting name
31-
either the global django settings or in the ones provided in DYNAMICSETTINGS_INCLUDE_MODULES
34+
either the global django settings or in the ones provided in
35+
DYNAMICSETTINGS_INCLUDE_MODULES
3236
3337
**Note**: if a setting provided in DYNAMICSETTINGS_INCLUDE_SETTINGS is not
3438
available it will raise an exception.

0 commit comments

Comments
 (0)