From 580948fe1da1904ba6418daafb48a0a64824a11b Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sat, 25 Jul 2020 17:23:54 +0200 Subject: [PATCH] :heavy_plus_sign: Add refresh highlighted buttons script --- .../commands/refresh_highlighted_buttons.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 management/commands/refresh_highlighted_buttons.py diff --git a/management/commands/refresh_highlighted_buttons.py b/management/commands/refresh_highlighted_buttons.py new file mode 100644 index 0000000..10e13cd --- /dev/null +++ b/management/commands/refresh_highlighted_buttons.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +from datetime import timedelta + +from django.core.management.base import BaseCommand +from django.db.models import Count +from django.utils import timezone + +from note.models import RecurrentTransaction, TransactionTemplate + + +class Command(BaseCommand): + """ + Command to add the ten most used buttons of the past month to the highlighted buttons. + """ + + def add_arguments(self, parser): + return parser + + def handle(self, *args, **kwargs): + queryset = RecurrentTransaction.objects.filter(template__display=True, created_at__gte=timezone.now() - timedelta(days=30)).values("template").annotate(transaction_count=Count("template")).order_by("-transaction_count")[:10] + for d in queryset.all(): + button_id = d["template"] + button = TransactionTemplate.objects.get(pk=button_id) + self.stdout.write(self.style.WARNING("Highlight button {name} ({count:d} transactions)...".format(name=button.name, count=d["transaction_count"]))) + button.highlighted = True + button.save()