pandora/pandora/archive/tasks.py

106 lines
3.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import os
2011-01-01 11:44:42 +00:00
from celery.decorators import task
2011-01-01 11:44:42 +00:00
from item.utils import parse_path
from item.models import get_item
2011-01-28 08:48:38 +00:00
from django.conf import settings
import models
2011-02-02 10:36:04 +00:00
_INSTANCE_KEYS = ('mtime', 'name', 'folder')
2011-02-22 16:09:13 +00:00
def get_or_create_item(volume, f, user):
2011-02-02 10:36:04 +00:00
in_same_folder = models.Instance.objects.filter(folder=f['folder'], volume=volume)
if in_same_folder.count() > 0:
i = in_same_folder[0].file.item
else:
if settings.USE_FOLDER:
item_info = parse_path(f['folder'])
else:
item_info = parse_path(f['path'])
2011-02-22 16:09:13 +00:00
i = get_item(item_info, user)
2011-02-02 10:36:04 +00:00
return i
2011-04-18 19:12:41 +00:00
def get_or_create_file(volume, f, user, item=None):
2011-02-02 10:36:04 +00:00
try:
file = models.File.objects.get(oshash=f['oshash'])
except models.File.DoesNotExist:
file = models.File()
file.oshash = f['oshash']
file.name = f['name']
2011-04-18 19:12:41 +00:00
if item:
file.item = item
else:
file.item = get_or_create_item(volume, f, user)
2011-02-02 10:36:04 +00:00
file.save()
return file
2011-02-22 15:42:56 +00:00
def update_or_create_instance(volume, f):
2011-04-18 19:12:41 +00:00
#instance with oshash exists
2011-02-22 15:45:12 +00:00
instance = models.Instance.objects.filter(file__oshash=f['oshash'], volume=volume)
2011-02-02 10:36:04 +00:00
if instance.count()>0:
instance = instance[0]
updated = False
for key in _INSTANCE_KEYS:
if f[key] != getattr(instance, key):
setattr(instance, key, f[key])
updated=True
if updated:
instance.save()
instance.file.save()
2011-02-02 10:36:04 +00:00
else:
2011-04-18 19:12:41 +00:00
instance = models.Instance.objects.filter(name=f['name'], folder=f['folder'], volume=volume)
if instance.count()>0:
#same path, other oshash, keep path/item mapping, remove instance
2011-06-27 19:55:40 +00:00
item = instance[0].file.item
2011-04-18 19:12:41 +00:00
instance.delete()
else: #new instance
item = None
2011-02-02 10:36:04 +00:00
instance = models.Instance()
instance.volume = volume
2011-04-18 19:12:41 +00:00
instance.file = get_or_create_file(volume, f, volume.user, item)
2011-02-02 10:36:04 +00:00
for key in _INSTANCE_KEYS:
setattr(instance, key, f[key])
2011-08-23 17:39:34 +00:00
if instance.name.lower().startswith('extras/') or \
instance.name.lower().startswith('versions/'):
instance.extra = True
2011-02-02 10:36:04 +00:00
instance.save()
instance.file.save()
2011-08-25 14:45:55 +00:00
instance.file.item.update_wanted()
2011-02-02 10:36:04 +00:00
return instance
2011-01-01 11:44:42 +00:00
@task(ignore_resulsts=True, queue='default')
def update_files(user, volume, files):
user = models.User.objects.get(username=user)
volume, created = models.Volume.objects.get_or_create(user=user, name=volume)
all_files = []
for f in files:
2011-02-02 10:36:04 +00:00
folder = f['path'].split('/')
name = folder.pop()
2011-02-24 16:28:44 +00:00
if folder and folder[-1].lower() in ('extras', 'versions', 'dvds'):
name = '/'.join([folder.pop(), name])
2011-02-02 10:36:04 +00:00
f['folder'] = '/'.join(folder)
f['name'] = name
2011-02-02 10:36:04 +00:00
all_files.append(f['oshash'])
update_or_create_instance(volume, f)
2011-04-18 19:12:41 +00:00
#remove deleted files
#FIXME: can this have any bad consequences? i.e. on the selction of used item files.
models.Instance.objects.filter(volume=volume).exclude(file__oshash__in=all_files).delete()
2011-06-01 11:03:07 +00:00
2011-08-19 16:54:42 +00:00
@task(queue="encoding")
def process_stream(fileId):
2011-08-20 08:45:54 +00:00
file = models.File.objects.get(id=fileId)
2011-08-19 16:54:42 +00:00
streams = file.streams.filter(source=None)
2011-08-20 08:45:54 +00:00
if streams.count() > 0:
2011-08-19 16:54:42 +00:00
stream = streams[0]
stream.make_timeline()
stream.extract_derivatives()
2011-08-20 08:45:54 +00:00
file.item.update_timeline()
2011-08-19 16:54:42 +00:00
return True