Code

Ticket #8960: 8960_View_on_site_does_not_work_if_contrib_sites_is_not_installed.2.diff

File 8960_View_on_site_does_not_work_if_contrib_sites_is_not_installed.2.diff, 3.1 KB (added by matthijs, 6 years ago)

Updated version

Line 
1Index: django/contrib/contenttypes/views.py
2===================================================================
3--- django/contrib/contenttypes/views.py        (revision 9294)
4+++ django/contrib/contenttypes/views.py        (working copy)
5@@ -23,41 +23,43 @@
6     if absurl.startswith('http://') or absurl.startswith('https://'):
7         return http.HttpResponseRedirect(absurl)
8 
9-    # Otherwise, we need to introspect the object's relationships for a
10-    # relation to the Site object
11     object_domain = None
12-    opts = obj._meta
13 
14-    # First, look for an many-to-many relationship to Site.
15-    for field in opts.many_to_many:
16-        if field.rel.to is Site:
17-            try:
18-                # Caveat: In the case of multiple related Sites, this just
19-                # selects the *first* one, which is arbitrary.
20-                object_domain = getattr(obj, field.name).all()[0].domain
21-            except IndexError:
22-                pass
23-            if object_domain is not None:
24-                break
25+    # Look for object domain only if contrib.sites is installed
26+    if Site._meta.installed:
27+        # Otherwise, we need to introspect the object's relationships for a
28+        # relation to the Site object
29+        opts = obj._meta
30 
31-    # Next, look for a many-to-one relationship to Site.
32-    if object_domain is None:
33-        for field in obj._meta.fields:
34-            if field.rel and field.rel.to is Site:
35+        # First, look for an many-to-many relationship to Site.
36+        for field in opts.many_to_many:
37+            if field.rel.to is Site:
38                 try:
39-                    object_domain = getattr(obj, field.name).domain
40-                except Site.DoesNotExist:
41+                    # Caveat: In the case of multiple related Sites, this just
42+                    # selects the *first* one, which is arbitrary.
43+                    object_domain = getattr(obj, field.name).all()[0].domain
44+                except IndexError:
45                     pass
46                 if object_domain is not None:
47                     break
48 
49-    # Fall back to the current site (if possible).
50-    if object_domain is None:
51-        try:
52-            object_domain = Site.objects.get_current().domain
53-        except Site.DoesNotExist:
54-            pass
55+        # Next, look for a many-to-one relationship to Site.
56+        if object_domain is None:
57+            for field in obj._meta.fields:
58+                if field.rel and field.rel.to is Site:
59+                    try:
60+                        object_domain = getattr(obj, field.name).domain
61+                    except Site.DoesNotExist:
62+                        pass
63+                    if object_domain is not None:
64+                        break
65 
66+        # Fall back to the current site (if possible).
67+        if object_domain is None:
68+            try:
69+                object_domain = Site.objects.get_current().domain
70+            except Site.DoesNotExist:
71+                pass
72     # If all that malarkey found an object domain, use it. Otherwise, fall back
73     # to whatever get_absolute_url() returned.
74     if object_domain is not None: