forked from DGNum/gestioCOF
67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
import json
|
|
import random
|
|
from typing import Dict
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from kfet.models import Account, Article, Operation
|
|
|
|
|
|
def gen_anonymisation_table() -> Dict[int, str]:
|
|
random.seed()
|
|
hashes = {}
|
|
for account_id in Account.objects.values_list("id", flat=True):
|
|
h = random.getrandbits(128)
|
|
hashes[account_id] = "{:032x}".format(h)
|
|
return hashes
|
|
|
|
|
|
def dump_articles(filename: str) -> None:
|
|
articles = [
|
|
{
|
|
"id": article.id,
|
|
"name": article.name,
|
|
"price": str(article.price),
|
|
"category": article.category.name,
|
|
"box_type": article.box_type,
|
|
"box_capacity": article.box_capacity,
|
|
}
|
|
for article in Article.objects.all()
|
|
]
|
|
with open(filename, "w") as file:
|
|
json.dump(articles, file, indent=4)
|
|
|
|
|
|
def dump_operations(filename: str, accounts_hashes: Dict[int, str]) -> None:
|
|
not_canceled_purchases = Operation.objects.filter(type=Operation.PURCHASE).filter(
|
|
canceled_at__isnull=True
|
|
)
|
|
operations = [
|
|
{
|
|
"amount": str(operation.amount),
|
|
"article": operation.article.id,
|
|
"number": operation.article_nb,
|
|
"date": str(operation.group.at),
|
|
"is_cof": operation.group.is_cof,
|
|
"on_account": accounts_hashes[operation.group.on_acc.id],
|
|
}
|
|
for operation in not_canceled_purchases
|
|
]
|
|
with open(filename, "w") as file:
|
|
json.dump(operations, file, indent=4)
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Dump un historique "anonymisé".'
|
|
|
|
def handle(self, *args, **options):
|
|
# XXX. This is not great for privacy.
|
|
accounts_hashes = gen_anonymisation_table()
|
|
|
|
article_file = "article.dump.json"
|
|
self.stdout.write('Dumping articles to "{}"'.format(article_file))
|
|
dump_articles(article_file)
|
|
|
|
operation_file = "operation.dump.json"
|
|
self.stdout.write('Dumping operations to "{}"'.format(operation_file))
|
|
dump_operations(operation_file, accounts_hashes)
|