def get_todays_meal(session: Session, group: Union[int, GroupInDB]) -> Recipe: """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." Args: session (Session): SqlAlchemy Session group (Union[int, GroupInDB]): Either the id of the group or the GroupInDB Object Returns: Recipe: Pydantic Recipe Object """ session = session or create_session() if isinstance(group, int): group: GroupInDB = db.groups.get(session, group) 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
def update_webhook_schedule(): """ A scheduled background job that runs every 30 minutes to poll the database for changes and reschedule the webhook time """ session = create_session() all_groups: list[GroupInDB] = db.groups.get_all(session) for group in all_groups: time = cron_parser(group.webhook_time) job = JOB_STORE.get(group.name) if not job: logger.error(f"No job found for group: {group.name}") logger.info(f"Creating scheduled task for {group.name}") JOB_STORE.update(add_group_to_schedule(scheduler, group)) continue scheduler.reschedule_job( job.scheduled_task.id, trigger="cron", hour=time.hours, minute=time.minutes, ) session.close() logger.info(scheduler.print_jobs())
def auto_backup_job(): for backup in app_dirs.BACKUP_DIR.glob("Auto*.zip"): backup.unlink() templates = [template for template in app_dirs.TEMPLATE_DIR.iterdir()] session = create_session() backup_all(session=session, tag="Auto", templates=templates) logger.info("Auto Backup Called")
def init_webhook_schedule(scheduler, job_store: dict): session = create_session() all_groups: list[GroupInDB] = db.groups.get_all(session) for group in all_groups: job_store.update(add_group_to_schedule(scheduler, group)) session.close() return job_store
def init_db(db: Session = None) -> None: if not db: db = create_session() default_group_init(db) default_settings_init(db) default_theme_init(db) default_user_init(db) db.close()
def main(): session = create_session() init_user = db.users.get(session, "1", "id") if init_user: print("Database Exists") else: print("Database Doesn't Exists, Initializing...") init_db() create_general_event("Initialize Database", "Initialize database with default values", session)
def save_event(title, text, category, session: Session, attachment=None): event = Event(title=title, text=text, category=category) session = session or create_session() db.events.create(session, event.dict()) notification_objects = db.event_notifications.get(session=session, match_value=True, match_key=category, limit=9999) notification_urls = [x.notification_url for x in notification_objects] post_notifications(event, notification_urls, attachment=attachment)
def purge_events_database(): """ Ran daily. Purges all events after 100 """ logger.info("Purging Events in Database") expiration_days = 7 limit = datetime.datetime.now() - datetime.timedelta(days=expiration_days) session = create_session() session.query(Event).filter(Event.time_stamp <= limit).delete() session.commit() session.close() logger.info("Events Purges")
def post_webhooks(group: int, session: Session = None): session = session or create_session() group_settings: GroupInDB = db.groups.get(session, group) if not group_settings.webhook_enable: return todays_recipe = get_todays_meal(session, group) if not todays_recipe: return for url in group_settings.webhook_urls: requests.post(url, json=todays_recipe.json()) session.close()
def post_webhooks(group: int, session: Session = None, force=True): session = session or create_session() group_settings: GroupInDB = db.groups.get(session, group) if not group_settings.webhook_enable and not force: return todays_recipe = get_todays_meal(session, group) if not todays_recipe: return for url in group_settings.webhook_urls: requests.post(url, json=json.loads(todays_recipe.json(by_alias=True))) create_scheduled_event( "Meal Plan Webhook", f"Meal plan webhook executed for group '{group}'") session.close()
def validate_slugs_in_database(session: Session = None): def check_image_path(image_name: str, slug_path: str) -> bool: existing_path: Path = app_dirs.IMG_DIR.joinpath(image_name) slug_path: Path = app_dirs.IMG_DIR.joinpath(slug_path) if existing_path.is_dir(): slug_path.rename(existing_path) else: logger.info("No Image Found") session = session or create_session() all_recipes = db.recipes.get_all(session) slugs_and_images = [(x.slug, x.image) for x in all_recipes] for slug, image in slugs_and_images: image_slug = image.split(".")[0] # Remove Extension if slug != image_slug: logger.info(f"{slug}, Doesn't Match '{image_slug}'") check_image_path(image, slug)
def init_webhook_schedule(scheduler, job_store: dict): session = create_session() all_groups: list[GroupInDB] = db.groups.get_all(session) for group in all_groups: cron = cron_parser(group.webhook_time) job_store.update({ group.name: ScheduledFunction( scheduler, post_webhooks, cron=cron, name=group.name, args=[group.id], ) }) session.close() return job_store
def update_webhook_schedule(): """ A scheduled background job that runs every 30 minutes to poll the database for changes and reschedule the webhook time """ session = create_session() all_groups: list[GroupInDB] = db.groups.get_all(session) for group in all_groups: time = cron_parser(group.webhook_time) job = JOB_STORE.get(group.name) scheduler.reschedule_job( job.scheduled_task.id, trigger="cron", hour=time.hours, minute=time.minutes, ) session.close() logger.info(scheduler.print_jobs())