-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.py
183 lines (149 loc) · 6.54 KB
/
query.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
from builtins import len, int
from models import *
from sqlalchemy import cast, Date, func, extract
# from datetime import date
from datetime import datetime, timedelta
# init_app()
### Calculate the remaining sugar intake per day 25-total intake for women and 38-total intake for men
def get_user_list_of_food(session):
"""Info on list of foods and costs"""
# todays_sugar is a list of objects
# todays_sugar = db.session.query(Sugar).filter(cast(Sugar.date_time, Date) == date.today(),
# Sugar.user_id == session['user_id']).all()
todays_sugar = db.session.query(Sugar).filter(cast(Sugar.date_time, Date) > datetime.utcnow() - timedelta(days=1),
Sugar.user_id == session['user_id']).all()
list_of_foods = []
for entry in todays_sugar:
list_of_foods.append((entry.food.food_name, entry.food.cost))
return list_of_foods
def get_lists_of_food(session):
"""Listing of food and price"""
# todays_sugar is a list of objects
todays_sugar = db.session.query(Sugar).filter(
cast(Sugar.date_time, Date) > datetime.utcnow() - timedelta(days=1),
Sugar.user_id == session['user_id']).all()
foods = []
for entry in todays_sugar:
foods.append((entry.food.food_name + "__________________" + str(entry.food.cost)))
return foods
def get_users(session):
"""Info on daily spending"""
users = User.query.all()
return users
def get_user(session):
"""Info on daily spending"""
user = User.query.filter(User.user_id == session['user_id']).first()
return user
def get_phone_number(session):
"""Info on daily spending"""
# import pdb; pdb.set_trace()
#session is a dictionary handled by the browser, limit query in multiple
user = User.query.filter(User.user_id == session['user_id']).first()
phone_number = user.phone
return phone_number
def get_user_allowance(session):
"""Info on daily spending"""
user = User.query.filter(User.user_id == session['user_id']).first()
user_allowance = user.gender.allowance
return user_allowance
def get_user_daily_spending(session):
"""Info on daily spending"""
total = sum([cost for _, cost in get_user_list_of_food(session)])
return total
def get_user_daily_balance(session):
"""Info on remaining"""
total = get_user_daily_spending(session) #cache it
if get_user_allowance(session) - total <= 0:
return 0
else:
return get_user_allowance(session) - total
def calculate_user_daily_spending_percentage(session):
"""Info on daily spending as a percentage"""
daily_spending = get_user_daily_spending(session)
return daily_spending / get_user_allowance(session) * 100
def get_monthly_spending(session):
"""Info on monthly spending"""
user_monthly_info = (db.session.query(Sugar.user_id, extract('month', Sugar.date_time), func.sum(Food.cost))
.filter(Sugar.user_id == session['user_id'])
.join(Food).group_by(Sugar.user_id, extract('month', Sugar.date_time)).all())
return user_monthly_info
def get_average_spending(session):
"""Info on average spending"""
user_spending = (db.session.query(Sugar.user_id, extract('year', Sugar.date_time), func.sum(Food.cost))
.filter(Sugar.user_id == session['user_id'])
.join(Food).group_by(Sugar.user_id, extract('year', Sugar.date_time)).all())
user_date_time = (db.session.query(Sugar.user_id, Sugar.date_time)
.filter(Sugar.user_id == session['user_id'])
.join(Food).group_by(Sugar.user_id, Sugar.date_time).all())
dates = []
for date in user_date_time:
dates.append(date)
spent = 0
for item in user_spending:
spent = item[2]
# default value for new users
if len(dates) == 0:
return 0
else:
user_average = spent/len(dates)
return int(user_average)
def get_user_notes(session):
#user_sugar = db.session.query(Sugar).filter(Sugar.user_id == session['user_id']).all()
todays_sugar = db.session.query(Sugar).filter(cast(Sugar.date_time, Date) > datetime.utcnow() - timedelta(days=1),
Sugar.user_id == session['user_id']).all()
user_notes = [object.notes for object in todays_sugar]
return user_notes
def get_user_moods(session):
moods = get_user_notes(session)
user_moods = {}
for mood in moods:
if mood not in user_moods:
user_moods[mood] = 1
else:
user_moods[mood] += 1
RESULTS = {'children': []}
for item in user_moods:
print(item)
print(user_moods[item])
RESULTS['children'].append({
'name': item,
'symbol': item,
'price': user_moods[item],
'net_change': 1,
'percent_change': 1,
'volume': 1,
'value': user_moods[item]
})
return RESULTS
def get_user_weight(session):
weekday = func.extract('isodow', Weight.date_time).label('weekday')
user_monthly_weight = (db.session.query(Weight.user_id, extract('month', Weight.date_time), weekday, Weight.current_weight)
.filter(Weight.user_id == session['user_id'])
.join(User).group_by(Weight.user_id, extract('month', Weight.date_time), weekday, Weight.current_weight).all())
return user_monthly_weight
def get_user_glucose(session):
weekday = func.extract('isodow', Glucose.date_time).label('weekday')
user_monthly_glucose = (db.session.query(Glucose.user_id, extract('month', Glucose.date_time), weekday, Glucose.current_glucose)
.filter(Glucose.user_id == session['user_id'])
.join(User).group_by(Glucose.user_id, extract('month', Glucose.date_time), weekday, Glucose.current_glucose).all())
return user_monthly_glucose
def get_user_current_weight(session):
user_weight_list = get_user_weight(session)
current_weight = []
for weight in user_weight_list:
current_weight.append(weight.current_weight)
# default value for new users
if current_weight == []:
return 0
else:
return current_weight[-1]
def get_user_current_glucose(session):
glucose_list = get_user_glucose(session)
current_glucose = []
for level in glucose_list:
current_glucose.append(level.current_glucose)
# default value for new users
if current_glucose == []:
return 0
else:
return current_glucose[-1]