Skip to content

Commit

Permalink
feat: turn dump anonymization into optional
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna committed Mar 15, 2024
1 parent f8a24b0 commit d5d28f9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
31 changes: 21 additions & 10 deletions bd_api/apps/core/management/commands/dumpfixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime
from json import dump, load

from django.core.management.base import CommandParser
from django.core.management.commands.dumpdata import Command as DumpDataCommand
from faker import Faker
from loguru import logger
Expand Down Expand Up @@ -46,19 +47,29 @@ def empty():
class Command(DumpDataCommand):
"""Dump data, avoiding profiles"""

def add_arguments(self, parser: CommandParser):
super().add_arguments(parser)
parser.add_argument(
"--anonymize",
dest="anonymize",
action="store_true",
help="Anonymize account fixtures with fake data",
)

def handle(self, *args, **options) -> str | None:
logger.info("Dump fixtures")
response = super().handle(*args, **options)

logger.info("Filter fixtures")
output = options["output"]
with open(output, "r") as file:
data = load(file)
for datum in data:
if datum["model"] == "account.account":
if datum["fields"]["profile"] in (2, 3):
datum["fields"] = {**datum["fields"], **empty()}
with open(output, "w") as file:
dump(data, file)
if options["anonymize"]:
logger.info("Filter fixtures")
output = options["output"]
with open(output, "r") as file:
data = load(file)
for datum in data:
if datum["model"] == "account.account":
if datum["fields"]["profile"] in (2, 3):
datum["fields"] = {**datum["fields"], **empty()}
with open(output, "w") as file:
dump(data, file)

return response
3 changes: 2 additions & 1 deletion bd_api/apps/core/management/commands/loadfixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django.conf import settings
from django.core.management import call_command
from django.core.management.base import CommandParser
from django.db import connection
from loguru import logger
from modeltranslation.management.commands.loaddata import Command as LoadDataCommand
Expand Down Expand Up @@ -33,7 +34,7 @@ class Command(LoadDataCommand):
4. Build index
"""

def add_arguments(self, parser):
def add_arguments(self, parser: CommandParser):
super().add_arguments(parser)
parser.add_argument(
"--build-index",
Expand Down

0 comments on commit d5d28f9

Please sign in to comment.