Dump script
This commit is contained in:
parent
28cb35e0b0
commit
16ec62fc4f
2 changed files with 90 additions and 0 deletions
31
example.py
Normal file
31
example.py
Normal file
|
@ -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}")
|
59
kfet/management/commands/dumpstuff.py
Normal file
59
kfet/management/commands/dumpstuff.py
Normal file
|
@ -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)
|
Loading…
Add table
Reference in a new issue