I would like to make Django support settings defaults for
applications. Ideally defaults would be loaded together with
global_settings, but of course that is impossible as INSTALLED_APPS
must first be configured. So my approach is to add a default setting
if settings doesn't already have the attribute.

I tried two approaches none of which quite work. First I tried
scanning the apps for default_settings.py after INSTALLED_APPS have
been expanded. This works except it causes some import mixup, so admin
won't load. It also seems to run 4 times.

Then I tried making an amend_defaults function on LazySettings. Not
ideal as you would have to explicitly set up defaults in the app
__init__.py. This doesn't work as I can't seem to call it before
settings have already been configured.

This is my patch in progress, any suggestions on how to get around the
admin import problem?

--- a/django/conf/__init__.py
+++ b/django/conf/__init__.py
@@ -70,6 +70,15 @@ class LazySettings(object):
         for name, value in options.items():
             setattr(holder, name, value)
         self._target = holder
+
+    def amend_defaults(self,**extra_defaults):
+        if self._target is None:
+            self._import_settings()
+        print extra_defaults
+        for setting in extra_defaults:
+            if setting == setting.upper() and not
hasattr(self._target,setting):
+                print setting
+                setattr(self._target,setting,extra_defaults[setting])

     def configured(self):
         """
@@ -89,7 +98,7 @@ class Settings(object):
         self.SETTINGS_MODULE = settings_module

         try:
-            mod = __import__(self.SETTINGS_MODULE, {}, {}, [''])
+            mod = __import__(self.SETTINGS_MODULE, {}, {},
[self.SETTINGS_MODULE.split('.')[-1]])
         except ImportError, e:
             raise ImportError, "Could not import settings '%s' (Is it
on sys.path? Does it have syntax errors?): %s" %
(self.SETTINGS_MODULE, e)

@@ -109,7 +118,7 @@ class Settings(object):
         new_installed_apps = []
         for app in self.INSTALLED_APPS:
             if app.endswith('.*'):
-                appdir = os.path.dirname(__import__(app[:-2], {}, {},
['']).__file__)
+                appdir = os.path.dirname(__import__(app[:-2], {}, {},
[app[:-2].split('.')[-1]]).__file__)
                 app_subdirs = os.listdir(appdir)
                 app_subdirs.sort()
                 for d in app_subdirs:
@@ -118,12 +127,24 @@ class Settings(object):
             else:
                 new_installed_apps.append(app)
         self.INSTALLED_APPS = new_installed_apps
-
+        #self.amend_app_defaults(new_installed_apps)
+
         if hasattr(time, 'tzset'):
             # Move the time zone info into os.environ. See ticket
#2315 for why
             # we don't do this unconditionally (breaks Windows).
             os.environ['TZ'] = self.TIME_ZONE
             time.tzset()
+
+    def amend_app_defaults(self,apps):
+        for app in apps:
+            try:
+                defaults = __import__("%s.default_settings" % app,
{}, {}, ['default_settings'])
+                for setting in dir(defaults):
+                    if setting == setting.upper() and not
hasattr(self,setting):
+                        print app+'.'+setting
+
setattr(self,setting,getattr(defaults,setting))
+            except ImportError:
+                pass

     def get_all_members(self):
         return dir(self)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to