diff --git a/apis_core/history/models.py b/apis_core/history/models.py index 46740ceda..153924a05 100644 --- a/apis_core/history/models.py +++ b/apis_core/history/models.py @@ -96,6 +96,24 @@ def set_version_tag(self, tag: str, include_triples: bool = True): triple.save() self.save() + 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..36664591b 100644 --- a/apis_core/history/serializers.py +++ b/apis_core/history/serializers.py @@ -57,24 +57,11 @@ class HistoryLogSerializer(serializers.Serializer): history_id = serializers.IntegerField() 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 = [] + obj_changes = obj.get_diff() changes = [] - for change in diff.changes: - if ( - (change.new == "" and change.old is None) - or change.field == "id" - or change.field.endswith("_ptr") - ): - continue - changed_fields.append(change.field) + for change in obj_changes: changes.append(ModelChangeSerializer(change, obj).data) - return {"changed_fields": changed_fields, "changes": changes} + return {"changed_fields": [change.field for change in obj_changes], "changes": changes} def get_action(self, obj): match obj.history_type: