support words/wordsperminute for specific layer

This commit is contained in:
j 2014-03-16 14:31:21 +00:00
parent 220e091460
commit 9e74d03301
2 changed files with 64 additions and 56 deletions

View file

@ -386,7 +386,7 @@
"columnWidth": 60, "columnWidth": 60,
"format": {"type": "number", "args": []}, "format": {"type": "number", "args": []},
"sort": true, "sort": true,
"value": {"layer": "subtitles", "type": "words"} "value": {"layer": "transcripts", "type": "words"}
}, },
{ {
"id": "wordsperminute", "id": "wordsperminute",
@ -395,7 +395,7 @@
"columnWidth": 60, "columnWidth": 60,
"format": {"type": "number", "args": [3]}, "format": {"type": "number", "args": [3]},
"sort": true, "sort": true,
"value": {"layer": "subtitles", "type": "wordsperminute"} "value": {"layer": "transcripts", "type": "wordsperminute"}
}, },
{ {
"id": "size", "id": "size",

View file

@ -818,6 +818,20 @@ class Item(models.Model):
value = None value = None
setattr(s, name, value) setattr(s, name, value)
def get_value(source, key):
if 'value' in key and 'layer' in key['value']:
value = [a.value for a in self.annotations.filter(layer=key['value']['layer']).exclude(value='')]
else:
value = self.get(source)
return value
def get_words(source, key):
value = get_value(source, key)
if isinstance(value, list):
value = '\n'.join(value)
value = len(value.split(' ')) if value else 0
return value
base_keys = ( base_keys = (
'aspectratio', 'aspectratio',
'bitrate', 'bitrate',
@ -847,59 +861,8 @@ class Item(models.Model):
'saturation', 'saturation',
'size', 'size',
'volume', 'volume',
'words',
'wordsperminute',
) )
for key in filter(lambda k: k.get('sort', False), settings.CONFIG['itemKeys']):
name = key['id']
source = name
sort_type = key.get('sortType', key['type'])
if 'value' in key:
if 'layer' in key['value']:
continue
source = key['value']['key']
sort_type = key['value'].get('type', sort_type)
if isinstance(sort_type, list):
sort_type = sort_type[0]
if name not in base_keys:
if sort_type == 'title':
value = get_title_sort(self.get(source, u'Untitled'))
value = utils.sort_title(value)[:955]
set_value(s, name, value)
elif sort_type == 'person':
value = sortNames(self.get(source, []))
value = utils.sort_string(value)[:955]
set_value(s, name, value)
elif sort_type == 'string':
value = self.get(source, u'')
if isinstance(value, list):
value = u','.join(value)
value = utils.sort_string(value)[:955]
set_value(s, name, value)
elif sort_type in ('length', 'integer', 'time', 'float'):
#can be length of strings or length of arrays, i.e. keywords
value = self.get(source)
if isinstance(value, list):
value = len(value)
set_value(s, name, value)
elif sort_type == 'words':
value = self.get(source)
if isinstance(value, list):
value = '\n'.join(value)
if value:
value = len(value.split(' '))
set_value(s, name, value)
elif sort_type == 'year':
value = self.get(source)
set_value(s, name, value)
elif sort_type == 'date':
value = self.get(source)
if isinstance(value, basestring):
value = datetime_safe.datetime.strptime(value, '%Y-%m-%d')
set_value(s, name, value)
#sort keys based on database, these will always be available #sort keys based on database, these will always be available
s.itemId = self.itemId.replace('0x', 'xx') s.itemId = self.itemId.replace('0x', 'xx')
s.oxdbId = self.oxdbId s.oxdbId = self.oxdbId
@ -911,7 +874,6 @@ class Item(models.Model):
s.aspectratio = self.get('aspectratio') s.aspectratio = self.get('aspectratio')
if self.id: if self.id:
s.words = sum([len(a.value.split()) for a in self.annotations.exclude(value='')])
s.clips = self.clips.count() s.clips = self.clips.count()
s.numberoffiles = self.files.all().count() s.numberoffiles = self.files.all().count()
@ -957,14 +919,60 @@ class Item(models.Model):
s.numberofdocuments = self.documents.count() s.numberofdocuments = self.documents.count()
if s.duration: if s.duration:
s.cutsperminute = s.numberofcuts / (s.duration/60) s.cutsperminute = s.numberofcuts / (s.duration/60)
s.wordsperminute = s.words / (s.duration / 60)
else: else:
s.cutsperminute = None s.cutsperminute = None
s.wordsperminute = None
s.timesaccessed = self.accessed.aggregate(Sum('accessed'))['accessed__sum'] s.timesaccessed = self.accessed.aggregate(Sum('accessed'))['accessed__sum']
if not s.timesaccessed: if not s.timesaccessed:
s.timesaccessed = 0 s.timesaccessed = 0
s.accessed = self.accessed.aggregate(Max('access'))['access__max'] s.accessed = self.accessed.aggregate(Max('access'))['access__max']
for key in filter(lambda k: k.get('sort', False), settings.CONFIG['itemKeys']):
name = key['id']
source = name
sort_type = key.get('sortType', key['type'])
if 'value' in key:
if 'key' in key['value']:
source = key['value']['key']
sort_type = key['value'].get('type', sort_type)
if isinstance(sort_type, list):
sort_type = sort_type[0]
if name not in base_keys:
if sort_type == 'title':
value = get_title_sort(self.get(source, u'Untitled'))
value = utils.sort_title(value)[:955]
set_value(s, name, value)
elif sort_type == 'person':
value = sortNames(self.get(source, []))
value = utils.sort_string(value)[:955]
set_value(s, name, value)
elif sort_type == 'string':
value = self.get(source, u'')
if isinstance(value, list):
value = u','.join(value)
value = utils.sort_string(value)[:955]
set_value(s, name, value)
elif sort_type == 'words':
value = get_words(source, key) if s.duration else None
set_value(s, name, value)
elif sort_type == 'wordsperminute':
value = get_words(source, key)
value = value / (s.duration / 60) if value and s.duration else None
set_value(s, name, value)
elif sort_type in ('length', 'integer', 'time', 'float'):
#can be length of strings or length of arrays, i.e. keywords
value = self.get(source)
if isinstance(value, list):
value = len(value)
set_value(s, name, value)
elif sort_type == 'year':
value = self.get(source)
set_value(s, name, value)
elif sort_type == 'date':
value = self.get(source)
if isinstance(value, basestring):
value = datetime_safe.datetime.strptime(value, '%Y-%m-%d')
set_value(s, name, value)
s.save() s.save()
def update_layer_facet(self, key): def update_layer_facet(self, key):