-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
65 lines (48 loc) · 1.74 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
62
63
64
65
from flask_login import UserMixin
from app import db
###
# The following file contains our database definition. This is where we indicate what tables we have
# and what field they each contain.
# Note: We would normally use a hash for the password with a salt but for the purpose
# of this demo, we intentionally removed all encryption.
###
class User(db.Model, UserMixin):
"""
Represents a database model of a user with columns 'id', 'username',
'password'.
"""
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String)
password = db.Column(db.String)
def __init__(self, username, password):
self.username = username
self.set_password(password)
def set_password(self, password):
"""
Set the password of the user with the value provided.
"""
self.password = password
def check_password(self, password):
"""
Check the given plaintext password with the password stored in the user record.
"""
return self.password == password
@classmethod
def get_by_username(cls, username):
"""
Returns the User associated with the given username.
"""
return cls.query.filter_by(username=username).first()
class Record(db.Model):
"""
Represents a database model of a record with columns 'id', 'description',
'date', 'amount' and as foreign key the 'user_id'.
"""
__tablename__ = 'record'
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.String)
date = db.Column(db.Date)
amount = db.Column(db.Float)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user = db.relationship('User', backref='user')