summaryrefslogtreecommitdiff
path: root/talerbank/app/management/commands/wire_transfer.py
blob: bf6bef10f0c43b3b89b0cd976a28eb2a2683f51e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
##
# This file is part of TALER
# (C) 2014, 2015, 2016 Taler Systems SA
#
# TALER is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your
# option) any later version.
#
# TALER 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with TALER; see the file COPYING.  If not, see
# <http://www.gnu.org/licenses/>
#
# @author Marcello Stanisci
# @brief CLI utility that issues a wire transfer.

import sys
import logging
import json
from django.core.management.base import BaseCommand
from django.contrib.auth import authenticate
from taler.util.amount import Amount, BadFormatAmount
from ...views import wire_transfer
from ...models import BankAccount, BankTransaction

LOGGER = logging.getLogger(__name__)


##
# Django-specific definition to register the CLI utility.
class Command(BaseCommand):
    help = "Wire transfer money and return the transaction id."

    ##
    # Register the command line options of this command.
    #
    # @param self this object.
    # @param parser API used to actually register the option.
    def add_arguments(self, parser):
        parser.add_argument(
            "user",
            type=str,
            metavar="USERNAME",
            help="Which user is performing the wire transfer"
        )
        parser.add_argument(
            "password",
            type=str,
            metavar="PASSWORD",
            help="Performing user's password."
        )
        parser.add_argument(
            "credit-account",
            type=int,
            metavar="CREDIT-ACCOUNT",
            help="Which account number will *receive* money."
        )
        parser.add_argument(
            "subject",
            type=str,
            metavar="SUBJECT",
            help="SUBJECT will be the wire transfer subject."
        )
        parser.add_argument(
            "amount", type=str, metavar="AMOUNT",
            help="Wire transfer's amount, given in the " \
            "CURRENCY:X.Y form.")

    ##
    # This callable gets invoked when the user invokes the
    # CLI utility; it is responsible of making the wire transfer
    # effective.
    #
    # @param self this object.
    # @param args arguments list -- currently unused.
    # @param options options given by the user at the command line.
    def handle(self, *args, **options):
        user = authenticate(
            username=options["user"], password=options["password"]
        )
        if not user:
            LOGGER.error("Wrong user/password.")
            sys.exit(1)
        try:
            amount = Amount.parse(options["amount"])
        except BadFormatAmount:
            LOGGER.error("Amount's format is wrong: respect C:X.Y.")
            sys.exit(1)

        try:
            credit_account = BankAccount.objects.get(
                account_no=options["credit-account"]
            )
        except BankAccount.DoesNotExist:
            LOGGER.error("Credit account does not exist.")
            sys.exit(1)

        try:
            transaction = wire_transfer(
                amount, user.bankaccount, credit_account, options["subject"]
            )
            print("Transaction id: " + str(transaction.id))
        except Exception as exc:
            LOGGER.error(exc)
            sys.exit(1)