-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
83 lines (68 loc) · 2.77 KB
/
models.py
File metadata and controls
83 lines (68 loc) · 2.77 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
"""
Data parsing functions to convert MyFitnessPal objects to database-ready dictionaries.
"""
def parse_food_entry(date, meal_name, entry):
"""
Convert MyFitnessPal food entry object to database dictionary.
NOTE: MyFitnessPal API only provides these nutrients in entry.totals:
- calories, carbohydrates, fat, protein, sodium, sugar
Other nutrients (fiber, vitamins, minerals) are not available through
the API and will always be null.
Args:
date: datetime.date object
meal_name: Name of meal (e.g., "Breakfast", "Lunch")
entry: MyFitnessPal Entry object
Returns:
dict: Dictionary with all food entry fields
"""
return {
'date': date,
'meal_type': meal_name.lower(),
'food_name': entry.name,
'short_name': getattr(entry, 'short_name', None),
'quantity': float(entry.quantity) if hasattr(entry, 'quantity') and entry.quantity else None,
'unit': getattr(entry, 'unit', None),
# Only these nutrients are available from MyFitnessPal API
'calories': entry.totals.get('calories'),
'carbohydrates': entry.totals.get('carbohydrates'),
'fat': entry.totals.get('fat'),
'protein': entry.totals.get('protein'),
'sodium': entry.totals.get('sodium'),
'sugar': entry.totals.get('sugar'),
}
def parse_exercise(date, exercise_type, entry):
"""
Convert MyFitnessPal exercise entry to database dictionary.
Args:
date: datetime.date object
exercise_type: Type of exercise ('cardiovascular' or 'strength training')
entry: Individual exercise entry dict from exercise.get_as_list()
Returns:
dict: Dictionary with exercise entry fields
"""
# Get nutrition information from the entry
nutrition_info = entry.get('nutrition_information', {})
return {
'date': date,
'exercise_type': exercise_type,
'exercise_name': entry.get('name'),
'calories_burned': nutrition_info.get('calories burned'),
'duration_minutes': nutrition_info.get('minutes'),
# Strength training fields (will be None for cardiovascular exercises)
'sets': nutrition_info.get('sets'),
'reps_per_set': nutrition_info.get('reps/set'),
'weight_per_set': nutrition_info.get('weight/set'),
}
def parse_weight_measurement(date, weight_value):
"""
Convert MyFitnessPal weight measurement to database dictionary.
Args:
date: datetime.date object
weight_value: float weight in pounds from MyFitnessPal
Returns:
dict: Dictionary with weight measurement fields
"""
return {
'date': date,
'weight_lbs': float(weight_value) if weight_value is not None else None,
}