-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
47 lines (36 loc) · 1.73 KB
/
models.py
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
from sqlalchemy import create_engine, Column, Integer, String, Float, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from passlib.hash import bcrypt
Base = declarative_base()
engine = create_engine('sqlite:///budget_app.db')
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
username = Column(String, unique=True, nullable=False)
password_hash = Column(String, nullable=False)
main_account_balance = Column(Float, default=0.0)
def set_password(self, password):
self.password_hash = bcrypt.hash(password)
def check_password(self, password):
return bcrypt.verify(password, self.password_hash)
budget_accounts = relationship('BudgetAccount', back_populates='user')
class BudgetAccount(Base):
__tablename__ = 'budget_accounts'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
balance = Column(Float, default=0.0)
user_id = Column(Integer, ForeignKey('users.id'))
user = relationship('User', back_populates='budget_accounts')
transactions = relationship('Transaction', back_populates='account')
class Transaction(Base):
__tablename__ = 'transactions'
id = Column(Integer, primary_key=True)
amount = Column(Float, nullable=False)
transaction_type = Column(String, nullable=False) # 'deposit' or 'withdrawal'
user_id = Column(Integer, ForeignKey('users.id'))
user = relationship('User')
account_id = Column(Integer, ForeignKey('budget_accounts.id'))
account = relationship('BudgetAccount', back_populates='transactions')
Base.metadata.create_all(engine)