diff --git a/anonymiser/models.py b/anonymiser/models.py index 9d6e444..67a49a9 100644 --- a/anonymiser/models.py +++ b/anonymiser/models.py @@ -85,12 +85,18 @@ def anonymise_field( new_value = getattr(obj, field_name) return old_value, new_value - def anonymise_object(self, obj: models.Model) -> None: - """Anonymise the model instance (NOT THREAD SAFE).""" + def anonymise_object(self, obj: models.Model) -> list[str]: + """ + Anonymise the model instance (NOT THREAD SAFE). + + Returns the list of fields that were anonymised. + + """ output = {} for field in self.get_anonymisable_fields(): output[field.name] = self.anonymise_field(obj, field) self.post_anonymise_object(obj, **output) + return list(output.keys()) def post_anonymise_object( self, obj: models.Model, **updates: AnonymisationResult diff --git a/anonymiser/registry.py b/anonymiser/registry.py index 5f4fb6a..ab227f6 100644 --- a/anonymiser/registry.py +++ b/anonymiser/registry.py @@ -13,12 +13,16 @@ logger = logging.getLogger(__name__) +def sort_by_name(models: list[type[models.Model]]) -> list[type[models.Model]]: + return sorted(models, key=lambda m: m._meta.label) + + class Registry(dict): def anonymisable_models(self) -> list[type[models.Model]]: - return [m for m in self.keys() if self[m]] + return sort_by_name([m for m in self.keys() if self[m]]) def non_anonymisable_models(self) -> list[type[models.Model]]: - return [m for m in self.keys() if self[m] is None] + return sort_by_name([m for m in self.keys() if self[m] is None]) def is_model_anonymisable(self, model: type[models.Model]) -> bool: return bool(self[model])