2020-07-25 19:57:46 +00:00
|
|
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-07-25 15:23:54 +00:00
|
|
|
|
|
|
|
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 handle(self, *args, **kwargs):
|
2020-07-25 15:42:32 +00:00
|
|
|
queryset = RecurrentTransaction.objects.filter(
|
|
|
|
template__display=True,
|
|
|
|
valid=True,
|
|
|
|
created_at__gte=timezone.now() - timedelta(days=30),
|
|
|
|
).values("template").annotate(transaction_count=Count("template")).order_by("-transaction_count")[:10]
|
2020-07-25 15:23:54 +00:00
|
|
|
for d in queryset.all():
|
|
|
|
button_id = d["template"]
|
|
|
|
button = TransactionTemplate.objects.get(pk=button_id)
|
2021-04-14 13:18:24 +00:00
|
|
|
if kwargs['verbosity'] > 0:
|
|
|
|
self.stdout.write(self.style.WARNING("Highlight button {name} ({count:d} transactions)..."
|
|
|
|
.format(name=button.name, count=d["transaction_count"])))
|
2020-07-25 15:23:54 +00:00
|
|
|
button.highlighted = True
|
|
|
|
button.save()
|