Source

Ochs / ochs_media / templatetags / media_list.py

Full commit
from django.contrib.admin.templatetags.admin_list import result_headers
from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
from django.core.exceptions import ObjectDoesNotExist
from django.utils.safestring import mark_safe
from django.utils.encoding import smart_unicode, smart_str, force_unicode
from django.template import Library

register = Library()

def items_for_result(cl, res):
    first = True
    for field_name in cl.list_display:
        if field_name == 'action_checkbox':
            continue
        try:
            attr = getattr(res, field_name)
            if callable(attr):
                value = attr()
            else:
                value = attr
            result_repr = smart_unicode(value)
        except (AttributeError, ObjectDoesNotExist):
            result_repr = EMPTY_CHANGELIST_VALUE
        else:
            result_repr = mark_safe(result_repr)
        if (first and not cl.list_display_links) or field_name in cl.list_display_links:
            table_tag = {True:'th', False:'td'}[first]
            first = False
            print res
            yield res
        else:
            yield res

def results(cl):
    for res in cl.result_list:
        yield items_for_result(cl, res)

def media_result_list(cl):
    return {'cl': cl,
            'result_headers': list(result_headers(cl)),
            'results': cl.result_list}
media_result_list = register.inclusion_tag("change_list_results.html")(media_result_list)