refactor file update code
This commit is contained in:
parent
ee1667c537
commit
50e2e2a8c9
2 changed files with 51 additions and 51 deletions
|
@ -10,6 +10,51 @@ from django.conf import settings
|
||||||
|
|
||||||
import models
|
import models
|
||||||
|
|
||||||
|
_INSTANCE_KEYS = ('mtime', 'name', 'folder')
|
||||||
|
|
||||||
|
|
||||||
|
def get_or_create_item(volume, f):
|
||||||
|
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'])
|
||||||
|
i = get_item(item_info)
|
||||||
|
return i
|
||||||
|
|
||||||
|
def get_or_create_file(volume, f):
|
||||||
|
try:
|
||||||
|
file = models.File.objects.get(oshash=f['oshash'])
|
||||||
|
except models.File.DoesNotExist:
|
||||||
|
file = models.File()
|
||||||
|
file.oshash = f['oshash']
|
||||||
|
file.name = f['name']
|
||||||
|
file.item = get_or_create_item(volume, f)
|
||||||
|
file.save()
|
||||||
|
return file
|
||||||
|
|
||||||
|
def update_or_create_instance(i, volume, f):
|
||||||
|
instance = models.Instance.objects.filter(file__oshash=f['oshash'], volume=f['volume'])
|
||||||
|
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()
|
||||||
|
else:
|
||||||
|
instance = models.Instance()
|
||||||
|
instance.volume = volume
|
||||||
|
instance.file = get_or_create_file(volume, f)
|
||||||
|
for key in _INSTANCE_KEYS:
|
||||||
|
setattr(instance, key, f[key])
|
||||||
|
instance.save()
|
||||||
|
return instance
|
||||||
|
|
||||||
@task(ignore_resulsts=True, queue='default')
|
@task(ignore_resulsts=True, queue='default')
|
||||||
def update_files(user, volume, files):
|
def update_files(user, volume, files):
|
||||||
|
@ -17,61 +62,14 @@ def update_files(user, volume, files):
|
||||||
volume, created = models.Volume.objects.get_or_create(user=user, name=volume)
|
volume, created = models.Volume.objects.get_or_create(user=user, name=volume)
|
||||||
all_files = []
|
all_files = []
|
||||||
for f in files:
|
for f in files:
|
||||||
path = f['path']
|
folder = f['path'].split('/')
|
||||||
folder = path.split('/')
|
|
||||||
name = folder.pop()
|
name = folder.pop()
|
||||||
if folder and folder[-1] in ('Extras', 'Versions', 'DVDs'):
|
if folder and folder[-1] in ('Extras', 'Versions', 'DVDs'):
|
||||||
name = '/'.join([folder.pop(), name])
|
name = '/'.join([folder.pop(), name])
|
||||||
folder = '/'.join(folder)
|
f['folder'] = '/'.join(folder)
|
||||||
#print folder
|
|
||||||
#print name
|
|
||||||
f['folder'] = folder
|
|
||||||
f['name'] = name
|
f['name'] = name
|
||||||
oshash = f['oshash']
|
all_files.append(f['oshash'])
|
||||||
all_files.append(oshash)
|
update_or_create_instance(volume, f)
|
||||||
|
|
||||||
same_folder = models.Instance.objects.filter(folder=folder, volume=volume)
|
|
||||||
if same_folder.count() > 0:
|
|
||||||
i = same_folder[0].file.item
|
|
||||||
else:
|
|
||||||
i = None
|
|
||||||
|
|
||||||
path = os.path.join(folder, name)
|
|
||||||
|
|
||||||
instance = models.Instance.objects.filter(file__oshash=oshash, volume=volume)
|
|
||||||
if instance.count()>0:
|
|
||||||
instance = instance[0]
|
|
||||||
updated = False
|
|
||||||
for key in ('mtime', 'name', 'folder'):
|
|
||||||
if f[key] != getattr(instance, key):
|
|
||||||
setattr(instance, key, f[key])
|
|
||||||
updated=True
|
|
||||||
if updated:
|
|
||||||
instance.save()
|
|
||||||
else:
|
|
||||||
#look if oshash is known
|
|
||||||
file_objects = models.File.objects.filter(oshash=oshash)
|
|
||||||
if file_objects.count() > 0:
|
|
||||||
file_object = file_objects[0]
|
|
||||||
#new oshash, add to database
|
|
||||||
else:
|
|
||||||
if not i:
|
|
||||||
if settings.USE_IMDB:
|
|
||||||
item_info = parse_path(folder)
|
|
||||||
else:
|
|
||||||
item_info = parse_path(path)
|
|
||||||
i = get_item(item_info)
|
|
||||||
file_object = models.File()
|
|
||||||
file_object.oshash = oshash
|
|
||||||
file_object.name = name
|
|
||||||
file_object.item = i
|
|
||||||
file_object.save()
|
|
||||||
instance = models.Instance()
|
|
||||||
instance.volume = volume
|
|
||||||
instance.file = file_object
|
|
||||||
for key in ('mtime', 'name', 'folder'):
|
|
||||||
setattr(instance, key, f[key])
|
|
||||||
instance.save()
|
|
||||||
|
|
||||||
#remove deleted files
|
#remove deleted files
|
||||||
#FIXME: can this have any bad consequences? i.e. on the selction of used item files.
|
#FIXME: can this have any bad consequences? i.e. on the selction of used item files.
|
||||||
|
|
|
@ -193,6 +193,8 @@ DEFAULT_LISTS = [
|
||||||
]
|
]
|
||||||
#0xdb.org
|
#0xdb.org
|
||||||
USE_IMDB = True
|
USE_IMDB = True
|
||||||
|
#this should idealy go away, one folder per item
|
||||||
|
USE_FOLDER = True
|
||||||
|
|
||||||
#POSTER_SERVICES=['http://data.0xdb.org/poster/']
|
#POSTER_SERVICES=['http://data.0xdb.org/poster/']
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue