Author: arthurk
Date: 2010-08-10 20:39:37 -0500 (Tue, 10 Aug 2010)
New Revision: 13570

Added:
   
django/branches/soc2010/app-loading/tests/appcachetests/model_app/othermodels.py
Modified:
   django/branches/soc2010/app-loading/django/core/apps.py
   django/branches/soc2010/app-loading/tests/appcachetests/model_app/__init__.py
   django/branches/soc2010/app-loading/tests/appcachetests/runtests.py
Log:
[soc2010/app-loading] load custom models if specified with models_path attribute

Modified: django/branches/soc2010/app-loading/django/core/apps.py
===================================================================
--- django/branches/soc2010/app-loading/django/core/apps.py     2010-08-10 
01:16:51 UTC (rev 13569)
+++ django/branches/soc2010/app-loading/django/core/apps.py     2010-08-11 
01:39:37 UTC (rev 13570)
@@ -25,7 +25,6 @@
         # errors raised when trying to import the app
         self.errors = []
         self.models = []
-        self.models_module = None
 
     def __repr__(self):
         return '<App: %s>' % self.name
@@ -112,8 +111,15 @@
             app_instance = app_class(app_instance_name)
             self.app_instances.append(app_instance)
 
+        # check if the app instance specifies a path to models
+        # if not, we use the models.py file from the package dir
         try:
-            models = import_module('.models', app_name)
+            models_path = app_instance.models_path
+        except AttributeError:
+            models_path = '%s.models' % app_name
+
+        try:
+            models = import_module(models_path)
         except ImportError:
             self.nesting_level -= 1
             # If the app doesn't have a models module, we can just ignore the
@@ -135,9 +141,7 @@
                     raise
 
         self.nesting_level -= 1
-        app = self.find_app(app_name.split('.')[-1])
-        if app and models is not app.models_module:
-            app.models_module = models
+        app_instance.models_module = models
         return models
 
     def find_app(self, name):

Modified: 
django/branches/soc2010/app-loading/tests/appcachetests/model_app/__init__.py
===================================================================
--- 
django/branches/soc2010/app-loading/tests/appcachetests/model_app/__init__.py   
    2010-08-10 01:16:51 UTC (rev 13569)
+++ 
django/branches/soc2010/app-loading/tests/appcachetests/model_app/__init__.py   
    2010-08-11 01:39:37 UTC (rev 13570)
@@ -0,0 +1,8 @@
+from django.core.apps import App
+
+class MyApp(App):
+    models_path = 'model_app.othermodels'
+
+    def __repr__(self):
+        return '<MyApp: %s>' % self.name
+

Added: 
django/branches/soc2010/app-loading/tests/appcachetests/model_app/othermodels.py
===================================================================
--- 
django/branches/soc2010/app-loading/tests/appcachetests/model_app/othermodels.py
                            (rev 0)
+++ 
django/branches/soc2010/app-loading/tests/appcachetests/model_app/othermodels.py
    2010-08-11 01:39:37 UTC (rev 13570)
@@ -0,0 +1,5 @@
+from django.db import models
+
+class Person(models.Model):
+    first_name = models.CharField(max_length=30)
+    last_name = models.CharField(max_length=30)

Modified: django/branches/soc2010/app-loading/tests/appcachetests/runtests.py
===================================================================
--- django/branches/soc2010/app-loading/tests/appcachetests/runtests.py 
2010-08-10 01:16:51 UTC (rev 13569)
+++ django/branches/soc2010/app-loading/tests/appcachetests/runtests.py 
2010-08-11 01:39:37 UTC (rev 13570)
@@ -221,10 +221,9 @@
         app = cache.app_instances[0]
         self.assertEqual(len(cache.app_instances), 1)
         self.assertEqual(app.name, 'nomodel_app')
-        self.assertEqual(app.models_module, None)
         self.assertEqual(rv, None)
 
-    def test_load_app_custom(self):
+    def test_custom_app(self):
         """
         Test that a custom app instance is created if the function
         gets passed a classname
@@ -235,11 +234,19 @@
         self.assertEqual(len(cache.app_instances), 1)
         self.assertEqual(app.name, 'nomodel_app')
         self.assertTrue(isinstance(app, MyApp))
-        self.assertEqual(app.models_module, None)
         self.assertEqual(rv, None)
 
-    def test_load_app_twice(self):
+    def test_custom_models_path(self):
         """
+        Test that custom models are imported correctly 
+        """
+        rv = cache.load_app('model_app.MyApp')
+        app = cache.app_instances[0]
+        self.assertEqual(app.models_module.__name__, 'model_app.othermodels')
+        self.assertEqual(rv.__name__, 'model_app.othermodels')
+
+    def test_twice(self):
+        """
         Test that loading an app twice results in only one app instance
         """
         rv = cache.load_app('model_app')
@@ -248,7 +255,7 @@
         self.assertEqual(rv.__name__, 'model_app.models')
         self.assertEqual(rv2.__name__, 'model_app.models')
 
-    def test_load_app_importerror(self):
+    def test_importerror(self):
         """
         Test that an ImportError exception is raised if a package cannot
         be imported

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" 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-updates?hl=en.

Reply via email to