mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed shortcut redirect handler
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1012 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
		| @@ -1,10 +1,11 @@ | |||||||
| from django.core.exceptions import Http404, ObjectDoesNotExist | from django.core.exceptions import Http404, ObjectDoesNotExist | ||||||
| from django.core.template import Context, loader | from django.core.template import Context, loader | ||||||
| from django.models.core import sites | from django.models.core import sites, contenttypes | ||||||
| from django.utils import httpwrappers | from django.utils import httpwrappers | ||||||
|  |  | ||||||
| def shortcut(request, content_type_id, object_id): | def shortcut(request, content_type_id, object_id): | ||||||
|     from django.models.core import contenttypes |     """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: |     try: | ||||||
|         content_type = contenttypes.get_object(pk=content_type_id) |         content_type = contenttypes.get_object(pk=content_type_id) | ||||||
|         obj = content_type.get_object_for_this_type(pk=object_id) |         obj = content_type.get_object_for_this_type(pk=object_id) | ||||||
| @@ -14,25 +15,37 @@ def shortcut(request, content_type_id, object_id): | |||||||
|         absurl = obj.get_absolute_url() |         absurl = obj.get_absolute_url() | ||||||
|     except AttributeError: |     except AttributeError: | ||||||
|         raise Http404, "%s objects don't have get_absolute_url() methods" % content_type.name |         raise 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://'): |     if absurl.startswith('http://'): | ||||||
|         return httpwrappers.HttpResponseRedirect(absurl) |         return httpwrappers.HttpResponseRedirect(absurl) | ||||||
|  |  | ||||||
|     object_domain = None |     object_domain = None | ||||||
|  |      | ||||||
|  |     # Next, look for an many-to-many relationship to sites | ||||||
|     if hasattr(obj, 'get_site_list'): |     if hasattr(obj, 'get_site_list'): | ||||||
|         site_list = obj.get_site_list() |         site_list = obj.get_site_list() | ||||||
|         if site_list: |         if site_list: | ||||||
|             object_domain = site_list[0].domain |             object_domain = site_list[0].domain | ||||||
|  |     | ||||||
|  |     # Next, look for a many-to-one relationship to sites  | ||||||
|     elif hasattr(obj, 'get_site'): |     elif hasattr(obj, 'get_site'): | ||||||
|         try: |         try: | ||||||
|             object_domain = obj.get_site().domain |             object_domain = obj.get_site().domain | ||||||
|         except sites.SiteDoesNotExist: |         except sites.SiteDoesNotExist: | ||||||
|             pass |             pass | ||||||
|  |  | ||||||
|  |     # Then, fall back to the current site (if possible) | ||||||
|  |     else: | ||||||
|         try: |         try: | ||||||
|             object_domain = sites.get_current().domain |             object_domain = sites.get_current().domain | ||||||
|         except sites.SiteDoesNotExist: |         except sites.SiteDoesNotExist: | ||||||
|         pass |             # Finally, give up and use a URL without the domain name | ||||||
|     if not object_domain: |             return httpwrappers.HttpResponseRedirect(obj.get_absolute_url()) | ||||||
|         return httpwrappers.HttpResponseRedirect(absurl) |     return httpwrappers.HttpResponseRedirect('http://%s%s' % (object_domain, obj.get_absolute_url())) | ||||||
|     return httpwrappers.HttpResponseRedirect('http://%s%s' % (object_domain, absurl)) |  | ||||||
|  |  | ||||||
| def page_not_found(request): | def page_not_found(request): | ||||||
|     """ |     """ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user