From 82c83c13099197d1d9ba705d5072b38a13f39821 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 31 Jan 2012 22:36:10 +0530 Subject: [PATCH] keep a log of changes --- pandora/annotation/models.py | 6 ++++++ pandora/annotation/views.py | 2 ++ pandora/changelog/__init__.py | 0 pandora/changelog/models.py | 13 +++++++++++++ pandora/changelog/tests.py | 16 ++++++++++++++++ pandora/changelog/views.py | 1 + pandora/clip/models.py | 3 +-- pandora/event/models.py | 6 ++++++ pandora/item/models.py | 8 +++++++- pandora/item/views.py | 2 ++ pandora/place/models.py | 6 ++++++ pandora/settings.py | 1 + 12 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 pandora/changelog/__init__.py create mode 100644 pandora/changelog/models.py create mode 100644 pandora/changelog/tests.py create mode 100644 pandora/changelog/views.py diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index 0fe0001e..a4f9260c 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -11,6 +11,7 @@ import ox from archive import extract from clip.models import Clip +from changelog.models import Changelog from item.utils import sort_string import managers @@ -202,6 +203,11 @@ class Annotation(models.Model): j['videoRatio'] = streams[0].aspect_ratio return j + def log(self): + c = Changelog(type='annotation') + c.value = self.json() + c.save() + def __unicode__(self): return u"%s %s-%s" %(self.public_id, self.start, self.end) diff --git a/pandora/annotation/views.py b/pandora/annotation/views.py index 2aa6b349..b2219a1b 100644 --- a/pandora/annotation/views.py +++ b/pandora/annotation/views.py @@ -164,6 +164,7 @@ def removeAnnotation(request): data = json.loads(request.POST['data']) a = get_object_or_404_json(models.Annotation, public_id=data['id']) if a.editable(request.user): + a.log() a.delete() if a.clip.annotations.count() == 0: a.clip.delete() @@ -193,6 +194,7 @@ def editAnnotation(request): data = json.loads(request.POST['data']) a = get_object_or_404_json(models.Annotation, public_id=data['id']) if a.editable(request.user): + a.log() for key in ('value', 'in', 'out'): if key in data: setattr(a, { diff --git a/pandora/changelog/__init__.py b/pandora/changelog/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pandora/changelog/models.py b/pandora/changelog/models.py new file mode 100644 index 00000000..28123227 --- /dev/null +++ b/pandora/changelog/models.py @@ -0,0 +1,13 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +from __future__ import division, with_statement +from django.db import models +from ox.django import fields + +class Changelog(models.Model): + created = models.DateTimeField(auto_now_add=True) + type = models.CharField(max_length=255, db_index=True) + value = fields.DictField(default={}) + + def __unicode__(self): + return u'%s %s' %(self.type, self.created) diff --git a/pandora/changelog/tests.py b/pandora/changelog/tests.py new file mode 100644 index 00000000..501deb77 --- /dev/null +++ b/pandora/changelog/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.assertEqual(1 + 1, 2) diff --git a/pandora/changelog/views.py b/pandora/changelog/views.py new file mode 100644 index 00000000..60f00ef0 --- /dev/null +++ b/pandora/changelog/views.py @@ -0,0 +1 @@ +# Create your views here. diff --git a/pandora/clip/models.py b/pandora/clip/models.py index 41129a9c..715c142a 100644 --- a/pandora/clip/models.py +++ b/pandora/clip/models.py @@ -36,7 +36,6 @@ class MetaClip: if streams: self.aspect_ratio = streams[0].aspect_ratio sortvalue = '' - findvalue = '' for l in settings.CONFIG['clipLayers']: sortvalue += ''.join(filter(lambda s: s, [a.sortvalue @@ -45,7 +44,7 @@ class MetaClip: self.sortvalue = sortvalue[:1000] else: self.sortvalue = None - self.findvalue = '\n'.join([a.findvalue for a in self.annotations.all()]) + self.findvalue = '\n'.join(filter(None, [a.findvalue for a in self.annotations.all()])) if self.id: for l in settings.CONFIG['clipLayers']: setattr(self, l, self.annotations.filter(layer=l).count()>0) diff --git a/pandora/event/models.py b/pandora/event/models.py index 8c4a9b54..bf6f77c1 100644 --- a/pandora/event/models.py +++ b/pandora/event/models.py @@ -16,6 +16,7 @@ from item.models import Item from item import utils from person.models import get_name_sort from title.models import get_title_sort +from changelog.models import Changelog import managers @@ -128,3 +129,8 @@ class Event(models.Model): j[key] = getattr(self, key) j['nameSort'] = self.name_sort return j + + def log(self): + c = Changelog(type='event') + c.value = self.json() + c.save() diff --git a/pandora/item/models.py b/pandora/item/models.py index 54b1884d..9cc67692 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -35,6 +35,7 @@ from data_api import external_data from archive import extract from annotation.models import Annotation from clip.models import Clip +from changelog.models import Changelog import archive.models from person.models import get_name_sort @@ -228,6 +229,11 @@ class Item(models.Model): self.data[key] = data[key] return self.save() + def log(self): + c = Changelog(type='item') + c.value = self.json + c.save() + def update_external(self): if settings.DATA_SERVICE and not self.itemId.startswith('0x'): response = external_data('getData', {'id': self.itemId}) @@ -572,7 +578,7 @@ class Item(models.Model): qs = Annotation.objects.filter(item=self) qs = qs.filter(layer=i) qs = qs.order_by('start') - save(i, u'\n'.join([l.findvalue for l in qs])) + save(i, u'\n'.join(filter(None, [l.findvalue for l in qs]))) elif i != '*' and i not in self.facet_keys: value = self.get(i) if isinstance(value, list): diff --git a/pandora/item/views.py b/pandora/item/views.py index 7527e742..701c50bf 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -429,6 +429,7 @@ def edit(request): data = json.loads(request.POST['data']) item = get_object_or_404_json(models.Item, itemId=data['id']) if item.editable(request.user): + item.log() response = json_response(status=200, text='ok') if 'notes' in data: if request.user.get_profile().capability('canEditMetadata'): @@ -459,6 +460,7 @@ def remove(request): data = json.loads(request.POST['data']) item = get_object_or_404_json(models.Item, itemId=data['id']) if item.editable(request.user): + item.log() #FIXME: is this cascading enough or do we end up with orphan files etc. item.delete() response = json_response(status=200, text='removed') diff --git a/pandora/place/models.py b/pandora/place/models.py index 29eee36d..6dba18bd 100644 --- a/pandora/place/models.py +++ b/pandora/place/models.py @@ -14,6 +14,7 @@ from ox.django import fields import managers from annotation.models import Annotation, get_matches from item.models import Item +from changelog.models import Changelog class Place(models.Model): @@ -119,3 +120,8 @@ class Place(models.Model): #self.area= ox.location.area(self.south, self.west, self.north, self.east) super(Place, self).save(*args, **kwargs) + + def log(self): + c = Changelog(type='place') + c.value = self.json() + c.save() diff --git a/pandora/settings.py b/pandora/settings.py index 780ddd45..53dc2944 100644 --- a/pandora/settings.py +++ b/pandora/settings.py @@ -99,6 +99,7 @@ INSTALLED_APPS = ( 'clip', 'archive', 'event', + 'changelog', 'item', 'itemlist', 'person',