Ticket #13659: pass_request_to_admin_display_callbacks.patch

File pass_request_to_admin_display_callbacks.patch, 8.0 KB (added by Sebastian Noack, 14 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 1f8ff6d..7a4f814 100644
    a b class ModelAdmin(BaseModelAdmin):  
    10811081            'actions_on_top': self.actions_on_top,
    10821082            'actions_on_bottom': self.actions_on_bottom,
    10831083            'actions_selection_counter': self.actions_selection_counter,
     1084            'request': request,
    10841085        }
    10851086        context.update(extra_context or {})
    10861087        context_instance = template.RequestContext(request, current_app=self.admin_site.name)
  • django/contrib/admin/templates/admin/change_list.html

    diff --git a/django/contrib/admin/templates/admin/change_list.html b/django/contrib/admin/templates/admin/change_list.html
    index 3b037e5..f319c95 100644
    a b  
    9292
    9393      {% block result_list %}
    9494          {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}
    95           {% result_list cl %}
     95          {% result_list request cl %}
    9696          {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
    9797      {% endblock %}
    9898      {% block pagination %}{% pagination cl %}{% endblock %}
  • django/contrib/admin/templatetags/admin_list.py

    diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
    index 565db32..32b5940 100644
    a b def _boolean_icon(field_val):  
    126126    BOOLEAN_MAPPING = {True: 'yes', False: 'no', None: 'unknown'}
    127127    return mark_safe(u'<img src="%simg/admin/icon-%s.gif" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[field_val], field_val))
    128128
    129 def items_for_result(cl, result, form):
     129def items_for_result(request, cl, result, form):
    130130    """
    131131    Generates the actual list of data.
    132132    """
    def items_for_result(cl, result, form):  
    135135    for field_name in cl.list_display:
    136136        row_class = ''
    137137        try:
    138             f, attr, value = lookup_field(field_name, result, cl.model_admin)
     138            f, attr, value = lookup_field(field_name, result, cl.model_admin, request)
    139139        except (AttributeError, ObjectDoesNotExist):
    140140            result_repr = EMPTY_CHANGELIST_VALUE
    141141        else:
    def items_for_result(cl, result, form):  
    192192    if form:
    193193        yield mark_safe(u'<td>%s</td>' % force_unicode(form[cl.model._meta.pk.name]))
    194194
    195 def results(cl):
     195def results(request, cl):
    196196    if cl.formset:
    197197        for res, form in zip(cl.result_list, cl.formset.forms):
    198             yield list(items_for_result(cl, res, form))
     198            yield list(items_for_result(request, cl, res, form))
    199199    else:
    200200        for res in cl.result_list:
    201             yield list(items_for_result(cl, res, None))
     201            yield list(items_for_result(request, cl, res, None))
    202202
    203 def result_list(cl):
     203def result_list(request, cl):
    204204    """
    205205    Displays the headers and data list together
    206206    """
    207207    return {'cl': cl,
    208208            'result_headers': list(result_headers(cl)),
    209             'results': list(results(cl))}
     209            'results': list(results(request, cl))}
    210210result_list = register.inclusion_tag("admin/change_list_results.html")(result_list)
    211211
    212212def date_hierarchy(cl):
  • django/contrib/admin/util.py

    diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py
    index 776a6f0..97393f8 100644
    a b def model_ngettext(obj, n=None):  
    250250    singular, plural = d["verbose_name"], d["verbose_name_plural"]
    251251    return ungettext(singular, plural, n or 0)
    252252
    253 def lookup_field(name, obj, model_admin=None):
     253def lookup_field(name, obj, model_admin=None, request=None):
    254254    opts = obj._meta
    255255    try:
    256256        f = opts.get_field(name)
    def lookup_field(name, obj, model_admin=None):  
    258258        # For non-field values, the value is either a method, property or
    259259        # returned via a callable.
    260260        if callable(name):
    261             attr = name
    262             value = attr(obj)
     261            attr = func = name
    263262        elif (model_admin is not None and hasattr(model_admin, name) and
    264263          not name == '__str__' and not name == '__unicode__'):
    265             attr = getattr(model_admin, name)
    266             value = attr(obj)
     264            attr = func = getattr(model_admin, name)
    267265        else:
    268266            attr = getattr(obj, name)
    269267            if callable(attr):
    270                 value = attr()
     268                func = getattr(obj.__class__, name)
    271269            else:
    272                 value = attr
     270                def func(*args): return attr
     271
     272        if getattr(attr, 'accepts_request', False):
     273            value = func(obj, request)
     274        else:
     275            value = func(obj)
     276
    273277        f = None
    274278    else:
    275279        attr = None
  • tests/regressiontests/admin_changelist/tests.py

    diff --git a/tests/regressiontests/admin_changelist/tests.py b/tests/regressiontests/admin_changelist/tests.py
    index b70d7c5..3bc0d92 100644
    a b class ChangeListTests(unittest.TestCase):  
    3131                m.list_select_related, m.list_per_page, m.list_editable, m)
    3232        FormSet = m.get_changelist_formset(request)
    3333        cl.formset = FormSet(queryset=cl.result_list)
    34         template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
    35         context = Context({'cl': cl})
     34        template = Template('{% load admin_list %}{% spaceless %}{% result_list request cl %}{% endspaceless %}')
     35        context = Context({'request': request, 'cl': cl})
    3636        table_output = template.render(context)
    3737        hidden_input_elem = '<input type="hidden" name="form-0-id" value="1" id="id_form-0-id" />'
    3838        self.failIf(table_output.find(hidden_input_elem) == -1,
    class ChangeListTests(unittest.TestCase):  
    4949                m.list_select_related, m.list_per_page, m.list_editable, m)
    5050        FormSet = m.get_changelist_formset(request)
    5151        cl.formset = FormSet(queryset=cl.result_list)
    52         template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
    53         context = Context({'cl': cl})
     52        template = Template('{% load admin_list %}{% spaceless %}{% result_list request cl %}{% endspaceless %}')
     53        context = Context({'request': request, 'cl': cl})
    5454        table_output = template.render(context)
    5555        self.failIf(table_output.find(hidden_input_elem) == -1,
    5656            'Failed to find expected hidden input element in: %s' % table_output)
  • tests/regressiontests/admin_util/tests.py

    diff --git a/tests/regressiontests/admin_util/tests.py b/tests/regressiontests/admin_util/tests.py
    index 5ea0ac5..b118742 100644
    a b class UtilTests(unittest.TestCase):  
    8383            def get_admin_value(self, obj):
    8484                return ADMIN_METHOD
    8585
     86        class MockRequest(object):
     87            pass
     88
    8689        simple_function = lambda obj: SIMPLE_FUNCTION
    8790
     91        request_function = lambda obj, req: repr(req)
     92        request_function.accepts_request = True
     93
     94        request = MockRequest()
     95
    8896        article = Article(
    8997            site=Site(domain=SITE_NAME),
    9098            title=TITLE_TEXT,
    class UtilTests(unittest.TestCase):  
    98106            ('title', TITLE_TEXT),
    99107            ('get_admin_value', ADMIN_METHOD),
    100108            (simple_function, SIMPLE_FUNCTION),
     109            (request_function, repr(request)),
    101110            ('test_from_model', article.test_from_model()),
    102111            ('non_field', INSTANCE_ATTRIBUTE)
    103112        )
    104113
    105114        mock_admin = MockModelAdmin()
    106115        for name, value in verifications:
    107             field, attr, resolved_value = lookup_field(name, article, mock_admin)
     116            field, attr, resolved_value = lookup_field(name, article, mock_admin, request)
    108117
    109118            if field is not None:
    110119                resolved_value = display_for_field(resolved_value, field)
Back to Top