avoid transaction.atomic insided of transaction.atomic

This commit is contained in:
j 2022-01-12 11:03:25 +01:00
parent 5cfe392e22
commit af65750363
2 changed files with 13 additions and 11 deletions

View file

@ -4,6 +4,7 @@ from glob import glob
from celery.task import task from celery.task import task
from django.conf import settings from django.conf import settings
from django.db import transaction
from django.db.models import Q from django.db.models import Q
from item.models import Item 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']): 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'): for a in old_item.annotations.all().order_by('id'):
a.item = i 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) Annotation.objects.filter(id=a.id).update(item=i, public_id=a.public_id)
old_item.clips.all().update(item=i, sort=i.sort) old_item.clips.all().update(item=i, sort=i.sort)

View file

@ -478,7 +478,8 @@ class Item(models.Model):
for a in self.annotations.all().order_by('id'): for a in self.annotations.all().order_by('id'):
a.item = other 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) Annotation.objects.filter(id=a.id).update(item=other, public_id=a.public_id)
try: try:
other_sort = other.sort other_sort = other.sort
@ -1910,13 +1911,12 @@ class AnnotationSequence(models.Model):
@classmethod @classmethod
def nextid(cls, item): def nextid(cls, item):
with transaction.atomic(): s, created = cls.objects.get_or_create(item=item)
s, created = cls.objects.get_or_create(item=item) if created:
if created: nextid = s.value
nextid = s.value else:
else: cursor = connection.cursor()
cursor = connection.cursor() sql = "UPDATE %s SET value = value + 1 WHERE item_id = %s RETURNING value" % (cls._meta.db_table, item.id)
sql = "UPDATE %s SET value = value + 1 WHERE item_id = %s RETURNING value" % (cls._meta.db_table, item.id) cursor.execute(sql)
cursor.execute(sql) nextid = cursor.fetchone()[0]
nextid = cursor.fetchone()[0]
return "%s/%s" % (item.public_id, ox.toAZ(nextid)) return "%s/%s" % (item.public_id, ox.toAZ(nextid))