def update_mapping(self, mapping, data): """ Update an existing mapping """ # Generate the diff diffs = {} if 'retired' in data and mapping.retired != data['retired']: diffs['retired'] = {'was': mapping.retired, 'is': data['retired']} original = mapping.clone(self.user) serializer = MappingUpdateSerializer( mapping, data=data, context={'request': MockRequest(self.user)}) if not serializer.is_valid(): raise IllegalInputException( 'Could not parse mapping to update mapping %s due to %s.' % (mapping.id, serializer.errors)) mapping = serializer.object if 'retired' in diffs: mapping.retired = data['retired'] diffs.update(Mapping.diff(original, mapping)) # Update concept if different if diffs: if not self.test_mode: serializer.save() if not serializer.is_valid(): raise IllegalInputException( 'Could not persist update to mapping %s due to %s' % (mapping.id, serializer.errors)) return ImportActionHelper.IMPORT_ACTION_UPDATE # No diff, so do nothing return ImportActionHelper.IMPORT_ACTION_NONE