From 16ec62fc4f3f46d7de183780993939c209fc37ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sun, 1 Dec 2019 19:15:13 +0100 Subject: [PATCH] Dump script --- example.py | 31 ++++++++++++++ kfet/management/commands/dumpstuff.py | 59 +++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 example.py create mode 100644 kfet/management/commands/dumpstuff.py diff --git a/example.py b/example.py new file mode 100644 index 00000000..5e34f261 --- /dev/null +++ b/example.py @@ -0,0 +1,31 @@ +"""Exemple d'utilisation des dumps.""" + +import json +from collections import Counter + + +def parse(filename): + with open(filename, "r") as file: + return json.load(file) + + +if __name__ == "__main__": + Articles = {a["id"]: a for a in parse("article.dump.json")} + Operations = parse("operation.dump.json") + + # Dépenses faites en K-Fêt depuis K-Psul + total_expenses = -sum((float(op["amount"]) for op in Operations)) + print(f"total expenses: {total_expenses:.2f}€") + + # Montant moyen d'une transaction + nb_ops = len(Operations) + print(f"Average transaction amount: {total_expenses / nb_ops:.2f}€") + + # Nombre de ventes par article + articles_sales = Counter() + for operation in Operations: + articles_sales[operation["article"]] += operation["number"] + print("Most sold articles:") + for id, nb in articles_sales.most_common(3): + article_name = Articles[id]["name"] + print(f" - {article_name} : {nb}") diff --git a/kfet/management/commands/dumpstuff.py b/kfet/management/commands/dumpstuff.py new file mode 100644 index 00000000..fe8a3eea --- /dev/null +++ b/kfet/management/commands/dumpstuff.py @@ -0,0 +1,59 @@ +import json + +from django.core.management.base import BaseCommand + +from kfet.models import Article, Operation, OperationGroup + + +def dump_articles(filename: str): + articles = Article.objects.values_list( + "id", "name", "price", "category__name", "box_type", "box_capacity" + ) + articles = [ + { + "id": id, + "name": name, + "price": str(price), + "category": cat, + "box_type": box_type, + "box_capacity": box_capacity, + } + for id, name, price, cat, box_type, box_capacity in articles + ] + with open(filename, "w") as file: + json.dump(articles, file, indent=4) + + +def dump_operations(filename: str): + operations = ( + Operation.objects.filter(type=Operation.PURCHASE) + .filter(canceled_at__isnull=True) + .values_list( + "amount", "article__id", "article_nb", "group__at", "group__is_cof" + ) + ) + operations = [ + { + "amount": str(amount), + "article": id, + "number": nb, + "date": str(at), + "is_cof": is_cof, + } + for amount, id, nb, at, is_cof in operations + ] + 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): + 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)