From 35213d99828865dfea86220060818c4b2320e807 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sun, 17 Jun 2012 16:49:24 -0400 Subject: [PATCH] Add link to filter event list by event type, update code to be more up to speed with other apps --- apps/history/__init__.py | 22 ++++++++++++++++++++++ apps/history/models.py | 9 ++++++++- apps/history/urls.py | 1 + apps/history/views.py | 32 ++++++++++++-------------------- apps/history/widgets.py | 8 ++++++++ 5 files changed, 51 insertions(+), 21 deletions(-) diff --git a/apps/history/__init__.py b/apps/history/__init__.py index 24469fbd2c..66dd9a3b85 100644 --- a/apps/history/__init__.py +++ b/apps/history/__init__.py @@ -3,9 +3,31 @@ from __future__ import absolute_import from django.utils.translation import ugettext_lazy as _ from project_tools.api import register_tool +from navigation.api import register_model_list_columns, register_links +from common.utils import encapsulate from .permissions import PERMISSION_HISTORY_VIEW +from .models import History +from .widgets import history_entry_summary, history_entry_type_link history_list = {'text': _(u'history'), 'view': 'history_list', 'famfam': 'book', 'icon': 'book.png', 'children_view_regex': [r'history_[l,v]']} +history_details = {'text': _(u'details'), 'view': 'history_view', 'famfam': 'book_open', 'args': 'object.pk'} register_tool(history_list) + +register_model_list_columns(History, [ + { + 'name': _(u'date and time'), + 'attribute': 'datetime' + }, + { + 'name': _(u'type'), + 'attribute': encapsulate(lambda entry: history_entry_type_link(entry)) + }, + { + 'name': _(u'summary'), + 'attribute': encapsulate(lambda entry: unicode(entry.get_processed_summary())) + } +]) + +register_links(History, [history_details]) diff --git a/apps/history/models.py b/apps/history/models.py index 82d289594f..56906a4fde 100644 --- a/apps/history/models.py +++ b/apps/history/models.py @@ -19,7 +19,14 @@ class HistoryType(models.Model): name = models.CharField(max_length=64, verbose_name=_(u'name')) def __unicode__(self): - return u'%s - %s' % (self.namespace, self.name) + try: + return unicode(history_types_dict[self.namespace][self.name]['label']) + except KeyError: + return u'obsolete history type: %s - %s' % (self.namespace, self.name) + + @models.permalink + def get_absolute_url(self): + return ('history_type_list', [self.pk]) class Meta: ordering = ('namespace', 'name') diff --git a/apps/history/urls.py b/apps/history/urls.py index 4f123f1be7..9c2b4b9778 100644 --- a/apps/history/urls.py +++ b/apps/history/urls.py @@ -4,4 +4,5 @@ urlpatterns = patterns('history.views', url(r'^list/$', 'history_list', (), 'history_list'), url(r'^list/for_object/(?P[\w\-]+)/(?P[\w\-]+)/(?P\d+)/$', 'history_for_object', (), 'history_for_object'), url(r'^(?P\d+)/$', 'history_view', (), 'history_view'), + url(r'^type/(?P\d+)/list/$', 'history_type_list', (), 'history_type_list'), ) diff --git a/apps/history/views.py b/apps/history/views.py index 8b29a5d1ca..0c2f603f0b 100644 --- a/apps/history/views.py +++ b/apps/history/views.py @@ -13,7 +13,7 @@ from permissions.models import Permission from common.utils import encapsulate from acls.models import AccessEntry -from .models import History +from .models import History, HistoryType from .forms import HistoryDetailForm from .permissions import PERMISSION_HISTORY_VIEW from .widgets import history_entry_object_link, history_entry_summary @@ -37,17 +37,9 @@ def history_list(request, object_list=None, title=None, extra_context=None): 'title': title if title else _(u'history events'), 'extra_columns': [ { - 'name': _(u'date and time'), - 'attribute': 'datetime' - }, - { - 'name': _(u'object'), + 'name': _(u'object link'), 'attribute': encapsulate(lambda x: history_entry_object_link(x)) }, - { - 'name': _(u'summary'), - 'attribute': encapsulate(lambda x: history_entry_summary(x)) - } ], 'hide_object': True, } @@ -75,16 +67,6 @@ def history_for_object(request, app_label, module_name, object_id): 'object_list': History.objects.filter(content_type=content_type, object_id=object_id), 'title': _(u'history events for: %s') % content_object, 'object': content_object, - 'extra_columns': [ - { - 'name': _(u'date and time'), - 'attribute': 'datetime' - }, - { - 'name': _(u'summary'), - 'attribute': encapsulate(lambda x: history_entry_summary(x)) - } - ], 'hide_object': True, } @@ -113,3 +95,13 @@ def history_view(request, object_id): 'form': form, }, context_instance=RequestContext(request)) + + +def history_type_list(request, history_type_pk): + history_type = get_object_or_404(HistoryType, pk=history_type_pk) + + return history_list( + request, + object_list=History.objects.filter(history_type=history_type), + title=_(u'history events of type: %s') % history_type, + ) diff --git a/apps/history/widgets.py b/apps/history/widgets.py index af78b0690a..8173ccbe2b 100644 --- a/apps/history/widgets.py +++ b/apps/history/widgets.py @@ -14,3 +14,11 @@ def history_entry_summary(entry): 'url': entry.get_absolute_url(), 'label': unicode(entry.get_processed_summary()) }) + + +def history_entry_type_link(entry): + return mark_safe(u'%(label)s' % { + 'url': entry.history_type.get_absolute_url(), + 'label': unicode(entry.history_type) + } + )