mirror of
https://github.com/sstent/foodplanner.git
synced 2026-01-25 03:01:35 +00:00
updated the tracker to have more features
This commit is contained in:
@@ -113,11 +113,10 @@ async def bulk_upload_meals(file: UploadFile = File(...), db: Session = Depends(
|
||||
|
||||
@router.post("/meals/add")
|
||||
async def add_meal(request: Request, db: Session = Depends(get_db),
|
||||
name: str = Form(...), meal_type: str = Form(...),
|
||||
meal_time: str = Form(...)):
|
||||
name: str = Form(...)):
|
||||
|
||||
try:
|
||||
meal = Meal(name=name, meal_type=meal_type, meal_time=meal_time)
|
||||
meal = Meal(name=name, meal_type="custom", meal_time="Custom")
|
||||
db.add(meal)
|
||||
db.commit()
|
||||
db.refresh(meal)
|
||||
@@ -128,8 +127,7 @@ async def add_meal(request: Request, db: Session = Depends(get_db),
|
||||
|
||||
@router.post("/meals/edit")
|
||||
async def edit_meal(request: Request, db: Session = Depends(get_db),
|
||||
meal_id: int = Form(...), name: str = Form(...),
|
||||
meal_type: str = Form(...), meal_time: str = Form(...)):
|
||||
meal_id: int = Form(...), name: str = Form(...)):
|
||||
|
||||
try:
|
||||
meal = db.query(Meal).filter(Meal.id == meal_id).first()
|
||||
@@ -137,8 +135,6 @@ async def edit_meal(request: Request, db: Session = Depends(get_db),
|
||||
return {"status": "error", "message": "Meal not found"}
|
||||
|
||||
meal.name = name
|
||||
meal.meal_type = meal_type
|
||||
meal.meal_time = meal_time # Update meal_time
|
||||
|
||||
db.commit()
|
||||
return {"status": "success", "message": "Meal updated successfully"}
|
||||
@@ -209,6 +205,38 @@ async def remove_food_from_meal(meal_food_id: int, db: Session = Depends(get_db)
|
||||
db.rollback()
|
||||
return {"status": "error", "message": str(e)}
|
||||
|
||||
@router.post("/meals/clone/{meal_id}")
|
||||
async def clone_meal(meal_id: int, db: Session = Depends(get_db)):
|
||||
"""Clone an existing meal"""
|
||||
try:
|
||||
original_meal = db.query(Meal).filter(Meal.id == meal_id).first()
|
||||
if not original_meal:
|
||||
return {"status": "error", "message": "Original meal not found"}
|
||||
|
||||
# Create new meal
|
||||
new_meal = Meal(
|
||||
name=f"{original_meal.name} - Copy",
|
||||
meal_type=original_meal.meal_type,
|
||||
meal_time=original_meal.meal_time
|
||||
)
|
||||
db.add(new_meal)
|
||||
db.flush()
|
||||
|
||||
# Copy meal foods
|
||||
for meal_food in original_meal.meal_foods:
|
||||
new_meal_food = MealFood(
|
||||
meal_id=new_meal.id,
|
||||
food_id=meal_food.food_id,
|
||||
quantity=meal_food.quantity
|
||||
)
|
||||
db.add(new_meal_food)
|
||||
|
||||
db.commit()
|
||||
return {"status": "success", "new_meal_id": new_meal.id}
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
return {"status": "error", "message": str(e)}
|
||||
|
||||
@router.post("/meals/delete")
|
||||
async def delete_meals(meal_ids: dict = Body(...), db: Session = Depends(get_db)):
|
||||
try:
|
||||
|
||||
@@ -34,13 +34,13 @@
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
<button type="button" class="btn btn-sm btn-outline-primary"
|
||||
onclick="editMeal({{ meal.id }})">
|
||||
<i class="bi bi-pencil"></i> Edit
|
||||
<button type="button" class="btn btn-sm btn-outline-primary"
|
||||
onclick="manageMeal({{ meal.id }})">
|
||||
<i class="bi bi-pencil-square"></i> Manage
|
||||
</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-success"
|
||||
onclick="manageMealFoods({{ meal.id }})">
|
||||
<i class="bi bi-list"></i> Foods
|
||||
<button type="button" class="btn btn-sm btn-outline-info"
|
||||
onclick="cloneMeal({{ meal.id }})">
|
||||
<i class="bi bi-front"></i> Clone
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -68,19 +68,6 @@
|
||||
<label class="form-label">Meal Name</label>
|
||||
<input type="text" class="form-control" name="name" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Meal Time</label>
|
||||
<select class="form-control" name="meal_time" required>
|
||||
<option value="Breakfast">Breakfast</option>
|
||||
<option value="Lunch">Lunch</option>
|
||||
<option value="Dinner">Dinner</option>
|
||||
<option value="Snack 1">Snack 1</option>
|
||||
<option value="Snack 2">Snack 2</option>
|
||||
<option value="Beverage 1">Beverage 1</option>
|
||||
<option value="Beverage 2">Beverage 2</option>
|
||||
<option value="Custom">Custom</option>
|
||||
</select>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
@@ -91,88 +78,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Edit Meal Modal -->
|
||||
<div class="modal fade" id="editMealModal" tabindex="-1" aria-labelledby="editMealModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="editMealModalLabel">Edit Meal</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form id="editMealForm" action="/meals/edit" method="post">
|
||||
<input type="hidden" name="meal_id" id="edit_meal_id">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Meal Name</label>
|
||||
<input type="text" class="form-control" name="name" id="edit_meal_name" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Meal Time</label>
|
||||
<select class="form-control" name="meal_time" id="edit_meal_time" required>
|
||||
<option value="Breakfast">Breakfast</option>
|
||||
<option value="Lunch">Lunch</option>
|
||||
<option value="Dinner">Dinner</option>
|
||||
<option value="Snack 1">Snack 1</option>
|
||||
<option value="Snack 2">Snack 2</option>
|
||||
<option value="Beverage 1">Beverage 1</option>
|
||||
<option value="Beverage 2">Beverage 2</option>
|
||||
<option value="Custom">Custom</option>
|
||||
</select>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
||||
<button type="button" class="btn btn-primary" onclick="submitMealForm('edit')">Update Meal</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Manage Meal Foods Modal -->
|
||||
<div class="modal fade" id="manageMealFoodsModal" tabindex="-1" aria-labelledby="manageMealFoodsModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="manageMealFoodsModalLabel">Manage Foods for Meal</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<h6>Add Food to Meal</h6>
|
||||
<form id="addFoodToMealForm">
|
||||
<input type="hidden" id="meal_id_for_food" name="meal_id">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Select Food</label>
|
||||
<select class="form-control" id="foodSelect" name="food_id" required>
|
||||
<option value="">Choose food...</option>
|
||||
{% for food in foods %}
|
||||
<option value="{{ food.id }}">{{ food.name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Quantity</label>
|
||||
<input type="number" step="0.01" class="form-control" name="quantity" value="1" required>
|
||||
</div>
|
||||
<button type="button" class="btn btn-success" onclick="addFoodToMeal()">Add Food</button>
|
||||
</form>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<h6>Current Foods</h6>
|
||||
<div id="currentMealFoods">
|
||||
<!-- This will be populated dynamically -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% include 'modals/manage_meal.html' %}
|
||||
|
||||
<script>
|
||||
function toggleAllMeals(source) {
|
||||
@@ -180,39 +86,21 @@ function toggleAllMeals(source) {
|
||||
checkboxes.forEach(checkbox => checkbox.checked = source.checked);
|
||||
}
|
||||
|
||||
// Edit meal function
|
||||
async function editMeal(id) {
|
||||
document.getElementById('edit_meal_id').value = id;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/meals/${id}`); // Assuming an endpoint /meals/{id} exists to get meal details
|
||||
const meal = await response.json();
|
||||
|
||||
if (meal.status === 'success') {
|
||||
document.getElementById('edit_meal_name').value = meal.name;
|
||||
document.getElementById('edit_meal_time').value = meal.meal_time;
|
||||
new bootstrap.Modal(document.getElementById('editMealModal')).show();
|
||||
} else {
|
||||
alert('Error fetching meal details: ' + meal.message);
|
||||
}
|
||||
} catch (error) {
|
||||
alert('Error fetching meal details: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Manage meal foods function
|
||||
async function manageMealFoods(mealId) {
|
||||
// Manage meal function
|
||||
async function manageMeal(mealId) {
|
||||
document.getElementById('manage_meal_id').value = mealId;
|
||||
document.getElementById('meal_id_for_food').value = mealId;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/meals/${mealId}`); // Assuming an endpoint /meals/{id} exists to get meal details
|
||||
const response = await fetch(`/meals/${mealId}`);
|
||||
const meal = await response.json();
|
||||
|
||||
if (meal.status === 'success') {
|
||||
document.getElementById('manageMealFoodsModalLabel').textContent = `Manage Foods for: ${meal.name}`;
|
||||
// Load current meal foods
|
||||
document.getElementById('manage_meal_name').value = meal.name;
|
||||
document.getElementById('manageMealModalLabel').textContent = `Manage Meal: ${meal.name}`;
|
||||
|
||||
await loadCurrentMealFoods(mealId);
|
||||
new bootstrap.Modal(document.getElementById('manageMealFoodsModal')).show();
|
||||
new bootstrap.Modal(document.getElementById('manageMealModal')).show();
|
||||
} else {
|
||||
alert('Error fetching meal details: ' + meal.message);
|
||||
}
|
||||
@@ -294,9 +182,34 @@ async function removeFoodFromMeal(mealFoodId) {
|
||||
}
|
||||
}
|
||||
|
||||
// Submit meal form (add or edit)
|
||||
// Submit meal form (add or manage)
|
||||
async function submitMealForm(action) {
|
||||
const form = document.getElementById(action + 'MealForm');
|
||||
if (action === 'manage') { // Handle name update separately
|
||||
const mealId = document.getElementById('manage_meal_id').value;
|
||||
const newName = document.getElementById('manage_meal_name').value;
|
||||
const formData = new FormData();
|
||||
formData.append('meal_id', mealId);
|
||||
formData.append('name', newName);
|
||||
|
||||
try {
|
||||
const response = await fetch('/meals/edit', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
const result = await response.json();
|
||||
if (result.status === 'success') {
|
||||
document.getElementById('manageMealModalLabel').textContent = `Manage Meal: ${newName}`;
|
||||
// No page reload, just confirmation
|
||||
alert('Meal name updated successfully!');
|
||||
} else {
|
||||
alert('Error: ' + result.message);
|
||||
}
|
||||
} catch (error) {
|
||||
alert('Error updating meal name: ' + error.message);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const formData = new FormData(form);
|
||||
|
||||
try {
|
||||
@@ -350,5 +263,23 @@ async function deleteSelectedMeals() {
|
||||
}
|
||||
}
|
||||
|
||||
// Clone meal function
|
||||
async function cloneMeal(mealId) {
|
||||
if (confirm('Are you sure you want to clone this meal?')) {
|
||||
try {
|
||||
const response = await fetch(`/meals/clone/${mealId}`, {
|
||||
method: 'POST'
|
||||
});
|
||||
const result = await response.json();
|
||||
if (result.status === 'success') {
|
||||
location.reload();
|
||||
} else {
|
||||
alert('Error cloning meal: ' + result.message);
|
||||
}
|
||||
} catch (error) {
|
||||
alert('Error cloning meal: ' + error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
{% endblock %}
|
||||
55
templates/modals/manage_meal.html
Normal file
55
templates/modals/manage_meal.html
Normal file
@@ -0,0 +1,55 @@
|
||||
<!-- templates/modals/manage_meal.html -->
|
||||
<div class="modal fade" id="manageMealModal" tabindex="-1" aria-labelledby="manageMealModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="manageMealModalLabel">Manage Meal</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="row">
|
||||
<!-- Edit Meal Name -->
|
||||
<div class="col-md-12 mb-4">
|
||||
<h6>Meal Name</h6>
|
||||
<form id="manageMealForm" class="d-flex gap-2">
|
||||
<input type="hidden" name="meal_id" id="manage_meal_id">
|
||||
<input type="text" class="form-control" name="name" id="manage_meal_name" required>
|
||||
<button type="button" class="btn btn-primary" onclick="submitMealForm('manage')">Update Name</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- Food Management -->
|
||||
<div class="col-md-6">
|
||||
<h6>Add Food to Meal</h6>
|
||||
<form id="addFoodToMealForm">
|
||||
<input type="hidden" id="meal_id_for_food" name="meal_id">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Select Food</label>
|
||||
<select class="form-control" id="foodSelect" name="food_id" required>
|
||||
<option value="">Choose food...</option>
|
||||
{% for food in foods %}
|
||||
<option value="{{ food.id }}">{{ food.name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Quantity</label>
|
||||
<input type="number" step="0.01" class="form-control" name="quantity" value="1" required>
|
||||
</div>
|
||||
<button type="button" class="btn btn-success" onclick="addFoodToMeal()">Add Food</button>
|
||||
</form>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<h6>Current Foods</h6>
|
||||
<div id="currentMealFoods">
|
||||
<!-- This will be populated dynamically -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user