Ticket #1425: django_1425.2.diff

File django_1425.2.diff, 2.6 KB (added by Christopher Lenz <cmlenz@…>, 10 years ago)

Updated patch that checks the target model of relations

  • django/views/defaults.py

     
    88    "Redirect to an object's page based on a content-type ID and an object ID."
    99    # Look up the object, making sure it's got a get_absolute_url() function.
    1010    try:
    11         content_type = ContentType.objects.get_object(pk=content_type_id)
     11        content_type = ContentType.objects.get(pk=content_type_id)
    1212        obj = content_type.get_object_for_this_type(pk=object_id)
    1313    except ObjectDoesNotExist:
    1414        raise http.Http404, "Content type %s object %s doesn't exist" % (content_type_id, object_id)
     
    2626
    2727    object_domain = None
    2828
    29     # Next, look for an many-to-many relationship to sites
    30     if hasattr(obj, 'get_site_list'):
    31         site_list = obj.get_site_list()
    32         if site_list:
    33             object_domain = site_list[0].domain
     29    # First, look for an many-to-many relationship to sites
     30    for field in obj._meta.many_to_many:
     31        if field.rel.to is Site:
     32            try:
     33                object_domain = getattr(obj, field.name).all()[0].domain
     34            except Site.DoesNotExist:
     35                pass
     36            break
     37    else:
     38        # Next, look for a many-to-one relationship to sites
     39        for field in obj._meta.fields:
     40            if field.rel and field.rel.to is Site:
     41                try:
     42                    object_domain = getattr(obj, field.name).domain
     43                except Site.DoesNotExist:
     44                    pass
     45                break
     46        else:
     47            # Then, fall back to the current site (if possible)
     48            try:
     49                object_domain = Site.objects.get_current().domain
     50            except Site.DoesNotExist:
     51                # Finally, give up and use a URL without the domain name
     52                return http.HttpResponseRedirect(obj.get_absolute_url())
    3453
    35     # Next, look for a many-to-one relationship to sites
    36     elif hasattr(obj, 'get_site'):
    37         try:
    38             object_domain = obj.get_site().domain
    39         except Site.DoesNotExist:
    40             pass
    41 
    42     # Then, fall back to the current site (if possible)
    43     else:
    44         try:
    45             object_domain = Site.objects.get_current().domain
    46         except Site.DoesNotExist:
    47             # Finally, give up and use a URL without the domain name
    48             return http.HttpResponseRedirect(obj.get_absolute_url())
    4954    return http.HttpResponseRedirect('http://%s%s' % (object_domain, obj.get_absolute_url()))
    5055
    5156def page_not_found(request, template_name='404'):
Back to Top