init
This commit is contained in:
commit
66729f7d08
24 files changed
+907
No files matched your search
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,120 @@
|
||||
# meal_tracker/models.py
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Ingredient(models.Model):
|
||||
name = models.CharField(max_length=255, db_index=True)
|
||||
calories_per_100g = models.FloatField()
|
||||
protein_per_100g = models.FloatField()
|
||||
fat_per_100g = models.FloatField()
|
||||
carbs_per_100g = models.FloatField()
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = "Ingredients"
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class Food(models.Model):
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
estimated_calories = models.IntegerField(null=True, blank=True)
|
||||
estimated_protein = models.FloatField(null=True, blank=True)
|
||||
estimated_fat = models.FloatField(null=True, blank=True)
|
||||
estimated_carbs = models.FloatField(null=True, blank=True)
|
||||
|
||||
ingredients = models.ManyToManyField('Ingredient', through='FoodComponent')
|
||||
|
||||
@property
|
||||
def total_calories(self):
|
||||
if self.estimated_calories:
|
||||
return self.estimated_calories
|
||||
return sum(c.get_component_calories() for c in self.ingredients.all())
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class FoodComponent(models.Model):
|
||||
food = models.ForeignKey(Food, related_name='components', on_delete=models.CASCADE)
|
||||
ingredient = models.ForeignKey(Ingredient, related_name='foods_used_in', on_delete=models.CASCADE)
|
||||
amount_grams = models.FloatField()
|
||||
calories = models.IntegerField(default=0)
|
||||
protein = models.FloatField(default=0)
|
||||
fat = models.FloatField(default=0)
|
||||
carbs = models.FloatField(default=0)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.ingredient:
|
||||
factor = self.amount_grams / 100.0
|
||||
self.calories = int(self.ingredient.calories_per_100g * factor)
|
||||
self.protein = self.ingredient.protein_per_100g * factor
|
||||
self.fat = self.ingredient.fat_per_100g * factor
|
||||
self.carbs = self.ingredient.carbs_per_100g * factor
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.food.name} component"
|
||||
|
||||
|
||||
class Meal(models.Model):
|
||||
date = models.DateField()
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def get_total_macros(self):
|
||||
totals = {'calories': 0, 'protein': 0, 'fat': 0, 'carbs': 0}
|
||||
for item in self.items.all():
|
||||
totals['calories'] += item.get_food_calories()
|
||||
totals['protein'] += item.get_food_protein()
|
||||
totals['fat'] += item.get_food_fat()
|
||||
totals['carbs'] += item.get_food_carbs()
|
||||
return totals
|
||||
|
||||
def __str__(self):
|
||||
return f"Meal on {self.date}"
|
||||
|
||||
|
||||
class MealItem(models.Model):
|
||||
meal = models.ForeignKey(Meal, related_name='items', on_delete=models.CASCADE)
|
||||
food = models.ForeignKey(Food, related_name='meals_contained', on_delete=models.PROTECT)
|
||||
quantity_grams = models.FloatField()
|
||||
|
||||
def get_food_calories(self):
|
||||
if self.food.estimated_calories:
|
||||
return int(self.food.estimated_calories)
|
||||
total = 0
|
||||
for comp in self.food.components.all():
|
||||
factor = self.quantity_grams / comp.amount_grams
|
||||
total += int(comp.calories * factor)
|
||||
return total
|
||||
|
||||
def get_food_protein(self):
|
||||
if self.food.estimated_protein is not None:
|
||||
return self.food.estimated_protein
|
||||
total = 0
|
||||
for comp in self.food.components.all():
|
||||
factor = self.quantity_grams / comp.amount_grams
|
||||
total += comp.protein * factor
|
||||
return total
|
||||
|
||||
def get_food_fat(self):
|
||||
if self.food.estimated_fat is not None:
|
||||
return self.food.estimated_fat
|
||||
total = 0
|
||||
for comp in self.food.components.all():
|
||||
factor = self.quantity_grams / comp.amount_grams
|
||||
total += comp.fat * factor
|
||||
return total
|
||||
|
||||
def get_food_carbs(self):
|
||||
if self.food.estimated_carbs is not None:
|
||||
return self.food.estimated_carbs
|
||||
total = 0
|
||||
for comp in self.food.components.all():
|
||||
factor = self.quantity_grams / comp.amount_grams
|
||||
total += comp.carbs * factor
|
||||
return total
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.food.name} ({self.quantity_grams}g)"
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
# meal_tracker/tasks.py
|
||||
from core.base import TaskModule
|
||||
from .models import Meal, MealItem, Food
|
||||
|
||||
|
||||
class MealTracker(TaskModule):
|
||||
MODULE_NAME = "meal_tracker"
|
||||
DESCRIPTION = "Log meals with food items and nutritional tracking"
|
||||
|
||||
TOOLS = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "log_meal",
|
||||
"description": "Log a meal entry with food, quantity, and macros",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"date": {"type": "string", "format": "date"},
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"food_name": {"type": "string"},
|
||||
"quantity_grams": {"type": "integer"},
|
||||
"estimated_calories": {"type": "integer"},
|
||||
"estimated_protein": {"type": "number"},
|
||||
"estimated_fat": {"type": "number"},
|
||||
"estimated_carbs": {"type": "number"}
|
||||
},
|
||||
"required": ["food_name", "quantity_grams"]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["date", "items"]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def execute_tool(cls, tool_name: str, arguments: dict) -> dict:
|
||||
if tool_name == "log_meal":
|
||||
meal = Meal.objects.create(date=arguments["date"])
|
||||
for item_data in arguments["items"]:
|
||||
food = Food.objects.create(
|
||||
name=item_data["food_name"],
|
||||
estimated_calories=item_data.get("estimated_calories"),
|
||||
estimated_protein=item_data.get("estimated_protein"),
|
||||
estimated_fat=item_data.get("estimated_fat"),
|
||||
estimated_carbs=item_data.get("estimated_carbs")
|
||||
)
|
||||
MealItem.objects.create(meal=meal, food=food, quantity_grams=item_data["quantity_grams"])
|
||||
return {"status": "success",
|
||||
"message": f"Logged {len(arguments['items'])} food items for {arguments['date']}"}
|
||||
raise ValueError(f"Unknown tool: {tool_name}")
|
||||
Reference in new issue
Block a user