Django

Code

root/django/branches/gis/django/views/defaults.py

Revision 7768, 3.3 kB (checked in by jbronn, 5 months ago)

gis: Merged revisions 7643-7662,7667-7668,7672-7682,7686-7693,7695-7698,7700-7702,7704-7706,7710,7712-7729,7731-7732,7738-7758,7760-7766 via svnmerge from trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to LastChangedRevision
Line 
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 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)
68
69 def page_not_found(request, template_name='404.html'):
70     """
71     Default 404 handler.
72
73     Templates: `404.html`
74     Context:
75         request_path
76             The path of the requested URL (e.g., '/app/pages/bad_page/')
77     """
78     t = loader.get_template(template_name) # You need to create a 404.html template.
79     return http.HttpResponseNotFound(t.render(RequestContext(request, {'request_path': request.path})))
80
81 def server_error(request, template_name='500.html'):
82     """
83     500 error handler.
84
85     Templates: `500.html`
86     Context: None
87     """
88     t = loader.get_template(template_name) # You need to create a 500.html template.
89     return http.HttpResponseServerError(t.render(Context({})))
Note: See TracBrowser for help on using the browser.