Track changed fields in activity to model code
This commit is contained in:
@ -106,6 +106,7 @@ class ActivityObject:
|
||||
value = field.default
|
||||
setattr(self, field.name, value)
|
||||
|
||||
# pylint: disable=too-many-locals,too-many-branches
|
||||
def to_model(self, model=None, instance=None, allow_create=True, save=True):
|
||||
"""convert from an activity to a model instance"""
|
||||
model = model or get_model_from_type(self.type)
|
||||
@ -126,27 +127,36 @@ class ActivityObject:
|
||||
return None
|
||||
instance = instance or model()
|
||||
|
||||
# keep track of what we've changed
|
||||
update_fields = []
|
||||
for field in instance.simple_fields:
|
||||
try:
|
||||
field.set_field_from_activity(instance, self)
|
||||
changed = field.set_field_from_activity(instance, self)
|
||||
if changed:
|
||||
update_fields.append(field.name)
|
||||
except AttributeError as e:
|
||||
raise ActivitySerializerError(e)
|
||||
|
||||
# image fields have to be set after other fields because they can save
|
||||
# too early and jank up users
|
||||
for field in instance.image_fields:
|
||||
field.set_field_from_activity(instance, self, save=save)
|
||||
changed = field.set_field_from_activity(instance, self, save=save)
|
||||
if changed:
|
||||
update_fields.append(field.name)
|
||||
|
||||
if not save:
|
||||
return instance
|
||||
|
||||
with transaction.atomic():
|
||||
# can't force an update on fields unless the object already exists in the db
|
||||
if not instance.id:
|
||||
update_fields = None
|
||||
# we can't set many to many and reverse fields on an unsaved object
|
||||
try:
|
||||
try:
|
||||
instance.save(broadcast=False)
|
||||
instance.save(broadcast=False, update_fields=update_fields)
|
||||
except TypeError:
|
||||
instance.save()
|
||||
instance.save(update_fields=update_fields)
|
||||
except IntegrityError as e:
|
||||
raise ActivitySerializerError(e)
|
||||
|
||||
|
Reference in New Issue
Block a user