summaryrefslogtreecommitdiff
path: root/talerbank/app/models.py
blob: 97da1596c9e04bdcaca6f6428bb0f5028c921cb7 (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
#  This file is part of TALER
#  (C) 2014, 2015, 2016 INRIA
#
#  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

from __future__ import unicode_literals
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
from . import amount
from .types import TA

class AmountField(models.Field):

    description = 'Amount object in Taler style'

    def deconstruct(self) -> Tuple[str, str, list, dict]:
        name, path, args, kwargs = super(AmountField, self).deconstruct()
        return name, path, args, kwargs

    def db_type(self, connection: Any) -> str:
        return "varchar"

    # Pass stringified object to db connector
    def get_prep_value(self, value: TA) -> str:
        if not value:
            return "%s:0.0" % settings.TALER_CURRENCY
        return value.stringify(settings.TALER_DIGITS)

    @staticmethod
    def from_db_value(value: str, *args) -> TA:
        del args # pacify PEP checkers
        if value is None:
            return amount.Amount.parse(settings.TALER_CURRENCY)
        return amount.Amount.parse(value)

    def to_python(self, value: Any) -> TA:
        if isinstance(value, amount.Amount):
            return value
        try:
            if value is None:
                return amount.Amount.parse(settings.TALER_CURRENCY)
            return amount.Amount.parse(value)
        except amount.BadFormatAmount:
            raise ValidationError("Invalid input for an amount string: %s" % value)

def get_zero_amount() -> TA:
    return amount.Amount(settings.TALER_CURRENCY)

class BankAccount(models.Model):
    is_public = models.BooleanField(default=False)
    debit = models.BooleanField(default=False)
    account_no = models.AutoField(primary_key=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    amount = AmountField(default=get_zero_amount)

class BankTransaction(models.Model):
    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)