2009-06-08 16:08:59 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2010-08-07 14:31:20 +00:00
|
|
|
from __future__ import division, with_statement
|
|
|
|
|
2010-02-03 12:05:38 +00:00
|
|
|
from datetime import datetime
|
2009-06-08 16:08:59 +00:00
|
|
|
import os.path
|
2010-09-03 13:28:44 +00:00
|
|
|
import subprocess
|
2010-09-10 14:09:41 +00:00
|
|
|
from glob import glob
|
2011-04-18 18:50:31 +00:00
|
|
|
import shutil
|
2011-01-28 08:48:38 +00:00
|
|
|
import uuid
|
|
|
|
import unicodedata
|
2011-07-03 16:21:27 +00:00
|
|
|
from urllib import quote
|
2009-10-04 22:00:08 +00:00
|
|
|
|
2011-10-29 14:59:12 +00:00
|
|
|
from django.db import models, transaction
|
2013-02-21 13:28:36 +00:00
|
|
|
from django.db.models import Q, Sum, Max
|
2010-02-16 12:41:57 +00:00
|
|
|
from django.conf import settings
|
2011-01-21 09:31:49 +00:00
|
|
|
from django.contrib.auth.models import User, Group
|
2011-04-18 18:50:31 +00:00
|
|
|
from django.db.models.signals import pre_delete
|
2010-01-22 23:09:07 +00:00
|
|
|
|
2010-07-07 22:46:41 +00:00
|
|
|
import ox
|
2011-01-01 11:44:42 +00:00
|
|
|
from ox.django import fields
|
2010-12-25 13:45:19 +00:00
|
|
|
import ox.web.imdb
|
2011-08-16 15:06:40 +00:00
|
|
|
import ox.image
|
2009-06-08 16:08:59 +00:00
|
|
|
|
2009-08-01 14:14:54 +00:00
|
|
|
import managers
|
2010-02-03 12:05:38 +00:00
|
|
|
import utils
|
2010-11-27 12:12:53 +00:00
|
|
|
import tasks
|
2012-05-17 09:38:59 +00:00
|
|
|
from .timelines import join_tiles
|
2011-10-15 15:21:41 +00:00
|
|
|
from data_api import external_data
|
2009-06-08 16:08:59 +00:00
|
|
|
|
2011-08-20 17:53:26 +00:00
|
|
|
from archive import extract
|
2011-11-02 14:06:34 +00:00
|
|
|
from annotation.models import Annotation
|
2011-11-02 14:27:02 +00:00
|
|
|
from clip.models import Clip
|
2012-01-31 17:06:10 +00:00
|
|
|
from changelog.models import Changelog
|
2011-08-23 17:39:34 +00:00
|
|
|
import archive.models
|
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
from person.models import get_name_sort
|
2011-10-11 11:29:05 +00:00
|
|
|
from title.models import get_title_sort
|
2012-06-18 14:36:04 +00:00
|
|
|
from sequence.tasks import get_sequences
|
2010-11-14 21:49:16 +00:00
|
|
|
|
2011-10-15 15:21:41 +00:00
|
|
|
def get_id(info):
|
|
|
|
q = Item.objects.all()
|
|
|
|
for key in ('title', 'director', 'year'):
|
|
|
|
# 'episodeTitle', 'episodeDirector', 'episodeYear', 'season', 'episode'):
|
|
|
|
if key in info and info[key]:
|
|
|
|
k = 'find__key'
|
|
|
|
v = 'find__value'
|
|
|
|
if key in Item.facet_keys + ['title']:
|
|
|
|
k = 'facets__key'
|
|
|
|
v = 'facets__value'
|
|
|
|
if isinstance(info[key], list):
|
|
|
|
for value in info[key]:
|
|
|
|
q = q.filter(**{k: key, v: value})
|
|
|
|
else:
|
|
|
|
q = q.filter(**{k:key, v:info[key]})
|
|
|
|
if q.count() == 1:
|
|
|
|
return q[0].itemId
|
|
|
|
if settings.DATA_SERVICE:
|
|
|
|
r = external_data('getId', info)
|
|
|
|
if r['status']['code'] == 200:
|
2011-10-18 20:06:01 +00:00
|
|
|
imdbId = r['data']['id']
|
2011-10-15 15:21:41 +00:00
|
|
|
return imdbId
|
|
|
|
return None
|
|
|
|
|
2011-08-20 09:04:19 +00:00
|
|
|
def get_item(info, user=None, async=False):
|
2010-01-16 20:42:11 +00:00
|
|
|
'''
|
|
|
|
info dict with:
|
2011-10-15 15:21:41 +00:00
|
|
|
imdbId, title, director, year,
|
|
|
|
season, episode, episodeTitle, episodeDirector, episodeYear
|
2010-01-16 20:42:11 +00:00
|
|
|
'''
|
2011-10-15 15:21:41 +00:00
|
|
|
item_data = {
|
2011-10-17 19:43:42 +00:00
|
|
|
'title': info.get('title', ''),
|
|
|
|
'director': info.get('director', []),
|
2011-10-15 15:21:41 +00:00
|
|
|
}
|
2013-04-23 14:14:48 +00:00
|
|
|
|
2012-03-06 22:05:21 +00:00
|
|
|
if filter(lambda k: k['id'] == 'year', settings.CONFIG['itemKeys']):
|
2013-04-28 17:00:59 +00:00
|
|
|
item_data['year'] = info.get('year', '') or ''
|
2013-04-23 14:14:48 +00:00
|
|
|
|
|
|
|
#add additional item metadata parsed from path
|
|
|
|
for key in [i for i in info
|
|
|
|
if i in set([k['id'] for k in settings.CONFIG['itemKeys']]) \
|
|
|
|
and i not in ('language', ) \
|
|
|
|
and i not in item_data]:
|
|
|
|
item_data[key] = info[key]
|
2012-12-16 16:21:05 +00:00
|
|
|
|
2011-10-15 15:21:41 +00:00
|
|
|
for key in ('episodeTitle', 'episodeDirector', 'episodeYear',
|
|
|
|
'season', 'episode', 'seriesTitle'):
|
|
|
|
if key in info and info[key]:
|
|
|
|
item_data[key] = info[key]
|
2013-04-23 14:14:48 +00:00
|
|
|
|
2011-01-16 13:28:57 +00:00
|
|
|
if settings.USE_IMDB:
|
|
|
|
if 'imdbId' in info and info['imdbId']:
|
2010-07-12 14:56:14 +00:00
|
|
|
try:
|
2011-01-16 13:28:57 +00:00
|
|
|
item = Item.objects.get(itemId=info['imdbId'])
|
2010-09-23 16:01:48 +00:00
|
|
|
except Item.DoesNotExist:
|
2011-01-16 13:28:57 +00:00
|
|
|
item = Item(itemId=info['imdbId'])
|
|
|
|
if 'title' in info and 'director' in info:
|
2011-10-15 15:21:41 +00:00
|
|
|
item.external_data = item_data
|
2011-02-22 16:09:13 +00:00
|
|
|
item.user = user
|
2011-09-28 12:47:13 +00:00
|
|
|
item.oxdbId = item.itemId
|
2011-01-16 13:28:57 +00:00
|
|
|
item.save()
|
2011-08-20 09:04:19 +00:00
|
|
|
if async:
|
|
|
|
tasks.update_external.delay(item.itemId)
|
|
|
|
else:
|
|
|
|
item.update_external()
|
2011-01-16 13:28:57 +00:00
|
|
|
else:
|
2011-10-15 15:21:41 +00:00
|
|
|
itemId = get_id(info)
|
|
|
|
if itemId:
|
|
|
|
try:
|
|
|
|
item = Item.objects.get(itemId=itemId)
|
|
|
|
except Item.DoesNotExist:
|
|
|
|
info['imdbId'] = itemId
|
2012-05-30 10:10:35 +00:00
|
|
|
item = get_item(info, user)
|
2011-10-15 15:21:41 +00:00
|
|
|
return item
|
|
|
|
try:
|
|
|
|
item = Item.objects.get(itemId=info.get('oxdbId'))
|
|
|
|
except Item.DoesNotExist:
|
2011-07-05 14:28:22 +00:00
|
|
|
item = Item()
|
2011-10-15 15:21:41 +00:00
|
|
|
item.user = user
|
|
|
|
item.data = item_data
|
|
|
|
item.itemId = info.get('oxdbId', item.oxdb_id())
|
2010-12-08 01:16:13 +00:00
|
|
|
try:
|
2011-10-15 15:21:41 +00:00
|
|
|
existing_item = Item.objects.get(oxdbId=item.oxdb_id())
|
|
|
|
item = existing_item
|
2010-12-08 01:16:13 +00:00
|
|
|
except Item.DoesNotExist:
|
2011-10-15 15:21:41 +00:00
|
|
|
item.oxdbId = item.oxdb_id()
|
2012-09-25 17:00:10 +00:00
|
|
|
p = item.save()
|
|
|
|
if not p:
|
|
|
|
tasks.update_poster.delay(item.itemId)
|
2011-01-16 13:28:57 +00:00
|
|
|
else:
|
2013-02-08 11:05:35 +00:00
|
|
|
qs = Item.objects.filter(find__key='title', find__value__iexact=info['title'])
|
2013-02-19 12:26:38 +00:00
|
|
|
if 'year' in info:
|
|
|
|
qs = qs.filter(find__key='year', find__value__iexact=str(info['year']))
|
2011-01-28 08:48:38 +00:00
|
|
|
if qs.count() == 1:
|
|
|
|
item = qs[0]
|
|
|
|
else:
|
|
|
|
item = Item()
|
2011-10-15 15:21:41 +00:00
|
|
|
item.data = item_data
|
2011-02-22 16:09:13 +00:00
|
|
|
item.user = user
|
2012-09-25 17:00:10 +00:00
|
|
|
p = item.save()
|
|
|
|
if not p:
|
|
|
|
tasks.update_poster.delay(item.itemId)
|
2010-09-23 16:01:48 +00:00
|
|
|
return item
|
2010-01-16 20:42:11 +00:00
|
|
|
|
2010-09-23 16:01:48 +00:00
|
|
|
class Item(models.Model):
|
2009-10-04 22:00:08 +00:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified = models.DateTimeField(auto_now=True)
|
2009-06-08 16:08:59 +00:00
|
|
|
|
2011-02-22 16:09:13 +00:00
|
|
|
user = models.ForeignKey(User, null=True, related_name='items')
|
2011-02-23 11:51:32 +00:00
|
|
|
groups = models.ManyToManyField(Group, blank=True, related_name='items')
|
2011-01-21 09:31:49 +00:00
|
|
|
|
2011-06-27 13:39:35 +00:00
|
|
|
#while metadata is updated, files are set to rendered=False
|
|
|
|
rendered = models.BooleanField(default=False, db_index=True)
|
2011-09-16 17:17:49 +00:00
|
|
|
#should be set based on user
|
2011-12-27 06:54:49 +00:00
|
|
|
level = models.IntegerField(db_index=True)
|
2011-01-21 09:31:49 +00:00
|
|
|
|
2010-09-23 16:01:48 +00:00
|
|
|
itemId = models.CharField(max_length=128, unique=True, blank=True)
|
2011-04-05 10:49:58 +00:00
|
|
|
oxdbId = models.CharField(max_length=42, unique=True, blank=True, null=True)
|
2010-11-27 02:33:31 +00:00
|
|
|
external_data = fields.DictField(default={}, editable=False)
|
|
|
|
data = fields.DictField(default={}, editable=False)
|
|
|
|
json = fields.DictField(default={}, editable=False)
|
2011-01-22 10:14:30 +00:00
|
|
|
poster = models.ImageField(default=None, blank=True,
|
|
|
|
upload_to=lambda i, x: i.path("poster.jpg"))
|
2011-07-30 12:52:49 +00:00
|
|
|
poster_source = models.TextField(blank=True)
|
2010-11-27 02:33:31 +00:00
|
|
|
poster_height = models.IntegerField(default=0)
|
|
|
|
poster_width = models.IntegerField(default=0)
|
|
|
|
poster_frame = models.FloatField(default=-1)
|
2011-01-04 07:32:32 +00:00
|
|
|
|
2011-01-22 10:14:30 +00:00
|
|
|
icon = models.ImageField(default=None, blank=True,
|
|
|
|
upload_to=lambda i, x: i.path("icon.jpg"))
|
2011-01-04 07:32:32 +00:00
|
|
|
|
2011-12-15 11:21:21 +00:00
|
|
|
torrent = models.FileField(default=None, blank=True, max_length=1000,
|
2011-07-03 16:21:27 +00:00
|
|
|
upload_to=lambda i, x: i.path('torrent.torrent'))
|
2011-07-03 18:11:35 +00:00
|
|
|
stream_info = fields.DictField(default={}, editable=False)
|
2011-07-03 16:21:27 +00:00
|
|
|
|
2010-11-27 02:33:31 +00:00
|
|
|
#stream related fields
|
|
|
|
stream_aspect = models.FloatField(default=4/3)
|
2009-10-04 22:00:08 +00:00
|
|
|
|
2010-09-23 16:01:48 +00:00
|
|
|
objects = managers.ItemManager()
|
2009-08-01 14:14:54 +00:00
|
|
|
|
2009-10-04 22:00:08 +00:00
|
|
|
def get(self, key, default=None):
|
2011-10-28 22:43:44 +00:00
|
|
|
if key == 'rightslevel':
|
|
|
|
return self.level
|
2010-11-14 21:49:16 +00:00
|
|
|
if self.data and key in self.data:
|
|
|
|
return self.data[key]
|
|
|
|
if self.external_data and key in self.external_data:
|
|
|
|
return self.external_data[key]
|
2009-10-04 22:00:08 +00:00
|
|
|
return default
|
|
|
|
|
2011-01-24 13:44:38 +00:00
|
|
|
def access(self, user):
|
2011-09-16 17:17:49 +00:00
|
|
|
if user.is_anonymous():
|
|
|
|
level = 'guest'
|
|
|
|
else:
|
|
|
|
level = user.get_profile().get_level()
|
|
|
|
allowed_level = settings.CONFIG['capabilities']['canSeeItem'][level]
|
2011-09-28 12:47:13 +00:00
|
|
|
if self.level <= allowed_level:
|
2011-01-24 13:44:38 +00:00
|
|
|
return True
|
2011-01-25 14:39:03 +00:00
|
|
|
elif user.is_authenticated() and \
|
2011-09-06 12:06:59 +00:00
|
|
|
(self.user == user or \
|
2011-01-25 14:39:03 +00:00
|
|
|
self.groups.filter(id__in=user.groups.all()).count() > 0):
|
|
|
|
return True
|
2011-01-24 13:44:38 +00:00
|
|
|
return False
|
|
|
|
|
2010-01-30 06:56:10 +00:00
|
|
|
def editable(self, user):
|
2011-06-06 18:38:16 +00:00
|
|
|
if user.is_anonymous():
|
|
|
|
return False
|
2011-10-22 22:12:56 +00:00
|
|
|
if user.get_profile().capability('canEditMetadata') == True or \
|
|
|
|
user.is_staff or \
|
2011-01-21 09:31:49 +00:00
|
|
|
self.user == user or \
|
|
|
|
self.groups.filter(id__in=user.groups.all()).count() > 0:
|
|
|
|
return True
|
2010-01-27 06:43:17 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def edit(self, data):
|
|
|
|
#FIXME: how to map the keys to the right place to write them to?
|
2011-01-21 09:31:49 +00:00
|
|
|
if 'id' in data:
|
|
|
|
#FIXME: check if id is valid and exists and move/merge items accordingly
|
|
|
|
del data['id']
|
2011-02-25 12:12:56 +00:00
|
|
|
if 'groups' in data:
|
2011-01-21 09:31:49 +00:00
|
|
|
groups = data.pop('groups')
|
2012-01-13 09:47:18 +00:00
|
|
|
if isinstance(groups, list):
|
|
|
|
groups = filter(lambda g: g.strip(), groups)
|
2012-02-22 10:14:07 +00:00
|
|
|
groups = [ox.escape_html(g) for g in groups]
|
2012-01-13 09:47:18 +00:00
|
|
|
self.groups.exclude(name__in=groups).delete()
|
|
|
|
current_groups = [g.name for g in self.groups.all()]
|
|
|
|
for g in filter(lambda g: g not in current_groups, groups):
|
|
|
|
group, created = Group.objects.get_or_create(name=g)
|
|
|
|
self.groups.add(group)
|
2012-01-15 15:05:37 +00:00
|
|
|
keys = [k['id'] for k in
|
|
|
|
filter(lambda i: i.get('description'), settings.CONFIG['itemKeys'])]
|
|
|
|
for k in keys:
|
|
|
|
key = '%sdescription' % k
|
|
|
|
if key in data:
|
2012-02-14 10:38:36 +00:00
|
|
|
description = data.pop(key)
|
2013-02-26 14:08:56 +00:00
|
|
|
if isinstance(description, dict):
|
|
|
|
for value in description:
|
|
|
|
d, created = Description.objects.get_or_create(key=k, value=value)
|
|
|
|
d.description = ox.sanitize_html(description[value])
|
|
|
|
d.save()
|
|
|
|
else:
|
|
|
|
value = data.get(k, self.get(k, ''))
|
|
|
|
if not description:
|
|
|
|
description = ''
|
|
|
|
d, created = Description.objects.get_or_create(key=k, value=value)
|
|
|
|
d.description = ox.sanitize_html(description)
|
|
|
|
d.save()
|
2011-01-01 11:44:42 +00:00
|
|
|
for key in data:
|
2012-02-13 11:20:34 +00:00
|
|
|
if data[key] == None:
|
|
|
|
if key in self.data:
|
|
|
|
del self.data[key]
|
|
|
|
else:
|
2012-02-21 16:26:06 +00:00
|
|
|
k = filter(lambda i: i['id'] == key, settings.CONFIG['itemKeys'])
|
2012-02-22 10:14:07 +00:00
|
|
|
ktype = k and k[0].get('type') or ''
|
2012-11-18 19:26:13 +00:00
|
|
|
if ktype == 'text':
|
2012-05-27 11:52:12 +00:00
|
|
|
self.data[key] = ox.sanitize_html(data[key])
|
2012-11-18 19:26:13 +00:00
|
|
|
elif ktype == '[text]':
|
2012-05-27 11:52:12 +00:00
|
|
|
self.data[key] = [ox.sanitize_html(t) for t in data[key]]
|
2012-11-18 19:26:13 +00:00
|
|
|
elif ktype == '[string]':
|
2012-02-22 10:14:07 +00:00
|
|
|
self.data[key] = [ox.escape_html(t) for t in data[key]]
|
2012-11-06 23:47:18 +00:00
|
|
|
elif key in ('episodeTitle', 'seriesTitle', 'episodeDirector', 'seriesYear'):
|
2012-11-06 23:41:22 +00:00
|
|
|
self.data[key] = ox.escape_html(data[key])
|
2012-02-21 16:26:06 +00:00
|
|
|
elif isinstance(data[key], basestring):
|
|
|
|
self.data[key] = ox.escape_html(data[key])
|
2012-02-22 10:14:07 +00:00
|
|
|
elif isinstance(data[key], list):
|
|
|
|
def cleanup(i):
|
|
|
|
if isinstance(i, basestring):
|
|
|
|
i = ox.escape_html(i)
|
|
|
|
return i
|
|
|
|
self.data[key] = [cleanup(i) for i in data[key]]
|
2012-02-21 16:26:06 +00:00
|
|
|
else:
|
|
|
|
self.data[key] = ox.escape_html(data[key])
|
2013-02-25 13:30:22 +00:00
|
|
|
p = self.save()
|
|
|
|
if not settings.USE_IMDB and filter(lambda k: k in ('title', 'director', 'year'), data):
|
|
|
|
p = tasks.update_poster.delay(self.itemId)
|
|
|
|
return p
|
2010-01-27 06:43:17 +00:00
|
|
|
|
2012-01-31 17:06:10 +00:00
|
|
|
def log(self):
|
|
|
|
c = Changelog(type='item')
|
|
|
|
c.value = self.json
|
|
|
|
c.save()
|
|
|
|
|
2011-01-16 13:28:57 +00:00
|
|
|
def update_external(self):
|
2011-10-15 15:21:41 +00:00
|
|
|
if settings.DATA_SERVICE and not self.itemId.startswith('0x'):
|
|
|
|
response = external_data('getData', {'id': self.itemId})
|
|
|
|
if response['status']['code'] == 200:
|
|
|
|
self.external_data = response['data']
|
2012-09-25 17:00:10 +00:00
|
|
|
p = self.save()
|
|
|
|
if p:
|
|
|
|
p.wait()
|
|
|
|
else:
|
|
|
|
self.make_poster(True)
|
2010-07-12 14:56:14 +00:00
|
|
|
|
2012-12-16 16:21:05 +00:00
|
|
|
def add_default_data(self):
|
|
|
|
for k in settings.CONFIG['itemKeys']:
|
|
|
|
if 'default' in k and not k['id'] in self.data:
|
|
|
|
self.data[k['id']] = k['default']
|
|
|
|
|
2011-09-30 17:37:41 +00:00
|
|
|
def expand_connections(self):
|
|
|
|
c = self.get('connections')
|
|
|
|
if c:
|
2011-09-30 18:13:56 +00:00
|
|
|
for t in c.keys():
|
|
|
|
if c[t]:
|
|
|
|
if isinstance(c[t][0], basestring):
|
|
|
|
c[t]= [{'id': i, 'title': None} for i in c[t]]
|
|
|
|
ids = [i['id'] for i in c[t]]
|
|
|
|
known = {}
|
|
|
|
for l in Item.objects.filter(itemId__in=ids):
|
|
|
|
known[l.itemId] = l.get('title')
|
|
|
|
for i in c[t]:
|
|
|
|
if i['id'] in known:
|
|
|
|
i['item'] = i['id']
|
|
|
|
i['title'] = known[i['id']]
|
|
|
|
c[t]= filter(lambda x: x['title'], c[t])
|
|
|
|
if not c[t]:
|
|
|
|
del c[t]
|
|
|
|
return c
|
2011-09-30 17:37:41 +00:00
|
|
|
|
2009-06-08 16:08:59 +00:00
|
|
|
def __unicode__(self):
|
2010-09-10 14:09:41 +00:00
|
|
|
year = self.get('year')
|
|
|
|
if year:
|
2012-03-10 17:29:46 +00:00
|
|
|
string = u'%s (%s)' % (self.get('title', 'Untitled'), self.get('year'))
|
|
|
|
else:
|
|
|
|
string = self.get('title', u'Untitled')
|
|
|
|
return u'[%s] %s' % (self.itemId,string)
|
2009-10-04 22:00:08 +00:00
|
|
|
|
2010-09-14 14:10:37 +00:00
|
|
|
def get_absolute_url(self):
|
2010-11-30 23:33:42 +00:00
|
|
|
return '/%s' % self.itemId
|
2010-09-14 14:10:37 +00:00
|
|
|
|
2009-06-08 16:08:59 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2011-10-24 20:48:14 +00:00
|
|
|
update_poster = False
|
2011-10-25 10:57:31 +00:00
|
|
|
update_ids = False
|
2011-01-28 08:48:38 +00:00
|
|
|
if not self.id:
|
2011-10-26 14:04:50 +00:00
|
|
|
if self.user:
|
|
|
|
self.level = settings.CONFIG['rightsLevel'][self.user.get_profile().get_level()]
|
2011-12-27 06:54:49 +00:00
|
|
|
else:
|
|
|
|
self.level = settings.CONFIG['rightsLevel']['member']
|
2011-01-28 08:48:38 +00:00
|
|
|
if not self.itemId:
|
|
|
|
self.itemId = str(uuid.uuid1())
|
2012-12-16 16:21:05 +00:00
|
|
|
self.add_default_data()
|
2011-01-28 08:48:38 +00:00
|
|
|
super(Item, self).save(*args, **kwargs)
|
|
|
|
if not settings.USE_IMDB:
|
2011-12-18 09:35:49 +00:00
|
|
|
self.itemId = ox.toAZ(self.id)
|
2011-10-15 15:21:41 +00:00
|
|
|
|
|
|
|
#this does not work if another item without imdbid has the same metadata
|
2011-09-28 12:47:13 +00:00
|
|
|
oxdbId = self.oxdb_id()
|
2012-06-13 15:59:10 +00:00
|
|
|
if not settings.USE_IMDB:
|
2012-06-26 08:48:43 +00:00
|
|
|
self.oxdbId = None
|
2012-06-13 15:59:10 +00:00
|
|
|
elif oxdbId:
|
2011-10-15 15:21:41 +00:00
|
|
|
if self.oxdbId != oxdbId:
|
|
|
|
q = Item.objects.filter(oxdbId=oxdbId).exclude(id=self.id)
|
|
|
|
if q.count() != 0:
|
2011-10-24 21:41:55 +00:00
|
|
|
if len(self.itemId) == 7:
|
|
|
|
self.oxdbId = None
|
|
|
|
q[0].merge_with(self, save=False)
|
|
|
|
else:
|
|
|
|
n = 1
|
|
|
|
key = 'episodeTitle' in self.data and 'episodeTitle' or 'title'
|
|
|
|
title = self.get(key, 'Untitled')
|
|
|
|
while q.count() != 0:
|
|
|
|
n += 1
|
|
|
|
self.data[key] = u'%s [%d]' % (title, n)
|
|
|
|
oxdbId = self.oxdb_id()
|
|
|
|
q = Item.objects.filter(oxdbId=oxdbId).exclude(id=self.id)
|
|
|
|
self.oxdbId = oxdbId
|
2011-10-24 20:48:14 +00:00
|
|
|
update_poster = True
|
2012-10-10 11:36:01 +00:00
|
|
|
if len(self.itemId) != 7:
|
|
|
|
update_ids = True
|
2012-11-18 19:26:13 +00:00
|
|
|
|
2011-07-05 14:28:22 +00:00
|
|
|
#id changed, what about existing item with new id?
|
|
|
|
if settings.USE_IMDB and len(self.itemId) != 7 and self.oxdbId != self.itemId:
|
|
|
|
self.itemId = self.oxdbId
|
|
|
|
#FIXME: move files to new id here
|
2011-11-22 16:49:38 +00:00
|
|
|
if settings.USE_IMDB and len(self.itemId) == 7:
|
|
|
|
for key in ('title', 'year', 'director', 'season', 'episode',
|
|
|
|
'seriesTitle', 'episodeTitle'):
|
|
|
|
if key in self.data:
|
|
|
|
del self.data[key]
|
2010-07-05 12:07:59 +00:00
|
|
|
|
2011-10-31 10:46:59 +00:00
|
|
|
if self.poster and os.path.exists(self.poster.path):
|
2010-09-06 20:31:12 +00:00
|
|
|
self.poster_height = self.poster.height
|
|
|
|
self.poster_width = self.poster.width
|
2010-09-10 10:41:08 +00:00
|
|
|
else:
|
|
|
|
self.poster_height = 128
|
|
|
|
self.poster_width = 80
|
2011-01-06 06:53:35 +00:00
|
|
|
self.json = self.get_json()
|
2012-09-27 04:32:35 +00:00
|
|
|
self.json['modified'] = datetime.now()
|
2011-01-06 06:53:35 +00:00
|
|
|
super(Item, self).save(*args, **kwargs)
|
2012-09-27 04:28:34 +00:00
|
|
|
self.update_find()
|
|
|
|
self.update_sort()
|
|
|
|
self.update_facets()
|
2011-10-25 10:57:31 +00:00
|
|
|
if update_ids:
|
|
|
|
for c in self.clips.all(): c.save()
|
|
|
|
for a in self.annotations.all():
|
|
|
|
public_id = a.public_id.split('/')[1]
|
2012-09-25 10:24:01 +00:00
|
|
|
public_id = "%s/%s" % (self.itemId, public_id)
|
|
|
|
if public_id != a.public_id:
|
|
|
|
a.public_id = public_id
|
|
|
|
a.save()
|
2012-09-11 13:17:35 +00:00
|
|
|
tasks.update_file_paths.delay(self.itemId)
|
2011-02-25 11:22:44 +00:00
|
|
|
if update_poster:
|
2011-10-24 20:48:14 +00:00
|
|
|
return tasks.update_poster.delay(self.itemId)
|
|
|
|
return None
|
2009-06-08 16:08:59 +00:00
|
|
|
|
2011-04-18 18:50:31 +00:00
|
|
|
def delete_files(self):
|
2011-05-25 19:11:08 +00:00
|
|
|
path = os.path.join(settings.MEDIA_ROOT, self.path())
|
|
|
|
if os.path.exists(path):
|
|
|
|
shutil.rmtree(path)
|
2011-04-18 18:50:31 +00:00
|
|
|
|
2010-09-12 14:23:23 +00:00
|
|
|
def delete(self, *args, **kwargs):
|
2011-04-18 18:50:31 +00:00
|
|
|
self.delete_files()
|
2010-09-23 16:01:48 +00:00
|
|
|
super(Item, self).delete(*args, **kwargs)
|
2010-09-12 14:23:23 +00:00
|
|
|
|
2011-10-15 15:21:41 +00:00
|
|
|
def merge_with(self, other, save=True):
|
2010-09-12 14:23:23 +00:00
|
|
|
'''
|
|
|
|
move all related tables to other and delete self
|
|
|
|
'''
|
|
|
|
for l in self.lists.all():
|
2011-05-25 19:11:08 +00:00
|
|
|
l.remove(self)
|
2013-02-10 13:08:49 +00:00
|
|
|
if l.items.filter(id=other.id).count() == 0:
|
2011-05-25 19:11:08 +00:00
|
|
|
l.add(other)
|
2013-03-19 14:04:03 +00:00
|
|
|
self.annotations.all().update(item=other, public_id=None)
|
|
|
|
for a in other.annotations.filter(public_id=None).order_by('id'):
|
|
|
|
a.set_public_id()
|
2011-01-03 20:27:40 +00:00
|
|
|
|
2010-09-12 14:23:23 +00:00
|
|
|
if hasattr(self, 'files'):
|
|
|
|
for f in self.files.all():
|
2010-09-23 16:01:48 +00:00
|
|
|
f.item = other
|
2010-09-12 14:23:23 +00:00
|
|
|
f.save()
|
|
|
|
self.delete()
|
2011-10-15 15:21:41 +00:00
|
|
|
if save:
|
|
|
|
other.save()
|
|
|
|
#FIXME: update poster, stills and streams after this
|
2010-09-12 14:23:23 +00:00
|
|
|
|
2010-09-10 15:12:22 +00:00
|
|
|
def get_posters(self):
|
2011-07-30 10:51:23 +00:00
|
|
|
url = self.prefered_poster_url()
|
2011-10-15 15:21:41 +00:00
|
|
|
external_posters = self.external_data.get('posters', {})
|
|
|
|
services = external_posters.keys()
|
2011-06-06 18:38:16 +00:00
|
|
|
index = []
|
2011-07-30 13:51:14 +00:00
|
|
|
for service in settings.POSTER_PRECEDENCE:
|
|
|
|
if service in services:
|
2011-06-06 18:38:16 +00:00
|
|
|
index.append(service)
|
2011-07-30 13:51:14 +00:00
|
|
|
for service in services:
|
2011-06-06 18:38:16 +00:00
|
|
|
if service not in index:
|
|
|
|
index.append(service)
|
|
|
|
if settings.URL not in index:
|
|
|
|
index.append(settings.URL)
|
2011-07-30 13:51:14 +00:00
|
|
|
|
2011-06-06 18:58:51 +00:00
|
|
|
posters = []
|
2011-08-23 17:39:34 +00:00
|
|
|
poster = self.path('siteposter.jpg')
|
2011-06-06 18:58:51 +00:00
|
|
|
poster = os.path.abspath(os.path.join(settings.MEDIA_ROOT, poster))
|
|
|
|
if os.path.exists(poster):
|
|
|
|
posters.append({
|
2011-08-23 17:39:34 +00:00
|
|
|
'url': '/%s/siteposter.jpg' % self.itemId,
|
2011-07-26 17:22:23 +00:00
|
|
|
'width': 640,
|
|
|
|
'height': 1024,
|
2011-07-30 11:04:30 +00:00
|
|
|
'source': settings.URL,
|
2011-07-30 13:51:14 +00:00
|
|
|
'selected': url == None,
|
2011-06-06 18:38:16 +00:00
|
|
|
'index': index.index(settings.URL)
|
2011-06-06 18:58:51 +00:00
|
|
|
})
|
|
|
|
|
2011-10-15 15:21:41 +00:00
|
|
|
for service in external_posters:
|
|
|
|
p = external_posters[service][0]
|
|
|
|
p['source'] = service
|
|
|
|
p['selected'] = p['url'] == url
|
|
|
|
p['index'] = index.index(service)
|
|
|
|
posters.append(p)
|
2011-06-06 18:38:16 +00:00
|
|
|
posters.sort(key=lambda a: a['index'])
|
2010-09-10 15:12:22 +00:00
|
|
|
return posters
|
|
|
|
|
2011-06-06 18:58:51 +00:00
|
|
|
def get_frames(self):
|
|
|
|
frames = []
|
2011-08-04 17:53:43 +00:00
|
|
|
pframes = self.poster_frames()
|
|
|
|
if pframes:
|
2011-06-06 18:58:51 +00:00
|
|
|
pos = self.poster_frame
|
|
|
|
if pos < 0:
|
2011-08-04 17:55:41 +00:00
|
|
|
pos = int(len(pframes) / 2)
|
2011-06-06 18:58:51 +00:00
|
|
|
p = 0
|
2011-08-04 17:53:43 +00:00
|
|
|
for f in pframes:
|
2011-06-06 18:58:51 +00:00
|
|
|
frames.append({
|
|
|
|
'index': p,
|
|
|
|
'position': f['position'],
|
|
|
|
'selected': p == pos,
|
2012-06-11 08:30:29 +00:00
|
|
|
'url': '/%s/posterframe%d.jpg' %(self.itemId, p),
|
2011-06-06 18:58:51 +00:00
|
|
|
'height': f['height'],
|
2012-11-18 19:26:13 +00:00
|
|
|
'width': f['width']
|
2011-06-06 18:58:51 +00:00
|
|
|
})
|
|
|
|
p += 1
|
|
|
|
return frames
|
|
|
|
|
2010-09-10 15:12:22 +00:00
|
|
|
def get_stream(self):
|
2011-08-18 19:37:12 +00:00
|
|
|
for s in self.streams():
|
|
|
|
return s.json()
|
2010-09-10 15:12:22 +00:00
|
|
|
|
2011-02-11 10:21:25 +00:00
|
|
|
def get_layers(self, user=None):
|
2010-09-18 14:44:35 +00:00
|
|
|
layers = {}
|
2011-11-02 14:06:34 +00:00
|
|
|
for l in settings.CONFIG['layers']:
|
|
|
|
name = l['id']
|
|
|
|
ll = layers.setdefault(name, [])
|
2012-01-03 20:17:44 +00:00
|
|
|
qs = Annotation.objects.filter(layer=name, item=self).order_by(
|
|
|
|
'start', 'end', 'sortvalue')
|
2011-11-02 14:06:34 +00:00
|
|
|
if name == 'subtitles':
|
2011-10-20 09:11:14 +00:00
|
|
|
qs = qs.exclude(value='')
|
2011-11-02 14:06:34 +00:00
|
|
|
if l.get('private'):
|
2011-08-08 15:59:11 +00:00
|
|
|
if user and user.is_anonymous():
|
2011-02-11 10:21:25 +00:00
|
|
|
user = None
|
|
|
|
qs = qs.filter(user=user)
|
|
|
|
for a in qs.order_by('start'):
|
2012-01-12 19:32:54 +00:00
|
|
|
ll.append(a.json(user=user))
|
2010-09-18 14:44:35 +00:00
|
|
|
return layers
|
2013-05-27 11:21:08 +00:00
|
|
|
|
|
|
|
def get_documents(self, user=None):
|
|
|
|
qs = self.documents.all()
|
|
|
|
documents = [d.json(item=self) for d in qs]
|
|
|
|
return sorted(documents, key=lambda d: d['index'])
|
2010-09-18 14:44:35 +00:00
|
|
|
|
2011-07-30 12:23:06 +00:00
|
|
|
def get_json(self, keys=None):
|
2011-01-24 09:38:46 +00:00
|
|
|
i = {
|
2011-06-27 13:39:35 +00:00
|
|
|
'id': self.itemId,
|
2011-10-22 12:51:56 +00:00
|
|
|
'rendered': self.rendered,
|
2011-10-25 13:59:27 +00:00
|
|
|
'rightslevel': self.level
|
2011-01-06 06:53:35 +00:00
|
|
|
}
|
2011-12-27 06:54:49 +00:00
|
|
|
if self.user:
|
|
|
|
i['user'] = self.user.username
|
2011-01-24 09:38:46 +00:00
|
|
|
i.update(self.external_data)
|
|
|
|
i.update(self.data)
|
2011-09-06 12:06:59 +00:00
|
|
|
for k in settings.CONFIG['itemKeys']:
|
2011-01-24 09:38:46 +00:00
|
|
|
key = k['id']
|
2011-07-30 12:23:06 +00:00
|
|
|
if not keys or key in keys:
|
|
|
|
if key not in i:
|
|
|
|
value = self.get(key)
|
|
|
|
#also get values from sort table, i.e. numberof values
|
2012-10-06 11:59:33 +00:00
|
|
|
if not value:
|
|
|
|
try:
|
|
|
|
if self.sort and hasattr(self.sort, key):
|
|
|
|
value = getattr(self.sort, key)
|
|
|
|
except ItemSort.DoesNotExist:
|
|
|
|
pass
|
2011-07-30 12:23:06 +00:00
|
|
|
if value:
|
|
|
|
i[key] = value
|
|
|
|
|
2011-08-06 07:17:35 +00:00
|
|
|
if 'cast' in i and isinstance(i['cast'][0], basestring):
|
|
|
|
i['cast'] = [i['cast']]
|
2011-08-05 18:26:27 +00:00
|
|
|
if 'cast' in i and isinstance(i['cast'][0], list):
|
|
|
|
i['cast'] = map(lambda x: {'actor': x[0], 'character': x[1]}, i['cast'])
|
2011-08-05 15:50:18 +00:00
|
|
|
|
2011-09-30 17:37:41 +00:00
|
|
|
if 'connections' in i:
|
|
|
|
i['connections'] = self.expand_connections()
|
|
|
|
|
2011-08-19 12:20:30 +00:00
|
|
|
if not keys or 'posterRatio' in keys:
|
|
|
|
i['posterRatio'] = self.poster_width / self.poster_height
|
2011-08-06 13:34:56 +00:00
|
|
|
|
2011-09-29 13:05:34 +00:00
|
|
|
|
2011-08-19 12:20:30 +00:00
|
|
|
streams = self.streams()
|
|
|
|
i['durations'] = [s.duration for s in streams]
|
2011-08-18 19:37:12 +00:00
|
|
|
i['duration'] = sum(i['durations'])
|
2013-01-31 08:52:37 +00:00
|
|
|
if not streams:
|
|
|
|
i['duration'] = self.files.filter(
|
|
|
|
Q(selected=True)|Q(wanted=True)
|
|
|
|
).aggregate(Sum('duration'))['duration__sum']
|
2011-08-19 12:25:46 +00:00
|
|
|
i['parts'] = len(i['durations'])
|
|
|
|
if i['parts']:
|
2011-08-19 12:20:30 +00:00
|
|
|
i['videoRatio'] = streams[0].aspect_ratio
|
2011-10-28 23:17:16 +00:00
|
|
|
i['resolution'] = (streams[0].file.width, streams[0].file.height)
|
2011-08-18 12:01:37 +00:00
|
|
|
|
2011-08-06 13:34:56 +00:00
|
|
|
#only needed by admins
|
2011-06-06 18:58:51 +00:00
|
|
|
if keys and 'posters' in keys:
|
2011-07-30 10:51:23 +00:00
|
|
|
i['posters'] = self.get_posters()
|
2011-09-29 13:05:34 +00:00
|
|
|
|
|
|
|
frames = self.get_frames()
|
2011-06-06 18:58:51 +00:00
|
|
|
if keys and 'frames' in keys:
|
2011-09-29 13:05:34 +00:00
|
|
|
i['frames'] = frames
|
|
|
|
|
2011-09-30 01:20:47 +00:00
|
|
|
selected_frame = filter(lambda f: f['selected'], frames)
|
|
|
|
if selected_frame:
|
|
|
|
i['posterFrame'] = selected_frame[0]['position']
|
2011-09-29 13:05:34 +00:00
|
|
|
elif self.poster_frame != -1.0:
|
|
|
|
i['posterFrame'] = self.poster_frame
|
|
|
|
|
2012-01-15 15:05:37 +00:00
|
|
|
dkeys = [k['id'] for k in
|
|
|
|
filter(lambda i: i.get('description'), settings.CONFIG['itemKeys'])]
|
|
|
|
if keys:
|
|
|
|
dkeys = filter(lambda k: k in keys, dkeys)
|
|
|
|
for key in dkeys:
|
2013-02-26 14:08:56 +00:00
|
|
|
k = filter(lambda i: i['id'] == key, settings.CONFIG['itemKeys'])
|
|
|
|
if isinstance((k and k[0].get('type') or ''), list):
|
|
|
|
i['%sdescription'%key] = {}
|
|
|
|
if key == 'name':
|
|
|
|
values = []
|
|
|
|
for ikey in filter(lambda i: i.get('sortType') == 'person',
|
|
|
|
settings.CONFIG['itemKeys']):
|
|
|
|
values += i.get(ikey['id'], [])
|
|
|
|
values = list(set(values))
|
|
|
|
else:
|
|
|
|
values = self.get(key)
|
|
|
|
if values:
|
|
|
|
for d in Description.objects.filter(key=key, value__in=values):
|
|
|
|
i['%sdescription'%key][d.value] = d.description
|
2012-01-15 15:05:37 +00:00
|
|
|
else:
|
2013-02-26 14:08:56 +00:00
|
|
|
qs = Description.objects.filter(key=key, value=self.get(key, ''))
|
|
|
|
i['%sdescription'%key] = '' if qs.count() == 0 else qs[0].description
|
2011-07-30 12:23:06 +00:00
|
|
|
if keys:
|
|
|
|
info = {}
|
|
|
|
for key in keys:
|
|
|
|
if key in i:
|
|
|
|
info[key] = i[key]
|
|
|
|
return info
|
2011-01-24 09:38:46 +00:00
|
|
|
return i
|
2009-06-08 16:08:59 +00:00
|
|
|
|
2013-03-09 09:35:04 +00:00
|
|
|
def get_item_description(self):
|
|
|
|
if settings.USE_IMDB:
|
|
|
|
info = tuple([self.data.get(k, 0) for k in ['hue', 'saturation', 'lightness']])
|
|
|
|
description = 'Hue: %.3f, Saturation: %.3f, Lightness: %.3f' % info
|
|
|
|
else:
|
|
|
|
description = ox.strip_tags(self.get('summary', ''))
|
|
|
|
return description
|
|
|
|
|
2010-11-28 16:03:23 +00:00
|
|
|
def oxdb_id(self):
|
2011-04-22 23:34:01 +00:00
|
|
|
if not self.get('title') and not self.get('director'):
|
2012-06-13 15:59:10 +00:00
|
|
|
return self.itemId
|
2011-10-18 12:01:01 +00:00
|
|
|
return ox.get_oxid(self.get('seriesTitle', self.get('title', '')),
|
|
|
|
self.get('director', []),
|
2011-10-24 19:33:03 +00:00
|
|
|
self.get('seriesYear', self.get('year', '')),
|
2011-10-18 12:01:01 +00:00
|
|
|
self.get('season', ''),
|
|
|
|
self.get('episode', ''),
|
2011-10-15 15:21:41 +00:00
|
|
|
self.get('episodeTitle', ''),
|
2011-10-18 12:01:01 +00:00
|
|
|
self.get('episodeDirector', []),
|
|
|
|
self.get('episodeYear', ''))
|
2010-02-16 12:41:57 +00:00
|
|
|
|
2010-09-10 15:12:22 +00:00
|
|
|
'''
|
|
|
|
Search related functions
|
|
|
|
'''
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-27 12:12:53 +00:00
|
|
|
def update_find(self):
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-06 16:14:00 +00:00
|
|
|
def save(key, value):
|
2011-09-04 21:56:22 +00:00
|
|
|
if value not in ('', None):
|
2011-10-24 20:48:14 +00:00
|
|
|
f, created = ItemFind.objects.get_or_create(item=self, key=key)
|
2011-11-08 17:43:12 +00:00
|
|
|
if isinstance(value, bool):
|
2011-11-10 14:59:12 +00:00
|
|
|
value = value and 'true' or 'false'
|
2011-01-16 13:28:57 +00:00
|
|
|
if isinstance(value, basestring):
|
2012-05-27 11:52:12 +00:00
|
|
|
value = ox.decode_html(ox.strip_tags(value.strip()))
|
2012-11-18 19:26:13 +00:00
|
|
|
value = unicodedata.normalize('NFKD', value).lower()
|
2011-01-16 13:28:57 +00:00
|
|
|
f.value = value
|
2010-11-06 16:14:00 +00:00
|
|
|
f.save()
|
|
|
|
else:
|
2011-10-24 20:48:14 +00:00
|
|
|
ItemFind.objects.filter(item=self, key=key).delete()
|
2009-10-04 22:00:08 +00:00
|
|
|
|
2011-10-29 14:59:12 +00:00
|
|
|
with transaction.commit_on_success():
|
|
|
|
for key in settings.CONFIG['itemKeys']:
|
|
|
|
i = key['id']
|
|
|
|
if i == 'title':
|
2013-03-24 19:44:48 +00:00
|
|
|
titles = [self.get('title', 'Untitled')]
|
|
|
|
if self.get('originalTitle'):
|
|
|
|
titles.append(self.get('originalTitle'))
|
|
|
|
at = self.get('alternativeTitles')
|
|
|
|
if at:
|
|
|
|
titles += [a[0] for a in at]
|
|
|
|
save(i, u'\n'.join(titles))
|
2011-10-29 14:59:12 +00:00
|
|
|
elif i == 'rightslevel':
|
|
|
|
save(i, self.level)
|
|
|
|
elif i == 'filename':
|
2012-09-07 22:25:26 +00:00
|
|
|
save(i, '\n'.join(self.all_paths()))
|
2012-02-16 14:13:08 +00:00
|
|
|
elif i == 'user':
|
2012-02-20 14:39:30 +00:00
|
|
|
if self.user:
|
|
|
|
save(i, self.user.username)
|
2012-01-18 19:03:29 +00:00
|
|
|
elif key['id'] == 'annotations':
|
|
|
|
qs = Annotation.objects.filter(item=self)
|
2012-06-18 16:36:40 +00:00
|
|
|
qs = qs.filter(layer__in=Annotation.public_layers()).exclude(findvalue=None)
|
2012-01-18 19:03:29 +00:00
|
|
|
qs = qs.order_by('start')
|
|
|
|
save(i, u'\n'.join([l.findvalue for l in qs]))
|
2011-10-29 14:59:12 +00:00
|
|
|
elif key['type'] == 'layer':
|
2012-06-18 16:36:40 +00:00
|
|
|
qs = Annotation.objects.filter(item=self).exclude(findvalue=None)
|
2012-01-18 19:03:29 +00:00
|
|
|
qs = qs.filter(layer=i)
|
2012-01-12 19:32:54 +00:00
|
|
|
qs = qs.order_by('start')
|
2012-01-31 17:06:10 +00:00
|
|
|
save(i, u'\n'.join(filter(None, [l.findvalue for l in qs])))
|
2011-10-29 14:59:12 +00:00
|
|
|
elif i != '*' and i not in self.facet_keys:
|
|
|
|
value = self.get(i)
|
|
|
|
if isinstance(value, list):
|
|
|
|
value = u'\n'.join(value)
|
|
|
|
save(i, value)
|
|
|
|
|
|
|
|
for key in self.facet_keys:
|
|
|
|
if key == 'character':
|
|
|
|
values = self.get('cast', '')
|
|
|
|
if values:
|
|
|
|
values = filter(lambda x: x.strip(),
|
|
|
|
[f['character'] for f in values])
|
|
|
|
values = list(set(values))
|
|
|
|
elif key == 'name':
|
|
|
|
values = []
|
|
|
|
for k in map(lambda x: x['id'],
|
2012-03-08 11:39:04 +00:00
|
|
|
filter(lambda x: x.get('sortType') == 'person',
|
2011-10-29 14:59:12 +00:00
|
|
|
settings.CONFIG['itemKeys'])):
|
|
|
|
values += self.get(k, [])
|
2011-10-20 08:43:33 +00:00
|
|
|
values = list(set(values))
|
2011-10-29 14:59:12 +00:00
|
|
|
else:
|
|
|
|
values = self.get(key, '')
|
|
|
|
if isinstance(values, list):
|
|
|
|
save(key, '\n'.join(values))
|
|
|
|
else:
|
|
|
|
save(key, values)
|
2011-01-05 13:06:09 +00:00
|
|
|
|
2011-11-08 17:43:12 +00:00
|
|
|
isSeries = self.get('series',
|
|
|
|
self.get('episodeTitle',
|
|
|
|
self.get('episode',
|
|
|
|
self.get('seriesTitle')))) != None
|
2011-11-10 14:59:12 +00:00
|
|
|
save('series', isSeries)
|
2011-11-08 17:43:12 +00:00
|
|
|
|
2010-11-27 12:12:53 +00:00
|
|
|
def update_sort(self):
|
2009-10-04 22:00:08 +00:00
|
|
|
try:
|
|
|
|
s = self.sort
|
2010-09-23 16:01:48 +00:00
|
|
|
except ItemSort.DoesNotExist:
|
|
|
|
s = ItemSort(item=self)
|
2009-08-16 12:23:29 +00:00
|
|
|
|
2010-07-12 14:56:14 +00:00
|
|
|
def sortNames(values):
|
2011-01-03 08:45:31 +00:00
|
|
|
sort_value = u''
|
2010-07-12 14:56:14 +00:00
|
|
|
if values:
|
2011-01-03 08:45:31 +00:00
|
|
|
sort_value = u'; '.join([get_name_sort(name) for name in values])
|
2009-08-16 12:23:29 +00:00
|
|
|
if not sort_value:
|
2011-01-03 08:45:31 +00:00
|
|
|
sort_value = u''
|
2009-08-16 12:23:29 +00:00
|
|
|
return sort_value
|
|
|
|
|
2011-01-25 14:39:03 +00:00
|
|
|
def set_value(s, name, value):
|
|
|
|
if not value:
|
|
|
|
value = None
|
2011-10-18 20:06:01 +00:00
|
|
|
if isinstance(value, basestring):
|
2012-05-27 11:52:12 +00:00
|
|
|
value = ox.decode_html(value.lower())
|
2011-01-25 14:39:03 +00:00
|
|
|
setattr(s, name, value)
|
|
|
|
|
2011-01-03 14:14:54 +00:00
|
|
|
base_keys = (
|
|
|
|
'aspectratio',
|
2011-10-20 08:43:33 +00:00
|
|
|
'bitrate',
|
|
|
|
'clips',
|
2011-11-10 19:52:26 +00:00
|
|
|
'created',
|
2011-10-20 08:43:33 +00:00
|
|
|
'cutsperminute',
|
2011-01-03 14:14:54 +00:00
|
|
|
'duration',
|
2011-01-25 14:39:03 +00:00
|
|
|
'hue',
|
2011-10-20 08:43:33 +00:00
|
|
|
'id',
|
2011-01-25 14:39:03 +00:00
|
|
|
'lightness',
|
2011-10-20 08:43:33 +00:00
|
|
|
'modified',
|
2012-02-20 17:42:13 +00:00
|
|
|
'numberofannotations',
|
2011-10-18 20:06:01 +00:00
|
|
|
'numberofcuts',
|
2011-01-25 14:39:03 +00:00
|
|
|
'numberoffiles',
|
2011-08-18 12:01:37 +00:00
|
|
|
'parts',
|
2011-10-20 08:43:33 +00:00
|
|
|
'pixels',
|
2012-02-15 20:39:21 +00:00
|
|
|
'random',
|
2011-11-10 19:52:26 +00:00
|
|
|
'timesaccessed',
|
2011-11-11 17:45:46 +00:00
|
|
|
'accessed',
|
2011-10-20 08:43:33 +00:00
|
|
|
'resolution',
|
2011-11-10 21:22:58 +00:00
|
|
|
'width',
|
|
|
|
'height',
|
2012-05-29 09:26:18 +00:00
|
|
|
'rendered',
|
2011-10-25 13:59:27 +00:00
|
|
|
'rightslevel',
|
2011-10-20 08:43:33 +00:00
|
|
|
'saturation',
|
|
|
|
'size',
|
|
|
|
'volume',
|
|
|
|
'words',
|
|
|
|
'wordsperminute',
|
2011-01-03 14:14:54 +00:00
|
|
|
)
|
2011-01-24 09:38:46 +00:00
|
|
|
|
2012-03-08 11:39:04 +00:00
|
|
|
for key in filter(lambda k: k.get('sort', False), settings.CONFIG['itemKeys']):
|
2011-01-03 14:14:54 +00:00
|
|
|
name = key['id']
|
2011-01-24 09:38:46 +00:00
|
|
|
source = name
|
2012-03-08 11:39:04 +00:00
|
|
|
sort_type = key.get('sortType', key['type'])
|
2011-01-24 09:38:46 +00:00
|
|
|
if 'value' in key:
|
2011-01-25 14:39:03 +00:00
|
|
|
if 'layer' in key['value']:
|
2012-11-18 19:26:13 +00:00
|
|
|
continue
|
2011-01-24 09:38:46 +00:00
|
|
|
source = key['value']['key']
|
2011-10-25 07:29:41 +00:00
|
|
|
sort_type = key['value'].get('type', sort_type)
|
2011-10-25 07:31:26 +00:00
|
|
|
if isinstance(sort_type, list):
|
|
|
|
sort_type = sort_type[0]
|
2011-01-05 13:06:09 +00:00
|
|
|
|
2011-01-03 14:14:54 +00:00
|
|
|
if name not in base_keys:
|
2011-01-24 09:38:46 +00:00
|
|
|
if sort_type == 'title':
|
2011-10-11 11:29:05 +00:00
|
|
|
value = get_title_sort(self.get(source, u'Untitled'))
|
2011-10-11 19:05:11 +00:00
|
|
|
value = utils.sort_title(value)[:955]
|
2011-01-24 22:46:16 +00:00
|
|
|
set_value(s, name, value)
|
2011-01-24 09:38:46 +00:00
|
|
|
elif sort_type == 'person':
|
2011-01-05 13:06:09 +00:00
|
|
|
value = sortNames(self.get(source, []))
|
2011-01-03 19:45:56 +00:00
|
|
|
value = utils.sort_string(value)[:955]
|
2011-01-24 22:46:16 +00:00
|
|
|
set_value(s, name, value)
|
2011-01-24 09:38:46 +00:00
|
|
|
elif sort_type == 'string':
|
2011-01-05 13:06:09 +00:00
|
|
|
value = self.get(source, u'')
|
2011-01-03 14:14:54 +00:00
|
|
|
if isinstance(value, list):
|
2011-01-03 08:45:31 +00:00
|
|
|
value = u','.join(value)
|
2011-01-03 19:45:56 +00:00
|
|
|
value = utils.sort_string(value)[:955]
|
2011-01-24 22:46:16 +00:00
|
|
|
set_value(s, name, value)
|
|
|
|
elif sort_type in ('length', 'integer', 'float'):
|
2011-01-05 13:06:09 +00:00
|
|
|
#can be length of strings or length of arrays, i.e. keywords
|
2011-04-22 23:34:01 +00:00
|
|
|
value = self.get(source)
|
2011-01-03 14:14:54 +00:00
|
|
|
if isinstance(value, list):
|
|
|
|
value = len(value)
|
2011-01-24 22:46:16 +00:00
|
|
|
set_value(s, name, value)
|
2011-01-24 09:38:46 +00:00
|
|
|
elif sort_type == 'words':
|
2011-04-22 23:34:01 +00:00
|
|
|
value = self.get(source)
|
2011-01-03 14:14:54 +00:00
|
|
|
if isinstance(value, list):
|
|
|
|
value = '\n'.join(value)
|
|
|
|
if value:
|
|
|
|
value = len(value.split(' '))
|
2011-01-24 22:46:16 +00:00
|
|
|
set_value(s, name, value)
|
2011-01-24 09:38:46 +00:00
|
|
|
elif sort_type == 'year':
|
2011-04-22 23:34:01 +00:00
|
|
|
value = self.get(source)
|
2011-01-24 22:46:16 +00:00
|
|
|
set_value(s, name, value)
|
2011-01-24 09:38:46 +00:00
|
|
|
elif sort_type == 'date':
|
2011-04-22 23:34:01 +00:00
|
|
|
value = self.get(source)
|
2011-01-03 14:14:54 +00:00
|
|
|
if isinstance(value, basestring):
|
|
|
|
value = datetime.strptime(value, '%Y-%m-%d')
|
2011-01-24 22:46:16 +00:00
|
|
|
set_value(s, name, value)
|
2011-01-03 14:14:54 +00:00
|
|
|
|
|
|
|
#sort keys based on database, these will always be available
|
2011-01-03 20:30:50 +00:00
|
|
|
s.itemId = self.itemId.replace('0x', 'xx')
|
2013-02-03 08:51:15 +00:00
|
|
|
if not settings.USE_IMDB:
|
2013-02-03 08:52:20 +00:00
|
|
|
s.itemId = ox.sort_string(str(ox.fromAZ(s.itemId)))
|
2013-02-10 12:48:38 +00:00
|
|
|
s.modified = self.modified or datetime.now()
|
|
|
|
s.created = self.created or datetime.now()
|
2011-10-25 13:59:27 +00:00
|
|
|
s.rightslevel = self.level
|
2011-01-03 14:14:54 +00:00
|
|
|
|
2011-10-28 23:27:44 +00:00
|
|
|
s.aspectratio = self.get('aspectratio')
|
2013-05-12 12:20:55 +00:00
|
|
|
if self.id:
|
|
|
|
s.words = sum([len(a.value.split()) for a in self.annotations.exclude(value='')])
|
|
|
|
s.clips = self.clips.count()
|
2011-09-06 12:06:59 +00:00
|
|
|
|
2012-01-21 10:14:25 +00:00
|
|
|
videos = self.files.filter(selected=True).filter(Q(is_video=True)|Q(is_audio=True))
|
2011-08-23 17:39:34 +00:00
|
|
|
if videos.count() > 0:
|
2010-12-22 18:42:35 +00:00
|
|
|
s.duration = sum([v.duration for v in videos])
|
2011-08-23 17:39:34 +00:00
|
|
|
v = videos[0]
|
2012-01-27 22:22:54 +00:00
|
|
|
if v.is_audio:
|
|
|
|
s.resolution = None
|
|
|
|
s.width = None
|
|
|
|
s.height = None
|
|
|
|
else:
|
|
|
|
s.resolution = v.width * v.height
|
|
|
|
s.width = v.width
|
|
|
|
s.height = v.height
|
2012-11-09 11:07:38 +00:00
|
|
|
if not s.aspectratio and v.display_aspect_ratio:
|
2011-10-20 08:24:41 +00:00
|
|
|
s.aspectratio = float(utils.parse_decimal(v.display_aspect_ratio))
|
2010-12-22 18:42:35 +00:00
|
|
|
s.pixels = sum([v.pixels for v in videos])
|
2011-01-25 14:39:03 +00:00
|
|
|
s.numberoffiles = self.files.all().count()
|
2011-08-23 17:39:34 +00:00
|
|
|
s.parts = videos.count()
|
2010-12-22 18:42:35 +00:00
|
|
|
s.size = sum([v.size for v in videos]) #FIXME: only size of movies?
|
2011-09-30 13:46:26 +00:00
|
|
|
if s.duration:
|
|
|
|
s.bitrate = s.size * 8 / s.duration
|
|
|
|
else:
|
|
|
|
s.bitrate = 0
|
2012-11-09 23:28:34 +00:00
|
|
|
s.volume = self.data.get('volume', None)
|
2010-12-22 18:45:41 +00:00
|
|
|
else:
|
2011-01-24 22:46:16 +00:00
|
|
|
s.duration = None
|
|
|
|
s.resolution = None
|
|
|
|
s.bitrate = None
|
|
|
|
s.pixels = None
|
|
|
|
s.filename = None
|
|
|
|
s.files = None
|
|
|
|
s.size = None
|
|
|
|
s.volume = None
|
2011-08-18 12:01:37 +00:00
|
|
|
s.parts = 0
|
2010-07-12 14:56:14 +00:00
|
|
|
|
2013-02-25 21:33:05 +00:00
|
|
|
for key in ('hue', 'saturation', 'lightness'):
|
|
|
|
if key in self.data:
|
|
|
|
setattr(s, key, self.data.get(key, None))
|
2012-02-20 17:42:13 +00:00
|
|
|
s.numberofannotations = self.annotations.all().count()
|
2011-10-18 20:06:01 +00:00
|
|
|
s.numberofcuts = len(self.data.get('cuts', []))
|
2010-12-25 13:45:19 +00:00
|
|
|
if s.duration:
|
2011-10-18 20:06:01 +00:00
|
|
|
s.cutsperminute = s.numberofcuts / (s.duration/60)
|
2011-09-06 12:06:59 +00:00
|
|
|
s.wordsperminute = s.words / (s.duration / 60)
|
2010-12-25 14:00:48 +00:00
|
|
|
else:
|
2012-11-18 19:26:13 +00:00
|
|
|
s.cutsperminute = None
|
2011-09-06 12:06:59 +00:00
|
|
|
s.wordsperminute = None
|
2011-11-10 19:52:26 +00:00
|
|
|
s.timesaccessed = self.accessed.aggregate(Sum('accessed'))['accessed__sum']
|
2011-11-10 20:00:57 +00:00
|
|
|
if not s.timesaccessed:
|
|
|
|
s.timesaccessed = 0
|
2011-11-11 17:45:46 +00:00
|
|
|
s.accessed = self.accessed.aggregate(Max('access'))['access__max']
|
2009-08-16 12:23:29 +00:00
|
|
|
s.save()
|
2009-06-08 16:08:59 +00:00
|
|
|
|
2012-02-13 14:38:43 +00:00
|
|
|
def update_layer_facet(self, key):
|
|
|
|
current_values = [a['value']
|
|
|
|
for a in self.annotations.filter(layer=key).distinct().values('value')]
|
2012-05-27 11:52:12 +00:00
|
|
|
current_values = [ox.decode_html(v) for v in current_values]
|
2012-02-13 14:38:43 +00:00
|
|
|
saved_values = [i.value for i in Facet.objects.filter(item=self, key=key)]
|
|
|
|
removed_values = filter(lambda i: i not in current_values, saved_values)
|
|
|
|
if removed_values:
|
|
|
|
Facet.objects.filter(item=self, key=key, value__in=removed_values).delete()
|
|
|
|
for value in current_values:
|
|
|
|
if value not in saved_values:
|
2012-05-22 08:12:41 +00:00
|
|
|
sortvalue = utils.sort_string(value).lower()[:900]
|
2012-02-13 14:38:43 +00:00
|
|
|
Facet.objects.get_or_create(item=self, key=key, value=value, sortvalue=sortvalue)
|
|
|
|
|
2011-12-26 19:13:41 +00:00
|
|
|
def update_layer_facets(self):
|
|
|
|
filters = [f['id'] for f in settings.CONFIG['filters']]
|
|
|
|
for layer in settings.CONFIG['layers']:
|
|
|
|
if layer['id'] in filters:
|
2012-02-13 14:38:43 +00:00
|
|
|
self.update_layer_facet(layer['id'])
|
2011-12-26 19:13:41 +00:00
|
|
|
|
2010-11-27 12:12:53 +00:00
|
|
|
def update_facets(self):
|
2011-08-25 15:17:07 +00:00
|
|
|
for key in self.facet_keys + ['title']:
|
2011-01-03 17:47:20 +00:00
|
|
|
current_values = self.get(key, [])
|
2011-08-25 15:17:07 +00:00
|
|
|
if key == 'title':
|
2011-08-26 14:45:04 +00:00
|
|
|
if current_values:
|
|
|
|
current_values = [current_values]
|
|
|
|
else:
|
|
|
|
current_values = []
|
2012-10-09 12:57:21 +00:00
|
|
|
ot = self.get('originalTitle')
|
2011-08-25 15:41:14 +00:00
|
|
|
if ot:
|
|
|
|
current_values.append(ot)
|
2013-03-24 19:44:48 +00:00
|
|
|
at = self.get('alternativeTitles')
|
|
|
|
if at:
|
|
|
|
current_values += [a[0] for a in at]
|
2011-10-20 08:43:33 +00:00
|
|
|
elif key == 'character':
|
|
|
|
current_values = filter(lambda x: x.strip(),
|
|
|
|
[f['character'] for f in self.get('cast', [])])
|
2011-10-20 09:39:21 +00:00
|
|
|
current_values = [item for sublist in [x.split(' / ') for x in current_values]
|
|
|
|
for item in sublist]
|
2011-10-20 08:43:33 +00:00
|
|
|
elif key == 'name':
|
2011-08-09 10:03:06 +00:00
|
|
|
current_values = []
|
2011-10-20 08:43:33 +00:00
|
|
|
#FIXME: is there a better way to build name collection?
|
2011-08-09 10:03:06 +00:00
|
|
|
for k in map(lambda x: x['id'],
|
2012-03-08 11:39:04 +00:00
|
|
|
filter(lambda x: x.get('sortType') == 'person',
|
2011-09-06 12:06:59 +00:00
|
|
|
settings.CONFIG['itemKeys'])):
|
2011-08-09 10:03:06 +00:00
|
|
|
current_values += self.get(k, [])
|
2011-01-03 17:47:20 +00:00
|
|
|
if not isinstance(current_values, list):
|
2012-03-27 17:42:49 +00:00
|
|
|
if not current_values:
|
|
|
|
current_values = []
|
|
|
|
else:
|
|
|
|
current_values = [unicode(current_values)]
|
2011-04-06 12:24:40 +00:00
|
|
|
current_values = list(set(current_values))
|
2012-05-27 11:52:12 +00:00
|
|
|
current_values = [ox.decode_html(v) for v in current_values]
|
2010-09-23 16:01:48 +00:00
|
|
|
saved_values = [i.value for i in Facet.objects.filter(item=self, key=key)]
|
2011-01-25 14:39:03 +00:00
|
|
|
removed_values = filter(lambda i: i not in current_values, saved_values)
|
2010-07-12 14:56:14 +00:00
|
|
|
if removed_values:
|
2010-09-23 16:01:48 +00:00
|
|
|
Facet.objects.filter(item=self, key=key, value__in=removed_values).delete()
|
2010-07-12 14:56:14 +00:00
|
|
|
for value in current_values:
|
|
|
|
if value not in saved_values:
|
2011-10-29 23:32:11 +00:00
|
|
|
sortvalue = value
|
2011-10-30 00:50:38 +00:00
|
|
|
if key in self.person_keys + ['name']:
|
2011-10-29 23:32:11 +00:00
|
|
|
sortvalue = get_name_sort(value)
|
2012-06-12 09:23:25 +00:00
|
|
|
sortvalue = utils.sort_string(sortvalue).lower()[:900]
|
2011-10-29 23:32:11 +00:00
|
|
|
Facet.objects.get_or_create(item=self, key=key, value=value, sortvalue=sortvalue)
|
2011-12-26 19:13:41 +00:00
|
|
|
self.update_layer_facets()
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-12-04 01:26:49 +00:00
|
|
|
def path(self, name=''):
|
2010-12-05 17:51:40 +00:00
|
|
|
h = self.itemId
|
2011-12-26 18:36:58 +00:00
|
|
|
h = (7-len(h))*'0' + h
|
2010-12-07 19:05:59 +00:00
|
|
|
return os.path.join('items', h[:2], h[2:4], h[4:6], h[6:], name)
|
2010-12-04 01:26:49 +00:00
|
|
|
|
2010-09-10 15:12:22 +00:00
|
|
|
'''
|
|
|
|
Video related functions
|
|
|
|
'''
|
2012-02-17 07:32:04 +00:00
|
|
|
def frame(self, position, height=None):
|
2011-08-18 12:01:37 +00:00
|
|
|
offset = 0
|
2011-08-18 19:37:12 +00:00
|
|
|
streams = self.streams()
|
|
|
|
for stream in streams:
|
|
|
|
if stream.duration + offset < position:
|
|
|
|
offset += stream.duration
|
2011-08-18 12:01:37 +00:00
|
|
|
else:
|
|
|
|
position = position - offset
|
2012-02-17 07:32:04 +00:00
|
|
|
if not height:
|
|
|
|
height = stream.resolution
|
|
|
|
else:
|
|
|
|
height = min(height, stream.resolution)
|
2011-08-18 12:01:37 +00:00
|
|
|
path = os.path.join(settings.MEDIA_ROOT, stream.path(),
|
|
|
|
'frames', "%dp"%height, "%s.jpg"%position)
|
2013-04-22 11:45:22 +00:00
|
|
|
if not os.path.exists(path) and stream.media:
|
|
|
|
extract.frame(stream.media.path, path, position, height)
|
2011-08-18 12:01:37 +00:00
|
|
|
if not os.path.exists(path):
|
|
|
|
return None
|
|
|
|
return path
|
2010-09-10 15:12:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def timeline_prefix(self):
|
2011-08-23 17:39:34 +00:00
|
|
|
videos = self.streams()
|
2011-08-18 12:01:37 +00:00
|
|
|
if len(videos) == 1:
|
2012-05-17 09:38:59 +00:00
|
|
|
return os.path.join(settings.MEDIA_ROOT, videos[0].path(''))
|
|
|
|
return os.path.join(settings.MEDIA_ROOT, self.path())
|
2010-09-10 15:12:22 +00:00
|
|
|
|
2012-08-21 22:18:00 +00:00
|
|
|
def all_paths(self):
|
|
|
|
return list(set([
|
|
|
|
item for sublist in
|
|
|
|
[f.all_paths() for f in self.files.all()]
|
|
|
|
for item in sublist
|
|
|
|
]))
|
|
|
|
|
2011-08-23 17:39:34 +00:00
|
|
|
def get_files(self, user):
|
2011-10-18 20:06:01 +00:00
|
|
|
files = self.files.all().select_related()
|
|
|
|
if user.get_profile().get_level() != 'admin':
|
|
|
|
files = files.filter(instances__volume__user=user)
|
|
|
|
return [f.json() for f in files]
|
2011-08-23 17:39:34 +00:00
|
|
|
|
|
|
|
def users_with_files(self):
|
2011-10-15 15:21:41 +00:00
|
|
|
return User.objects.filter(
|
|
|
|
volumes__files__file__item=self
|
|
|
|
).order_by('-profile__level', 'date_joined').distinct()
|
|
|
|
|
|
|
|
def sets(self):
|
|
|
|
sets = []
|
|
|
|
for user in self.users_with_files():
|
|
|
|
files = self.files.filter(instances__volume__user=user, instances__ignore=False)
|
2012-06-28 12:59:08 +00:00
|
|
|
if files.count():
|
|
|
|
sets.append(files)
|
2011-10-15 15:21:41 +00:00
|
|
|
return sets
|
2011-08-23 17:39:34 +00:00
|
|
|
|
|
|
|
def update_wanted(self):
|
2011-10-15 15:21:41 +00:00
|
|
|
wanted = []
|
|
|
|
for s in self.sets():
|
|
|
|
if s.filter(selected=False).count() != 0:
|
|
|
|
wanted += [i.id for i in s]
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
self.files.filter(id__in=wanted).update(wanted=True)
|
|
|
|
self.files.exclude(id__in=wanted).update(wanted=False)
|
2011-08-23 17:39:34 +00:00
|
|
|
|
|
|
|
def update_selected(self):
|
2011-10-15 15:21:41 +00:00
|
|
|
for s in self.sets():
|
|
|
|
if s.filter(Q(is_video=True)|Q(is_audio=True)).filter(available=False).count() == 0:
|
2011-10-18 09:08:09 +00:00
|
|
|
update = False
|
2011-10-18 20:06:01 +00:00
|
|
|
self.files.exclude(id__in=s).exclude(part=None).update(part=None)
|
2011-10-18 09:08:09 +00:00
|
|
|
deselect = self.files.filter(selected=True).exclude(id__in=s)
|
|
|
|
if deselect.count() > 0:
|
|
|
|
deselect.update(selected=False)
|
|
|
|
update = True
|
2011-10-15 15:21:41 +00:00
|
|
|
if s.filter(selected=False).count() > 0:
|
|
|
|
s.update(selected=True, wanted=False)
|
2011-10-18 09:08:09 +00:00
|
|
|
update = True
|
|
|
|
if update:
|
2011-10-15 15:21:41 +00:00
|
|
|
self.rendered = False
|
2013-03-12 09:36:00 +00:00
|
|
|
self.save()
|
|
|
|
tasks.update_timeline.delay(self.itemId)
|
2011-10-15 15:21:41 +00:00
|
|
|
break
|
2010-10-16 11:49:45 +00:00
|
|
|
|
2012-01-17 10:39:45 +00:00
|
|
|
def get_torrent(self, request):
|
|
|
|
if self.torrent:
|
|
|
|
self.torrent.seek(0)
|
|
|
|
data = ox.torrent.bdecode(self.torrent.read())
|
|
|
|
url = request.build_absolute_uri("%s/torrent/"%self.get_absolute_url())
|
2013-02-19 14:49:30 +00:00
|
|
|
if url.startswith('https://'):
|
|
|
|
url = 'http' + url[5:]
|
2012-01-17 10:39:45 +00:00
|
|
|
data['url-list'] = ['%s%s' % (url, u.split('torrent/')[1]) for u in data['url-list']]
|
|
|
|
return ox.torrent.bencode(data)
|
|
|
|
|
2011-07-03 16:21:27 +00:00
|
|
|
def make_torrent(self):
|
2011-12-15 11:21:21 +00:00
|
|
|
streams = self.streams()
|
|
|
|
if streams.count() == 0:
|
|
|
|
return
|
2011-07-03 16:21:27 +00:00
|
|
|
base = self.path('torrent')
|
|
|
|
base = os.path.abspath(os.path.join(settings.MEDIA_ROOT, base))
|
|
|
|
if os.path.exists(base):
|
|
|
|
shutil.rmtree(base)
|
2011-10-15 15:21:41 +00:00
|
|
|
ox.makedirs(base)
|
2011-07-03 16:21:27 +00:00
|
|
|
|
2012-01-17 11:16:06 +00:00
|
|
|
filename = utils.safe_filename(self.get('title'))
|
2012-01-17 11:12:39 +00:00
|
|
|
base = self.path('torrent/%s' % filename)
|
2011-07-03 16:21:27 +00:00
|
|
|
base = os.path.abspath(os.path.join(settings.MEDIA_ROOT, base))
|
|
|
|
size = 0
|
|
|
|
duration = 0.0
|
2011-08-23 17:39:34 +00:00
|
|
|
if streams.count() == 1:
|
2011-07-03 16:21:27 +00:00
|
|
|
url = "%s/torrent/%s.webm" % (self.get_absolute_url(),
|
2012-01-17 11:12:39 +00:00
|
|
|
quote(filename.encode('utf-8')))
|
2011-07-03 16:21:27 +00:00
|
|
|
video = "%s.webm" % base
|
2011-08-23 17:39:34 +00:00
|
|
|
v = streams[0]
|
2013-04-22 11:45:22 +00:00
|
|
|
os.symlink(v.media.path, video)
|
|
|
|
size = v.media.size
|
2011-08-23 17:39:34 +00:00
|
|
|
duration = v.duration
|
2011-07-03 16:21:27 +00:00
|
|
|
else:
|
|
|
|
url = "%s/torrent/" % self.get_absolute_url()
|
|
|
|
part = 1
|
|
|
|
os.makedirs(base)
|
2011-08-23 17:39:34 +00:00
|
|
|
for v in streams:
|
2012-01-17 11:12:39 +00:00
|
|
|
video = "%s/%s.Part %d.webm" % (base, filename, part)
|
2011-07-03 16:21:27 +00:00
|
|
|
part += 1
|
2013-04-22 11:45:22 +00:00
|
|
|
os.symlink(v.media.path, video)
|
|
|
|
size += v.media.size
|
2011-08-23 17:39:34 +00:00
|
|
|
duration += v.duration
|
2011-07-03 16:21:27 +00:00
|
|
|
video = base
|
|
|
|
|
|
|
|
torrent = '%s.torrent' % base
|
2012-01-16 08:27:56 +00:00
|
|
|
url = "http://%s%s" % (settings.CONFIG['site']['url'], url)
|
2011-07-03 16:21:27 +00:00
|
|
|
meta = {
|
|
|
|
'target': torrent,
|
|
|
|
'url-list': url,
|
|
|
|
}
|
|
|
|
if duration:
|
2012-08-15 15:15:06 +00:00
|
|
|
meta['playtime'] = ox.format_duration(duration*1000)[:-4]
|
2011-07-03 16:21:27 +00:00
|
|
|
|
|
|
|
#slightly bigger torrent file but better for streaming
|
|
|
|
piece_size_pow2 = 15 #1 mbps -> 32KB pieces
|
|
|
|
if size / duration >= 1000000:
|
|
|
|
piece_size_pow2 = 16 #2 mbps -> 64KB pieces
|
|
|
|
meta['piece_size_pow2'] = piece_size_pow2
|
|
|
|
|
2012-08-21 08:49:20 +00:00
|
|
|
ox.torrent.create_torrent(video, settings.TRACKER_URL, meta)
|
2012-04-18 09:11:12 +00:00
|
|
|
self.torrent.name = torrent[len(settings.MEDIA_ROOT)+1:]
|
2011-07-03 16:21:27 +00:00
|
|
|
self.save()
|
|
|
|
|
2011-08-18 19:37:12 +00:00
|
|
|
def streams(self):
|
2012-01-21 10:14:25 +00:00
|
|
|
return archive.models.Stream.objects.filter(
|
|
|
|
source=None, available=True, file__item=self, file__selected=True
|
2013-02-08 11:05:35 +00:00
|
|
|
).filter(
|
|
|
|
Q(file__is_audio=True)|Q(file__is_video=True)
|
|
|
|
).order_by('file__part', 'file__sort_path')
|
2011-08-18 19:37:12 +00:00
|
|
|
|
2013-03-12 09:36:00 +00:00
|
|
|
def update_timeline(self, force=False, async=True):
|
2011-08-19 15:37:37 +00:00
|
|
|
streams = self.streams()
|
|
|
|
self.make_timeline()
|
2011-10-21 18:02:36 +00:00
|
|
|
if streams.count() == 1:
|
2013-02-25 21:33:05 +00:00
|
|
|
self.data['hue'], self.data['saturation'], self.data['lightness'] = streams[0].color
|
2011-10-21 18:02:36 +00:00
|
|
|
self.data['cuts'] = streams[0].cuts
|
2012-05-17 09:38:59 +00:00
|
|
|
self.data['volume'] = streams[0].volume
|
2011-10-21 18:02:36 +00:00
|
|
|
else:
|
2012-05-17 09:38:59 +00:00
|
|
|
self.data['cuts'] = extract.cuts(self.timeline_prefix)
|
|
|
|
self.data['volume'] = 0
|
2011-10-21 18:02:36 +00:00
|
|
|
offset = 0
|
|
|
|
color = [0, 0, 0]
|
|
|
|
n = streams.count()
|
|
|
|
for s in streams:
|
2012-11-09 23:28:34 +00:00
|
|
|
self.data['volume'] += s.volume * s.duration
|
2011-10-28 09:58:40 +00:00
|
|
|
color = map(lambda a,b: (a+b)/n, color,ox.image.getRGB(s.color))
|
2011-10-21 18:02:36 +00:00
|
|
|
offset += s.duration
|
2013-02-25 21:33:05 +00:00
|
|
|
self.data['hue'], self.data['saturation'], self.data['lightness'] = ox.image.getHSL(color)
|
2012-05-30 15:52:50 +00:00
|
|
|
if offset:
|
|
|
|
self.data['volume'] /= offset
|
2011-08-19 15:37:37 +00:00
|
|
|
#extract.timeline_strip(self, self.data['cuts'], stream.info, self.timeline_prefix[:-8])
|
2013-03-01 09:14:57 +00:00
|
|
|
self.json = self.get_json()
|
|
|
|
self.update_sort()
|
2011-08-20 10:06:18 +00:00
|
|
|
self.select_frame()
|
2011-12-17 10:59:57 +00:00
|
|
|
self.make_poster(True)
|
2011-08-19 15:37:37 +00:00
|
|
|
self.make_icon()
|
2011-09-06 12:06:59 +00:00
|
|
|
if settings.CONFIG['video']['download']:
|
2011-08-20 10:06:18 +00:00
|
|
|
self.make_torrent()
|
2011-10-23 11:57:52 +00:00
|
|
|
self.rendered = streams.count() > 0
|
2011-08-19 15:37:37 +00:00
|
|
|
self.save()
|
2013-03-12 09:36:00 +00:00
|
|
|
if async:
|
|
|
|
get_sequences.delay(self.itemId)
|
|
|
|
else:
|
|
|
|
get_sequences(self.itemId)
|
2010-09-10 15:12:22 +00:00
|
|
|
|
2011-12-29 19:13:03 +00:00
|
|
|
def save_poster(self, data):
|
|
|
|
self.poster.name = self.path('poster.jpg')
|
|
|
|
poster = self.poster.path
|
|
|
|
with open(poster, 'w') as f:
|
|
|
|
f.write(data)
|
|
|
|
|
2010-09-17 20:23:03 +00:00
|
|
|
def prefered_poster_url(self):
|
2011-10-15 15:21:41 +00:00
|
|
|
external_posters = self.external_data.get('posters', {})
|
2011-07-30 12:52:49 +00:00
|
|
|
service = self.poster_source
|
2011-10-15 15:21:41 +00:00
|
|
|
if service and service != settings.URL and service in external_posters:
|
|
|
|
return external_posters[service][0]['url']
|
2011-07-30 12:52:49 +00:00
|
|
|
if not service:
|
|
|
|
for service in settings.POSTER_PRECEDENCE:
|
2011-10-15 15:21:41 +00:00
|
|
|
if service in external_posters:
|
|
|
|
return external_posters[service][0]['url']
|
2010-09-17 20:23:03 +00:00
|
|
|
return None
|
|
|
|
|
2011-08-11 14:15:56 +00:00
|
|
|
def make_timeline(self):
|
2011-08-19 15:37:37 +00:00
|
|
|
streams = self.streams()
|
2011-10-29 14:45:27 +00:00
|
|
|
if streams.count() > 1:
|
2011-08-20 17:53:26 +00:00
|
|
|
timelines = [s.timeline_prefix for s in self.streams()]
|
2012-05-17 09:38:59 +00:00
|
|
|
join_tiles(timelines, self.timeline_prefix)
|
2012-02-06 15:03:57 +00:00
|
|
|
else:
|
|
|
|
#remove joined timeline if it was created at some point
|
2012-05-30 12:48:08 +00:00
|
|
|
for f in glob(os.path.join(settings.MEDIA_ROOT, self.path(), 'timeline*.jpg')):
|
2012-02-06 15:03:57 +00:00
|
|
|
os.unlink(f)
|
2011-08-11 14:15:56 +00:00
|
|
|
|
2010-12-01 00:00:33 +00:00
|
|
|
def make_poster(self, force=False):
|
2011-10-31 00:18:52 +00:00
|
|
|
ox.makedirs(os.path.join(settings.MEDIA_ROOT,self.path()))
|
2010-09-10 15:12:22 +00:00
|
|
|
if not self.poster or force:
|
2011-10-24 20:04:14 +00:00
|
|
|
poster = self.make_siteposter()
|
2011-10-24 23:07:50 +00:00
|
|
|
url = self.prefered_poster_url()
|
2010-09-10 14:09:41 +00:00
|
|
|
if url:
|
2012-08-15 15:15:06 +00:00
|
|
|
data = ox.net.read_url(url)
|
2011-12-29 19:13:03 +00:00
|
|
|
self.save_poster(data)
|
2011-10-24 23:07:50 +00:00
|
|
|
elif os.path.exists(poster):
|
2011-07-26 17:22:23 +00:00
|
|
|
with open(poster) as f:
|
2011-10-26 14:04:50 +00:00
|
|
|
data = f.read()
|
|
|
|
if data:
|
2011-12-29 19:13:03 +00:00
|
|
|
self.save_poster(data)
|
2013-04-28 17:00:59 +00:00
|
|
|
poster = self.path('poster.jpg')
|
|
|
|
poster = os.path.abspath(os.path.join(settings.MEDIA_ROOT, poster))
|
2012-09-25 17:00:10 +00:00
|
|
|
for f in glob(poster.replace('.jpg', '*.jpg')):
|
|
|
|
if f != poster:
|
|
|
|
try:
|
|
|
|
os.unlink(f)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2010-09-10 14:09:41 +00:00
|
|
|
|
2011-10-24 20:04:14 +00:00
|
|
|
def make_siteposter(self):
|
2011-08-23 17:39:34 +00:00
|
|
|
poster = self.path('siteposter.jpg')
|
2011-07-26 17:22:23 +00:00
|
|
|
poster = os.path.abspath(os.path.join(settings.MEDIA_ROOT, poster))
|
|
|
|
|
|
|
|
frame = self.get_poster_frame_path()
|
2012-05-30 12:48:08 +00:00
|
|
|
timeline = '%stimelineantialias64p.jpg' % self.timeline_prefix
|
2011-07-26 17:22:23 +00:00
|
|
|
|
2012-10-09 10:46:51 +00:00
|
|
|
director = u', '.join(self.get('director', []))
|
2013-03-22 16:16:46 +00:00
|
|
|
director = ox.decode_html(director)
|
|
|
|
title = self.get('title', '')
|
|
|
|
title = ox.decode_html(title)
|
|
|
|
|
2011-07-26 17:22:23 +00:00
|
|
|
cmd = [settings.ITEM_POSTER,
|
2013-03-22 16:16:46 +00:00
|
|
|
'-t', title.encode('utf-8'),
|
2011-07-26 17:22:23 +00:00
|
|
|
'-y', str(self.get('year', '')),
|
|
|
|
'-p', poster
|
|
|
|
]
|
2012-10-09 10:46:51 +00:00
|
|
|
if director:
|
|
|
|
cmd += [
|
|
|
|
'-d', director.encode('utf-8'),
|
|
|
|
]
|
2011-07-26 17:22:23 +00:00
|
|
|
if frame:
|
|
|
|
cmd += [
|
|
|
|
'-f', frame,
|
2011-08-04 13:50:13 +00:00
|
|
|
]
|
|
|
|
if os.path.exists(timeline):
|
|
|
|
cmd += [
|
2011-07-26 17:22:23 +00:00
|
|
|
'-l', timeline,
|
|
|
|
]
|
2012-06-13 15:59:10 +00:00
|
|
|
cmd += [
|
|
|
|
'-i', self.itemId,
|
|
|
|
'-o', self.oxdbId or self.oxdb_id() or self.itemId
|
|
|
|
]
|
2011-07-26 17:22:23 +00:00
|
|
|
ox.makedirs(os.path.join(settings.MEDIA_ROOT,self.path()))
|
|
|
|
p = subprocess.Popen(cmd)
|
|
|
|
p.wait()
|
2011-08-23 17:39:34 +00:00
|
|
|
for f in glob(poster.replace('.jpg', '*.jpg')):
|
2011-08-24 11:31:23 +00:00
|
|
|
if f != poster:
|
|
|
|
os.unlink(f)
|
2011-07-26 17:22:23 +00:00
|
|
|
return poster
|
|
|
|
|
|
|
|
def poster_frames(self):
|
|
|
|
frames = []
|
2013-02-28 11:57:56 +00:00
|
|
|
if settings.CONFIG['media']['importFrames']:
|
|
|
|
offset = 0
|
|
|
|
for f in self.files.filter(selected=True, is_video=True).order_by('sort_path'):
|
|
|
|
for ff in f.frames.all().order_by('position'):
|
|
|
|
frames.append({
|
|
|
|
'position': offset + ff.position,
|
|
|
|
'path': ff.frame.path,
|
|
|
|
'width': ff.frame.width,
|
|
|
|
'height': ff.frame.height
|
|
|
|
})
|
|
|
|
offset += f.duration
|
|
|
|
else:
|
|
|
|
if 'videoRatio' in self.json:
|
|
|
|
width, height = self.json['resolution']
|
|
|
|
pos = self.sort.duration / 2
|
|
|
|
for p in map(int, [pos/2, pos, pos+pos/2]):
|
2013-03-26 14:09:14 +00:00
|
|
|
path = self.frame(p, height)
|
|
|
|
if path:
|
|
|
|
frames.append({
|
|
|
|
'position': p,
|
|
|
|
'path': path,
|
|
|
|
'width': width,
|
|
|
|
'height': height,
|
|
|
|
})
|
2011-07-26 17:22:23 +00:00
|
|
|
return frames
|
2010-09-03 13:28:44 +00:00
|
|
|
|
2011-08-16 15:06:40 +00:00
|
|
|
def select_frame(self):
|
|
|
|
frames = self.poster_frames()
|
|
|
|
if frames:
|
2012-11-18 19:26:13 +00:00
|
|
|
heat = [ox.image.getImageHeat(f['path']) for f in frames]
|
2011-08-16 15:06:40 +00:00
|
|
|
self.poster_frame = heat.index(max(heat))
|
2013-02-28 11:57:56 +00:00
|
|
|
if not settings.CONFIG['media']['importFrames']:
|
|
|
|
self.poster_frame = frames[self.poster_frame]['position']
|
2011-08-16 15:06:40 +00:00
|
|
|
|
2011-01-21 10:49:24 +00:00
|
|
|
def get_poster_frame_path(self):
|
2013-02-28 11:57:56 +00:00
|
|
|
path = None
|
2013-03-01 09:14:57 +00:00
|
|
|
frames = self.poster_frames()
|
|
|
|
if frames and self.poster_frame < 0:
|
|
|
|
self.select_frame()
|
2011-01-21 10:49:24 +00:00
|
|
|
if self.poster_frame >= 0:
|
2013-02-28 11:57:56 +00:00
|
|
|
if settings.CONFIG['media']['importFrames']:
|
|
|
|
if frames and len(frames) > int(self.poster_frame):
|
|
|
|
path = frames[int(self.poster_frame)]['path']
|
|
|
|
elif frames:
|
|
|
|
path = frames[int(len(frames)/2)]['path']
|
2013-02-28 20:47:02 +00:00
|
|
|
else:
|
2013-03-04 15:39:08 +00:00
|
|
|
size = max(settings.CONFIG['video']['resolutions'])
|
2013-02-28 20:47:02 +00:00
|
|
|
path = self.frame(self.poster_frame, size)
|
2013-03-01 09:14:57 +00:00
|
|
|
|
2013-02-28 11:57:56 +00:00
|
|
|
return path
|
2011-01-21 10:49:24 +00:00
|
|
|
|
|
|
|
def make_icon(self):
|
|
|
|
frame = self.get_poster_frame_path()
|
2011-08-10 14:00:03 +00:00
|
|
|
icon = self.path('icon.jpg')
|
|
|
|
self.icon.name = icon
|
2012-05-30 12:48:08 +00:00
|
|
|
timeline = '%stimelineantialias64p.jpg' % self.timeline_prefix
|
2013-05-07 16:40:37 +00:00
|
|
|
audio_timeline = '%stimelineaudio64p.jpg' % self.timeline_prefix
|
|
|
|
if not os.path.exists(timeline) and os.path.exists(audio_timeline):
|
|
|
|
timeline = audio_timeline
|
2011-08-10 14:00:03 +00:00
|
|
|
cmd = [settings.ITEM_ICON,
|
|
|
|
'-i', self.icon.path
|
|
|
|
]
|
|
|
|
if os.path.exists(timeline):
|
|
|
|
cmd += ['-l', timeline]
|
2011-01-21 10:49:24 +00:00
|
|
|
if frame:
|
2011-08-10 14:00:03 +00:00
|
|
|
cmd += ['-f', frame]
|
|
|
|
p = subprocess.Popen(cmd)
|
|
|
|
p.wait()
|
2011-12-28 12:35:10 +00:00
|
|
|
#remove cached versions
|
2012-09-25 17:00:10 +00:00
|
|
|
icon = os.path.abspath(os.path.join(settings.MEDIA_ROOT, icon))
|
|
|
|
for f in glob(icon.replace('.jpg', '*.jpg')):
|
|
|
|
if f != icon:
|
|
|
|
try:
|
|
|
|
os.unlink(f)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2011-08-10 14:00:03 +00:00
|
|
|
return icon
|
2011-01-04 07:32:32 +00:00
|
|
|
|
2011-08-20 10:06:18 +00:00
|
|
|
def load_subtitles(self):
|
2013-02-26 16:12:00 +00:00
|
|
|
subtitles = utils.get_by_key(settings.CONFIG['layers'], 'isSubtitles', True)
|
2013-05-09 16:05:23 +00:00
|
|
|
if not settings.USE_IMDB or not subtitles:
|
2012-09-08 12:41:19 +00:00
|
|
|
return False
|
2011-10-29 14:59:12 +00:00
|
|
|
with transaction.commit_on_success():
|
2013-02-26 16:12:00 +00:00
|
|
|
layer = subtitles['id']
|
2011-10-29 14:59:12 +00:00
|
|
|
Annotation.objects.filter(layer=layer,item=self).delete()
|
|
|
|
offset = 0
|
|
|
|
language = ''
|
|
|
|
subtitles = self.files.filter(selected=True, is_subtitle=True, available=True)
|
|
|
|
languages = [f.language for f in subtitles]
|
|
|
|
if languages:
|
|
|
|
if 'en' in languages:
|
|
|
|
language = 'en'
|
|
|
|
elif '' in languages:
|
|
|
|
language = ''
|
|
|
|
else:
|
2012-11-18 19:26:13 +00:00
|
|
|
language = languages[0]
|
2011-10-29 14:59:12 +00:00
|
|
|
|
|
|
|
#loop over all videos
|
|
|
|
for f in self.files.filter(Q(is_audio=True)|Q(is_video=True)) \
|
2012-09-11 13:17:35 +00:00
|
|
|
.filter(selected=True).order_by('sort_path'):
|
2011-10-29 14:59:12 +00:00
|
|
|
subtitles_added = False
|
|
|
|
prefix = os.path.splitext(f.path)[0]
|
|
|
|
if f.instances.all().count() > 0:
|
|
|
|
user = f.instances.all()[0].volume.user
|
|
|
|
else:
|
|
|
|
#FIXME: allow annotations from no user instead?
|
|
|
|
user = User.objects.all().order_by('id')[0]
|
|
|
|
#if there is a subtitle with the same prefix, import
|
|
|
|
q = subtitles.filter(path__startswith=prefix,
|
|
|
|
language=language)
|
|
|
|
if q.count() == 1:
|
|
|
|
s = q[0]
|
|
|
|
for data in s.srt(offset):
|
|
|
|
subtitles_added = True
|
2012-02-25 08:57:24 +00:00
|
|
|
value = data['value'].replace('\n', '<br>\n').replace('<br><br>\n', '<br>\n')
|
2011-10-29 14:59:12 +00:00
|
|
|
annotation = Annotation(
|
|
|
|
item=self,
|
|
|
|
layer=layer,
|
2012-06-16 23:10:34 +00:00
|
|
|
start=float('%0.03f'%data['in']),
|
|
|
|
end=float('%0.03f'%data['out']),
|
2012-02-25 08:57:24 +00:00
|
|
|
value=value,
|
2011-10-29 14:59:12 +00:00
|
|
|
user=user
|
|
|
|
)
|
|
|
|
annotation.save()
|
|
|
|
#otherwise add empty 5 seconds annotation every minute
|
|
|
|
if not subtitles_added:
|
|
|
|
start = offset and int (offset / 60) * 60 + 60 or 0
|
|
|
|
for i in range(start,
|
|
|
|
int(offset + f.duration) - 5,
|
|
|
|
60):
|
|
|
|
annotation = Annotation(
|
|
|
|
item=self,
|
|
|
|
layer=layer,
|
|
|
|
start=i,
|
|
|
|
end=i + 5,
|
|
|
|
value='',
|
|
|
|
user=user
|
|
|
|
)
|
|
|
|
annotation.save()
|
|
|
|
offset += f.duration
|
2011-11-02 14:27:02 +00:00
|
|
|
#remove left over clips without annotations
|
|
|
|
Clip.objects.filter(item=self, annotations__id=None).delete()
|
2012-09-08 12:41:19 +00:00
|
|
|
return True
|
2011-08-20 10:06:18 +00:00
|
|
|
|
2012-01-02 17:08:19 +00:00
|
|
|
def srt(self, layer):
|
2012-03-16 16:16:02 +00:00
|
|
|
def format_value(value):
|
|
|
|
value = value.replace('<br/>', '<br>').replace('<br>\n', '\n').replace('<br>', '\n')
|
|
|
|
value = value.replace('\n\n', '<br>\n')
|
|
|
|
return value
|
2012-01-02 17:08:19 +00:00
|
|
|
return ox.srt.encode([{
|
|
|
|
'in': a.start,
|
|
|
|
'out': a.end,
|
2012-03-16 16:16:02 +00:00
|
|
|
'value': format_value(a.value)
|
2012-01-03 20:17:44 +00:00
|
|
|
} for a in self.annotations.filter(layer=layer).order_by('start', 'end', 'sortvalue')])
|
2012-01-02 17:08:19 +00:00
|
|
|
|
2011-04-18 18:50:31 +00:00
|
|
|
def delete_item(sender, **kwargs):
|
|
|
|
i = kwargs['instance']
|
|
|
|
i.delete_files()
|
|
|
|
pre_delete.connect(delete_item, sender=Item)
|
2011-01-24 09:38:46 +00:00
|
|
|
|
2011-01-03 14:14:54 +00:00
|
|
|
Item.facet_keys = []
|
2011-09-06 12:06:59 +00:00
|
|
|
for key in settings.CONFIG['itemKeys']:
|
2012-01-20 17:53:21 +00:00
|
|
|
if 'autocomplete' in key and not 'autocompleteSortKey' in key or \
|
|
|
|
key.get('filter'):
|
2011-01-24 09:38:46 +00:00
|
|
|
Item.facet_keys.append(key['id'])
|
|
|
|
|
2011-01-03 14:14:54 +00:00
|
|
|
Item.person_keys = []
|
2011-09-06 12:06:59 +00:00
|
|
|
for key in settings.CONFIG['itemKeys']:
|
2012-03-08 11:39:04 +00:00
|
|
|
if key.get('sortType') == 'person':
|
2011-01-24 09:38:46 +00:00
|
|
|
Item.person_keys.append(key['id'])
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-09-23 16:01:48 +00:00
|
|
|
class ItemFind(models.Model):
|
2009-08-16 12:23:29 +00:00
|
|
|
"""
|
2010-11-06 16:14:00 +00:00
|
|
|
used to find items,
|
2010-11-27 12:12:53 +00:00
|
|
|
item.update_find populates this table
|
2010-11-06 16:14:00 +00:00
|
|
|
its used in manager.ItemManager
|
2009-08-16 12:23:29 +00:00
|
|
|
"""
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-06 16:14:00 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("item", "key")
|
|
|
|
|
|
|
|
item = models.ForeignKey('Item', related_name='find', db_index=True)
|
|
|
|
key = models.CharField(max_length=200, db_index=True)
|
2012-03-27 11:26:51 +00:00
|
|
|
value = models.TextField(blank=True, db_index=settings.DB_GIN_TRGM)
|
2009-06-08 16:08:59 +00:00
|
|
|
|
2011-01-15 14:22:29 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s=%s" % (self.key, self.value)
|
2011-01-05 13:06:09 +00:00
|
|
|
'''
|
|
|
|
ItemSort
|
2011-09-06 12:06:59 +00:00
|
|
|
table constructed based on info in settings.CONFIG['itemKeys']
|
2011-01-05 13:06:09 +00:00
|
|
|
'''
|
2011-01-03 14:14:54 +00:00
|
|
|
attrs = {
|
|
|
|
'__module__': 'item.models',
|
|
|
|
'item': models.OneToOneField('Item', related_name='sort', primary_key=True),
|
2011-09-05 16:33:47 +00:00
|
|
|
'duration': models.FloatField(null=True, blank=True, db_index=True),
|
2011-11-10 21:22:58 +00:00
|
|
|
'width': models.BigIntegerField(null=True, blank=True, db_index=True),
|
|
|
|
'height': models.BigIntegerField(null=True, blank=True, db_index=True),
|
2011-11-11 13:04:15 +00:00
|
|
|
'created': models.DateTimeField(null=True, blank=True, db_index=True),
|
2011-01-03 14:14:54 +00:00
|
|
|
}
|
2012-03-08 11:39:04 +00:00
|
|
|
for key in filter(lambda k: k.get('sort', False) or k['type'] in ('integer', 'time', 'float', 'date', 'enum'), settings.CONFIG['itemKeys']):
|
2011-01-03 14:14:54 +00:00
|
|
|
name = key['id']
|
2011-01-03 20:30:50 +00:00
|
|
|
name = {'id': 'itemId'}.get(name, name)
|
2012-03-08 11:39:04 +00:00
|
|
|
sort_type = key.get('sortType', key['type'])
|
2011-01-25 14:39:03 +00:00
|
|
|
if isinstance(sort_type, list):
|
|
|
|
sort_type = sort_type[0]
|
2011-01-24 09:38:46 +00:00
|
|
|
model = {
|
2011-01-24 13:44:38 +00:00
|
|
|
'char': (models.CharField, dict(null=True, max_length=1000, db_index=True)),
|
|
|
|
'year': (models.CharField, dict(null=True, max_length=4, db_index=True)),
|
|
|
|
'integer': (models.BigIntegerField, dict(null=True, blank=True, db_index=True)),
|
|
|
|
'float': (models.FloatField, dict(null=True, blank=True, db_index=True)),
|
|
|
|
'date': (models.DateTimeField, dict(null=True, blank=True, db_index=True))
|
2011-01-24 09:38:46 +00:00
|
|
|
}[{
|
|
|
|
'string': 'char',
|
|
|
|
'title': 'char',
|
|
|
|
'person': 'char',
|
|
|
|
'year': 'year',
|
|
|
|
'words': 'integer',
|
|
|
|
'length': 'integer',
|
|
|
|
'date': 'date',
|
2011-01-25 14:39:03 +00:00
|
|
|
'hue': 'float',
|
2011-11-10 16:34:45 +00:00
|
|
|
'time': 'integer',
|
2011-11-10 21:22:58 +00:00
|
|
|
'enum': 'integer',
|
2011-01-24 09:38:46 +00:00
|
|
|
}.get(sort_type, sort_type)]
|
2013-03-03 11:28:28 +00:00
|
|
|
if name not in attrs:
|
|
|
|
attrs[name] = model[0](**model[1])
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2011-01-03 14:14:54 +00:00
|
|
|
ItemSort = type('ItemSort', (models.Model,), attrs)
|
2011-01-24 22:46:16 +00:00
|
|
|
ItemSort.fields = [f.name for f in ItemSort._meta.fields]
|
2011-01-03 14:14:54 +00:00
|
|
|
|
2011-01-24 13:44:38 +00:00
|
|
|
class Access(models.Model):
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("item", "user")
|
|
|
|
|
|
|
|
access = models.DateTimeField(auto_now=True)
|
|
|
|
item = models.ForeignKey(Item, related_name='accessed')
|
|
|
|
user = models.ForeignKey(User, null=True, related_name='accessed_items')
|
2011-01-25 14:39:03 +00:00
|
|
|
accessed = models.IntegerField(default=0)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.accessed:
|
|
|
|
self.accessed = 0
|
|
|
|
self.accessed += 1
|
|
|
|
super(Access, self).save(*args, **kwargs)
|
2011-11-11 10:34:57 +00:00
|
|
|
timesaccessed = Access.objects.filter(item=self.item).aggregate(Sum('accessed'))['accessed__sum']
|
2011-11-11 17:45:46 +00:00
|
|
|
ItemSort.objects.filter(item=self.item).update(timesaccessed=timesaccessed, accessed=self.access)
|
2011-01-25 14:39:03 +00:00
|
|
|
|
2011-01-24 13:44:38 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
if self.user:
|
|
|
|
return u"%s/%s/%s" % (self.user, self.item, self.access)
|
|
|
|
return u"%s/%s" % (self.item, self.access)
|
|
|
|
|
2010-07-12 14:56:14 +00:00
|
|
|
class Facet(models.Model):
|
2011-01-05 13:06:09 +00:00
|
|
|
'''
|
|
|
|
used for keys that can have multiple values like people, languages etc.
|
|
|
|
does not perform to well if total number of items goes above 10k
|
|
|
|
this happens for keywords in 0xdb right now
|
|
|
|
'''
|
2012-11-18 19:26:13 +00:00
|
|
|
|
2011-01-03 14:14:54 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("item", "key", "value")
|
|
|
|
|
2010-09-23 16:01:48 +00:00
|
|
|
item = models.ForeignKey('Item', related_name='facets')
|
2010-07-12 14:56:14 +00:00
|
|
|
key = models.CharField(max_length=200, db_index=True)
|
2011-08-25 15:41:14 +00:00
|
|
|
value = models.CharField(max_length=1000, db_index=True)
|
2011-10-29 23:32:11 +00:00
|
|
|
sortvalue = models.CharField(max_length=1000, db_index=True)
|
2009-06-08 16:08:59 +00:00
|
|
|
|
2011-04-05 10:49:58 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s=%s" % (self.key, self.value)
|
|
|
|
|
2010-07-12 14:56:14 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2011-10-29 23:32:11 +00:00
|
|
|
if not self.sortvalue:
|
2012-02-14 13:44:54 +00:00
|
|
|
self.sortvalue = utils.sort_string(self.value).lower()[:900]
|
2010-07-12 14:56:14 +00:00
|
|
|
super(Facet, self).save(*args, **kwargs)
|
2009-08-16 12:23:29 +00:00
|
|
|
|
2012-01-15 15:05:37 +00:00
|
|
|
class Description(models.Model):
|
|
|
|
'''
|
|
|
|
shared itemkey descriptions
|
|
|
|
'''
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("key", "value")
|
|
|
|
key = models.CharField(max_length=200, db_index=True)
|
|
|
|
value = models.CharField(max_length=1000, db_index=True)
|
|
|
|
description = models.TextField()
|