mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 14:33:33 -07:00
Fix Meal-Plan Today
This commit is contained in:
parent
94edc73f25
commit
47e25ab705
8 changed files with 69 additions and 38 deletions
|
@ -16,6 +16,7 @@ const mealPlanURLs = {
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
async create(postBody) {
|
async create(postBody) {
|
||||||
|
console.log(postBody);
|
||||||
let response = await apiReq.post(mealPlanURLs.create, postBody);
|
let response = await apiReq.post(mealPlanURLs.create, postBody);
|
||||||
return response;
|
return response;
|
||||||
},
|
},
|
||||||
|
|
|
@ -17,7 +17,9 @@
|
||||||
:src="getImage(meal.slug)"
|
:src="getImage(meal.slug)"
|
||||||
@click="openSearch(index)"
|
@click="openSearch(index)"
|
||||||
></v-img>
|
></v-img>
|
||||||
<v-card-title class="my-n3 mb-n6">{{ $d( new Date(meal.date), 'short' ) }}</v-card-title>
|
<v-card-title class="my-n3 mb-n6">
|
||||||
|
{{ $d(new Date(meal.date.split("-")), "short") }}
|
||||||
|
</v-card-title>
|
||||||
<v-card-subtitle> {{ meal.name }}</v-card-subtitle>
|
<v-card-subtitle> {{ meal.name }}</v-card-subtitle>
|
||||||
</v-card>
|
</v-card>
|
||||||
</v-hover>
|
</v-hover>
|
||||||
|
|
|
@ -25,8 +25,8 @@
|
||||||
>
|
>
|
||||||
<v-card class="mt-1">
|
<v-card class="mt-1">
|
||||||
<v-card-title>
|
<v-card-title>
|
||||||
{{ $d(new Date(mealplan.startDate), "short") }} -
|
{{ $d(new Date(mealplan.startDate.split("-")), "short") }} -
|
||||||
{{ $d(new Date(mealplan.endDate), "short") }}
|
{{ $d(new Date(mealplan.endDate.split("-")), "short") }}
|
||||||
</v-card-title>
|
</v-card-title>
|
||||||
<v-list nav>
|
<v-list nav>
|
||||||
<v-list-item-group color="primary">
|
<v-list-item-group color="primary">
|
||||||
|
@ -44,7 +44,7 @@
|
||||||
<v-list-item-content>
|
<v-list-item-content>
|
||||||
<v-list-item-title v-text="meal.name"></v-list-item-title>
|
<v-list-item-title v-text="meal.name"></v-list-item-title>
|
||||||
<v-list-item-subtitle
|
<v-list-item-subtitle
|
||||||
v-text="$d(new Date(meal.date), 'short')"
|
v-text="$d(new Date(meal.date.split('-')), 'short')"
|
||||||
>
|
>
|
||||||
</v-list-item-subtitle>
|
</v-list-item-subtitle>
|
||||||
</v-list-item-content>
|
</v-list-item-content>
|
||||||
|
|
|
@ -73,8 +73,8 @@ export default {
|
||||||
return `${dow}, ${month} ${day}`;
|
return `${dow}, ${month} ${day}`;
|
||||||
},
|
},
|
||||||
getDateAsPythonDate(dateObject) {
|
getDateAsPythonDate(dateObject) {
|
||||||
const month = dateObject.getMonth() + 1;
|
const month = dateObject.getUTCMonth() + 1;
|
||||||
const day = dateObject.getDate();
|
const day = dateObject.getUTCDate();
|
||||||
const year = dateObject.getFullYear();
|
const year = dateObject.getFullYear();
|
||||||
|
|
||||||
return `${year}-${month}-${day}`;
|
return `${year}-${month}-${day}`;
|
||||||
|
|
|
@ -29,7 +29,7 @@ class MealPlanModel(SqlAlchemyBase, BaseMixins):
|
||||||
uid = sa.Column(sa.Integer, primary_key=True, unique=True) #! Probably Bad?
|
uid = sa.Column(sa.Integer, primary_key=True, unique=True) #! Probably Bad?
|
||||||
startDate = sa.Column(sa.Date)
|
startDate = sa.Column(sa.Date)
|
||||||
endDate = sa.Column(sa.Date)
|
endDate = sa.Column(sa.Date)
|
||||||
meals: List[Meal] = orm.relationship(Meal, cascade="all, delete")
|
meals: List[Meal] = orm.relationship(Meal, cascade="all, delete, delete-orphan")
|
||||||
group_id = sa.Column(sa.String, sa.ForeignKey("groups.id"))
|
group_id = sa.Column(sa.String, sa.ForeignKey("groups.id"))
|
||||||
group = orm.relationship("Group", back_populates="mealplans")
|
group = orm.relationship("Group", back_populates="mealplans")
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ class MealPlanModel(SqlAlchemyBase, BaseMixins):
|
||||||
self.meals = [Meal(**meal) for meal in meals]
|
self.meals = [Meal(**meal) for meal in meals]
|
||||||
|
|
||||||
def update(self, session, startDate, endDate, meals, uid, group) -> None:
|
def update(self, session, startDate, endDate, meals, uid, group) -> None:
|
||||||
MealPlanModel._sql_remove_list(session, [Meal], uid)
|
# MealPlanModel._sql_remove_list(session, [Meal], uid)
|
||||||
|
|
||||||
self.__init__(
|
self.__init__(
|
||||||
startDate=startDate,
|
startDate=startDate,
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import datetime
|
||||||
|
|
||||||
from db.database import db
|
from db.database import db
|
||||||
from db.db_setup import generate_session
|
from db.db_setup import generate_session
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
|
@ -64,10 +66,17 @@ def get_this_week(session: Session = Depends(generate_session)):
|
||||||
|
|
||||||
|
|
||||||
@router.get("/today", tags=["Meal Plan"])
|
@router.get("/today", tags=["Meal Plan"])
|
||||||
def get_today(session: Session = Depends(generate_session)):
|
def get_today(
|
||||||
|
session: Session = Depends(generate_session),
|
||||||
|
current_user: UserInDB = Depends(manager),
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Returns the recipe slug for the meal scheduled for today.
|
Returns the recipe slug for the meal scheduled for today.
|
||||||
If no meal is scheduled nothing is returned
|
If no meal is scheduled nothing is returned
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return get_todays_meal(session)
|
group_in_db: GroupInDB = db.groups.get(session, current_user.group, "name")
|
||||||
|
recipe = get_todays_meal(session, group_in_db)
|
||||||
|
print(datetime.date.today())
|
||||||
|
|
||||||
|
return recipe.slug
|
||||||
|
|
|
@ -1,14 +1,23 @@
|
||||||
from datetime import date, timedelta
|
from datetime import date, timedelta, timezone
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
import pytz
|
||||||
from db.database import db
|
from db.database import db
|
||||||
|
from db.db_setup import create_session
|
||||||
|
from pydantic.tools import T
|
||||||
from schema.meal import MealIn, MealOut, MealPlanIn, MealPlanInDB, MealPlanProcessed
|
from schema.meal import MealIn, MealOut, MealPlanIn, MealPlanInDB, MealPlanProcessed
|
||||||
from schema.recipe import Recipe
|
from schema.recipe import Recipe
|
||||||
|
from schema.user import GroupInDB
|
||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
|
|
||||||
def process_meals(session: Session, meal_plan_base: MealPlanIn) -> MealPlanProcessed:
|
def process_meals(session: Session, meal_plan_base: MealPlanIn) -> MealPlanProcessed:
|
||||||
meals = []
|
meals = []
|
||||||
for x, meal in enumerate(meal_plan_base.meals):
|
for x, meal in enumerate(meal_plan_base.meals):
|
||||||
|
# europe = pytz.timezone("America/Anchorage")
|
||||||
|
# d = europe.localize(meal_plan_base.startDate)
|
||||||
|
# print(d)
|
||||||
|
|
||||||
meal: MealIn
|
meal: MealIn
|
||||||
try:
|
try:
|
||||||
recipe: Recipe = db.recipes.get(session, meal.slug)
|
recipe: Recipe = db.recipes.get(session, meal.slug)
|
||||||
|
@ -37,10 +46,35 @@ def process_meals(session: Session, meal_plan_base: MealPlanIn) -> MealPlanProce
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_todays_meal(session):
|
def get_todays_meal(session: Session, group: Union[int, GroupInDB]) -> Recipe:
|
||||||
meal_plan: MealPlanInDB = db.groups.get(session, limit=1, order_by="startDate")
|
"""Returns the given mealplan for today based off the group. If the group
|
||||||
|
Type is of type int, then a query will be made to the database to get the
|
||||||
|
grop object."
|
||||||
|
|
||||||
for meal in meal_plan.meals:
|
Args:
|
||||||
meal: MealOut
|
session (Session): SqlAlchemy Session
|
||||||
if meal.date == date.today():
|
group (Union[int, GroupInDB]): Either the id of the group or the GroupInDB Object
|
||||||
return meal.slug
|
|
||||||
|
Returns:
|
||||||
|
Recipe: Pydantic Recipe Object
|
||||||
|
"""
|
||||||
|
session = session if session else create_session()
|
||||||
|
|
||||||
|
if isinstance(group, int):
|
||||||
|
group: GroupInDB = db.groups.get(session, group)
|
||||||
|
|
||||||
|
if group.webhook_enable:
|
||||||
|
today_slug = None
|
||||||
|
|
||||||
|
for mealplan in group.mealplans:
|
||||||
|
mealplan: MealPlanInDB
|
||||||
|
for meal in mealplan.meals:
|
||||||
|
meal: MealOut
|
||||||
|
if meal.date == date.today():
|
||||||
|
today_slug = meal.slug
|
||||||
|
break
|
||||||
|
|
||||||
|
if today_slug:
|
||||||
|
return db.recipes.get(session, today_slug)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
|
@ -1,35 +1,20 @@
|
||||||
import json
|
|
||||||
from datetime import date
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from db.database import db
|
from db.database import db
|
||||||
from db.db_setup import create_session
|
from db.db_setup import create_session
|
||||||
from schema.meal import MealOut, MealPlanInDB
|
|
||||||
from schema.user import GroupInDB
|
from schema.user import GroupInDB
|
||||||
|
from services.meal_services import get_todays_meal
|
||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
|
|
||||||
def post_webhooks(group: int, session: Session = None):
|
def post_webhooks(group: int, session: Session = None):
|
||||||
session = session if session else create_session()
|
session = session if session else create_session()
|
||||||
group_settings: GroupInDB = db.groups.get(session, group)
|
group_settings: GroupInDB = db.groups.get(session, group)
|
||||||
|
todays_recipe = get_todays_meal(session, group)
|
||||||
|
|
||||||
if group_settings.webhook_enable:
|
if not todays_recipe:
|
||||||
today_slug = None
|
return
|
||||||
|
|
||||||
for mealplan in group_settings.mealplans:
|
for url in group_settings.webhook_urls:
|
||||||
mealplan: MealPlanInDB
|
requests.post(url, json=todays_recipe.json())
|
||||||
for meal in mealplan.meals:
|
|
||||||
meal: MealOut
|
|
||||||
if meal.date == date.today():
|
|
||||||
today_slug = meal.slug
|
|
||||||
break
|
|
||||||
|
|
||||||
if not today_slug:
|
|
||||||
return
|
|
||||||
|
|
||||||
todays_meal = db.recipes.get(session, today_slug)
|
|
||||||
|
|
||||||
for url in group_settings.webhook_urls:
|
|
||||||
requests.post(url, json=todays_meal.json())
|
|
||||||
|
|
||||||
session.close()
|
session.close()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue