-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_meals.py
180 lines (129 loc) · 5.09 KB
/
day_meals.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
import json
import os
from datetime import date
import file_handler as fh
import food_itens as fi
class DayMeals:
def __init__(self):
self.day_meals_file= './day_meals.json'
self.foods = fi.FoodItem()
def addDayMeal(self,meal,params,_date=None):
if not _date: _date = str(date.today())
# verifying if day exists in data base
days = fh.getJson(self.day_meals_file)
if not days:
print("Error | day_meals | addDayMeal")
exit()
elif days == 1:
self.addDay(_date)
else:
date_list = []
for _id in days:
date_list.append(days[_id].get("date"))
if _date not in date_list:
self.addDay(_date)
days = fh.getJson(self.day_meals_file)
if days:
for _id in list(days.keys()):
# veryfing if day is in the base
if days[_id].get("date") == _date:
# veryfing if the meal already exists
if(meal in days[_id].get("meals")):
print("Ihhhhh parece que ja temos um registro dessa refecao aqui, deseja alterar ? ")
#todo
# sending meal to the base
else:
days[_id]["meals"][meal] = params
days = fh.getUpdatedData(self.day_meals_file,days)
fh.updateJson(self.day_meals_file, days)
def addDay(self,dayDate=None):
if not dayDate: dayDate = str(date.today())
print("oi")
data = fh.getJson(self.day_meals_file)
if not data:
print("Error | day_meals | addDay")
exit()
elif data == 1:
print("foi")
newId = 1
else:
newId = int(max(list(data.keys())))+1
day = {
newId:{
"date":dayDate,
"meals": {
}
}
}
data = fh.getUpdatedData(self.day_meals_file,day)
if data == 0:
data = day
fh.updateJson(self.day_meals_file,data)
def getMealsByDay(self,day=None):
if not day: day = str(date.today())
data = fh.getJson(self.day_meals_file)
dayKey =None
for key in list(data.keys()):
if data[key].get("date") == day:
dayKey = key
meals = data[dayKey].get("meals")
if data[dayKey].get("dayOff"):
meals = {
"breakfast": {
'jejum':1
},
"lunch": {
'jejum':1
},
"afternoon-snack": {
'jejum':1
},
"dinner": {
"jejum": 1
}
}
if meals:
return meals
else:
print("Error | day_meals | getMealsByDay", meals,day)
def getAmountPropertiesByDay(self, day=None):
meals = self.getMealsByDay(day)
breakfastAmount = self.getAmountPropertiesByMeal(meals['breakfast'])
lunchAmount = self.getAmountPropertiesByMeal(meals['lunch'])
dinnerAmount = self.getAmountPropertiesByMeal(meals['dinner'])
afternoon_snackAmount = self.getAmountPropertiesByMeal(meals['afternoon-snack'])
response = {
'beakfast':breakfastAmount,
'lunch':lunchAmount,
'afternoon-snack':afternoon_snackAmount,
'dinner':dinnerAmount
}
total = self.foods.formatedProperties()
for value in response.values():
for valueByProperty in list(value.keys()):
amountProperty = value[valueByProperty]
total[valueByProperty] = round(total[valueByProperty] + amountProperty)
response['total'] = total
return response
def getAmountPropertiesByMeal(self ,meal):
amountDay = self.foods.formatedProperties()
for item in meal:
for _property in list(amountDay.keys()):
amountProperty = 0
amountProperty = self.foods.getPropertyByItem(item,_property)
amountProperty = (meal[item]*amountProperty)/100
amountDay[_property] = round(amountDay[_property] + amountProperty)
return amountDay
def getAllDaysResume(self):
daysList = fh.getJsonKeys(self.day_meals_file)
result = {}
for day in daysList:
day_info = fh.getJsonObjectByKey(self.day_meals_file, day)
resume = self.getAmountPropertiesByDay(day_info["date"])
result[day] = {}
result[day]["total"] = resume['total']
result[day]["date"] = day_info["date"]
if result:
return result
else:
print("Error | day_meals | getAllDaysResume")