From d947c4447f4db1e621baee328cb11405f60d0940 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Mon, 7 Dec 2020 15:42:28 +0100 Subject: [PATCH 4/5] lingo: add command to poll backends (#49149) --- .../lingo/management/commands/poll-backend.py | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 combo/apps/lingo/management/commands/poll-backend.py diff --git a/combo/apps/lingo/management/commands/poll-backend.py b/combo/apps/lingo/management/commands/poll-backend.py new file mode 100644 index 00000000..95f8dda4 --- /dev/null +++ b/combo/apps/lingo/management/commands/poll-backend.py @@ -0,0 +1,68 @@ +# lingo - basket and payment system +# Copyright (C) 2021 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import datetime + +from django.core.management.base import BaseCommand, CommandError + +from combo.apps.lingo.models import PaymentBackend + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument('--backend', default=None, help='slug of the backend') + + parser.add_argument('--all-backends', default=False, action='store_true', help='target all backends') + + parser.add_argument( + '--max-age-in-days', default=365 * 2, type=int, help='max age of the transaction in days' + ) + + def handle(self, *args, backend=None, all_backends=False, max_age_in_days=None, **kwargs): + qs = PaymentBackend.objects.all() + if backend and all_backends: + raise CommandError('--backend and --all-baskends cannot be used together') + elif backend: + try: + backend = qs.get(slug=backend) + except PaymentBackend.DoesNotExist: + raise CommandError('no backend with slug "%s".' % backend) + else: + if not backend.can_poll_backend(): + raise CommandError('backend "%s" cannot be polled.' % backend) + backends = [backend] + else: + backends = [backend for backend in qs if backend.can_poll_backend()] + if not backends: + raise CommandError('no backend found.') + if not all_backends: + print('Choose backend by slug:') + while True: + for backend in backends: + print(' - %s: %s' % (backend.slug, backend)) + print('> ', end=' ') + slug = input().strip() + if not slug: + continue + filtered_backends = qs.filter(slug__icontains=slug) + if filtered_backends: + backends = filtered_backends + break + + for backend in backends: + print('Polling backend', backend, '... ', end='') + backend.poll_backend(max_age=max_age_in_days and datetime.timedelta(days=max_age_in_days)) + print('DONE') -- 2.31.1