mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-19 05:09:40 +02:00
* add basic pre-commit file * add flake8 * add isort * add pep585-upgrade (typing upgrades) * use namespace for import * add mypy * update ci for backend * flake8 scope * fix version format * update makefile * disable strict option (temporary) * fix mypy issues * upgrade type hints (pre-commit) * add vscode typing check * add types to dev deps * remote container draft * update setup script * update compose version * run setup on create * dev containers update * remove unused pages * update setup tips * expose ports * Update pre-commit to include flask8-print (#1053) * Add in flake8-print to pre-commit * pin version of flake8-print * formatting * update getting strated docs * add mypy to pre-commit * purge .mypy_cache on clean * drop mypy Co-authored-by: zackbcom <zackbcom@users.noreply.github.com>
58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
import json
|
|
|
|
import requests
|
|
from sqlalchemy.orm.session import Session
|
|
|
|
from mealie.core import root_logger
|
|
from mealie.db.db_setup import create_session
|
|
from mealie.repos.all_repositories import get_repositories
|
|
from mealie.schema.group.webhook import ReadWebhook
|
|
|
|
from ..scheduled_func import Cron, ScheduledFunc
|
|
from ..scheduler_service import SchedulerService
|
|
|
|
logger = root_logger.get_logger()
|
|
|
|
|
|
def post_webhooks(webhook_id: int, session: Session = None):
|
|
session = session or create_session()
|
|
db = get_repositories(session)
|
|
webhook: ReadWebhook = db.webhooks.get_one(webhook_id)
|
|
|
|
if not webhook.enabled:
|
|
logger.info(f"Skipping webhook {webhook_id}. reasons: is disabled")
|
|
return
|
|
|
|
todays_recipe = db.meals.get_today(webhook.group_id)
|
|
|
|
if not todays_recipe:
|
|
return
|
|
|
|
payload = json.loads([x.json(by_alias=True) for x in todays_recipe]) # type: ignore
|
|
response = requests.post(webhook.url, json=payload)
|
|
|
|
if response.status_code != 200:
|
|
logger.error(f"Error posting webhook to {webhook.url} ({response.status_code})")
|
|
|
|
session.close()
|
|
|
|
|
|
def update_group_webhooks():
|
|
session = create_session()
|
|
db = get_repositories(session)
|
|
|
|
webhooks: list[ReadWebhook] = db.webhooks.get_all()
|
|
|
|
for webhook in webhooks:
|
|
cron = Cron.parse(webhook.time)
|
|
|
|
job_func = ScheduledFunc(
|
|
id=webhook.id,
|
|
name=f"Group {webhook.group_id} webhook",
|
|
callback=post_webhooks,
|
|
hour=cron.hours,
|
|
minute=cron.minutes,
|
|
args=(webhook.id),
|
|
)
|
|
|
|
SchedulerService.add_cron_job(job_func)
|