From 1cdf3e2b344ed29e2639e1881446111ec8dc3b37 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 22 May 2015 21:08:22 +0100 Subject: [PATCH] Added activity stream view --- RIGS/templates/RIGS/activity_stream.html | 156 +++++++++++++++++++++++ RIGS/urls.py | 4 + RIGS/versioning.py | 87 +++++++++++-- 3 files changed, 234 insertions(+), 13 deletions(-) create mode 100644 RIGS/templates/RIGS/activity_stream.html diff --git a/RIGS/templates/RIGS/activity_stream.html b/RIGS/templates/RIGS/activity_stream.html new file mode 100644 index 00000000..99c07dab --- /dev/null +++ b/RIGS/templates/RIGS/activity_stream.html @@ -0,0 +1,156 @@ +{% extends request.is_ajax|yesno:"base_ajax.html,base.html" %} +{% load static %} + +{% block title %}Rigboard Activity Stream{% endblock %} + +{% block js %} + + + + +{% endblock %} + +{% block content %} +

Rigboard Activity Stream

+
+ + + + + + + + + + + + {% for version in object_list %} + + + + + + + + {% endfor %} + + +
DateEventVersion IDUserChanges
{{ version.revision.date_created|date:"Y-m-d H:i:s" }}N{{ version.thisVersion.object_id_int|stringformat:"05d" }}{{ version.pk }}|{{ version.revision.pk }}{{ version.revision.user.name }}{% for key, old, new in version.changes %} + + {% endfor %} + {% for key, old, new in version.item_changes %} + + {% endfor %} +
+ +
+

Stream View

+{% for version in object_list %} +
+ changed + {% for key, old, new in version.changes %} + + {% endfor %} + {% for key, old, new in version.item_changes %} + + {% endfor %} + in ({{ version.revision.date_created|date:"Y-m-d H:i:s" }}) + +
+{% endfor %} + + +
+ + {{ thediff }} +{% endblock %} \ No newline at end of file diff --git a/RIGS/urls.py b/RIGS/urls.py index 3517e203..1ff59e5e 100644 --- a/RIGS/urls.py +++ b/RIGS/urls.py @@ -84,6 +84,10 @@ urlpatterns = patterns('', permission_required_with_403('RIGS.view_event')(versioning.EventRevisions.as_view()), name='event_history'), + url(r'^rigboard/activity$', + permission_required_with_403('RIGS.view_event')(versioning.ActivityStream.as_view()), + name='activity_stream'), + # Finance url(r'^invoice/$', permission_required_with_403('RIGS.view_invoice')(finance.InvoiceIndex.as_view()), diff --git a/RIGS/versioning.py b/RIGS/versioning.py index 898cd9db..dee6600b 100644 --- a/RIGS/versioning.py +++ b/RIGS/versioning.py @@ -95,6 +95,57 @@ def compare_items(old, new): return zip(key,old,new) +def get_versions_for_model(model): + content_type = ContentType.objects.get_for_model(model) + versions = reversion.models.Version.objects.filter( + content_type = content_type, + ).select_related("revision").order_by("-pk") + + return versions + +def strip_duplicate_versions(versions): #probably don't need this function + changed_versions = [] + last_serialized_data = None + for version in versions: + if last_serialized_data != version.serialized_data: + changed_versions.append(version) + last_serialized_data = version.serialized_data + + return changed_versions + +def get_previous_version(version): + thisEventId = version.object_id + thisVersionId = version.pk + + versions = reversion.get_for_object_reference(models.Event, thisEventId) + + previousVersions = versions.filter(pk__lt=thisVersionId) + + if len(previousVersions) >= 1: + return previousVersions[0] + else: #this is probably the initial version + return False + +def get_changes_for_version(thisVersion, previousVersion=None): + + if previousVersion == None: + previousVersion = get_previous_version(thisVersion) + + compare = {} + compare['pk'] = thisVersion.pk + compare['thisVersion'] = thisVersion + compare['prevVersion'] = previousVersion + compare['revision'] = thisVersion.revision + + if previousVersion: + compare['changes'] = compare_events(previousVersion.field_dict,thisVersion.field_dict) + compare['item_changes'] = compare_items(previousVersion, thisVersion) + else: + compare['changes'] = [["(initial version)",None,"Event Created"]] + + return compare + + class EventRevisions(generic.ListView): model = reversion.revisions.Version @@ -104,22 +155,13 @@ class EventRevisions(generic.ListView): thisEvent = get_object_or_404(models.Event, pk=self.kwargs['pk']) versions = reversion.get_for_object(thisEvent) items = [] - for revisionNo, thisRevision in enumerate(versions): - thisItem = {'pk': thisRevision.pk} - thisItem['revision'] = thisRevision.revision - logger.info(thisRevision.revision.version_set.all()) - - if revisionNo >= len(revisions)-1: - # oldest version - thisItem['changes'] = [["(initial version)",None,"Event Created"]] + for versionNo, thisVersion in enumerate(versions): + if versionNo >= len(versions)-1: + thisItem = get_changes_for_version(thisVersion, None) else: - changes = compare_events(revisions[revisionNo+1].field_dict,thisRevision.field_dict) - thisItem['item_changes'] = compare_items(revisions[revisionNo+1], thisRevision) - logger.debug(thisItem['item_changes']) - thisItem['changes'] = changes + thisItem = get_changes_for_version(thisVersion, versions[versionNo+1]) items.append(thisItem) - logger.info(thisItem) context = { 'object_list': items, @@ -128,3 +170,22 @@ class EventRevisions(generic.ListView): return context +class ActivityStream(generic.ListView): + model = reversion.revisions.Version + template_name = "RIGS/activity_stream.html" + + def get_context_data(self, **kwargs): + + versions = get_versions_for_model(models.Event); + + items = [] + + for thisVersion in versions[:20]: + thisItem = get_changes_for_version(thisVersion, None) + items.append(thisItem) + + context = { + 'object_list': items, + } + + return context \ No newline at end of file