summaryrefslogtreecommitdiff
path: root/talerbank/app/models.py
blob: 08da11104ca6a550177e4da6488389064a495027 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
##
# 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 Affero 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
#  @author Florian Dold

import uuid
from typing import Any, Tuple
from django.contrib.auth.models import User
from django.db import models
from django.conf import settings
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from taler.util.amount import Amount, SignedAmount, CurrencyMismatchError


def get_zero_amount() -> Amount:
    """
    Helper function that instantiates a zero-valued Amount
    object in the currency that the bank runs on.
    """
    return Amount(settings.TALER_CURRENCY, 0, 0)


def get_zero_signed_amount() -> SignedAmount:
    """
    Helper function that instantiates a zero-valued SignedAmount
    object in the currency that the bank runs on.
    """
    return SignedAmount(True, get_zero_amount())


class SignedAmountField(models.Field):
    """Custom implementation of the SignedAmount class as a database type."""

    description = "Signed amount object in Taler style"

    def db_type(self, connection: Any) -> str:
        """
        Return the database type of the serialized amount.
        """
        return "varchar"

    def get_prep_value(self, value: SignedAmount) -> str:
        """
        Stringifies the Amount object to feed the DB connector.
        """
        c = value.amount.currency
        if settings.TALER_CURRENCY != c:
            raise CurrencyMismatchError(settings.TALER_CURRENCY, c)
        return value.stringify()

    @staticmethod
    def from_db_value(value: str, *args) -> Amount:
        """
        Parse the stringified Amount back to Python.

        Parameters
        ----------
        value : str
            Serialized amount coming from the database.
            (String in the usual CURRENCY:X.Y format)
        args : any
            Unused
        """
        del args  # pacify PEP checkers
        return SignedAmount.parse(value)

    def to_python(self, value: Any) -> Amount:
        """
        Parse the stringified Amount back to Python. FIXME:
        why this serializer consider _more_ cases respect to the
        one above ('from_db_value')?

        Parameters
        ----------
        value: serialized amount coming from the database

        """

        if isinstance(value, SignedAmount):
            return value
        try:
            return SignedAmount.parse(value)
        except BadFormatAmount:
            raise ValidationError(
                "Invalid input for a signed amount string: %s" % value
            )


class AmountField(models.Field):
    """Custom implementation of the Amount class as a database type."""

    description = "Amount object in Taler style"

    def db_type(self, connection: Any) -> str:
        """
        Return the database type of the serialized amount.
        """
        return "varchar"

    def get_prep_value(self, value: Amount) -> str:
        """
        Stringifies the Amount object to feed the DB connector.
        """
        if settings.TALER_CURRENCY != value.currency:
            raise CurrencyMismatchError(settings.TALER_CURRENCY, value.currency)
        return value.stringify()

    @staticmethod
    def from_db_value(value: str, *args) -> Amount:
        """
        Parse the stringified Amount back to Python.

        Parameters
        ----------
        value : str
            Serialized amount coming from the database.
            (String in the usual CURRENCY:X.Y format)
        args : any
            Unused
        """
        del args  # pacify PEP checkers
        return Amount.parse(value)

    def to_python(self, value: Any) -> Amount:
        """
        Parse the stringified Amount back to Python. FIXME:
        why this serializer consider _more_ cases respect to the
        one above ('from_db_value')?

        Parameters
        ----------
        value: serialized amount coming from the database

        """

        if isinstance(value, Amount):
            return value
        try:
            return Amount.parse(value)
        except BadFormatAmount:
            raise ValidationError("Invalid input for an amount string: %s" % value)


def join_dict(**inputDict):
    return ", ".join(["%s==%s" % (key, value) for (key, value) in inputDict.items()])


class BankAccount(models.Model):
    """
    The class representing a bank account.
    """

    is_public = models.BooleanField(default=False)
    account_no = models.AutoField(primary_key=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    balance = SignedAmountField(default=get_zero_signed_amount)


class BankTransaction(models.Model):
    """
    The class representing a bank transaction.
    """

    amount = AmountField(default=False)
    debit_account = models.ForeignKey(
        BankAccount,
        on_delete=models.CASCADE,
        db_index=True,
        related_name="debit_account",
    )
    credit_account = models.ForeignKey(
        BankAccount,
        on_delete=models.CASCADE,
        db_index=True,
        related_name="credit_account",
    )
    subject = models.CharField(default="(no subject given)", max_length=200)
    date = models.DateTimeField(auto_now=True, db_index=True)
    cancelled = models.BooleanField(default=False)


class TalerWithdrawOperation(models.Model):
    withdraw_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    amount = AmountField(default=False)
    withdraw_account = models.ForeignKey(
        BankAccount,
        on_delete=models.CASCADE,
        db_index=True,
        related_name="withdraw_account",
    )
    selection_done = models.BooleanField(default=False)
    withdraw_done = models.BooleanField(default=False)
    selected_exchange_account = models.ForeignKey(
        BankAccount,
        null=True,
        on_delete=models.CASCADE,
        related_name="selected_exchange_account",
    )
    selected_reserve_pub = models.TextField(null=True)