summaryrefslogtreecommitdiffstats
path: root/bitvend/models.py
blob: bab5836278df7e2db3b0eaf05f5d981d1f3b3e29 (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
from flask import current_app as app
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.sql import func, select

db = SQLAlchemy()

class TransferException(Exception): pass
class NoFunds(TransferException): pass


class User(db.Model):
    __tablename__ = 'users'

    uid = db.Column(db.String(64), primary_key=True)
    transactions = db.relationship('Transaction', backref='user', lazy='dynamic')

    def __str__(self):
        return self.uid

    @hybrid_property
    def balance(self):
        return sum((_.amount or 0) for _ in self.transactions)

    @balance.expression
    def balance(self):
        return (select([func.sum(Transaction.amount)]).
                where(Transaction.uid == cls.uid).
                label("balance")
                )

    def transfer(self, target, amount):
        if amount > self.amount_available:
            raise NoFunds()

        self.transactions.append(Transaction(
            amount=-amount, type='transfer', related=target.uid
            ))
        target.transactions.append(Transaction(
            amount=amount, type='transfer', related=self.uid
            ))

    @property
    def debt_limit(self):
        return app.config.get('DEBT_LIMIT', 5000)

    @hybrid_property
    def amount_available(self):
        return self.balance + self.debt_limit

    is_authenticated = True
    is_active = True
    is_anonymous = False

    def get_id(self):
        return self.uid

    @property
    def transaction_in_progress(self):
        return self.transactions.filter(Transaction.finished == False).count()


class Transaction(db.Model):
    __tablename__ = 'transactions'

    id = db.Column(db.Integer, primary_key=True)
    tx_hash = db.Column(db.String)
    uid = db.Column(db.String(64), db.ForeignKey('users.uid'))

    amount = db.Column(db.Integer)

    type = db.Column(db.String(32), default='manual')

    related = db.Column(db.String)
    related_user = db.relationship('User', foreign_keys=[related], primaryjoin=related==User.uid)

    created = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)

    #value = db.Column(db.Integer)
    @hybrid_property
    def value(self):
        return self.amount

    product_id = db.Column(db.Integer)
    product_value = db.Column(db.Integer)

    @hybrid_property
    def finished(self):
        return (self.type != 'purchase') | (self.product_id != None)