diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 0b48af7..aba095b 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -29,10 +29,10 @@ jobs: run: poetry install - name: Run black - run: poetry run black --check app + run: poetry run black --check . - name: Check imports - run: poetry run isort --check app + run: poetry run isort --check . - name: Lint with pylint run: poetry run pylint app diff --git a/README.md b/README.md index 183ac71..d0e7af2 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,10 @@ You need to supply the following environment variables (required ones are marked - šŸš© **MYSQL_PORT** (`str`): mysql port. - šŸš© **MYSQL_USER** (`str`): mysql user. +### Other + +- **LOCALE_WEEKDAY_NAMES** (`list(str)`): weekday names, starting with Sunday and ending with Saturday. Must contain 7 elements (one for each week day). + ## Future AWS and notion settings are currently needed. If you want to use this app without one of them (or both) add an issue and I'll make it optional and configurable. diff --git a/alembic/versions/032390abe0c6_create_views.py b/alembic/versions/032390abe0c6_create_views.py index 00e57c7..5eefe35 100644 --- a/alembic/versions/032390abe0c6_create_views.py +++ b/alembic/versions/032390abe0c6_create_views.py @@ -9,7 +9,6 @@ from alembic import op - # revision identifiers, used by Alembic. revision = "032390abe0c6" down_revision = "f2b0ccdc9f11" diff --git a/app/api/utils.py b/app/api/utils.py index 9aba2e5..44a69d2 100644 --- a/app/api/utils.py +++ b/app/api/utils.py @@ -2,6 +2,7 @@ from fastapi import APIRouter +from ..core.config import Settings, settings from ..utils.misc import get_version from ..utils.responses import Version @@ -12,3 +13,9 @@ def get_version_endpoint(): """Returns the current backend version.""" return Version(version=get_version()) + + +@router.get("/settings", response_model=Settings, summary="Get settings") +def get_settings(): + """Returns the backend settings.""" + return settings diff --git a/app/core/config.py b/app/core/config.py index cef02f5..67ff92f 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -1,10 +1,18 @@ """Config module.""" -from typing import Any, Dict, Optional +import json +from typing import Any, Dict, List, Optional from pydantic import UUID4, BaseSettings, validator +def list_parse_fallback(v): + try: + return json.loads(v) + except json.JSONDecodeError: + return v.split(",") + + class Settings(BaseSettings): # Server API_TOKEN: str @@ -36,6 +44,9 @@ class Settings(BaseSettings): # Defined dinamically DATABASE_URI: str = "" + # Other + LOCALE_WEEKDAY_NAMES: Optional[List[str]] + @validator("DATABASE_URI", pre=True) def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any: if isinstance(v, str): @@ -46,9 +57,17 @@ def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any f"{values.get('MYSQL_PORT')}/{values.get('MYSQL_DATABASE')}" ) + @validator("LOCALE_WEEKDAY_NAMES") + def check_locale_names(cls, v): + if isinstance(v, list): + if len(v) != 7: + raise ValueError("must have 7 elements (one for each week day)") + return v + class Config: case_sensitive = True env_file = ".env" + json_loads = list_parse_fallback settings = Settings() diff --git a/app/cron/update_notion_meals.py b/app/cron/update_notion_meals.py index 97bf3a9..68bd83e 100644 --- a/app/cron/update_notion_meals.py +++ b/app/cron/update_notion_meals.py @@ -1,5 +1,7 @@ """Update notion meals cron sript.""" +from datetime import datetime, timedelta + from .. import crud from ..core.config import settings from ..core.notion import create_notion_block, update_notion_text @@ -8,6 +10,16 @@ from .base import scheduler +def get_weekday(delta_days: int = 0) -> str: + """Return the weekday in words given a delta in days.""" + date = datetime.now() + timedelta(days=delta_days) + + if settings.LOCALE_WEEKDAY_NAMES: + return settings.LOCALE_WEEKDAY_NAMES[int(date.strftime("%w"))] + + return date.strftime("%A") + + # Should fire everyday at 05:00 @scheduler.scheduled_job("cron", id="update-notion-meals", hour="5", minute="0") def update_notion_meals(): @@ -22,21 +34,24 @@ def update_notion_meals(): dat_meal = Meal.from_orm(dat_meal) if dat_meal else None blocks = [] - blocks.append(create_notion_block("Hoy\n", bold=True)) + weekday = get_weekday(0) + blocks.append(create_notion_block(f"Hoy ({weekday})\n", bold=True)) if today_meal: blocks.extend(today_meal.to_notion_blocks()) - blocks.append(create_notion_block("\nMaƱana\n", bold=True)) + weekday = get_weekday(1) + blocks.append(create_notion_block(f"\nMaƱana ({weekday})\n", bold=True)) if tomorrow_meal: blocks.extend(tomorrow_meal.to_notion_blocks()) if settings.NOTION_ADD_DAY_AFTER_TOMORROW: - blocks.append(create_notion_block("\nPasado maƱana\n", bold=True)) + weekday = get_weekday(2) + blocks.append(create_notion_block(f"\nPasado maƱana ({weekday})\n", bold=True)) if dat_meal: blocks.extend(dat_meal.to_notion_blocks()) if len(blocks) <= 3: - print("warning: not blocks detected in cron-script update-notion-meals") + print("warning: no blocks detected in cron-script update-notion-meals") return update_notion_text(blocks) diff --git a/docker-compose.yaml b/docker-compose.yaml index 7ca9ab7..1b6e084 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,6 +6,12 @@ services: env_file: - .env environment: + - MYSQL_USER=root + - MYSQL_PASSWORD=root + - MYSQL_HOST=database + - MYSQL_PORT=3306 + - MYSQL_DATABASE=meal-planner + # If you don't have this environment variables in your # terminal, put them in the .env file - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} @@ -16,5 +22,6 @@ services: database: image: mysql:5.7 - env_file: - - .env + environment: + - MYSQL_ROOT_PASSWORD=root + - MYSQL_DATABASE=meal-planner diff --git a/pyproject.toml b/pyproject.toml index c69f228..6e4ee57 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "meal-planner" -version = "1.4.0" +version = "1.5.0" description = "" authors = ["Diego Alloza GonzĆ”lez "] packages = [