From e4133ffa1d0137cf68d3860a62480ac1bb8ff9a7 Mon Sep 17 00:00:00 2001 From: j Date: Mon, 3 Jan 2022 12:26:16 +0100 Subject: [PATCH 1/4] missing import --- pandora/document/fulltext.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandora/document/fulltext.py b/pandora/document/fulltext.py index d58e3751..40ff52be 100644 --- a/pandora/document/fulltext.py +++ b/pandora/document/fulltext.py @@ -1,3 +1,4 @@ +import os import subprocess import tempfile From 79c5d948b57510dbc70a15d8e9c30cc3d10b9ea4 Mon Sep 17 00:00:00 2001 From: j Date: Mon, 3 Jan 2022 12:33:15 +0100 Subject: [PATCH 2/4] close filedescriptor after use --- pandora/document/fulltext.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandora/document/fulltext.py b/pandora/document/fulltext.py index 40ff52be..1872b9ce 100644 --- a/pandora/document/fulltext.py +++ b/pandora/document/fulltext.py @@ -19,10 +19,11 @@ def extract_text(pdf, page=None): # split page from pdf and ocr fd, page_pdf = tempfile.mkstemp('.pdf') cmd = ['pdfseparate', '-f', page, '-l', page, pdf, page_pdf] - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) stdout, stderr = p.communicate() text = ocr_image(page_pdf) os.unlink(page_pdf) + os.close(fd) return text else: return ocr_image(pdf) From 5cfe392e22936f1183a47e0edcf190e1e6ab4132 Mon Sep 17 00:00:00 2001 From: j Date: Mon, 3 Jan 2022 12:37:02 +0100 Subject: [PATCH 3/4] close more fds --- pandora/item/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandora/item/models.py b/pandora/item/models.py index 6279948d..31d0631c 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -522,6 +522,7 @@ class Item(models.Model): cmd, stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'), close_fds=True) p.wait() os.unlink(tmp_output_txt) + os.close(fd) return True else: return None From af657503636edf045a687ffd8c3d1933bfc742e3 Mon Sep 17 00:00:00 2001 From: j Date: Wed, 12 Jan 2022 11:03:25 +0100 Subject: [PATCH 4/4] avoid transaction.atomic insided of transaction.atomic --- pandora/archive/tasks.py | 4 +++- pandora/item/models.py | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pandora/archive/tasks.py b/pandora/archive/tasks.py index a2aa3d8e..b9f379d2 100644 --- a/pandora/archive/tasks.py +++ b/pandora/archive/tasks.py @@ -4,6 +4,7 @@ from glob import glob from celery.task import task from django.conf import settings +from django.db import transaction from django.db.models import Q from item.models import Item @@ -248,7 +249,8 @@ def move_media(data, user): if old_item and old_item.files.count() == 0 and i.files.count() == len(data['ids']): for a in old_item.annotations.all().order_by('id'): a.item = i - a.set_public_id() + with transaction.atomic(): + a.set_public_id() Annotation.objects.filter(id=a.id).update(item=i, public_id=a.public_id) old_item.clips.all().update(item=i, sort=i.sort) diff --git a/pandora/item/models.py b/pandora/item/models.py index 31d0631c..e0aebbd4 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -478,7 +478,8 @@ class Item(models.Model): for a in self.annotations.all().order_by('id'): a.item = other - a.set_public_id() + with transaction.atomic(): + a.set_public_id() Annotation.objects.filter(id=a.id).update(item=other, public_id=a.public_id) try: other_sort = other.sort @@ -1910,13 +1911,12 @@ class AnnotationSequence(models.Model): @classmethod def nextid(cls, item): - with transaction.atomic(): - s, created = cls.objects.get_or_create(item=item) - if created: - nextid = s.value - else: - cursor = connection.cursor() - sql = "UPDATE %s SET value = value + 1 WHERE item_id = %s RETURNING value" % (cls._meta.db_table, item.id) - cursor.execute(sql) - nextid = cursor.fetchone()[0] + s, created = cls.objects.get_or_create(item=item) + if created: + nextid = s.value + else: + cursor = connection.cursor() + sql = "UPDATE %s SET value = value + 1 WHERE item_id = %s RETURNING value" % (cls._meta.db_table, item.id) + cursor.execute(sql) + nextid = cursor.fetchone()[0] return "%s/%s" % (item.public_id, ox.toAZ(nextid))