2010-12-23 15:01:53 +00:00
|
|
|
# -*- 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
|
2010-12-23 15:01:53 +00:00
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
from item.utils import parse_path
|
2010-12-23 15:01:53 +00:00
|
|
|
from item.models import get_item
|
2011-01-28 08:48:38 +00:00
|
|
|
from django.conf import settings
|
2010-12-23 15:01:53 +00:00
|
|
|
|
|
|
|
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()
|
2011-04-06 18:57:25 +00:00
|
|
|
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])
|
|
|
|
instance.save()
|
2011-04-06 18:57:25 +00:00
|
|
|
instance.file.save()
|
2011-02-02 10:36:04 +00:00
|
|
|
return instance
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-12-23 15:01:53 +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('/')
|
2010-12-23 15:01:53 +00:00
|
|
|
name = folder.pop()
|
2011-02-24 16:28:44 +00:00
|
|
|
if folder and folder[-1].lower() in ('extras', 'versions', 'dvds'):
|
2010-12-23 15:01:53 +00:00
|
|
|
name = '/'.join([folder.pop(), name])
|
2011-02-02 10:36:04 +00:00
|
|
|
f['folder'] = '/'.join(folder)
|
2010-12-23 15:01:53 +00:00
|
|
|
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
|
|
|
|
2010-12-23 15:01:53 +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
|
|
|
|