Add randomly generated account ids in kfet dump

This commit is contained in:
Martin Pépin 2020-02-06 19:46:08 +01:00
parent 16ec62fc4f
commit 8baaa1d339
No known key found for this signature in database
GPG key ID: E7520278B1774448

View file

@ -1,59 +1,67 @@
import json import json
import random
from typing import Dict
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from kfet.models import Article, Operation, OperationGroup from kfet.models import Account, Article, Operation
def dump_articles(filename: str): def gen_anonymisation_table() -> Dict[int, str]:
articles = Article.objects.values_list( random.seed()
"id", "name", "price", "category__name", "box_type", "box_capacity" 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 = [ articles = [
{ {
"id": id, "id": article.id,
"name": name, "name": article.name,
"price": str(price), "price": str(article.price),
"category": cat, "category": article.category.name,
"box_type": box_type, "box_type": article.box_type,
"box_capacity": box_capacity, "box_capacity": article.box_capacity,
} }
for id, name, price, cat, box_type, box_capacity in articles for article in Article.objects.all()
] ]
with open(filename, "w") as file: with open(filename, "w") as file:
json.dump(articles, file, indent=4) json.dump(articles, file, indent=4)
def dump_operations(filename: str): def dump_operations(filename: str, accounts_hashes: Dict[int, str]) -> None:
operations = ( not_canceled_purchases = Operation.objects.filter(type=Operation.PURCHASE).filter(
Operation.objects.filter(type=Operation.PURCHASE) canceled_at__isnull=True
.filter(canceled_at__isnull=True)
.values_list(
"amount", "article__id", "article_nb", "group__at", "group__is_cof"
)
) )
operations = [ operations = [
{ {
"amount": str(amount), "amount": str(operation.amount),
"article": id, "article": operation.article.id,
"number": nb, "number": operation.article_nb,
"date": str(at), "date": str(operation.group.at),
"is_cof": is_cof, "is_cof": operation.group.is_cof,
"on_account": accounts_hashes[operation.group.on_acc.id],
} }
for amount, id, nb, at, is_cof in operations for operation in not_canceled_purchases
] ]
with open(filename, "w") as file: with open(filename, "w") as file:
json.dump(operations, file, indent=4) json.dump(operations, file, indent=4)
class Command(BaseCommand): class Command(BaseCommand):
help = "Dump un historique anonymisé." help = 'Dump un historique "anonymisé".'
def handle(self, *args, **options): def handle(self, *args, **options):
# XXX. This is not great for privacy.
accounts_hashes = gen_anonymisation_table()
article_file = "article.dump.json" article_file = "article.dump.json"
self.stdout.write('Dumping articles to "{}"'.format(article_file)) self.stdout.write('Dumping articles to "{}"'.format(article_file))
dump_articles(article_file) dump_articles(article_file)
operation_file = "operation.dump.json" operation_file = "operation.dump.json"
self.stdout.write('Dumping operations to "{}"'.format(operation_file)) self.stdout.write('Dumping operations to "{}"'.format(operation_file))
dump_operations(operation_file) dump_operations(operation_file, accounts_hashes)