Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moved the bulk of the shortcut() function in django/views/defaults.py…

… to a new module, django/contrib/contenttypes/views.py. As a result, django/views/defaults.py no longer relies on django.contrib.contenttypes. Of course, the shortcut() function is still available in the former module, for backwards compatibility. See the new FutureBackwardsIncompatibleChanges wiki page.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9001 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 40cb11a55495c187bfd713b6e85dda72f076346b 1 parent 5cc9350
Adrian Holovaty authored September 10, 2008
4  django/contrib/admin/sites.py
@@ -148,9 +148,9 @@ def root(self, request, url):
148 148
             return self.password_change_done(request)
149 149
         elif url == 'jsi18n':
150 150
             return self.i18n_javascript(request)
151  
-        # urls starting with 'r/' are for the "show in web" links
  151
+        # URLs starting with 'r/' are for the "View on site" links.
152 152
         elif url.startswith('r/'):
153  
-            from django.views.defaults import shortcut
  153
+            from django.contrib.contenttypes.views import shortcut
154 154
             return shortcut(request, *url.split('/')[1:])
155 155
         else:
156 156
             if '/' in url:
67  django/contrib/contenttypes/views.py
... ...
@@ -0,0 +1,67 @@
  1
+from django import http
  2
+from django.contrib.contenttypes.models import ContentType
  3
+from django.contrib.sites.models import Site
  4
+from django.core.exceptions import ObjectDoesNotExist
  5
+
  6
+def shortcut(request, content_type_id, object_id):
  7
+    "Redirect to an object's page based on a content-type ID and an object ID."
  8
+    # Look up the object, making sure it's got a get_absolute_url() function.
  9
+    try:
  10
+        content_type = ContentType.objects.get(pk=content_type_id)
  11
+        obj = content_type.get_object_for_this_type(pk=object_id)
  12
+    except ObjectDoesNotExist:
  13
+        raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id))
  14
+    try:
  15
+        absurl = obj.get_absolute_url()
  16
+    except AttributeError:
  17
+        raise http.Http404("%s objects don't have get_absolute_url() methods" % content_type.name)
  18
+
  19
+    # Try to figure out the object's domain, so we can do a cross-site redirect
  20
+    # if necessary.
  21
+
  22
+    # If the object actually defines a domain, we're done.
  23
+    if absurl.startswith('http://') or absurl.startswith('https://'):
  24
+        return http.HttpResponseRedirect(absurl)
  25
+
  26
+    # Otherwise, we need to introspect the object's relationships for a
  27
+    # relation to the Site object
  28
+    object_domain = None
  29
+    opts = obj._meta
  30
+
  31
+    # First, look for an many-to-many relationship to Site.
  32
+    for field in opts.many_to_many:
  33
+        if field.rel.to is Site:
  34
+            try:
  35
+                # Caveat: In the case of multiple related Sites, this just
  36
+                # selects the *first* one, which is arbitrary.
  37
+                object_domain = getattr(obj, field.name).all()[0].domain
  38
+            except IndexError:
  39
+                pass
  40
+            if object_domain is not None:
  41
+                break
  42
+
  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:
  56
+        try:
  57
+            object_domain = Site.objects.get_current().domain
  58
+        except Site.DoesNotExist:
  59
+            pass
  60
+
  61
+    # If all that malarkey found an object domain, use it. Otherwise, fall back
  62
+    # to whatever get_absolute_url() returned.
  63
+    if object_domain is not None:
  64
+        protocol = request.is_secure() and 'https' or 'http'
  65
+        return http.HttpResponseRedirect('%s://%s%s' % (protocol, object_domain, absurl))
  66
+    else:
  67
+        return http.HttpResponseRedirect(absurl)
78  django/views/defaults.py
... ...
@@ -1,70 +1,5 @@
1  
-from django.core.exceptions import ObjectDoesNotExist
2  
-from django.template import Context, RequestContext, loader
3  
-from django.contrib.contenttypes.models import ContentType
4  
-from django.contrib.sites.models import Site
5 1
 from django import http
6  
-
7  
-def shortcut(request, content_type_id, object_id):
8  
-    "Redirect to an object's page based on a content-type ID and an object ID."
9  
-    # Look up the object, making sure it's got a get_absolute_url() function.
10  
-    try:
11  
-        content_type = ContentType.objects.get(pk=content_type_id)
12  
-        obj = content_type.get_object_for_this_type(pk=object_id)
13  
-    except ObjectDoesNotExist:
14  
-        raise http.Http404, "Content type %s object %s doesn't exist" % (content_type_id, object_id)
15  
-    try:
16  
-        absurl = obj.get_absolute_url()
17  
-    except AttributeError:
18  
-        raise http.Http404, "%s objects don't have get_absolute_url() methods" % content_type.name
19  
-
20  
-    # Try to figure out the object's domain, so we can do a cross-site redirect
21  
-    # if necessary.
22  
-
23  
-    # If the object actually defines a domain, we're done.
24  
-    if absurl.startswith('http://') or absurl.startswith('https://'):
25  
-        return http.HttpResponseRedirect(absurl)
26  
-
27  
-    object_domain = None
28  
-
29  
-    # Otherwise, we need to introspect the object's relationships for a
30  
-    # relation to the Site object
31  
-    opts = obj._meta
32  
-
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 IndexError:
39  
-                pass
40  
-            if object_domain is not None:
41  
-                break
42  
-
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:
56  
-        try:
57  
-            object_domain = Site.objects.get_current().domain
58  
-        except Site.DoesNotExist:
59  
-            pass
60  
-
61  
-    # If all that malarkey found an object domain, use it; otherwise fall back
62  
-    # to whatever get_absolute_url() returned.
63  
-    if object_domain is not None:
64  
-        protocol = request.is_secure() and 'https' or 'http'
65  
-        return http.HttpResponseRedirect('%s://%s%s' % (protocol, object_domain, absurl))
66  
-    else:
67  
-        return http.HttpResponseRedirect(absurl)
  2
+from django.template import Context, RequestContext, loader
68 3
 
69 4
 def page_not_found(request, template_name='404.html'):
70 5
     """
@@ -87,3 +22,14 @@ def server_error(request, template_name='500.html'):
87 22
     """
88 23
     t = loader.get_template(template_name) # You need to create a 500.html template.
89 24
     return http.HttpResponseServerError(t.render(Context({})))
  25
+
  26
+def shortcut(request, content_type_id, object_id):
  27
+    # TODO: Remove this in Django 2.0.
  28
+    # This is a legacy view that depends on the contenttypes framework.
  29
+    # The core logic was moved to django.contrib.contenttypes.views after
  30
+    # Django 1.0, but this remains here for backwards compatibility.
  31
+    # Note that the import is *within* this function, rather than being at
  32
+    # module level, because we don't want to assume people have contenttypes
  33
+    # installed.
  34
+    from django.contrib.contenttypes.views import shortcut as real_shortcut
  35
+    return real_shortcut(request, content_type_id, object_id)

0 notes on commit 40cb11a

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