Code

Ticket #8960: 8960_View_on_site_does_not_work_if_contrib_sites_is_not_installed.diff

File 8960_View_on_site_does_not_work_if_contrib_sites_is_not_installed.diff, 3.0 KB (added by bmihelac, 6 years ago)
Line 
1Index: django/views/defaults.py
2===================================================================
3--- django/views/defaults.py    (revision 8977)
4+++ django/views/defaults.py    (working copy)
5@@ -25,45 +25,48 @@
6         return http.HttpResponseRedirect(absurl)
7 
8     object_domain = None
9+   
10+    # Look for object domain only if contrib.sites is installed
11+    if Site._meta.installed:
12+        # Otherwise, we need to introspect the object's relationships for a
13+        # relation to the Site object
14+        opts = obj._meta
15 
16-    # Otherwise, we need to introspect the object's relationships for a
17-    # relation to the Site object
18-    opts = obj._meta
19-
20-    # First, look for an many-to-many relationship to sites
21-    for field in opts.many_to_many:
22-        if field.rel.to is Site:
23-            try:
24-                object_domain = getattr(obj, field.name).all()[0].domain
25-            except IndexError:
26-                pass
27-            if object_domain is not None:
28-                break
29-
30-    # Next look for a many-to-one relationship to site
31-    if object_domain is None:
32-        for field in obj._meta.fields:
33-            if field.rel and field.rel.to is Site:
34+        # First, look for an many-to-many relationship to sites
35+        for field in opts.many_to_many:
36+            if field.rel.to is Site:
37                 try:
38-                    object_domain = getattr(obj, field.name).domain
39-                except Site.DoesNotExist:
40+                    object_domain = getattr(obj, field.name).all()[0].domain
41+                except IndexError:
42                     pass
43                 if object_domain is not None:
44                     break
45 
46-    # Fall back to the current site (if possible)
47-    if object_domain is None:
48-        try:
49-            object_domain = Site.objects.get_current().domain
50-        except Site.DoesNotExist:
51-            pass
52+        # Next look for a many-to-one relationship to site
53+        if object_domain is None:
54+            for field in obj._meta.fields:
55+                if field.rel and field.rel.to is Site:
56+                    try:
57+                        object_domain = getattr(obj, field.name).domain
58+                    except Site.DoesNotExist:
59+                        pass
60+                    if object_domain is not None:
61+                        break
62 
63+        # Fall back to the current site (if possible)
64+        if object_domain is None:
65+            try:
66+                object_domain = Site.objects.get_current().domain
67+            except (Site.DoesNotExist):
68+                pass
69+
70     # If all that malarkey found an object domain, use it; otherwise fall back
71     # to whatever get_absolute_url() returned.
72     if object_domain is not None:
73         protocol = request.is_secure() and 'https' or 'http'
74         return http.HttpResponseRedirect('%s://%s%s' % (protocol, object_domain, absurl))
75     else:
76+        print absurl
77         return http.HttpResponseRedirect(absurl)
78 
79 def page_not_found(request, template_name='404.html'):