From b13c43c832e0d3752759256564d178bd23e598ba Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Fri, 11 Feb 2011 15:51:25 +0530 Subject: [PATCH] make private annotations private --- pandora/annotation/models.py | 25 +++++++++++++++++++++++-- pandora/annotation/views.py | 9 +++++++-- pandora/item/models.py | 10 +++++++--- pandora/item/views.py | 2 +- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index 27544676..461691e3 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -8,6 +8,21 @@ import ox import utils + +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: @@ -26,7 +41,7 @@ class Layer(models.Model): overlap = models.BooleanField(default=True) overlay = models.BooleanField(default=True) - public = models.BooleanField(default=True) #false=users only see there own bins + 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 @@ -42,7 +57,13 @@ class Layer(models.Model): return p def json(self): - return {'id': self.name, 'title': self.title, 'type': self.type} + return { + 'id': self.name, + 'overlap': self.overlap, + 'private': self.private, + 'title': self.title, + 'type': self.type + } def __unicode__(self): return self.title diff --git a/pandora/annotation/views.py b/pandora/annotation/views.py index c87af88e..c60266c0 100644 --- a/pandora/annotation/views.py +++ b/pandora/annotation/views.py @@ -6,11 +6,13 @@ from ox.utils import json from ox.django.decorators import login_required_json from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response -import models -from item.models import Item +from app.models import site_config +from item.models import Item from api.actions import actions +import models + def findAnnotations(request): ''' @@ -56,6 +58,9 @@ 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(site_config()['layers']) + item = get_object_or_404_json(Item, itemId=data['item']) layer = get_object_or_404_json(models.Layer, name=data['layer']) diff --git a/pandora/item/models.py b/pandora/item/models.py index 30373a77..94af172a 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -336,7 +336,7 @@ class Item(models.Model): stream['formats'] = list(set(map(lambda s: os.path.splitext(s['profile'])[1][1:], self.streams.all().values('profile')))) return stream - def get_layers(self): + def get_layers(self, user=None): layers = {} layers['subtitles'] = [] #FIXME: should subtitles be stored in Annotation? @@ -345,7 +345,12 @@ class Item(models.Model): layers['subtitles'] = qs[0].srt() for l in Layer.objects.all(): ll = layers.setdefault(l.name, []) - for a in Annotation.objects.filter(layer=l, item=self).order_by('start'): + qs = Annotation.objects.filter(layer=l, item=self) + if l.private: + if user.is_anonymous(): + user = None + qs = qs.filter(user=user) + for a in qs.order_by('start'): ll.append(a.json()) return layers @@ -863,7 +868,6 @@ for key in filter(lambda k: 'columnWidth' in k, config['itemKeys']): ItemSort = type('ItemSort', (models.Model,), attrs) ItemSort.fields = [f.name for f in ItemSort._meta.fields] - class Access(models.Model): class Meta: unique_together = ("item", "user") diff --git a/pandora/item/views.py b/pandora/item/views.py index 25817d62..5a4c0843 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -314,7 +314,7 @@ def getItem(request): if item.access(request.user): info = item.get_json() info['stream'] = item.get_stream() - info['layers'] = item.get_layers() + info['layers'] = item.get_layers(request.user) response['data']['item'] = info else: response = json_response(status=403, text='permission denied')