From b12ddb7cc40ede9701f3be7a576a38ad23ce8edd Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Thu, 10 Feb 2011 00:13:30 -0400 Subject: [PATCH] Added method to link a model with it's column list to display on the generic list template, this way the same column layout is displayed regarless of the view that generates it. --- apps/common/api.py | 8 ++++++++ .../templates/generic_list_subtemplate.html | 7 +++++++ apps/common/templatetags/attribute_tags.py | 5 +++++ apps/documents/__init__.py | 17 ++++++++++++++--- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/apps/common/api.py b/apps/common/api.py index 30b6579118..6fc788ef1b 100755 --- a/apps/common/api.py +++ b/apps/common/api.py @@ -2,6 +2,7 @@ import copy object_navigation = {} menu_links = [] +model_list_columns = {} def register_links(src, links, menu_name=None): if menu_name in object_navigation: @@ -30,3 +31,10 @@ def register_menu(links): menu_links.append(link) menu_links.sort(lambda x,y: 1 if x>y else -1, lambda x:x['position'] if 'position' in x else 1) + + +def register_model_list_columns(model, columns): + if model in model_list_columns: + model_list_columns[model].extend(columns) + else: + model_list_columns[model] = copy.copy(columns) diff --git a/apps/common/templates/generic_list_subtemplate.html b/apps/common/templates/generic_list_subtemplate.html index 340765d59c..37e3f138ce 100755 --- a/apps/common/templates/generic_list_subtemplate.html +++ b/apps/common/templates/generic_list_subtemplate.html @@ -32,6 +32,9 @@ {% if not hide_object %} {% trans 'Identifier' %} {% endif %} + {% for column in object_list.0|get_model_list_columns %} + {{ column.name|capfirst }} + {% endfor %} {% for column in extra_columns %} {{ column.name|capfirst }} {% endfor %} @@ -51,6 +54,10 @@ {% if not hide_link %}{{ object }}{% else %}{{ object }}{% endif %} {% endif %} {% endif %} + {% for column in object|get_model_list_columns %} + {{ object|object_property:column.attribute|safe }} + {% endfor %} + {% for column in extra_columns %} {{ object|object_property:column.attribute|safe }} {% endfor %} diff --git a/apps/common/templatetags/attribute_tags.py b/apps/common/templatetags/attribute_tags.py index 51807faf60..2fafff9a97 100755 --- a/apps/common/templatetags/attribute_tags.py +++ b/apps/common/templatetags/attribute_tags.py @@ -3,9 +3,14 @@ from django.template.defaultfilters import stringfilter from django.template import Library, Node, Variable, VariableDoesNotExist from common.utils import return_attrib +from common.api import model_list_columns register = Library() @register.filter def object_property(value, arg): return return_attrib(value, arg) + +@register.filter +def get_model_list_columns(value): + return model_list_columns.get(type(value), []) diff --git a/apps/documents/__init__.py b/apps/documents/__init__.py index 3eeb6e6515..ca61be4646 100755 --- a/apps/documents/__init__.py +++ b/apps/documents/__init__.py @@ -1,8 +1,10 @@ import tempfile from django.utils.translation import ugettext_lazy as _ +from django.core.urlresolvers import reverse -from common.api import register_links, register_menu +from common.api import register_links, register_menu, register_model_list_columns +from common.utils import pretty_size from models import Document from staging import StagingFile @@ -23,12 +25,21 @@ document_download = {'text':_('download'), 'view':'document_download', 'args':'o staging_file_preview = {'text':_('preview'), 'class':'fancybox', 'view':'staging_file_preview', 'args':'object.id', 'famfam':'drive_magnify'} -register_links(Document, [document_edit, document_edit_metadata, document_delete, document_preview, document_download]) -register_links(Document, [document_list, document_create, document_create_multiple, document_create_sibling, document_view], menu_name='sidebar') +register_links(Document, [document_edit, document_edit_metadata, document_delete, document_download]) +register_links(Document, [document_list, document_create, document_create_multiple, document_create_sibling, document_view, document_preview], menu_name='sidebar') register_links(['document_list', 'document_create', 'document_create_multiple', 'upload_document_with_type', 'upload_multiple_documents_with_type'], [document_list, document_create, document_create_multiple], menu_name='sidebar') register_links(StagingFile, [staging_file_preview]) +register_model_list_columns(Document, [ + {'name':_(u'mimetype'), 'attribute':'file_mimetype'}, + {'name':_(u'added'), 'attribute':lambda x: x.date_added.date()}, + {'name':_(u'file size'), 'attribute':lambda x: pretty_size(x.file.storage.size(x.file.path)) if x.exists() else '-'}, + {'name':_(u'thumbnail'), 'attribute': + lambda x: '' % (reverse('document_preview', args=[x.id]), + reverse('document_thumbnail', args=[x.id])) + }, + ]) register_menu([ {'text':_('documents'), 'view':'document_list', 'links':[