-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
261 lines (197 loc) · 7.37 KB
/
models.py
File metadata and controls
261 lines (197 loc) · 7.37 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
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
from extensions import db
import datetime
import typing
# todo need to review the new structure - > user->facility->station->inventory -- DONE
# todo barcode->user -- DONE
class userData(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(128), nullable=False)
password = db.Column(db.String(10), nullable=False)
type = db.Column(db.String(5), nullable=False)
facility_id = db.Column(db.Integer, db.ForeignKey('facility.id'), nullable=True)
auth_hash = db.Column(db.String, nullable=True)
def __init__(self, *args, **kwargs) -> None:
super(userData, self).__init__(*args, **kwargs)
@property
def facility(self)->str:
facility = facilityData.query.filter(facilityData.id == self.facility_id).first()
if facility is None:
return "No Facility Found"
else:
return facility.facility_name
def save(self):
db.session.add(self)
db.session.commit()
def update(self, data):
for key, item in data.items():
setattr(self, key, item)
def delete(self):
db.session.delete(self)
db.session.commit()
def verify_password(self, password):
return password == self.password
def _asdict(self):
return dict(
id=self.id,
name=self.name,
type=self.type,
facility_id=self.facility.facility_name
)
class barcodeData(db.Model):
__tablename__ = 'barcodes'
'''
intent of the barcode class is to share barcodes across platform
'''
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
barcode = db.Column(db.String(128), nullable=False)
medid = db.Column(db.String(128), nullable=False)
user = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
def __init__(self, data):
'''class constructor'''
self.barcode = data.get('barcode')
self.medid = data.get('medid')
self.user = data.get('user')
def save(self):
db.session.add(self)
db.session.commit()
def update(self, data):
for key, item in data.items():
setattr(self, key, item)
def delete(self):
db.session.delete(self)
db.session.commit()
def _asdict(self):
return dict(
barcode=self.barcode,
medid=self.medid,
user=self.user
)
def __repr__(self) -> str:
return "Barcode: {} ||| MedID: {} ||| Submitted by: {}".format(self.barcode, self.medid, self.user)
class MedstationData(db.Model):
__tablename__ = "medstation"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
station_name = db.Column(db.String(128), nullable=False)
inventory_data = db.relationship('InventoryData', backref='medstation', cascade="all, delete, delete-orphan")
facility_id = db.Column(db.Integer, db.ForeignKey('facility.id'), nullable=True)
def __init__(self, data: typing.Dict['str', 'str']) -> None:
self.station_name = data.get('station_name')
def save(self):
db.session.add(self)
db.session.commit()
def update(self, data):
for key, item in data.items():
setattr(self, key, item)
def delete(self):
db.session.delete(self)
db.session.commit()
def _asdict(self):
output = dict(
station_name=self.station_name,
)
return output
def _refill_list_as_dict(self):
#todo filter list by checked is false--DONE
output = dict(
station_name=self.station_name,
refill_list=[refilldata._asdict_refill() for refilldata in self.inventory_data]
)
return output
class InventoryData(db.Model):
__tablename__ = "inventory"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
med_description = db.Column(db.String(255), nullable=False)
medid = db.Column(db.String(10), nullable=False)
min = db.Column(db.Integer, nullable=False)
max = db.Column(db.Integer, nullable=False)
current = db.Column(db.Integer, nullable=False)
pick_amount = db.Column(db.Integer, nullable=False)
checked = db.Column(db.Boolean)
entry_date = db.Column(db.DateTime())
station_id = db.Column(db.Integer, db.ForeignKey('medstation.id'), nullable=True)
def __init__(self, data) -> None:
self.med_description = data.get('med_description')
self.medid = data.get('medid')
self.min = data.get('min')
self.max = data.get('max')
self.current = data.get('current')
self.pick_amount = data.get('pick_amount')
self.checked = False
self.entry_date = datetime.datetime.now()
def save(self):
db.session.add(self)
db.session.commit()
def update(self, data):
for key, item in data.items():
setattr(self, key, item)
def delete(self):
db.session.delete(self)
db.session.commit()
def _asdict_refill(self):
if self.checked is not True:
return self._asdict()
def _asdict(self):
dict_return = dict(
med_description=self.med_description,
medid=self.medid,
min=self.min,
max=self.max,
current=self.current,
pick_amount=self.pick_amount,
checked=str(self.checked),
entry_date=self.entry_date.isoformat(' ', 'seconds'),
)
return dict_return
class facilityData(db.Model):
__tablename__ = 'facility'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
facility_name = db.Column(db.String(128), nullable=False)
stations = db.relationship('MedstationData', backref='facility', lazy=True, cascade="all, delete, delete-orphan")
users = db.relationship('userData', backref='facility', lazy=True, cascade="all, delete, delete-orphan")
def __init__(self, data) -> None:
'''class constructor'''
self.facility_name = data.get('facility_name')
def save(self):
db.session.add(self)
db.session.commit()
def update(self, data):
for key, item in data.items():
setattr(self, key, item)
def delete(self):
db.session.delete(self)
db.session.commit()
def _asdict(self):
return dict(
id=self.id,
facility_name=self.facility_name,
users=[user._asdict() for user in self.users],
stations=[station._asdict() for station in self.stations]
)
def __repr__(self) -> str:
return "{}".format(self.facility_name)
class BlackListToken(db.Model):
__tablename__ = "blacklist"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
hash = db.Column(db.String, nullable=True)
def __init__(self, data: typing.Dict[str, str]):
self.hash = data.get('hash')
def save(self):
db.session.add(self)
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
@staticmethod
def validate_blacklist(hash):
blacklist = BlackListToken.query.filter(BlackListToken.hash == hash).first()
if blacklist is None:
'''
token isn't in blacklist
'''
return True
else:
'''
token is in blacklist
'''
return False