2019-12-01 19:15:13 +01:00
|
|
|
import json
|
2020-02-06 19:46:08 +01:00
|
|
|
import random
|
|
|
|
from typing import Dict
|
2019-12-01 19:15:13 +01:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
2020-02-06 19:46:08 +01:00
|
|
|
from kfet.models import Account, Article, Operation
|
2019-12-01 19:15:13 +01:00
|
|
|
|
|
|
|
|
2020-02-06 19:46:08 +01:00
|
|
|
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:
|
2019-12-01 19:15:13 +01:00
|
|
|
articles = [
|
|
|
|
{
|
2020-02-06 19:46:08 +01:00
|
|
|
"id": article.id,
|
|
|
|
"name": article.name,
|
|
|
|
"price": str(article.price),
|
|
|
|
"category": article.category.name,
|
|
|
|
"box_type": article.box_type,
|
|
|
|
"box_capacity": article.box_capacity,
|
2019-12-01 19:15:13 +01:00
|
|
|
}
|
2020-02-06 19:46:08 +01:00
|
|
|
for article in Article.objects.all()
|
2019-12-01 19:15:13 +01:00
|
|
|
]
|
|
|
|
with open(filename, "w") as file:
|
|
|
|
json.dump(articles, file, indent=4)
|
|
|
|
|
|
|
|
|
2020-02-06 19:46:08 +01:00
|
|
|
def dump_operations(filename: str, accounts_hashes: Dict[int, str]) -> None:
|
2020-02-06 20:03:40 +01:00
|
|
|
not_canceled_purchases = (
|
|
|
|
Operation.objects.filter(type=Operation.PURCHASE)
|
|
|
|
.filter(canceled_at__isnull=True)
|
|
|
|
.values_list(
|
|
|
|
"amount",
|
|
|
|
"article__id",
|
|
|
|
"article_nb",
|
|
|
|
"group__at",
|
|
|
|
"group__is_cof",
|
|
|
|
"group__on_acc__id",
|
|
|
|
)
|
2019-12-01 19:15:13 +01:00
|
|
|
)
|
|
|
|
operations = [
|
|
|
|
{
|
2020-02-06 20:03:40 +01:00
|
|
|
"amount": str(amount),
|
|
|
|
"article": article_id,
|
|
|
|
"number": nb,
|
|
|
|
"date": str(at),
|
|
|
|
"is_cof": is_cof,
|
|
|
|
"on_account": accounts_hashes[on_acc],
|
2019-12-01 19:15:13 +01:00
|
|
|
}
|
2020-02-06 20:03:40 +01:00
|
|
|
for amount, article_id, nb, at, is_cof, on_acc in not_canceled_purchases
|
2019-12-01 19:15:13 +01:00
|
|
|
]
|
|
|
|
with open(filename, "w") as file:
|
|
|
|
json.dump(operations, file, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2020-02-06 19:46:08 +01:00
|
|
|
help = 'Dump un historique "anonymisé".'
|
2019-12-01 19:15:13 +01:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2020-02-06 19:46:08 +01:00
|
|
|
# XXX. This is not great for privacy.
|
|
|
|
accounts_hashes = gen_anonymisation_table()
|
|
|
|
|
2019-12-01 19:15:13 +01:00
|
|
|
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))
|
2020-02-06 19:46:08 +01:00
|
|
|
dump_operations(operation_file, accounts_hashes)
|