~kkubasik/django/aggregation-branch

« back to all changes in this revision

Viewing changes to django/views/defaults.py

  • Committer: adrian
  • Date: 2006-05-02 01:31:56 UTC
  • Revision ID: vcs-imports@canonical.com-20060502013156-2941fcd40d080649
MERGED MAGIC-REMOVAL BRANCH TO TRUNK. This change is highly backwards-incompatible. Please read http://code.djangoproject.com/wiki/RemovingTheMagic for upgrade instructions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from django.core.exceptions import Http404, ObjectDoesNotExist
2
 
from django.core.template import Context, loader
3
 
from django.models.core import sites, contenttypes
4
 
from django.utils import httpwrappers
 
1
from django.core.exceptions import ObjectDoesNotExist
 
2
from django.template import Context, loader
 
3
from django.contrib.contenttypes.models import ContentType
 
4
from django.contrib.sites.models import Site
 
5
from django import http
5
6
 
6
7
def shortcut(request, content_type_id, object_id):
7
8
    "Redirect to an object's page based on a content-type ID and an object ID."
8
9
    # Look up the object, making sure it's got a get_absolute_url() function.
9
10
    try:
10
 
        content_type = contenttypes.get_object(pk=content_type_id)
 
11
        content_type = ContentType.objects.get(pk=content_type_id)
11
12
        obj = content_type.get_object_for_this_type(pk=object_id)
12
13
    except ObjectDoesNotExist:
13
 
        raise Http404, "Content type %s object %s doesn't exist" % (content_type_id, object_id)
 
14
        raise http.Http404, "Content type %s object %s doesn't exist" % (content_type_id, object_id)
14
15
    try:
15
16
        absurl = obj.get_absolute_url()
16
17
    except AttributeError:
17
 
        raise Http404, "%s objects don't have get_absolute_url() methods" % content_type.name
 
18
        raise http.Http404, "%s objects don't have get_absolute_url() methods" % content_type.name
18
19
 
19
20
    # Try to figure out the object's domain, so we can do a cross-site redirect
20
21
    # if necessary.
21
22
 
22
23
    # If the object actually defines a domain, we're done.
23
24
    if absurl.startswith('http://'):
24
 
        return httpwrappers.HttpResponseRedirect(absurl)
 
25
        return http.HttpResponseRedirect(absurl)
25
26
 
26
27
    object_domain = None
27
28
 
28
 
    # Next, look for an many-to-many relationship to sites
29
 
    if hasattr(obj, 'get_site_list'):
30
 
        site_list = obj.get_site_list()
31
 
        if site_list:
32
 
            object_domain = site_list[0].domain
33
 
 
34
 
    # Next, look for a many-to-one relationship to sites
35
 
    elif hasattr(obj, 'get_site'):
 
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 Site.DoesNotExist:
 
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:
36
56
        try:
37
 
            object_domain = obj.get_site().domain
38
 
        except sites.SiteDoesNotExist:
 
57
            object_domain = Site.objects.get_current().domain
 
58
        except Site.DoesNotExist:
39
59
            pass
40
60
 
41
 
    # Then, fall back to the current site (if possible)
 
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
        return http.HttpResponseRedirect('http://%s%s' % (object_domain, absurl))
42
65
    else:
43
 
        try:
44
 
            object_domain = sites.get_current().domain
45
 
        except sites.SiteDoesNotExist:
46
 
            # Finally, give up and use a URL without the domain name
47
 
            return httpwrappers.HttpResponseRedirect(obj.get_absolute_url())
48
 
    return httpwrappers.HttpResponseRedirect('http://%s%s' % (object_domain, obj.get_absolute_url()))
 
66
        return http.HttpResponseRedirect(absurl)
49
67
 
50
 
def page_not_found(request, template_name='404'):
 
68
def page_not_found(request, template_name='404.html'):
51
69
    """
52
70
    Default 404 handler, which looks for the requested URL in the redirects
53
71
    table, redirects if found, and displays 404 page if not redirected.
54
72
 
55
 
    Templates: `404`
56
 
    Context: None
 
73
    Templates: `404.html`
 
74
    Context:
 
75
        request_path
 
76
            The path of the requested URL (e.g., '/app/pages/bad_page/')
57
77
    """
58
78
    t = loader.get_template(template_name)
59
 
    return httpwrappers.HttpResponseNotFound(t.render(Context()))
 
79
    return http.HttpResponseNotFound(t.render(Context({'request_path': request.path})))
60
80
 
61
 
def server_error(request, template_name='500'):
 
81
def server_error(request, template_name='500.html'):
62
82
    """
63
83
    500 error handler.
64
84
 
65
 
    Templates: `500`
 
85
    Templates: `500.html`
66
86
    Context: None
67
87
    """
68
88
    t = loader.get_template(template_name)
69
 
    return httpwrappers.HttpResponseServerError(t.render(Context()))
 
89
    return http.HttpResponseServerError(t.render(Context()))