diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2006-05-02 01:31:56 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2006-05-02 01:31:56 +0000 |
| commit | f69cf70ed813a8cd7e1f963a14ae39103e8d5265 (patch) | |
| tree | d3b32e84cd66573b3833ddf662af020f8ef2f7a8 /django/views/defaults.py | |
| parent | d5dbeaa9be359a4c794885c2e9f1b5a7e5e51fb8 (diff) | |
MERGED MAGIC-REMOVAL BRANCH TO TRUNK. This change is highly backwards-incompatible. Please read http://code.djangoproject.com/wiki/RemovingTheMagic for upgrade instructions.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@2809 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/views/defaults.py')
| -rw-r--r-- | django/views/defaults.py | 82 |
1 files changed, 51 insertions, 31 deletions
diff --git a/django/views/defaults.py b/django/views/defaults.py index 95c18b4263..d5460a7495 100644 --- a/django/views/defaults.py +++ b/django/views/defaults.py @@ -1,69 +1,89 @@ -from django.core.exceptions import Http404, ObjectDoesNotExist -from django.core.template import Context, loader -from django.models.core import sites, contenttypes -from django.utils import httpwrappers +from django.core.exceptions import ObjectDoesNotExist +from django.template import Context, loader +from django.contrib.contenttypes.models import ContentType +from django.contrib.sites.models import Site +from django import http def shortcut(request, content_type_id, object_id): "Redirect to an object's page based on a content-type ID and an object ID." # Look up the object, making sure it's got a get_absolute_url() function. try: - content_type = contenttypes.get_object(pk=content_type_id) + content_type = ContentType.objects.get(pk=content_type_id) obj = content_type.get_object_for_this_type(pk=object_id) except ObjectDoesNotExist: - raise Http404, "Content type %s object %s doesn't exist" % (content_type_id, object_id) + raise http.Http404, "Content type %s object %s doesn't exist" % (content_type_id, object_id) try: absurl = obj.get_absolute_url() except AttributeError: - raise Http404, "%s objects don't have get_absolute_url() methods" % content_type.name + raise http.Http404, "%s objects don't have get_absolute_url() methods" % content_type.name # Try to figure out the object's domain, so we can do a cross-site redirect # if necessary. # If the object actually defines a domain, we're done. if absurl.startswith('http://'): - return httpwrappers.HttpResponseRedirect(absurl) + return http.HttpResponseRedirect(absurl) object_domain = None - # Next, look for an many-to-many relationship to sites - if hasattr(obj, 'get_site_list'): - site_list = obj.get_site_list() - if site_list: - object_domain = site_list[0].domain + # Otherwise, we need to introspect the object's relationships for a + # relation to the Site object + opts = obj._meta - # Next, look for a many-to-one relationship to sites - elif hasattr(obj, 'get_site'): + # First, look for an many-to-many relationship to sites + for field in opts.many_to_many: + if field.rel.to is Site: + try: + object_domain = getattr(obj, field.name).all()[0].domain + except Site.DoesNotExist: + pass + if object_domain is not None: + break + + # Next look for a many-to-one relationship to site + if object_domain is None: + for field in obj._meta.fields: + if field.rel and field.rel.to is Site: + try: + object_domain = getattr(obj, field.name).domain + except Site.DoesNotExist: + pass + if object_domain is not None: + break + + # Fall back to the current site (if possible) + if object_domain is None: try: - object_domain = obj.get_site().domain - except sites.SiteDoesNotExist: + object_domain = Site.objects.get_current().domain + except Site.DoesNotExist: pass - # Then, fall back to the current site (if possible) + # If all that malarkey found an object domain, use it; otherwise fall back + # to whatever get_absolute_url() returned. + if object_domain is not None: + return http.HttpResponseRedirect('http://%s%s' % (object_domain, absurl)) else: - try: - object_domain = sites.get_current().domain - except sites.SiteDoesNotExist: - # Finally, give up and use a URL without the domain name - return httpwrappers.HttpResponseRedirect(obj.get_absolute_url()) - return httpwrappers.HttpResponseRedirect('http://%s%s' % (object_domain, obj.get_absolute_url())) + return http.HttpResponseRedirect(absurl) -def page_not_found(request, template_name='404'): +def page_not_found(request, template_name='404.html'): """ Default 404 handler, which looks for the requested URL in the redirects table, redirects if found, and displays 404 page if not redirected. - Templates: `404` - Context: None + Templates: `404.html` + Context: + request_path + The path of the requested URL (e.g., '/app/pages/bad_page/') """ t = loader.get_template(template_name) - return httpwrappers.HttpResponseNotFound(t.render(Context())) + return http.HttpResponseNotFound(t.render(Context({'request_path': request.path}))) -def server_error(request, template_name='500'): +def server_error(request, template_name='500.html'): """ 500 error handler. - Templates: `500` + Templates: `500.html` Context: None """ t = loader.get_template(template_name) - return httpwrappers.HttpResponseServerError(t.render(Context())) + return http.HttpResponseServerError(t.render(Context())) |
