forked from mirrors/bookwyrm
Change transaction atomic to avoid cascading errors
This commit is contained in:
parent
27ec80515e
commit
1204679cbd
1 changed files with 32 additions and 27 deletions
|
@ -63,7 +63,6 @@ class ActivityObject:
|
||||||
setattr(self, field.name, value)
|
setattr(self, field.name, value)
|
||||||
|
|
||||||
|
|
||||||
@transaction.atomic
|
|
||||||
def to_model(self, model, instance=None, save=True):
|
def to_model(self, model, instance=None, save=True):
|
||||||
''' convert from an activity to a model instance '''
|
''' convert from an activity to a model instance '''
|
||||||
if not isinstance(self, model.activity_serializer):
|
if not isinstance(self, model.activity_serializer):
|
||||||
|
@ -91,6 +90,7 @@ class ActivityObject:
|
||||||
if not save:
|
if not save:
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
with transaction.atomic():
|
||||||
# we can't set many to many and reverse fields on an unsaved object
|
# we can't set many to many and reverse fields on an unsaved object
|
||||||
instance.save()
|
instance.save()
|
||||||
|
|
||||||
|
@ -106,20 +106,23 @@ class ActivityObject:
|
||||||
values = getattr(self, activity_field_name)
|
values = getattr(self, activity_field_name)
|
||||||
if values is None or values is MISSING:
|
if values is None or values is MISSING:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
model_field = getattr(model, model_field_name)
|
||||||
try:
|
try:
|
||||||
# this is for one to many
|
# this is for one to many
|
||||||
related_model = getattr(model, model_field_name).field.model
|
related_model = model_field.field.model
|
||||||
|
related_field_name = model_field.field.name
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# it's a one to one or foreign key
|
# it's a one to one or foreign key
|
||||||
related_model = getattr(model, model_field_name)\
|
related_model = model_field.related.related_model
|
||||||
.related.related_model
|
related_field_name = model_field.related.related_name
|
||||||
values = [values]
|
values = [values]
|
||||||
|
|
||||||
for item in values:
|
for item in values:
|
||||||
set_related_field.delay(
|
set_related_field.delay(
|
||||||
related_model.__name__,
|
related_model.__name__,
|
||||||
instance.__class__.__name__,
|
instance.__class__.__name__,
|
||||||
instance.__class__.__name__.lower(),
|
related_field_name,
|
||||||
instance.remote_id,
|
instance.remote_id,
|
||||||
item
|
item
|
||||||
)
|
)
|
||||||
|
@ -145,6 +148,7 @@ def set_related_field(
|
||||||
require_ready=True
|
require_ready=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
with transaction.atomic():
|
||||||
if isinstance(data, str):
|
if isinstance(data, str):
|
||||||
item = resolve_remote_id(model, data, save=False)
|
item = resolve_remote_id(model, data, save=False)
|
||||||
else:
|
else:
|
||||||
|
@ -157,7 +161,8 @@ def set_related_field(
|
||||||
# this must exist because it's the object that triggered this function
|
# this must exist because it's the object that triggered this function
|
||||||
instance = origin_model.find_existing_by_remote_id(related_remote_id)
|
instance = origin_model.find_existing_by_remote_id(related_remote_id)
|
||||||
if not instance:
|
if not instance:
|
||||||
raise ValueError('Invalid related remote id: %s' % related_remote_id)
|
raise ValueError(
|
||||||
|
'Invalid related remote id: %s' % related_remote_id)
|
||||||
|
|
||||||
# edition.parent_work = instance, for example
|
# edition.parent_work = instance, for example
|
||||||
setattr(item, related_field_name, instance)
|
setattr(item, related_field_name, instance)
|
||||||
|
|
Loading…
Reference in a new issue