generated from yunojuno/poetry-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update display_model_anonymisation output
- Loading branch information
1 parent
5eefe50
commit 13fe7d8
Showing
3 changed files
with
43 additions
and
8 deletions.
There are no files selected for viewing
41 changes: 38 additions & 3 deletions
41
anonymiser/management/commands/display_model_anonymisation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
.../templates/display_model_anonymisation.md → ...anonymiser/display_model_anonymisation.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters