diff --git a/anonymiser/management/commands/display_model_anonymisation.py b/anonymiser/management/commands/display_model_anonymisation.py index e53d8de..c48f000 100644 --- a/anonymiser/management/commands/display_model_anonymisation.py +++ b/anonymiser/management/commands/display_model_anonymisation.py @@ -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) diff --git a/anonymiser/registry.py b/anonymiser/registry.py index ab227f6..21e67a7 100644 --- a/anonymiser/registry.py +++ b/anonymiser/registry.py @@ -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]]: diff --git a/anonymiser/templates/display_model_anonymisation.md b/anonymiser/templates/anonymiser/display_model_anonymisation.md similarity index 75% rename from anonymiser/templates/display_model_anonymisation.md rename to anonymiser/templates/anonymiser/display_model_anonymisation.md index 3760ac2..37f2c9e 100644 --- a/anonymiser/templates/display_model_anonymisation.md +++ b/anonymiser/templates/anonymiser/display_model_anonymisation.md @@ -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 %}