From df9fc699ff3e3d05b9386a74a34793540a5c4385 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 2 Nov 2011 15:06:34 +0100 Subject: [PATCH] remove layer of indirection --- pandora/annotation/admin.py | 4 --- pandora/annotation/models.py | 62 +----------------------------------- pandora/annotation/views.py | 7 ++-- pandora/clip/managers.py | 2 +- pandora/clip/models.py | 2 +- pandora/clip/views.py | 4 +-- pandora/item/models.py | 17 +++++----- 7 files changed, 17 insertions(+), 81 deletions(-) diff --git a/pandora/annotation/admin.py b/pandora/annotation/admin.py index 9566cc409..f7142eacc 100644 --- a/pandora/annotation/admin.py +++ b/pandora/annotation/admin.py @@ -6,10 +6,6 @@ from django.contrib import admin import models -class LayerAdmin(admin.ModelAdmin): - search_fields = ['name', 'title'] -admin.site.register(models.Layer, LayerAdmin) - class AnnotationAdmin(admin.ModelAdmin): search_fields = ['name', 'title'] admin.site.register(models.Annotation, AnnotationAdmin) diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index 00f7e84d9..1964eecf8 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -16,66 +16,6 @@ from tasks import update_matching_events, update_matching_places -def load_layers(layers): - for l in layers: - create_or_update_layer(l) - -def create_or_update_layer(data): - layer, created = Layer.objects.get_or_create(name=data['id']) - for key in ('title', 'type', 'overlap', 'overlay', 'private'): - if key in data and getattr(layer, key) != data[key]: - setattr(layer, key, data[key]) - created = True - if created: - layer.save() - return layer - -class Layer(models.Model): - - #class Meta: - # ordering = ('position', ) - - enabled = models.BooleanField(default=True) - - name = models.CharField(null=True, max_length=255, unique=True) - title = models.CharField(null=True, max_length=255) - #text, string, string from list(fixme), date, place, person, pingback, - #What about: smart layers? for date, place, person - type = models.CharField(null=True, max_length=255) - - #can this be changed per user? - position = models.IntegerField(default=0) - - overlap = models.BooleanField(default=True) - overlay = models.BooleanField(default=True) - private = models.BooleanField(default=False) #false=users only see there own bins - - #find/sort integration - find = models.BooleanField(default=True) #true part of find all - #words / item duration(wpm), total words, cuts per minute, cuts, number of annotations, number of annotations/duration - sort = models.CharField(null=True, max_length=255) - - def properties(self): - p = {} - if self.find: - p[self.name] = {'type': 'bin', 'find': True} - if self.sort: - print 'FIXME: need to add sort stuff' - return p - - def json(self): - return { - 'id': self.name, - 'overlap': self.overlap, - 'private': self.private, - 'title': self.title, - 'type': self.type - } - - def __unicode__(self): - return self.title - - class Annotation(models.Model): objects = managers.AnnotationManager() @@ -91,7 +31,7 @@ class Annotation(models.Model): start = models.FloatField(default=-1, db_index=True) end = models.FloatField(default=-1) - layer = models.ForeignKey(Layer) + layer = models.CharField(max_length=255, db_index=True) value = models.TextField() def editable(self, user): diff --git a/pandora/annotation/views.py b/pandora/annotation/views.py index 355e36a01..ff58c6661 100644 --- a/pandora/annotation/views.py +++ b/pandora/annotation/views.py @@ -129,11 +129,10 @@ def addAnnotation(request): return render_to_json_response(json_response(status=400, text='invalid data')) - #FIXME: this should be only called starting up server - models.load_layers(settings.CONFIG['layers']) - item = get_object_or_404_json(Item, itemId=data['item']) - layer = get_object_or_404_json(models.Layer, name=data['layer']) + + #FIXME: check that layer is a valid layer id + layer = data['layer'] annotation = models.Annotation( item=item, diff --git a/pandora/clip/managers.py b/pandora/clip/managers.py index 4c2a9ae52..60319f1be 100644 --- a/pandora/clip/managers.py +++ b/pandora/clip/managers.py @@ -41,7 +41,7 @@ def parseCondition(condition, user): return parseCondition({'key': 'annotations__value', 'value': v, 'operator': op}, user) \ - & parseCondition({'key': 'annotations__layer__name', + & parseCondition({'key': 'annotations__layer', 'value': k, 'operator': '=='}, user) diff --git a/pandora/clip/models.py b/pandora/clip/models.py index e2fc055f7..6039730c8 100644 --- a/pandora/clip/models.py +++ b/pandora/clip/models.py @@ -31,7 +31,7 @@ class MetaClip: if streams: self.aspect_ratio = streams[0].aspect_ratio for l in self.layers: - setattr(self, l, self.annotations.filter(layer__name=l).count()>0) + setattr(self, l, self.annotations.filter(layer=l).count()>0) models.Model.save(self, *args, **kwargs) def json(self, keys=None): diff --git a/pandora/clip/views.py b/pandora/clip/views.py index 3b012539f..f0b8516b2 100644 --- a/pandora/clip/views.py +++ b/pandora/clip/views.py @@ -105,10 +105,10 @@ def findClips(request): if response['data']['items']: if 'annotations' in keys: add_annotations('annotations', - Annotation.objects.filter(layer__name__in=models.Clip.layers, clip__in=ids)) + Annotation.objects.filter(layer__in=models.Clip.layers, clip__in=ids)) for layer in filter(lambda l: l in keys, models.Clip.layers): add_annotations(layer, - Annotation.objects.filter(layer__name=layer, clip__in=ids)) + Annotation.objects.filter(layer=layer, clip__in=ids)) elif 'position' in query: qs = order_query(qs, query['sort']) ids = [i.public_id for i in qs] diff --git a/pandora/item/models.py b/pandora/item/models.py index 04ac0789e..0739ae48e 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -33,7 +33,7 @@ from .timelines import join_timelines from data_api import external_data from archive import extract -from annotation.models import Annotation, Layer +from annotation.models import Annotation import archive.models from person.models import get_name_sort @@ -414,12 +414,13 @@ class Item(models.Model): def get_layers(self, user=None): layers = {} - for l in Layer.objects.all(): - ll = layers.setdefault(l.name, []) - qs = Annotation.objects.filter(layer=l, item=self) - if l.name == 'subtitles': + for l in settings.CONFIG['layers']: + name = l['id'] + ll = layers.setdefault(name, []) + qs = Annotation.objects.filter(layer=name, item=self) + if name == 'subtitles': qs = qs.exclude(value='') - if l.private: + if l.get('private'): if user and user.is_anonymous(): user = None qs = qs.filter(user=user) @@ -530,7 +531,7 @@ class Item(models.Model): save(i, '\n'.join([f.path for f in self.files.all()])) elif key['type'] == 'layer': - qs = Annotation.objects.filter(layer__name=i, item=self).order_by('start') + qs = Annotation.objects.filter(layer=i, item=self).order_by('start') save(i, '\n'.join([l.value for l in qs])) elif i != '*' and i not in self.facet_keys: value = self.get(i) @@ -1046,7 +1047,7 @@ class Item(models.Model): def load_subtitles(self): with transaction.commit_on_success(): - layer = Layer.objects.get(name='subtitles') + layer = 'subtitles' Annotation.objects.filter(layer=layer,item=self).delete() offset = 0 language = ''