-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
37 lines (25 loc) · 1.33 KB
/
models.py
File metadata and controls
37 lines (25 loc) · 1.33 KB
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
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import bcrypt
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(150), unique=True, nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
role = db.Column(db.String(50),nullable=False,default="client")
created_at = db.Column(db.DateTime, default=datetime.utcnow)
def set_password(self, password):
self.password_hash = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
def check_password(self, password):
return bcrypt.checkpw(password.encode('utf-8'), self.password_hash.encode('utf-8'))
class Account(db.Model):
__tablename__ = 'accounts'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150), nullable=False)
email = db.Column(db.String(150), unique=True, nullable=False)
contact_number = db.Column(db.String(15), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
added_by = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
# Define a relationship to access the User who added the account
added_by_user = db.relationship('User', backref=db.backref('accounts', lazy=True))