Merge pull request #410 from mouse-reeve/atomicity

Change transaction atomic to avoid cascading errors
This commit is contained in:
Mouse Reeve 2020-12-20 11:24:37 -08:00 committed by GitHub
commit fdb9b71030
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -63,7 +63,6 @@ class ActivityObject:
setattr(self, field.name, value)
@transaction.atomic
def to_model(self, model, instance=None, save=True):
''' convert from an activity to a model instance '''
if not isinstance(self, model.activity_serializer):
@ -91,6 +90,7 @@ class ActivityObject:
if not save:
return instance
with transaction.atomic():
# we can't set many to many and reverse fields on an unsaved object
instance.save()
@ -106,20 +106,23 @@ class ActivityObject:
values = getattr(self, activity_field_name)
if values is None or values is MISSING:
continue
model_field = getattr(model, model_field_name)
try:
# 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:
# it's a one to one or foreign key
related_model = getattr(model, model_field_name)\
.related.related_model
related_model = model_field.related.related_model
related_field_name = model_field.related.related_name
values = [values]
for item in values:
set_related_field.delay(
related_model.__name__,
instance.__class__.__name__,
instance.__class__.__name__.lower(),
related_field_name,
instance.remote_id,
item
)
@ -145,6 +148,7 @@ def set_related_field(
require_ready=True
)
with transaction.atomic():
if isinstance(data, str):
item = resolve_remote_id(model, data, save=False)
else:
@ -157,7 +161,8 @@ def set_related_field(
# this must exist because it's the object that triggered this function
instance = origin_model.find_existing_by_remote_id(related_remote_id)
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
setattr(item, related_field_name, instance)