Commits

and...@69d324d9-c39d-4fdc-8679-7745eae9e2c8  committed 1aaf190

Alias-fixing code (#134). Needs a fix for cross-app importing after this, though.

  • Participants
  • Parent commits b448b13

Comments (0)

Files changed (2)

File south/modelsparser.py

         return " " + repr(tree)
 
 
+def isclass(obj):
+    "Simple test to see if something is a class."
+    return issubclass(type(obj), type)
+
+
+def aliased_models(module):
+    """
+    Given a models module, returns a dict mapping all alias imports of models
+    (e.g. import Foo as Bar) back to their original names. Bug #134.
+    """
+    aliases = {}
+    for name, obj in module.__dict__.items():
+        if isclass(obj) and issubclass(obj, models.Model):
+            # Test to see if this has a different name to what it should
+            if name != obj._meta.object_name:
+                aliases[name] = obj._meta.object_name
+    return aliases
+    
+
+
 class STTree(object):
     
     "A syntax tree wrapper class."
     possible_field_defs = tree.find("^ > classdef > suite > stmt > simple_stmt > small_stmt > expr_stmt")
     field_defs = {}
     
+    # Get aliases, ready for alias fixing (#134)
+    aliases = aliased_models(models.get_app(model._meta.app_label))
+    
     # Go through all the found defns, and try to parse them
     for pfd in possible_field_defs:
         field = extract_field(pfd)
         else:
             fields[fieldname] = None
     
-    # Now, try seeing if we can resolve the values of defaults.
+    # Now, try seeing if we can resolve the values of defaults, and fix aliases.
     for field, defn in fields.items():
         
         if not isinstance(defn, (list, tuple)):
             continue # We don't have a defn for this one, or it's a string
         
+        # Fix aliases if we can (#134)
+        for i, arg in enumerate(defn[1]):
+            if arg in aliases:
+                defn[1][i] = aliases[arg]
+        
+        # Fix defaults if we can
         for arg, val in defn[2].items():
             if arg in ['default']:
                 try:

File south/tests/fakeapp/models.py

 # -*- coding: UTF-8 -*-
 
 from django.db import models
-from django.contrib.auth.models import User
+from django.contrib.auth.models import User as UserAlias
 
 def default_func():
     return "yays"
     o2 = models.ForeignKey('Other2')
     
     # Now to something outside
-    user = models.ForeignKey(User, related_name="horribles")
+    user = models.ForeignKey(UserAlias, related_name="horribles")
     
     # Unicode!
     code = models.CharField(max_length=25, default="↑↑↓↓←→←→BA")