django.contrib.contenttypes.views: 49 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/django/contrib/contenttypes/views.py

Stats: 0 executed, 44 missed, 5 excluded, 31 ignored

  1. from django import http
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.contrib.sites.models import Site, get_current_site
  4. from django.core.exceptions import ObjectDoesNotExist
  5. from django.utils.translation import ugettext as _
  6. 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.
  9. """
  10. # Look up the object, making sure it's got a get_absolute_url() function.
  11. try:
  12. content_type = ContentType.objects.get(pk=content_type_id)
  13. if not content_type.model_class():
  14. raise http.Http404(_(u"Content type %(ct_id)s object has no associated model") %
  15. {'ct_id': content_type_id})
  16. obj = content_type.get_object_for_this_type(pk=object_id)
  17. except (ObjectDoesNotExist, ValueError):
  18. raise http.Http404(_(u"Content type %(ct_id)s object %(obj_id)s doesn't exist") %
  19. {'ct_id': content_type_id, 'obj_id': object_id})
  20. try:
  21. get_absolute_url = obj.get_absolute_url
  22. except AttributeError:
  23. raise http.Http404(_("%(ct_name)s objects don't have a get_absolute_url() method") %
  24. {'ct_name': content_type.name})
  25. absurl = get_absolute_url()
  26. # Try to figure out the object's domain, so we can do a cross-site redirect
  27. # if necessary.
  28. # If the object actually defines a domain, we're done.
  29. if absurl.startswith('http://') or absurl.startswith('https://'):
  30. return http.HttpResponseRedirect(absurl)
  31. # Otherwise, we need to introspect the object's relationships for a
  32. # relation to the Site object
  33. object_domain = None
  34. if Site._meta.installed:
  35. opts = obj._meta
  36. # First, look for an many-to-many relationship to Site.
  37. for field in opts.many_to_many:
  38. if field.rel.to is Site:
  39. try:
  40. # Caveat: In the case of multiple related Sites, this just
  41. # selects the *first* one, which is arbitrary.
  42. object_domain = getattr(obj, field.name).all()[0].domain
  43. except IndexError:
  44. pass
  45. if object_domain is not None:
  46. break
  47. # Next, look for a many-to-one relationship to Site.
  48. if object_domain is None:
  49. for field in obj._meta.fields:
  50. if field.rel and field.rel.to is Site:
  51. try:
  52. object_domain = getattr(obj, field.name).domain
  53. except Site.DoesNotExist:
  54. pass
  55. if object_domain is not None:
  56. break
  57. # Fall back to the current site (if possible).
  58. if object_domain is None:
  59. try:
  60. object_domain = get_current_site(request).domain
  61. except Site.DoesNotExist:
  62. pass
  63. # If all that malarkey found an object domain, use it. Otherwise, fall back
  64. # to whatever get_absolute_url() returned.
  65. if object_domain is not None:
  66. protocol = request.is_secure() and 'https' or 'http'
  67. return http.HttpResponseRedirect('%s://%s%s'
  68. % (protocol, object_domain, absurl))
  69. else:
  70. return http.HttpResponseRedirect(absurl)