-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
61 lines (45 loc) · 2.01 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
db = SQLAlchemy()
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(24), index=True, unique=True, nullable=False)
email = db.Column(db.String(120), index=True, unique=True)
password = db.Column(db.String(100))
expenses = db.relationship('Expense', backref='creator', lazy='dynamic')
def get_id(self):
try:
return unicode(self.id) # python 2
except NameError:
return str(self.id) # python 3
def __repr__(self):
return '<User {}>'.format(self.username) # how this class is displayed
class Budget(db.Model):
id = db.Column(db.Integer, primary_key=True)
category = db.relationship('Category', backref='budget', uselist=False)
limit = db.Column(db.Integer)
def __repr__(self):
return '<Budget for {}>'.format(self.category)
class Expense(db.Model):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.Date)
description = db.Column(db.String(160))
cost = db.Column(db.Integer)
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
merchant_id = db.Column(db.Integer, db.ForeignKey('merchant.id'))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __repr__(self):
return '<Purchase {}>'.format(self.description)
class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
category_text = db.Column(db.String(120), unique=True)
expenses = db.relationship('Expense', backref='category', lazy='dynamic')
budget_id = db.Column(db.Integer, db.ForeignKey('budget.id'))
def __repr__(self):
return '{}'.format(self.category_text.title())
class Merchant(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120), unique=True)
expenses = db.relationship('Expense', backref='merchant', lazy='dynamic')
def __repr__(self):
return '{}'.format(self.name.title())