Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
fix: update-notion-meals script (#2)
Browse files Browse the repository at this point in the history
* chore: improve notion handler

* chore: skip cornjobs on dev

* chore: bump version
  • Loading branch information
sralloza authored Oct 11, 2021
1 parent 3627bec commit db3833d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 32 deletions.
1 change: 1 addition & 0 deletions app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class Settings(BaseSettings):
PRODUCTION: bool = False
ENABLE_PROMETHEUS: bool = False

AWS_ACCESS_KEY_ID: str
Expand Down
18 changes: 18 additions & 0 deletions app/core/notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
from .config import settings


def create_notion_block(content: str, bold=False, italics=False, code=False):
block = {
"type": "text",
"text": {"content": content},
"annotations": {},
}
if bold:
block["annotations"]["bold"] = True
if italics:
block["annotations"]["italics"] = True
if code:
block["annotations"]["code"] = True
if not bold and not italics and not code:
del block["annotations"]

return block


def update_notion_text(blocks: List[Dict]):
headers = {
"Authorization": f"Bearer {settings.NOTION_KEY}",
Expand Down
32 changes: 10 additions & 22 deletions app/cron/update_notion_meals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .. import crud
from ..core.notion import update_notion_text
from ..core.notion import create_notion_block, update_notion_text
from ..deps.database import manual_db
from ..schemas.meal import Meal
from .base import scheduler
Expand All @@ -16,28 +16,16 @@ def update_notion_meals():
tomorrow_meal = Meal.from_orm(tomorrow_meal) if tomorrow_meal else None

blocks = []
blocks.append(create_notion_block("Hoy\n", bold=True))
if today_meal:
blocks.append(
{
"type": "text",
"text": {"content": "Hoy\n"},
"annotations": {"bold": True},
}
)
blocks.append(
{"type": "text", "text": {"content": today_meal.describe(indent=2)}}
)
blocks.extend(today_meal.to_notion_blocks())

blocks.append(create_notion_block("\nMañana\n", bold=True))
if tomorrow_meal:
title = "\n" * bool(blocks) + "Mañana"
blocks.append(
{
"type": "text",
"text": {"content": f"{title}\n"},
"annotations": {"bold": True},
}
)
blocks.append(
{"type": "text", "text": {"content": tomorrow_meal.describe(indent=2)}}
)
blocks.extend(tomorrow_meal.to_notion_blocks())

if len(blocks) <= 2:
print("warning: not blocks detected in cron-script update-notion-meals")
return

update_notion_text(blocks)
5 changes: 5 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def get_application():
@_app.on_event("startup")
def on_startup():
create_db_and_tables()

if not settings.PRODUCTION:
print("Warning: development env, skipping cronjobs")
return

scheduler.print_jobs()
scheduler.start()
update_notion_meals()
Expand Down
27 changes: 18 additions & 9 deletions app/schemas/meal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from pydantic import BaseModel, Field, validator

from ..core.notion import create_notion_block


class MealId(BaseModel):
id: date = Field(alias="date")
Expand Down Expand Up @@ -34,19 +36,26 @@ def frozen(self) -> List[str]:
out.append(f"{self.dinner.lower()} (D)")
return out

def describe(self, indent: int = 0):
start = " " * indent + "- "
msg = ""
def to_notion_blocks(self):
blocks = []

# Little alias for code reading
_cnb = create_notion_block

if self.lunch1:
msg += f"{start} Comida: {self.lunch1.lower()}"
blocks.append(_cnb(" - Comida: "))
blocks.append(_cnb(self.lunch1.lower(), code=self.lunch1_frozen))
if self.lunch2:
msg += f" y {self.lunch2.lower()}"
blocks.append(_cnb(" y "))
blocks.append(_cnb(self.lunch2.lower(), code=self.lunch2_frozen))

if self.dinner:
if msg:
msg += "\n"
msg += f"{start} Cena: {self.dinner.lower()}"
return msg + "\n"
if blocks:
blocks.append(_cnb("\n"))
blocks.append(_cnb(" - Cena: "))
blocks.append(_cnb(self.dinner.lower(), code=self.dinner_frozen))

return blocks


class Meal(BaseMeal, MealId):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "meal-planner"
version = "1.0.0"
version = "1.0.1"
description = ""
authors = ["Diego Alloza González <[email protected]>"]
packages = [
Expand Down

0 comments on commit db3833d

Please sign in to comment.