Author: arthurk
Date: 2010-06-28 15:22:42 -0500 (Mon, 28 Jun 2010)
New Revision: 13403

Modified:
   django/branches/soc2010/app-loading/django/core/apps.py
   django/branches/soc2010/app-loading/django/db/models/loading.py
Log:
[soc2010/app-loading] save models in app; adjust get_model/get_models

Modified: django/branches/soc2010/app-loading/django/core/apps.py
===================================================================
--- django/branches/soc2010/app-loading/django/core/apps.py     2010-06-24 
11:21:10 UTC (rev 13402)
+++ django/branches/soc2010/app-loading/django/core/apps.py     2010-06-28 
20:22:42 UTC (rev 13403)
@@ -1,9 +1,11 @@
 class App(object):
-    def __init__(self, name, models):
-        # fully qualified name (e.g. 'django.contrib.auth')
-        self.name = name
-        self.label = name.split('.')[-1]
-        self.models = models
+    def __init__(self, label):
+        if '.' in label:
+            label = label.split('.')[-1]
+        self.label = label
+        self.errors = {}
+        self.models = []
+        self.models_module = None
 
     def __repr__(self):
-        return '<App: %s>' % self.name
+        return '<App: %s>' % self.label

Modified: django/branches/soc2010/app-loading/django/db/models/loading.py
===================================================================
--- django/branches/soc2010/app-loading/django/db/models/loading.py     
2010-06-24 11:21:10 UTC (rev 13402)
+++ django/branches/soc2010/app-loading/django/db/models/loading.py     
2010-06-28 20:22:42 UTC (rev 13403)
@@ -46,6 +46,9 @@
 
     def __init__(self):
         self.__dict__ = self.__shared_state
+        # Create App instances for the apps in INSTALLED_APPS
+        for app_name in settings.INSTALLED_APPS:
+            self.app_instances.append(App(app_name))
 
     def _populate(self):
         """
@@ -103,9 +106,17 @@
         self.nesting_level -= 1
         if models not in self.app_store:
             self.app_store[models] = len(self.app_store)
-            self.app_instances.append(App(app_name, models))
+            app = self.find_app(app_name.split('.')[-1])
+            if app:
+                app.models_module = models
         return models
 
+    def find_app(self, app_label):
+        "Returns the App instance that matches app_label"
+        for app in self.app_instances:
+            if app.label == app_label:
+                return app
+
     def app_cache_ready(self):
         """
         Returns true if the model cache is fully populated.
@@ -149,7 +160,9 @@
     def get_app_errors(self):
         "Returns the map of known problems with the INSTALLED_APPS."
         self._populate()
-        return self.app_errors
+        for app in app_instances:
+            self.app_errors.update(app.errors)
+        return errors
 
     def get_models(self, app_mod=None, include_auto_created=False, 
include_deferred=False):
         """
@@ -173,11 +186,13 @@
         if app_mod:
             app_list = [self.app_models.get(app_mod.__name__.split('.')[-2], 
SortedDict())]
         else:
-            app_list = self.app_models.itervalues()
+            #app_list = self.app_models.itervalues()
+            app_list = self.app_instances
         model_list = []
         for app in app_list:
+            models = app.models
             model_list.extend(
-                model for model in app.values()
+                model for model in models#app.values()
                 if ((not model._deferred or include_deferred)
                     and (not model._meta.auto_created or include_auto_created))
             )
@@ -193,12 +208,22 @@
         """
         if seed_cache:
             self._populate()
-        return self.app_models.get(app_label, 
SortedDict()).get(model_name.lower())
+        app = self.find_app(app_label)
+        if app:
+            for model in app.models:
+                if model_name.lower() == model._meta.object_name.lower():
+                    return model
 
     def register_models(self, app_label, *models):
         """
         Register a set of models as belonging to an app.
         """
+        # Create a new App instance if an app in INSTALLED_APPS
+        # imports another package that has models
+        app = self.find_app(app_label)
+        if not app:
+            app = App(app_label)
+            self.app_instances.append(app)
         for model in models:
             # Store as 'name: model' pair in a dictionary
             # in the app_models dictionary
@@ -216,6 +241,7 @@
                 if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]:
                     continue
             model_dict[model_name] = model
+            app.models.append(model)
         self._get_models_cache.clear()
 
 cache = AppCache()

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to