2005-07-13 09:25:57 +08:00
|
|
|
from django.core.exceptions import Http404, ObjectDoesNotExist
|
2005-10-15 04:10:13 +08:00
|
|
|
from django.core.template import Context, loader
|
2005-10-26 22:01:53 +08:00
|
|
|
from django.models.core import sites, contenttypes
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils import httpwrappers
|
|
|
|
|
|
|
|
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:
|
2005-07-27 00:11:43 +08:00
|
|
|
content_type = contenttypes.get_object(pk=content_type_id)
|
|
|
|
obj = content_type.get_object_for_this_type(pk=object_id)
|
2005-07-13 09:25:57 +08:00
|
|
|
except ObjectDoesNotExist:
|
|
|
|
raise 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:
|
2005-07-13 09:25:57 +08:00
|
|
|
raise 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.
|
2005-10-17 21:24:29 +08:00
|
|
|
if absurl.startswith('http://'):
|
|
|
|
return httpwrappers.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
|
|
|
|
2005-10-26 22:01:53 +08:00
|
|
|
# Next, look for an many-to-many relationship to sites
|
2005-07-14 11:17:09 +08:00
|
|
|
if hasattr(obj, 'get_site_list'):
|
|
|
|
site_list = obj.get_site_list()
|
2005-07-13 09:25:57 +08:00
|
|
|
if site_list:
|
|
|
|
object_domain = site_list[0].domain
|
2005-11-11 12:45:05 +08:00
|
|
|
|
|
|
|
# Next, look for a many-to-one relationship to sites
|
2005-07-13 09:25:57 +08:00
|
|
|
elif hasattr(obj, 'get_site'):
|
|
|
|
try:
|
|
|
|
object_domain = obj.get_site().domain
|
|
|
|
except sites.SiteDoesNotExist:
|
|
|
|
pass
|
2005-10-26 22:01:53 +08:00
|
|
|
|
|
|
|
# Then, fall back to the current site (if possible)
|
|
|
|
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()))
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-22 13:47:51 +08:00
|
|
|
def page_not_found(request, template_name='404'):
|
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.
|
|
|
|
|
|
|
|
Templates: `404`
|
|
|
|
Context: None
|
|
|
|
"""
|
2005-11-22 13:47:51 +08:00
|
|
|
t = loader.get_template(template_name)
|
2005-11-11 12:45:05 +08:00
|
|
|
return httpwrappers.HttpResponseNotFound(t.render(Context()))
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-22 13:47:51 +08:00
|
|
|
def server_error(request, template_name='500'):
|
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
|
|
|
|
|
|
|
Templates: `500`
|
|
|
|
Context: None
|
|
|
|
"""
|
2005-11-22 13:47:51 +08:00
|
|
|
t = loader.get_template(template_name)
|
2005-11-11 12:45:05 +08:00
|
|
|
return httpwrappers.HttpResponseServerError(t.render(Context()))
|