-
Notifications
You must be signed in to change notification settings - Fork 1
/
crud.py
144 lines (101 loc) · 3.24 KB
/
crud.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
"""CRUD operations."""
from model import db, User, Med, Opioid, connect_to_db
import decimal
from datetime import datetime
# Create, Read, Update, Delete functions
# User functions
def create_user(email, password):
"""Create and return a new user."""
user = User(email=email, password=password)
db.session.add(user)
db.session.commit()
return user
def get_users():
"""Return all users."""
return User.query.all()
def get_user_by_id(user_id):
"""Return a user by primary key."""
return User.query.get(user_id)
def get_user_by_email(email):
"""Return a user by email."""
return User.query.filter(User.email == email).first()
def get_user_password(email):
"""Return password by user email"""
user = User.query.filter(User.email == email).first()
password = user.password
return password
def get_user_by_email_and_password(email, password):
"""Return user by email and password."""
user = User.query.filter(User.email == email).first()
if user:
if user.password == password:
return user
else:
return None
def change_user_password(email, password):
"""Change user password and save new password"""
user = get_user_by_email(email)
User.query.filter(User.email == email).update(
{
"password" : password
}
)
db.session.commit()
return user
# MME and drug functions
def get_opioid_by_name(opioid_name):
"""Return `Opioid` with the given `opioid_name`."""
return Opioid.query.filter_by(opioid_name=opioid_name).first()
def add_opioid_to_user_medlist(
user,
opioid,
drug_dose,
quantity,
days_supply,
daily_MME,
date_filled
):
"""Create `Med` object associated with `user`."""
med = Med(
drug_dose=drug_dose,
quantity=quantity,
days_supply=days_supply,
daily_MME=daily_MME,
date_filled=date_filled,
)
med.opioid = opioid
user.med_list.append(med)
db.session.add(user)
db.session.commit()
def calculate_MME(drug, dose, quantity, days_supply):
"""Calculate MME with unqiue conversion factor from db for specific drug.
Args:
- drug (str): the name of a drug (must be present in database)
- dose (decimal.Decimal)
- quantity (decimal.Decimal)
- days_supply (decimal.Decimal)
"""
if not dose or not quantity or not days_supply:
return 0
# Get `Opioid` from database to get its conversion factor
opioid = get_opioid_by_name(drug)
MME = dose * (quantity // days_supply) * opioid.conversion_factor
return MME
def get_meds():
"""view all meds in med list"""
return Med.query.all()
def get_meds_by_date_range(date_filled, end_date):
"""Takes in a date_filled and end_date and returns a list of med items that fit within the date range.
Args:
date_filled = db.Column(db.Date, nullable=True)
end_date = db.Column(db.Date, nullable=True)
"""
med_list = Med.query.filter(
Med.end_date <= end_date
).filter(
Med.date_filled >= date_filled
).all()
return med_list
if __name__ == '__main__':
from server import app
connect_to_db(app)