Skip to content

Commit

Permalink
Add count return value to anonymise_queryset
Browse files Browse the repository at this point in the history
  • Loading branch information
hugorodgerbrown committed Sep 10, 2023
1 parent 0f357a9 commit 7cf6869
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion anonymiser/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ def anonymise_object(self, obj: models.Model) -> None:
output[field.name] = self.anonymise_field(obj, field.name)
self.post_anonymise_object(obj, **output)

def anonymise_queryset(self, queryset: models.QuerySet) -> None:
def anonymise_queryset(self, queryset: models.QuerySet) -> int:
"""Anonymise all objects in the queryset (and SAVE)."""
count = 0
for obj in queryset:
self.anonymise_object(obj)
obj.save()
count += 1
return count

def post_anonymise_object(
self, obj: models.Model, **updates: AnonymisationResult
Expand Down
6 changes: 6 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,9 @@ def test_get_anonymisable_fields(self, user_anonymiser: UserAnonymiser) -> None:
assert user_anonymiser.get_anonymisable_fields() == [
User._meta.get_field("first_name")
]

def test_anonymise_queryset(
self, user: User, user_anonymiser: UserAnonymiser
) -> None:
assert user_anonymiser.anonymise_queryset(User.objects.none()) == 0
assert user_anonymiser.anonymise_queryset(User.objects.all()) == 1

0 comments on commit 7cf6869

Please sign in to comment.