~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/contrib/admin/templatetags/admin_list.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
 
71
71
def result_headers(cl):
72
72
    lookup_opts = cl.lookup_opts
73
 
    
 
73
 
74
74
    for i, field_name in enumerate(cl.list_display):
75
75
        attr = None
76
76
        try:
97
97
                            raise AttributeError, \
98
98
                                "'%s' model or '%s' objects have no attribute '%s'" % \
99
99
                                    (lookup_opts.object_name, cl.model_admin.__class__, field_name)
100
 
                
 
100
 
101
101
                try:
102
102
                    header = attr.short_description
103
103
                except AttributeError:
133
133
    BOOLEAN_MAPPING = {True: 'yes', False: 'no', None: 'unknown'}
134
134
    return mark_safe(u'<img src="%simg/admin/icon-%s.gif" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[field_val], field_val))
135
135
 
136
 
def items_for_result(cl, result):
 
136
def items_for_result(cl, result, form):
137
137
    first = True
138
138
    pk = cl.lookup_opts.pk.attname
139
139
    for field_name in cl.list_display:
205
205
                    result_repr = EMPTY_CHANGELIST_VALUE
206
206
            # Fields with choices are special: Use the representation
207
207
            # of the choice.
208
 
            elif f.choices:
209
 
                result_repr = dict(f.choices).get(field_val, EMPTY_CHANGELIST_VALUE)
 
208
            elif f.flatchoices:
 
209
                result_repr = dict(f.flatchoices).get(field_val, EMPTY_CHANGELIST_VALUE)
210
210
            else:
211
211
                result_repr = escape(field_val)
212
212
        if force_unicode(result_repr) == '':
222
222
                attr = str(cl.to_field)
223
223
            else:
224
224
                attr = pk
225
 
            result_id = repr(force_unicode(getattr(result, attr)))[1:]
 
225
            value = result.serializable_value(attr)
 
226
            result_id = repr(force_unicode(value))[1:]
226
227
            yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \
227
228
                (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag))
228
229
        else:
229
 
            yield mark_safe(u'<td%s>%s</td>' % (row_class, conditional_escape(result_repr)))
 
230
            # By default the fields come from ModelAdmin.list_editable, but if we pull
 
231
            # the fields out of the form instead of list_editable custom admins
 
232
            # can provide fields on a per request basis
 
233
            if form and field_name in form.fields:
 
234
                bf = form[field_name]
 
235
                result_repr = mark_safe(force_unicode(bf.errors) + force_unicode(bf))
 
236
            else:
 
237
                result_repr = conditional_escape(result_repr)
 
238
            yield mark_safe(u'<td%s>%s</td>' % (row_class, result_repr))
 
239
    if form:
 
240
        yield mark_safe(force_unicode(form[cl.model._meta.pk.name]))
230
241
 
231
242
def results(cl):
232
 
    for res in cl.result_list:
233
 
        yield list(items_for_result(cl,res))
 
243
    if cl.formset:
 
244
        for res, form in zip(cl.result_list, cl.formset.forms):
 
245
            yield list(items_for_result(cl, res, form))
 
246
    else:
 
247
        for res in cl.result_list:
 
248
            yield list(items_for_result(cl, res, None))
234
249
 
235
250
def result_list(cl):
236
251
    return {'cl': cl,
310
325
def admin_list_filter(cl, spec):
311
326
    return {'title': spec.title(), 'choices' : list(spec.choices(cl))}
312
327
admin_list_filter = register.inclusion_tag('admin/filter.html')(admin_list_filter)
 
328
 
 
329
def admin_actions(context):
 
330
    """
 
331
    Track the number of times the action field has been rendered on the page,
 
332
    so we know which value to use.
 
333
    """
 
334
    context['action_index'] = context.get('action_index', -1) + 1
 
335
    return context
 
336
admin_actions = register.inclusion_tag("admin/actions.html", takes_context=True)(admin_actions)