-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
156 lines (125 loc) · 3.94 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
from flask import Flask, render_template_string
from flask_sqlalchemy import SQLAlchemy
from flask_authorize import RestrictionsMixin, AllowancesMixin
from flask_authorize import PermissionsMixin
from sqlalchemy import (
Column,
String,
Integer,
create_engine,
ForeignKey,
Boolean,
DateTime,
LargeBinary
)
import json
from dotenv import load_dotenv
from encryption import (
decrypt,
encrypt,
BLOCK_SIZE,
pad,
unpad,
SALT
)
DB_USER = os.environ.get("DB_USER")
DB_PASSWORD = os.environ.get("DB_PASSWORD")
DB_HOST = os.environ.get("DB_HOST")
database_name = 'microservice'
database_path = 'postgresql://{}:{}@{}/{}'.format(DB_USER, DB_PASSWORD, DB_HOST, database_name)
db = SQLAlchemy()
def setup_db(app, database_path=database_path):
app.config['SQLALCHEMY_DATABASE_URI'] = database_path
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
with db.app.app_context():
db.init_app(app)
db.create_all()
UserRole = db.Table(
'user_role', db.Model.metadata,
db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
db.Column('role_id', db.Integer, db.ForeignKey('roles.id'))
)
class User(db.Model):
__tablename__ = 'users'
id = Column(Integer, primary_key=True, autoincrement=True)
email = Column(LargeBinary, nullable=False, unique=True)
password = Column(LargeBinary, nullable=False, server_default='')
active = Column(Boolean, default=False)
roles = db.relationship('Role', secondary=UserRole)
def __init__(self, email, password, role):
self.email = email
self.password = password
self.roles.append(Role.query.get(role))
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'email': self.email,
'role': self.roles
}
def is_active(self):
"""True, as all users are active."""
return True
def get_id(self):
"""Return the email address to satisfy Flask-Login's requirements."""
return str(self.id)
def is_authenticated(self):
"""Return True if the user is authenticated."""
return self.authenticated
def is_anonymous(self):
"""False, as anonymous users aren't supported."""
return False
class Role(db.Model):
__tablename__ = 'roles'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False, unique=True)
# class Role(db.Model):
# __tablename__ = 'roles'
# id = Column(Integer(), primary_key=True)
# name = Column(String(50), unique=True)
# def __init__(self, name):
# self.name = name
# def insert(self):
# db.session.add(self)
# db.session.commit()
# def update(self):
# db.session.commit()
# def delete(self):
# db.session.delete(self)
# db.session.commit()
# def format(self):
# return {
# 'id': self.id,
# 'name': self.name
# }
# class UserRoles(db.Model):
# __tablename__ = 'user_roles'
# id = Column(Integer(), primary_key=True)
# user_id = Column(Integer(), ForeignKey('users.id', ondelete='CASCADE'))
# role_id = Column(Integer(), ForeignKey('roles.id', ondelete='CASCADE'))
# def __init__(self, user_id, role_id):
# self.user_id = user_id
# self.role_id = role_id
# def insert(self):
# db.session.add(self)
# db.session.commit()
# def update(self):
# db.session.commit()
# def delete(self):
# db.session.delete(self)
# db.session.commit()
# def format(self):
# return {
# 'id': self.id,
# 'user_id': self.user_id,
# 'role_id': self.role_id
# }