Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

magic-removal: fixed #1425 -- django.views.defaults.shortcut now work…

…s correctly (thanks, Christopher Lenz)

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2615 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 537f01c1624f5624eb83546dd74d6a39a0c3f212 1 parent 3825cb0
Jacob Kaplan-Moss authored April 05, 2006

Showing 1 changed file with 34 additions and 17 deletions. Show diff stats Hide diff stats

  1. 51  django/views/defaults.py
51  django/views/defaults.py
@@ -26,28 +26,45 @@ def shortcut(request, content_type_id, object_id):
26 26
 
27 27
     object_domain = None
28 28
 
29  
-    # Next, look for an many-to-many relationship to sites
30  
-    if hasattr(obj, 'get_site_list'):
31  
-        site_list = obj.get_site_list()
32  
-        if site_list:
33  
-            object_domain = site_list[0].domain
  29
+    # Otherwise, we need to introspect the object's relationships for a 
  30
+    # relation to the Site object
  31
+    opts = obj._meta
34 32
 
35  
-    # Next, look for a many-to-one relationship to sites
36  
-    elif hasattr(obj, 'get_site'):
37  
-        try:
38  
-            object_domain = obj.get_site().domain
39  
-        except Site.DoesNotExist:
40  
-            pass
  33
+    # First, look for an many-to-many relationship to sites
  34
+    for field in opts.many_to_many:
  35
+        if field.rel.to is Site:
  36
+            try:
  37
+                object_domain = getattr(obj, field.name).all()[0].domain
  38
+            except Site.DoesNotExist:
  39
+                pass
  40
+            if object_domain is not None:
  41
+                break
41 42
 
42  
-    # Then, fall back to the current site (if possible)
43  
-    else:
  43
+    # Next look for a many-to-one relationship to site
  44
+    if object_domain is None:
  45
+        for field in obj._meta.fields:
  46
+            if field.rel and field.rel.to is Site:
  47
+                try:
  48
+                    object_domain = getattr(obj, field.name).domain
  49
+                except Site.DoesNotExist:
  50
+                    pass
  51
+                if object_domain is not None:
  52
+                    break
  53
+
  54
+    # Fall back to the current site (if possible)
  55
+    if object_domain is None:
44 56
         try:
45 57
             object_domain = Site.objects.get_current().domain
46 58
         except Site.DoesNotExist:
47  
-            # Finally, give up and use a URL without the domain name
48  
-            return http.HttpResponseRedirect(obj.get_absolute_url())
49  
-    return http.HttpResponseRedirect('http://%s%s' % (object_domain, obj.get_absolute_url()))
50  
-
  59
+            pass
  60
+            
  61
+    # If all that malarky found an object domain, use it; otherwise fall back
  62
+    # to whatever get_absolute_url() returned.
  63
+    if object_domain is not None:
  64
+        return http.HttpResponseRedirect('http://%s%s' % (object_domain, absurl))
  65
+    else:
  66
+        return http.HttpResponseRedirect(absurl)
  67
+    
51 68
 def page_not_found(request, template_name='404'):
52 69
     """
53 70
     Default 404 handler, which looks for the requested URL in the redirects

0 notes on commit 537f01c

Please sign in to comment.
Something went wrong with that request. Please try again.