-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
357 lines (241 loc) · 11.4 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# REDB tables - class construction:
# Each class represents a table in the database. The classes allow for simpler table queries (INSERT, SELECT, UPDATE, DELETE, etc.) using db.session
## INSTRUCTIONS
# To add (new) table in the database, create class below then enter following in python terminal window (once in app's folder path):
# SET FLASK_APP = redb.py
# python
# >>> from redb import db
# >>> db.create_all()
# If an error occurs and/or corrections are made, restart python interpreter (i.e. 'quit()' then 'python')
# If wanting to reset / correct table, ALTER TABLE; and make changes here accordingly.
# If wanting to reset table completely, save information, DROP TABLE; then re-add table using method above (then re-add information to the table)
# Removing many to many relationships
# https://stackoverflow.com/questions/26948397/how-to-delete-records-from-many-to-many-secondary-table-in-sqlalchemy
## IMPORT
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
### TABLES
technology_component = db.Table('technology_component',
db.Column('technology', db.Integer, db.ForeignKey('technologies.id')),
db.Column('component', db.Integer, db.ForeignKey('components.id'))
)
supplier_type = db.Table('supplier_type',
db.Column('supplier', db.Integer, db.ForeignKey('suppliers.id')),
db.Column('type', db.Integer, db.ForeignKey('types.id'))
)
supplier_component = db.Table('supplier_component',
db.Column('supplier', db.Integer, db.ForeignKey('suppliers.id')),
db.Column('component', db.Integer, db.ForeignKey('components.id'))
)
plant_partaker = db.Table('plant_partaker',
db.Column('plant', db.Integer, db.ForeignKey('plants.id')),
db.Column('partaker', db.Integer, db.ForeignKey('partakers.id'))
)
project_plant = db.Table('project_plant',
db.Column('project', db.Integer, db.ForeignKey('projects.code')),
db.Column('plant', db.Integer, db.ForeignKey('plants.id'))
)
plant_technology = db.Table('plant_technology',
db.Column('plant', db.Integer, db.ForeignKey('plants.id')),
db.Column('technology', db.Integer, db.ForeignKey('technologies.id'))
)
class Users(db.Model):
# Table columns
staffnum = db.Column('staffnum', db.Integer, primary_key=True)
name = db.Column('name', db.String(30), nullable=False, unique=True)
passwordHash = db.Column('password', db.String(100), nullable=False, unique=True)
email = db.Column('email', db.String(50), nullable=False, unique=True)
project_pm = db.relationship('Projects', backref='pm', primaryjoin='Users.staffnum==Projects.pm_id')
project_dpm = db.relationship('Projects', backref='dpm', primaryjoin='Users.staffnum==Projects.dpm_id')
project_pp = db.relationship('Projects', backref='pp', primaryjoin='Users.staffnum==Projects.pp_id')
# Sets return data {User_# : staffnum}
def __repr__(self):
return '<User %r>' % self.staffnum
class Roles(db.Model):
# Table columns
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
short = db.Column(db.String(10), unique=True, nullable=False)
# Sets return data {Role_# : id}
def __repr__(self):
return '<Role %r>' % self.id
class Services(db.Model):
# Table columns
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
short = db.Column(db.String(10), unique=True, nullable=False)
# Sets return data {Service_# : id}
def __repr__(self):
return '<Service %r>' % self.id
class Parties(db.Model):
# Table columns
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
short = db.Column(db.String(10), unique=True, nullable=False)
comments = db.Column(db.String(1000))
# Sets return data {Party_# : id}
def __repr__(self):
return '<Party %r>' % self.id
class PartakersRoles(db.Model):
# Table columns
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
short = db.Column(db.String(10), unique=True, nullable=False)
# Sets return data {PartakersRole_# : id}
def __repr__(self):
return '<PartakersRole %r>' % self.id
class ClientsRoles(db.Model):
# Table columns
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
short = db.Column(db.String(10), unique=True, nullable=False)
# Sets return data {ClientRole_# : id}
def __repr__(self):
return '<ClientRole %r>' % self.id
class Partakers(db.Model):
# Table columns
id = db.Column(db.Integer, primary_key=True)
version = db.Column(db.Integer, nullable=False)
# Relationships
party_id = db.Column(db.Integer, db.ForeignKey('parties.id'))
partaker_role_id = db.Column(db.Integer, db.ForeignKey('partakers_roles.id'))
# Sets return data {Partaker_# : id}
def __repr__(self):
return '<Partaker %r>' % self.id
class Clients(db.Model):
# Table columns
id = db.Column(db.Integer, primary_key=True)
confidential = db.Column(db.Integer, nullable=False)
# Relationships
party_id = db.Column(db.Integer, db.ForeignKey('parties.id'))
client_role_id = db.Column(db.Integer, db.ForeignKey('clients_roles.id'))
# Sets return data {Client_# : id}
def __repr__(self):
return '<Client %r>' % self.id
class Countries(db.Model):
# Table columns
id = db.Column('id', db.String(3), primary_key=True)
name = db.Column('name', db.String(50), nullable=False, unique=True)
sub_region = db.Column('sub_region', db.String(30), nullable=False)
region = db.Column('region', db.String(10), nullable=False)
#Relationships
plants = db.relationship('Plants', backref='country')
# Sets return data {Country_# : id}
def __repr__(self):
return '<Country %r>' % self.id
class Technologies(db.Model):
# Table columns
id = db.Column('id', db.Integer, primary_key=True)
name = db.Column('name', db.String(50), nullable=False, unique=True)
short = db.Column('short', db.String(10), nullable=False, unique=True)
comments = db.Column('comments', db.String(1000))
# Relationships
components = db.relationship("Components", secondary=technology_component, backref='technology')
# Format return data
def __repr__(self):
return '<Technology %r>' % self.name
class Components(db.Model):
# Table columns
id = db.Column('id', db.Integer, primary_key=True)
name = db.Column('name', db.String(30), nullable=False, unique=True)
short = db.Column('short', db.String(15), nullable=False, unique=True)
comments = db.Column('comments', db.String(1000))
# Relationships
tech_id = db.Column(db.Integer, db.ForeignKey('technologies.id'))
types = db.relationship("Types", backref="component")
suppliers = db.relationship("Suppliers", secondary=supplier_component, backref='components')
models = db.relationship("Models", backref="component")
# Format return data
def __repr__(self):
return '<Component %r>' % self.name
class Types(db.Model):
# Table columns
id = db.Column('id', db.Integer, primary_key=True)
name = db.Column('name', db.String(50), nullable=False, unique=True)
short = db.Column('short', db.String(20), nullable=False, unique=True)
# Relationships
component_id = db.Column(db.Integer, db.ForeignKey('components.id'))
#models = db.relationship("Models", backref="type")
# Format return data
def __repr__(self):
return '<Type %r>' % self.name
class Suppliers(db.Model):
# Table columns
id = db.Column('id', db.Integer, primary_key=True)
name = db.Column('name', db.String(50), nullable=False, unique=True)
short = db.Column('short', db.String(10), nullable=False, unique=True)
comments = db.Column('comments', db.String(1000))
# Relationships
types = db.relationship("Types", secondary=supplier_type, backref='suppliers')
models = db.relationship("Models", backref="supplier")
# Formats return data
def __repr__(self):
return '<Supplier %r>' % self.name
class Models(db.Model):
# Table columns
id = db.Column('id', db.Integer, primary_key=True)
name = db.Column('name', db.String(30), nullable=False, unique=True)
power = db.Column('power', db.Integer)
energy = db.Column('energy', db.Integer)
char1 = db.Column('char1', db.String(30))
char2 = db.Column('char2', db.String(30))
comments = db.Column('comments', db.String(1000))
# Relationships
component_id = db.Column(db.Integer, db.ForeignKey('components.id'))
supplier_id = db.Column(db.Integer, db.ForeignKey('suppliers.id'))
type1_id = db.Column(db.Integer, db.ForeignKey('types.id'))
type2_id = db.Column(db.Integer, db.ForeignKey('types.id'))
# Formats return data
def __repr__(self):
return '<Model %r>' % self.name
class Configurations(db.Model):
# Table columns
id = db.Column(db.Integer, primary_key=True)
version = db.Column('status', db.String(20), default='active')
quantity = db.Column('quantity', db.Integer)
comments = db.Column('comments', db.String(1000))
# Relationships
plant_id = db.Column(db.Integer, db.ForeignKey('plants.id'))
component_id = db.Column(db.Integer, db.ForeignKey('components.id'))
type1_id = db.Column(db.Integer, db.ForeignKey('types.id'))
type2_id = db.Column(db.Integer, db.ForeignKey('types.id'))
model_id = db.Column(db.Integer, db.ForeignKey('models.id'))
supplier_id = db.Column(db.Integer, db.ForeignKey('suppliers.id'))
# Formats return data
def __repr__(self):
return '<Configurations %r>' % self.id
class Plants(db.Model):
# Table columns
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(30), nullable=False)
capacity_ac = db.Column(db.Integer)
capacity_dc = db.Column(db.Integer)
latitude = db.Column(db.Integer)
longitude = db.Column(db.Integer)
status = db.Column(db.String(20))
comments = db.Column(db.String(1000), nullable=True)
# Relationships
country_id = db.Column(db.String, db.ForeignKey('countries.id'))
technologies = db.relationship("Technologies", secondary=plant_technology, backref='plants')
configurations = db.relationship("Configurations", backref="plant")
partakers = db.relationship("Partakers", secondary=plant_partaker, backref='plants')
# Sets return data {Plant_# : name}
def __repr__(self):
return '<Plant %r>' % self.name
class Projects(db.Model):
# Table columns
code = db.Column(db.Integer, primary_key=True)
name = db.Column("Name", db.String(50), nullable=False)
description = db.Column('Description', db.String(10000))
link = db.Column('Sharepoint Site', db.String(200))
services = db.Column('Services', db.JSON, nullable=False)
# Relationships
plants = db.relationship("Plants", secondary=project_plant, backref='projects')
role_id= db.Column(db.Integer, db.ForeignKey('roles.id'))
pm_id = db.Column(db.Integer, db.ForeignKey('users.staffnum'))
dpm_id = db.Column(db.Integer, db.ForeignKey('users.staffnum'))
pp_id = db.Column(db.Integer, db.ForeignKey('users.staffnum'))
client_id = db.Column(db.Integer, db.ForeignKey('clients.id'))
# Sets return data {Project_# : code}
def __repr__(self):
return '<Project %r>' % self.code