Skip to content

Commit

Permalink
Update display_model_anonymisation output
Browse files Browse the repository at this point in the history
  • Loading branch information
hugorodgerbrown committed Oct 5, 2023
1 parent 5eefe50 commit 13fe7d8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
41 changes: 38 additions & 3 deletions anonymiser/management/commands/display_model_anonymisation.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
from __future__ import annotations

from collections import namedtuple
from typing import Any

from django.apps import apps
from django.core.management.base import BaseCommand
from django.template.loader import render_to_string

from anonymiser import registry

ModelAnonymiserSummary = namedtuple(
"ModelAnonymiserSummary",
["model", "anonymiser"],
)


def get_model_anonymisers() -> list[ModelAnonymiserSummary]:
"""
Return model_name: anonymiser_name for all models.
Return the names, not the objects, as Django templates cannnot access
_meta attributes of models, and all we need is the name.
"""
output = []
for m in apps.get_models():
if m._meta.abstract:
continue
if anonymiser := registry.get_model_anonymiser(m):
output.append(
ModelAnonymiserSummary(
m._meta.model_name,
anonymiser.__class__.__name__,
)
)
else:
output.append(ModelAnonymiserSummary(m._meta.label, ""))
return output


class Command(BaseCommand):
def handle(self, *args: Any, **options: Any) -> None:
model_fields = registry.get_all_model_fields()
out = render_to_string(
"display_model_anonymisation.md",
{"model_fields": model_fields},
"anonymiser/display_model_anonymisation.md",
{
"model_anonymisers": get_model_anonymisers(),
"model_fields": registry.get_all_model_fields(),
},
)
self.stdout.write(out)
5 changes: 0 additions & 5 deletions anonymiser/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ def get_model_anonymiser(model: type[models.Model]) -> ModelAnonymiser | None:
return None


def get_anonymisable_models() -> list[type[models.Model]]:
"""Return list of all models that have an anonymiser."""
return _registry.anonymisable_models()


def get_all_model_fields(
anonymised_only: bool = False,
) -> dict[str, list[ModelFieldSummary]]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
**DEMO PURPOSES ONLY**

The following models have no registered anonymiser:
{% for m in models %}{% if not m.anonymiser %}
* {{ m.model }}{% endif %}{% endfor %}

## Model field anonymisation
App | Model | Field | Type | Anonymise | Redact
--- | --- | --- | --- | --- | ---{% for model,fields in model_fields.items %}{% for field in fields %}
Expand Down

0 comments on commit 13fe7d8

Please sign in to comment.