diff --git a/apis_core/history/models.py b/apis_core/history/models.py index 31e408eeb..f7d3d1f1e 100644 --- a/apis_core/history/models.py +++ b/apis_core/history/models.py @@ -100,6 +100,24 @@ def get_absolute_url(self): ct = ContentType.objects.get_for_model(self) return reverse("apis_core:generic:detail", args=[ct, self.history_id]) + def get_diff(self, other_version=None): + if self.history_type == "-": + return None + version = other_version or self.prev_record + if version: + delta = self.diff_against(version) + else: + delta = self.diff_against(self.__class__()) + changes = list( + filter( + lambda x: (x.new != "" or x.old is not None) + and x.field != "id" + and not x.field.endswith("_ptr"), + delta.changes, + ) + ) + return sorted(changes, key=lambda change: change.field) + class VersionMixin(models.Model): history = APISHistoricalRecords( diff --git a/apis_core/history/serializers.py b/apis_core/history/serializers.py index 77e91a9af..625e58119 100644 --- a/apis_core/history/serializers.py +++ b/apis_core/history/serializers.py @@ -59,19 +59,9 @@ class HistoryLogSerializer(serializers.Serializer): def get_diff(self, obj): if obj.history_type == "-": return None - if obj.prev_record is None: - diff = obj.diff_against(obj.__class__()) - else: - diff = obj.diff_against(obj.prev_record) changed_fields = [] changes = [] - for change in diff.changes: - if ( - (change.new == "" and change.old is None) - or change.field == "id" - or change.field.endswith("_ptr") - ): - continue + for change in obj.get_diff(): changed_fields.append(change.field) changes.append(ModelChangeSerializer(change, obj).data) return {"changed_fields": changed_fields, "changes": changes}