cablegates/pandora/annotation/models.py

133 lines
3.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement
from django.db import models
from django.contrib.auth.models import User
2011-01-28 08:43:46 +00:00
import ox
2010-12-28 14:04:28 +00:00
import utils
2011-06-17 07:44:45 +00:00
import managers
2010-12-28 14:04:28 +00:00
2011-02-11 10:21:25 +00:00
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
2010-11-28 16:03:23 +00:00
class Layer(models.Model):
2011-01-01 11:44:42 +00:00
class Meta:
ordering = ('position', )
2011-01-18 09:54:14 +00:00
enabled = models.BooleanField(default=True)
name = models.CharField(null=True, max_length=255, unique=True)
title = models.CharField(null=True, max_length=255)
2011-01-18 09:54:14 +00:00
#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)
2011-01-18 09:54:14 +00:00
#can this be changed per user?
position = models.IntegerField(default=0)
2011-01-18 09:54:14 +00:00
overlap = models.BooleanField(default=True)
overlay = models.BooleanField(default=True)
2011-02-11 10:21:25 +00:00
private = models.BooleanField(default=False) #false=users only see there own bins
2011-01-18 09:54:14 +00:00
#find/sort integration
find = models.BooleanField(default=True) #true part of find all
2011-01-01 11:44:42 +00:00
#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
2010-11-28 16:03:23 +00:00
def json(self):
2011-02-11 10:21:25 +00:00
return {
'id': self.name,
'overlap': self.overlap,
'private': self.private,
'title': self.title,
'type': self.type
}
2010-11-28 16:03:23 +00:00
2010-11-28 16:31:53 +00:00
def __unicode__(self):
return self.title
2011-01-01 11:44:42 +00:00
2010-11-28 16:03:23 +00:00
class Annotation(models.Model):
2011-06-17 07:44:45 +00:00
objects = managers.AnnotationManager()
2011-01-01 11:44:42 +00:00
#FIXME: here having a item,start index would be good
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User)
2011-01-06 03:11:00 +00:00
item = models.ForeignKey('item.Item', related_name='annotations')
#seconds
start = models.FloatField(default=-1)
2011-01-18 09:54:14 +00:00
end = models.FloatField(default=-1)
2010-12-28 14:04:28 +00:00
layer = models.ForeignKey(Layer)
value = models.TextField()
def editable(self, user):
if user.is_authenticated():
2011-01-21 09:31:49 +00:00
if user.is_staff or \
self.user == user or \
user.groups.filter(id__in=self.groups.all()).count() > 0:
return True
return False
2010-12-28 14:04:28 +00:00
def html(self):
if self.layer.type == 'string':
return utils.html_parser(self.value)
else:
return self.value
2011-01-18 09:54:14 +00:00
def get_id(self):
2011-08-18 11:53:38 +00:00
return '%s/%s' % (self.item.itemId, ox.to32(self.id))
2011-01-18 09:54:14 +00:00
2011-06-16 20:00:10 +00:00
def json(self, layer=False, keys=None):
2011-06-01 11:03:07 +00:00
j = {
2011-01-18 09:54:14 +00:00
'id': self.get_id(),
2010-12-28 14:04:28 +00:00
'user': self.user.username,
2011-01-28 08:43:46 +00:00
'in': self.start,
'out': self.end,
2010-12-28 14:04:28 +00:00
'value': self.value,
'created': self.created,
'modified': self.modified
2010-12-28 14:04:28 +00:00
}
2011-06-01 11:03:07 +00:00
if layer:
j['layer'] = self.layer.name
2011-06-16 20:00:10 +00:00
if keys:
_j = {}
for key in keys:
if key in j:
_j[key] = j[key]
j = _j
if 'aspectRatio' in keys:
j['aspectRatio'] = self.item.stream_aspect
if 'item' in keys:
j['item'] = self.item.itemId
2011-06-01 11:03:07 +00:00
return j
2010-12-28 14:04:28 +00:00
2010-11-28 16:31:53 +00:00
def __unicode__(self):
2011-01-18 09:54:14 +00:00
return u"%s/%s-%s" %(self.item, self.start, self.end)
2011-06-04 16:19:06 +00:00