2006-05-02 09:31:56 +08:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2006-06-23 00:28:40 +08:00
|
|
|
from django.template import Context, RequestContext, loader
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
from django import http
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def shortcut(request, content_type_id, object_id):
|
2005-11-11 12:45:05 +08:00
|
|
|
"Redirect to an object's page based on a content-type ID and an object ID."
|
2005-10-26 22:01:53 +08:00
|
|
|
# Look up the object, making sure it's got a get_absolute_url() function.
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2006-05-02 09:31:56 +08:00
|
|
|
content_type = ContentType.objects.get(pk=content_type_id)
|
2005-07-27 00:11:43 +08:00
|
|
|
obj = content_type.get_object_for_this_type(pk=object_id)
|
2005-07-13 09:25:57 +08:00
|
|
|
except ObjectDoesNotExist:
|
2006-05-02 09:31:56 +08:00
|
|
|
raise http.Http404, "Content type %s object %s doesn't exist" % (content_type_id, object_id)
|
2005-10-17 21:24:29 +08:00
|
|
|
try:
|
|
|
|
absurl = obj.get_absolute_url()
|
|
|
|
except AttributeError:
|
2006-05-02 09:31:56 +08:00
|
|
|
raise http.Http404, "%s objects don't have get_absolute_url() methods" % content_type.name
|
2005-11-11 12:45:05 +08:00
|
|
|
|
|
|
|
# Try to figure out the object's domain, so we can do a cross-site redirect
|
|
|
|
# if necessary.
|
2005-10-26 22:01:53 +08:00
|
|
|
|
|
|
|
# If the object actually defines a domain, we're done.
|
2007-07-03 23:11:49 +08:00
|
|
|
if absurl.startswith('http://') or absurl.startswith('https://'):
|
2006-05-02 09:31:56 +08:00
|
|
|
return http.HttpResponseRedirect(absurl)
|
2005-10-26 22:01:53 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
object_domain = None
|
2005-11-11 12:45:05 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# Otherwise, we need to introspect the object's relationships for a
|
|
|
|
# relation to the Site object
|
|
|
|
opts = obj._meta
|
2005-11-11 12:45:05 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# 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
|
2006-05-07 02:46:53 +08:00
|
|
|
except IndexError:
|
2006-05-02 09:31:56 +08:00
|
|
|
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:
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2006-05-02 09:31:56 +08:00
|
|
|
object_domain = Site.objects.get_current().domain
|
|
|
|
except Site.DoesNotExist:
|
2005-07-13 09:25:57 +08:00
|
|
|
pass
|
2005-10-26 22:01:53 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# 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:
|
2007-07-03 23:11:49 +08:00
|
|
|
protocol = request.is_secure() and 'https' or 'http'
|
|
|
|
return http.HttpResponseRedirect('%s://%s%s' % (protocol, object_domain, absurl))
|
2005-10-26 22:01:53 +08:00
|
|
|
else:
|
2006-05-02 09:31:56 +08:00
|
|
|
return http.HttpResponseRedirect(absurl)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def page_not_found(request, template_name='404.html'):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Default 404 handler, which looks for the requested URL in the redirects
|
|
|
|
table, redirects if found, and displays 404 page if not redirected.
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Templates: `404.html`
|
|
|
|
Context:
|
|
|
|
request_path
|
|
|
|
The path of the requested URL (e.g., '/app/pages/bad_page/')
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-02-27 01:43:41 +08:00
|
|
|
t = loader.get_template(template_name) # You need to create a 404.html template.
|
2007-06-22 15:15:04 +08:00
|
|
|
return http.HttpResponseNotFound(t.render(RequestContext(request, {'request_path': request.path})))
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def server_error(request, template_name='500.html'):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2005-11-11 12:45:05 +08:00
|
|
|
500 error handler.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Templates: `500.html`
|
2005-07-13 09:25:57 +08:00
|
|
|
Context: None
|
|
|
|
"""
|
2007-02-27 01:43:41 +08:00
|
|
|
t = loader.get_template(template_name) # You need to create a 500.html template.
|
2007-06-22 15:15:04 +08:00
|
|
|
return http.HttpResponseServerError(t.render(Context({})))
|