From 22f83288c5497b85806e8ae31c46341bf694c912 Mon Sep 17 00:00:00 2001 From: j Date: Wed, 15 Jun 2016 18:29:09 +0200 Subject: [PATCH] avoid looking up item twice --- pandora/item/models.py | 7 ++++++- pandora/item/tasks.py | 4 +--- pandora/person/tasks.py | 8 ++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pandora/item/models.py b/pandora/item/models.py index 09f7f6a9..f0570b93 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -434,7 +434,7 @@ class Item(models.Model): a.public_id = public_id a.save() if sync: - tasks.update_file_paths(self.public_id) + self.update_file_paths() if update_poster: tasks.update_poster(self.public_id) else: @@ -1099,6 +1099,11 @@ class Item(models.Model): else: self.update_facet(key) + def update_file_paths(self): + for f in item.files.all(): + if f.normalize_path() != f.path: + f.save() + def path(self, name=''): h = self.public_id h = (7-len(h))*'0' + h diff --git a/pandora/item/tasks.py b/pandora/item/tasks.py index 614a17c1..d0076fe2 100644 --- a/pandora/item/tasks.py +++ b/pandora/item/tasks.py @@ -67,9 +67,7 @@ def update_poster(public_id): @task(ignore_results=True, queue='default') def update_file_paths(public_id): item = models.Item.objects.get(public_id=public_id) - for f in item.files.all(): - if f.normalize_path() != f.path: - f.save() + item.update_file_paths() @task(ignore_results=True, queue='default') def update_external(public_id): diff --git a/pandora/person/tasks.py b/pandora/person/tasks.py index 8d65ca64..81a44e54 100644 --- a/pandora/person/tasks.py +++ b/pandora/person/tasks.py @@ -15,8 +15,8 @@ def update_itemsort(id): @task(ignore_results=True, queue='default') def update_file_paths(id): - from item.models import Item - from item.tasks import update_file_paths + from item.models import Item, ItemFind p = models.Person.objects.get(pk=id) - for i in Item.objects.filter(find__value__icontains=p.name).distinct(): - update_file_paths(i.public_id) + items = ItemFind.objects.filter(key='name', value__icontains=p.name).values('item_id') + for i in Item.objects.filter(id__in=items): + i.update_file_paths()