university-final-iot-backend/app/mod_accounts/models.py

62 lines
1.5 KiB
Python
Raw Normal View History

2018-05-03 13:28:57 +00:00
from app import db
2018-05-03 07:48:24 +00:00
2018-05-03 13:28:57 +00:00
class Account(db.Model):
2018-05-03 07:48:24 +00:00
__tablename__ = 'accounts'
2018-05-03 14:58:44 +00:00
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, index=True, unique=True)
password = db.Column(db.String)
email = db.Column(db.String, index=True, unique=True)
role_id = db.Column(db.Integer, db.ForeignKey("roles.id"))
2018-05-03 14:40:30 +00:00
role = db.relationship("Role", foreign_keys=[role_id])
2018-05-03 07:48:24 +00:00
2018-05-03 14:58:44 +00:00
def __init__(self, username, password, email, role):
2018-05-03 07:48:24 +00:00
self.username = str(username)
self.password = str(password)
2018-05-03 14:58:44 +00:00
self.email = str(email)
2018-05-03 07:48:24 +00:00
if isinstance(role, Role):
self.role_id = role.id
else:
self.role_id = int(role)
2018-05-03 14:58:44 +00:00
def save(self):
db.session.add(self)
db.session.commit()
@staticmethod
def get_all():
return Account.query.all()
@staticmethod
def get(accId):
return Account.query.filter_by(id = accId)
2018-05-03 07:48:24 +00:00
def __repr__(self):
return '<Account (name=%s, role=%s)>' % self.username, self.role
2018-05-03 13:28:57 +00:00
class Role(db.Model):
2018-05-03 07:48:24 +00:00
__tablename__ = 'roles'
2018-05-03 14:58:44 +00:00
id = db.Column(db.Integer, primary_key=True)
display_name = db.Column(db.String)
2018-05-03 07:48:24 +00:00
def __init__(self, name):
self.display_name = str(name)
2018-05-03 14:58:44 +00:00
def save(self):
db.session.add(self)
db.session.commit()
@staticmethod
def get_all():
return Role.query.all()
@staticmethod
def get(roleId):
return Role.query.filter_by(id = accId)
2018-05-03 07:48:24 +00:00
def __repr__(self):
return '<Role %s>' % self.name