Skip to content

Commit

Permalink
Update anonymise_object to return fields updated
Browse files Browse the repository at this point in the history
  • Loading branch information
hugorodgerbrown committed Oct 4, 2023
1 parent 0dc01fe commit 5eefe50
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 8 additions & 2 deletions anonymiser/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions anonymiser/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 5eefe50

Please sign in to comment.