Projet

Général

Profil

0004-lingo-add-command-to-poll-backends-49149.patch

Benjamin Dauvergne, 30 avril 2021 02:33

Télécharger (3,57 ko)

Voir les différences:

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
combo/apps/lingo/management/commands/poll-backend.py
1
# lingo - basket and payment system
2
# Copyright (C) 2021  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
import datetime
18

  
19
from django.core.management.base import BaseCommand, CommandError
20

  
21
from combo.apps.lingo.models import PaymentBackend
22

  
23

  
24
class Command(BaseCommand):
25
    def add_arguments(self, parser):
26
        parser.add_argument('--backend', default=None, help='slug of the backend')
27

  
28
        parser.add_argument('--all-backends', default=False, action='store_true', help='target all backends')
29

  
30
        parser.add_argument(
31
            '--max-age-in-days', default=365 * 2, type=int, help='max age of the transaction in days'
32
        )
33

  
34
    def handle(self, *args, backend=None, all_backends=False, max_age_in_days=None, **kwargs):
35
        qs = PaymentBackend.objects.all()
36
        if backend and all_backends:
37
            raise CommandError('--backend and --all-baskends cannot be used together')
38
        elif backend:
39
            try:
40
                backend = qs.get(slug=backend)
41
            except PaymentBackend.DoesNotExist:
42
                raise CommandError('no backend with slug "%s".' % backend)
43
            else:
44
                if not backend.can_poll_backend():
45
                    raise CommandError('backend "%s" cannot be polled.' % backend)
46
                backends = [backend]
47
        else:
48
            backends = [backend for backend in qs if backend.can_poll_backend()]
49
            if not backends:
50
                raise CommandError('no backend found.')
51
            if not all_backends:
52
                print('Choose backend by slug:')
53
                while True:
54
                    for backend in backends:
55
                        print(' - %s: %s' % (backend.slug, backend))
56
                    print('> ', end=' ')
57
                    slug = input().strip()
58
                    if not slug:
59
                        continue
60
                    filtered_backends = qs.filter(slug__icontains=slug)
61
                    if filtered_backends:
62
                        backends = filtered_backends
63
                        break
64

  
65
        for backend in backends:
66
            print('Polling backend', backend, '... ', end='')
67
            backend.poll_backend(max_age=max_age_in_days and datetime.timedelta(days=max_age_in_days))
68
            print('DONE')
0
-