From 4d76a2bc9c88d8671892a783ef4c3764fbbf64da Mon Sep 17 00:00:00 2001 From: Joren de Graaf Date: Fri, 23 Oct 2026 14:30:58 +0200 Subject: [PATCH] recipe links --- .gitignore | 1 + autoremove-preview.txt | 115 ++++ main/MCP.md | 2 +- main/README.md | 11 + .../tasknest-home-assistant-sync.service | 9 + .../tasknest-home-assistant-sync.timer | 11 + main/food/api.py | 5 +- main/food/api_urls.py | 4 + main/food/test_api.py | 27 +- main/groceries/static/groceries/groceries.css | 1 + main/groceries/templates/groceries/index.html | 2 +- main/groceries/tests.py | 14 + main/groceries/views.py | 6 +- main/home_assistant/README.md | 13 + main/home_assistant/__init__.py | 0 main/home_assistant/client.py | 202 ++++++++ main/home_assistant/management/__init__.py | 1 + .../management/commands/__init__.py | 1 + .../commands/sync_home_assistant.py | 14 + .../home_assistant/migrations/0001_initial.py | 25 + main/home_assistant/migrations/0002_sync.py | 28 + .../migrations/0003_background_history.py | 22 + .../migrations/0004_task_lists.py | 20 + main/home_assistant/migrations/__init__.py | 0 main/home_assistant/models.py | 80 +++ main/home_assistant/sync.py | 362 +++++++++++++ main/home_assistant/task_sync.py | 357 +++++++++++++ .../templates/home_assistant/preview.html | 17 + .../templates/home_assistant/settings.html | 47 ++ .../home_assistant/task_preview.html | 17 + main/home_assistant/tests.py | 490 ++++++++++++++++++ main/home_assistant/urls.py | 9 + main/home_assistant/views.py | 219 ++++++++ main/mcp_server/server.py | 61 ++- main/mcp_server/tests.py | 15 + main/planner/middleware_demo.py | 2 +- .../templates/planner/user_settings.html | 2 +- main/recipes/api.py | 181 ++++++- main/recipes/migrations/0007_recipelink.py | 35 ++ main/recipes/models.py | 38 ++ main/recipes/static/recipes/recipe_picker.js | 33 ++ main/recipes/static/recipes/recipes.css | 3 + main/recipes/templates/recipes/compose.html | 3 + main/recipes/templates/recipes/library.html | 4 + main/recipes/templates/recipes/link_form.html | 3 + main/recipes/test_api.py | 34 ++ main/recipes/tests.py | 38 +- main/recipes/urls.py | 4 + main/recipes/views.py | 126 ++++- main/requirements.txt | 1 + main/tasksnest/settings.py | 1 + main/tasksnest/urls.py | 1 + 52 files changed, 2698 insertions(+), 19 deletions(-) create mode 100644 autoremove-preview.txt create mode 100644 main/deploy/systemd/tasknest-home-assistant-sync.service create mode 100644 main/deploy/systemd/tasknest-home-assistant-sync.timer create mode 100644 main/home_assistant/README.md create mode 100644 main/home_assistant/__init__.py create mode 100644 main/home_assistant/client.py create mode 100644 main/home_assistant/management/__init__.py create mode 100644 main/home_assistant/management/commands/__init__.py create mode 100644 main/home_assistant/management/commands/sync_home_assistant.py create mode 100644 main/home_assistant/migrations/0001_initial.py create mode 100644 main/home_assistant/migrations/0002_sync.py create mode 100644 main/home_assistant/migrations/0003_background_history.py create mode 100644 main/home_assistant/migrations/0004_task_lists.py create mode 100644 main/home_assistant/migrations/__init__.py create mode 100644 main/home_assistant/models.py create mode 100644 main/home_assistant/sync.py create mode 100644 main/home_assistant/task_sync.py create mode 100644 main/home_assistant/templates/home_assistant/preview.html create mode 100644 main/home_assistant/templates/home_assistant/settings.html create mode 100644 main/home_assistant/templates/home_assistant/task_preview.html create mode 100644 main/home_assistant/tests.py create mode 100644 main/home_assistant/urls.py create mode 100644 main/home_assistant/views.py create mode 100644 main/recipes/migrations/0007_recipelink.py create mode 100644 main/recipes/static/recipes/recipe_picker.js create mode 100644 main/recipes/templates/recipes/compose.html create mode 100644 main/recipes/templates/recipes/link_form.html diff --git a/.gitignore b/.gitignore index 94d6be2..8ef1a2e 100644 --- a/.gitignore +++ b/.gitignore @@ -64,4 +64,5 @@ Thumbs.db *.sqlite3 playwright/.auth/ +.playwright-mcp/ #AGENTS.md diff --git a/autoremove-preview.txt b/autoremove-preview.txt new file mode 100644 index 0000000..1c66cab --- /dev/null +++ b/autoremove-preview.txt @@ -0,0 +1,115 @@ + +WARNING: apt does not have a stable CLI interface. Use with caution in scripts. + +Reading package lists... +Building dependency tree... +Reading state information... +Starting pkgProblemResolver with broken count: 0 +Starting 2 pkgProblemResolver with broken count: 0 +Done +The following packages will be REMOVED: + anthy anthy-common anthy-unicode-common docker-neon fonts-liberation2 + gimp-data graphviz hunspell-fr kasumi kinit libamd3 libann0 + libanthy-unicode-0t64 libanthy1t64 libanthyinput0t64 libbabl-0.1-0 libcamd3 + libccolamd3 libcdt5 libcgraph6 libchewing3 libchewing3-data libcholmod5 + libcolamd3 libddcutil4 libfwupd2 libgegl-0.4-0t64 libgegl-common libgexiv2-2 + libgimp2.0t64 libgts-0.7-5t64 libgts-bin libgvc6 libgvpr2 liblab-gamut1 + libmaliit-plugins2 libmbim-proxy libmypaint-1.5-1 libmypaint-common + libpathplan4 libpinyin-data libpinyin15 libprotobuf-c1 libpyside6-py3-6.10 + libqmi-proxy libqt5feedback5 libqt5glib-2.0-0 libqt5gstreamer-1.0-0 + libqt5gstreamerquick-1.0-0 libqt5multimedia5 libqt5multimediaquick5 + libqt5opengl5t64 libroc0.3 libshiboken6-py3-6.10 libsuitesparseconfig7 + libumfpack6 libwireplumber-0.4-0 libwmf-0.2-7 libwmflite-0.2-7 + linux-headers-6.17.0-29-generic linux-hwe-6.17-headers-6.17.0-29 + linux-hwe-6.17-tools-6.17.0-29 linux-image-6.17.0-29-generic + linux-modules-6.17.0-29-generic linux-modules-extra-6.17.0-29-generic + linux-tools-6.17.0-29-generic maliit-framework maliit-keyboard + plasma-integration5 plasma-workspace-wayland + qml-module-org-kde-qqc2desktopstyle qml-module-org-kde-sonnet + qml-module-org-kde-syntaxhighlighting qml-module-qtfeedback + qml-module-qtgstreamer qml-module-qtmultimedia + qml-module-qtquick-localstorage qt6-virtualkeyboard-plugin + qtgstreamer-plugins-qt5 sddm sddm-theme-breeze sddm-theme-maya +0 upgraded, 0 newly installed, 82 to remove and 383 not upgraded. +Remv anthy [1:0.4-3build1] +Remv libanthyinput0t64 [1:0.4-3build1] +Remv kasumi [2.5+debian1-1build2] +Remv libanthy1t64 [1:0.4-3build1] +Remv anthy-common [1:0.4-3build1] +Remv maliit-keyboard [2.3.1+p24.04+vrelease+git20250831.2310-0] +Remv libanthy-unicode-0t64 [1.0.0.20240502-1+24.04+noble+release+build5] +Remv anthy-unicode-common [1.0.0.20240502-1+24.04+noble+release+build5] +Remv docker-neon [0.0+p24.04+vrelease+git20240819.0606-0] +Remv fonts-liberation2 [1:2.1.5-3] +Remv gimp-data [2.10.36-3ubuntu0.24.04.1] +Remv graphviz [2.42.2-9ubuntu0.1] +Remv hunspell-fr [1:7.0-1] +Remv kinit [5.116.0-1zneon+24.04+noble+release+build3] +Remv libgimp2.0t64 [2.10.36-3ubuntu0.24.04.1] +Remv libgegl-0.4-0t64 [1:0.4.48-2.4build2] +Remv libumfpack6 [1:7.6.1+dfsg-1build1] +Remv libcholmod5 [1:7.6.1+dfsg-1build1] +Remv libamd3 [1:7.6.1+dfsg-1build1] +Remv libann0 [1.1.2+doc-9build1] +Remv libbabl-0.1-0 [1:0.1.108-1] +Remv libcamd3 [1:7.6.1+dfsg-1build1] +Remv libccolamd3 [1:7.6.1+dfsg-1build1] +Remv libgvpr2 [2.42.2-9ubuntu0.1] +Remv libgvc6 [2.42.2-9ubuntu0.1] +Remv libcdt5 [2.42.2-9ubuntu0.1] [libcgraph6:amd64 ] +Remv libcgraph6 [2.42.2-9ubuntu0.1] +Remv libchewing3 [0.6.0-1build1] +Remv libchewing3-data [0.6.0-1build1] +Remv libcolamd3 [1:7.6.1+dfsg-1build1] +Remv libddcutil4 [1.4.1-1build2] +Remv libfwupd2 [1.9.34-0ubuntu1~24.04.1] +Remv libgegl-common [1:0.4.48-2.4build2] +Remv libgexiv2-2 [0.14.2-2build3] +Remv libgts-bin [0.7.6+darcs121130-5.2build1] +Remv libgts-0.7-5t64 [0.7.6+darcs121130-5.2build1] +Remv liblab-gamut1 [2.42.2-9ubuntu0.1] +Remv maliit-framework [2.3.0+p24.04+vrelease+git20250831.2250-0] +Remv libmaliit-plugins2 [2.3.0+p24.04+vrelease+git20250831.2250-0] +Remv libmbim-proxy [1.32.0-1+24.04+noble+release+build1] +Remv libmypaint-1.5-1 [1.6.0-2build2] +Remv libmypaint-common [1.6.0-2build2] +Remv libpathplan4 [2.42.2-9ubuntu0.1] +Remv libpinyin15 [2.8.1-1build3] +Remv libpinyin-data [2.8.1-1build3] +Remv libprotobuf-c1 [1.4.1-1ubuntu4] +Remv libpyside6-py3-6.10 [6.11.1-0zneon+24.04+noble+release+build89] +Remv libqmi-proxy [1.36.0-1+24.04+noble+release+build1] +Remv qml-module-qtfeedback [5.0+p24.04+vrelease+git20250829.0151-0] +Remv libqt5feedback5 [5.0+p24.04+vrelease+git20250829.0151-0] +Remv qml-module-qtgstreamer [1.2.0-5.2build4] +Remv libqt5gstreamerquick-1.0-0 [1.2.0-5.2build4] +Remv libqt5gstreamer-1.0-0 [1.2.0-5.2build4] +Remv libqt5glib-2.0-0 [1.2.0-5.2build4] +Remv qml-module-qtmultimedia [5.15.17+p24.04+vrelease+git20250828.1415-0] +Remv libqt5multimediaquick5 [5.15.17+p24.04+vrelease+git20250828.1415-0] +Remv libqt5multimedia5 [5.15.17+p24.04+vrelease+git20250828.1415-0] +Remv qtgstreamer-plugins-qt5 [1.2.0-5.2build4] +Remv libqt5opengl5t64 [5.15.17+p24.04+vrelease+git20250828.1236-0] +Remv libroc0.3 [0.3.0+dfsg-7ubuntu2] +Remv libshiboken6-py3-6.10 [6.11.1-0zneon+24.04+noble+release+build89] +Remv libsuitesparseconfig7 [1:7.6.1+dfsg-1build1] +Remv libwireplumber-0.4-0 [0.4.17-1ubuntu4.1] +Remv libwmf-0.2-7 [0.2.13-1.1build3] +Remv libwmflite-0.2-7 [0.2.13-1.1build3] +Remv linux-headers-6.17.0-29-generic [6.17.0-29.29~24.04.1] +Remv linux-hwe-6.17-headers-6.17.0-29 [6.17.0-29.29~24.04.1] +Remv linux-tools-6.17.0-29-generic [6.17.0-29.29~24.04.1] +Remv linux-hwe-6.17-tools-6.17.0-29 [6.17.0-29.29~24.04.1] +Remv linux-image-6.17.0-29-generic [6.17.0-29.29~24.04.1] +Remv linux-modules-extra-6.17.0-29-generic [6.17.0-29.29~24.04.1] +Remv linux-modules-6.17.0-29-generic [6.17.0-29.29~24.04.1] +Remv plasma-integration5 [6.7.5-0zneon+24.04+noble+release+build60] +Remv plasma-workspace-wayland [4:6.7.5-0zneon+24.04+noble+release+build95] +Remv qml-module-org-kde-qqc2desktopstyle [5.116.1-0zneon+24.04+noble+release+build4] +Remv qml-module-org-kde-sonnet [5.116.0-1zneon+24.04+noble+release+build2] +Remv qml-module-org-kde-syntaxhighlighting [5.116.0-1zneon+24.04+noble+release+build3] +Remv qml-module-qtquick-localstorage [5.15.17+p24.04+vrelease+git20250828.1340-0] +Remv qt6-virtualkeyboard-plugin [6.11.1-0zneon+24.04+noble+release+build27] +Remv sddm-theme-maya [0.21.0-6+24.04+noble+release+build32] +Remv sddm-theme-breeze [4:6.7.5-0zneon+24.04+noble+release+build95] +Remv sddm [0.21.0-6+24.04+noble+release+build32] diff --git a/main/MCP.md b/main/MCP.md index 54170e1..531dc22 100644 --- a/main/MCP.md +++ b/main/MCP.md @@ -110,7 +110,7 @@ The corresponding HTTP routes are listed in `GET /api/v1/schema/`. Recipe meal d ### Bulk inventory housekeeping -`inventory_bulk_update` accepts 1–20 `{item_id, changes}` entries. `changes` uses the same fields and stock rules as `inventory_update`; omitted fields stay unchanged. Optional `expected_updated_at` rejects an item that changed since it was read. `dry_run` defaults to true and returns per-item before/after snapshots without saving. Set `dry_run: false` and provide an `idempotency_key` to apply. The default allows valid entries to succeed when others fail; `atomic: true` rolls back the whole batch on any failure. Duplicate IDs and oversized batches are rejected before writes. +`inventory_bulk_update` accepts 1–50 `{item_id, changes}` entries. `changes` uses the same fields and stock rules as `inventory_update`; omitted fields stay unchanged. Optional `expected_updated_at` rejects an item that changed since it was read. `dry_run` defaults to true and returns per-item before/after snapshots without saving. Set `dry_run: false` and provide an `idempotency_key` to apply. The default allows valid entries to succeed when others fail; `atomic: true` rolls back the whole batch on any failure. Duplicate IDs and oversized batches are rejected before writes. For example, preview two location changes with `items: [{"item_id": "…", "changes": {"location": "Metalen stellingkast"}}, {"item_id": "…", "changes": {"location": "Zwarte stellingkast"}}]`. Use the returned IDs and current item timestamps for execution. To correct expiry dates, set `best_before` to `YYYY-MM-DD` (or `null` to clear it). diff --git a/main/README.md b/main/README.md index 1219874..55e1ec1 100644 --- a/main/README.md +++ b/main/README.md @@ -106,6 +106,17 @@ sudo systemctl daemon-reload sudo systemctl enable --now tasknest-fasting-notifications.timer ``` +Home Assistant grocery and task syncing use a separate timer. After deploying the app, +install `deploy/systemd/tasknest-home-assistant-sync.service` and +`deploy/systemd/tasknest-home-assistant-sync.timer` the same way. The command +checks due connections every minute; users must enable an interval in settings: + +```sh +sudo cp main/deploy/systemd/tasknest-home-assistant-sync.{service,timer} /etc/systemd/system/ +sudo systemctl daemon-reload +sudo systemctl enable --now tasknest-home-assistant-sync.timer +``` + Run database migrations before enabling the timer. Adjust the service's user and paths for your deployment. diff --git a/main/deploy/systemd/tasknest-home-assistant-sync.service b/main/deploy/systemd/tasknest-home-assistant-sync.service new file mode 100644 index 0000000..ef13529 --- /dev/null +++ b/main/deploy/systemd/tasknest-home-assistant-sync.service @@ -0,0 +1,9 @@ +[Unit] +Description=Run due Tasknest Home Assistant grocery syncs +After=network.target + +[Service] +Type=oneshot +User=jorenmartijn +WorkingDirectory=/home/jorenmartijn/django/main +ExecStart=/home/jorenmartijn/django/tasksnest-host/bin/python manage.py sync_home_assistant diff --git a/main/deploy/systemd/tasknest-home-assistant-sync.timer b/main/deploy/systemd/tasknest-home-assistant-sync.timer new file mode 100644 index 0000000..dcdef23 --- /dev/null +++ b/main/deploy/systemd/tasknest-home-assistant-sync.timer @@ -0,0 +1,11 @@ +[Unit] +Description=Periodically sync TasksNest grocery items with Home Assistant + +[Timer] +OnBootSec=1min +OnUnitActiveSec=5min +Persistent=true +Unit=tasknest-home-assistant-sync.service + +[Install] +WantedBy=timers.target diff --git a/main/food/api.py b/main/food/api.py index f7a49e1..00122ba 100644 --- a/main/food/api.py +++ b/main/food/api.py @@ -751,8 +751,9 @@ def _inventory_validation(field, reason): def _inventory_batch_input(request, data, *, deleting=False): rows = data.get('item_ids' if deleting else 'items') - if not isinstance(rows, list) or not 1 <= len(rows) <= 20: - raise ValueError('Provide between 1 and 20 inventory items.') + limit = 20 if deleting else 50 + if not isinstance(rows, list) or not 1 <= len(rows) <= limit: + raise ValueError(f'Provide between 1 and {limit} inventory items.') if set(data) - ({'item_ids', 'dry_run', 'atomic', 'confirm'} if deleting else {'items', 'dry_run', 'atomic'}): raise ValueError('Unknown bulk inventory fields.') if any(type(data.get(key, default)) is not bool for key, default in (('dry_run', False), ('atomic', False))): diff --git a/main/food/api_urls.py b/main/food/api_urls.py index 1d7e85e..a98855e 100644 --- a/main/food/api_urls.py +++ b/main/food/api_urls.py @@ -42,7 +42,11 @@ urlpatterns = [ path('recipes/cook-later/', recipe_api.recipe_cook_later_collection), path('recipes/cook-later//', recipe_api.recipe_cook_later_item), path('recipes/create/', recipe_api.recipe_create), + path('recipes/compositions/preview/', recipe_api.recipe_composition_preview), + path('recipes/compositions/', recipe_api.recipe_composition_create), path('recipes//', recipe_api.recipe_item), + path('recipes//links/', recipe_api.recipe_links), + path('recipes//links//', recipe_api.recipe_link_item), path('recipes//variations/', recipe_api.recipe_variation), path('recipes//ingredients/', recipe_api.recipe_ingredient_create), path('recipes//ingredients//', recipe_api.recipe_ingredient_item), diff --git a/main/food/test_api.py b/main/food/test_api.py index 6db4f2f..8593276 100644 --- a/main/food/test_api.py +++ b/main/food/test_api.py @@ -112,7 +112,7 @@ class FoodApiTests(TestCase): path = '/api/v1/kitchen/bulk-update/' row = {'item_id': str(item.api_id), 'changes': {'notes': 'new'}} self.assertEqual(self.call('post', path, {'items': [row, row], 'dry_run': False}, HTTP_IDEMPOTENCY_KEY='dup').status_code, 400) - self.assertEqual(self.call('post', path, {'items': [row] * 21, 'dry_run': False}, HTTP_IDEMPOTENCY_KEY='large').status_code, 400) + self.assertEqual(self.call('post', path, {'items': [row] * 51, 'dry_run': False}, HTTP_IDEMPOTENCY_KEY='large').status_code, 400) self.assertEqual(self.call('post', path, {'items': [row], 'dry_run': False}).json()['error']['code'], 'idempotency_key_required') result = self.call('post', path, {'items': [{**row, 'expected_updated_at': '2000-01-01T00:00:00+00:00'}], 'dry_run': False}, HTTP_IDEMPOTENCY_KEY='stale') @@ -120,16 +120,31 @@ class FoodApiTests(TestCase): item.refresh_from_db() self.assertEqual(item.notes, '') - def test_bulk_inventory_accepts_twenty_updates_in_one_request(self): + def test_bulk_inventory_accepts_twenty_three_updates_in_one_request(self): location = InventoryLocation.objects.create(user=self.a, name='Pantry') items = [KitchenInventoryItem.objects.create(user=self.a, product=self.product_a, location=location) - for _ in range(20)] + for _ in range(23)] payload = {'items': [{'item_id': str(item.api_id), 'changes': {'notes': 'Checked'}} for item in items], 'dry_run': False} - result = self.call('post', '/api/v1/kitchen/bulk-update/', payload, HTTP_IDEMPOTENCY_KEY='twenty-items') + path = '/api/v1/kitchen/bulk-update/' + preview = self.call('post', path, {**payload, 'dry_run': True}) + self.assertEqual(preview.json()['counts']['validated'], 23) + self.assertEqual(KitchenInventoryItem.objects.filter(user=self.a, notes='Checked').count(), 0) + result = self.call('post', path, payload, HTTP_IDEMPOTENCY_KEY='twenty-three-items') self.assertEqual(result.status_code, 200, result.content) - self.assertEqual(result.json()['counts']['changed'], 20) - self.assertEqual(KitchenInventoryItem.objects.filter(user=self.a, notes='Checked').count(), 20) + self.assertEqual(result.json()['counts']['changed'], 23) + self.assertEqual(KitchenInventoryItem.objects.filter(user=self.a, notes='Checked').count(), 23) + self.assertEqual(self.call('post', path, payload, HTTP_IDEMPOTENCY_KEY='twenty-three-items').json(), result.json()) + + def test_bulk_inventory_rejects_more_than_fifty_distinct_items(self): + location = InventoryLocation.objects.create(user=self.a, name='Pantry') + items = [KitchenInventoryItem.objects.create(user=self.a, product=self.product_a, location=location) + for _ in range(51)] + payload = {'items': [{'item_id': str(item.api_id), 'changes': {'notes': 'Checked'}} for item in items], + 'dry_run': False, 'atomic': True} + result = self.call('post', '/api/v1/kitchen/bulk-update/', payload, HTTP_IDEMPOTENCY_KEY='over-limit') + self.assertEqual(result.status_code, 400) + self.assertEqual(KitchenInventoryItem.objects.filter(user=self.a, notes='Checked').count(), 0) def test_bulk_inventory_delete_preview_confirm_and_history(self): location = InventoryLocation.objects.create(user=self.a, name='Pantry') diff --git a/main/groceries/static/groceries/groceries.css b/main/groceries/static/groceries/groceries.css index 8e4e80b..8c93dbc 100644 --- a/main/groceries/static/groceries/groceries.css +++ b/main/groceries/static/groceries/groceries.css @@ -1 +1,2 @@ .grocery-page{max-width:1050px;margin:0 auto;padding:clamp(1rem,3vw,2.5rem);color:var(--text,#21372c)}.grocery-page h1{font-size:clamp(2rem,4vw,3rem);margin:.2rem 0}.grocery-kicker{text-transform:uppercase;letter-spacing:.12em;font-weight:700}.grocery-panel{background:var(--surface,#fff);border:1px solid #d3ddd5;border-radius:1rem;padding:clamp(1rem,2vw,1.5rem);margin:1.25rem 0;box-shadow:0 6px 24px #172a2010}.grocery-panel h2{margin:0 0 1rem}.grocery-add,.grocery-filter,.grocery-edit{display:flex;flex-wrap:wrap;align-items:end;gap:.75rem}.grocery-add label,.grocery-filter label,.grocery-edit label{display:grid;gap:.3rem;min-width:120px;flex:1}.grocery-add label:first-child{flex:2;min-width:200px}.grocery-page input,.grocery-page select,.grocery-page textarea{box-sizing:border-box;width:100%;padding:.65rem;border:1px solid #a7b7a9;border-radius:.5rem;background:var(--surface,#fff);color:inherit;font:inherit}.grocery-page button{padding:.65rem 1rem;border:0;border-radius:.5rem;background:#315d4d;color:white;font:inherit;cursor:pointer}.grocery-page button:focus-visible,.grocery-page a:focus-visible,.grocery-page input:focus-visible,.grocery-page select:focus-visible,.grocery-page summary:focus-visible{outline:3px solid #bd7319;outline-offset:2px}.grocery-panel-head{display:flex;justify-content:space-between;align-items:center;gap:1rem}.grocery-page .grocery-text-button{background:transparent;color:#315d4d;text-decoration:underline;padding:.4rem}.grocery-group h3{border-bottom:1px solid #d3ddd5;padding-bottom:.4rem}.grocery-group ul,.grocery-preview-list{list-style:none;padding:0}.grocery-item{display:flex;align-items:start;gap:.8rem;border-bottom:1px solid #e2e8e2;padding:.8rem 0}.grocery-item-body{flex:1;min-width:0}.grocery-item-body strong,.grocery-item-body span,.grocery-item-body small{display:block}.grocery-item-body small{color:#52655b}.grocery-item-body details{margin-top:.4rem}.grocery-check{min-width:2.7rem}.grocery-uncertain{display:block;color:#8a4400;font-weight:600}.grocery-confirm{display:flex;gap:.5rem;align-items:center;margin:1rem 0}.grocery-confirm input{width:auto}.grocery-preview-list li{padding:.8rem 0;border-bottom:1px solid #d3ddd5}@media(max-width:600px){.grocery-page{padding:1rem}.grocery-add,.grocery-filter,.grocery-edit{display:grid;grid-template-columns:1fr 1fr}.grocery-add label:first-child,.grocery-add button,.grocery-edit label:first-child,.grocery-edit button{grid-column:1/-1}.grocery-item{gap:.4rem}.grocery-page .grocery-text-button{font-size:.9rem}} +.grocery-page-header{display:flex;align-items:center;justify-content:space-between;gap:1.25rem}.grocery-sync-action{display:grid;justify-items:start;gap:.35rem;flex:none}.grocery-sync-button{display:inline-flex;align-items:center;justify-content:center;min-height:44px;padding:.65rem 1rem;border-radius:.5rem;background:#315d4d;color:#fff;text-decoration:none;font-weight:700}.grocery-sync-action small{color:#52655b}@media(max-width:600px){.grocery-page-header{align-items:flex-start;flex-direction:column}.grocery-sync-action{width:100%}.grocery-sync-button{width:100%;box-sizing:border-box}} diff --git a/main/groceries/templates/groceries/index.html b/main/groceries/templates/groceries/index.html index 4b05449..6de3c54 100644 --- a/main/groceries/templates/groceries/index.html +++ b/main/groceries/templates/groceries/index.html @@ -2,7 +2,7 @@ {% block title %}Groceries · Tasknest{% endblock %} {% block content %} -

Food

Grocery list

Plan what to buy. Marking an item purchased does not add it to Kitchen Inventory.

+

Food

Grocery list

Plan what to buy. Marking an item purchased does not add it to Kitchen Inventory.

{% if ha_grocery_sync_available %}
Sync with Home AssistantReview changes before confirming.
{% endif %}

Quick add

{% csrf_token %}

Items

{% csrf_token %}
{% include 'groceries/partials/list.html' %}
{% endblock %} diff --git a/main/groceries/tests.py b/main/groceries/tests.py index d266164..fa36125 100644 --- a/main/groceries/tests.py +++ b/main/groceries/tests.py @@ -8,6 +8,7 @@ from django.conf import settings from food.models import FoodProduct from food.api import create_token +from home_assistant.models import HomeAssistantConnection from recipes.models import Recipe, RecipeIngredient from .models import GroceryItem from .services import GroceryError, add, add_purchased_to_inventory, apply_recipe, edit, recipe_preview, search @@ -71,6 +72,19 @@ class GroceryTests(TestCase): RecipeIngredient.objects.create(recipe=recipe, name='Water', quantity=200, unit='ml') self.assertContains(self.client.get(f'/groceries/recipe/{recipe.api_id}/preview/'), 'Review groceries') + @override_settings(MIDDLEWARE=[item for item in settings.MIDDLEWARE if not item.startswith('whitenoise.')], + SECURE_SSL_REDIRECT=False) + def test_grocery_page_shows_home_assistant_sync_action_when_list_is_configured(self): + self.client.force_login(self.user) + response = self.client.get('/groceries/') + self.assertNotContains(response, 'Sync with Home Assistant') + HomeAssistantConnection.objects.create(user=self.user, base_url='https://ha.example', + encrypted_token='encrypted', grocery_entity_id='todo.shopping') + response = self.client.get('/groceries/') + self.assertContains(response, 'Sync with Home Assistant') + self.assertContains(response, 'Review changes before confirming.') + self.assertContains(response, 'href="/settings/home-assistant/preview/"') + def test_inventory_transfer_requires_purchase_and_is_single_use(self): item = add(self.user, name='Rice', quantity='500', unit='g') product = FoodProduct.objects.create(owner=self.user, product_name='Rice') diff --git a/main/groceries/views.py b/main/groceries/views.py index b4f5663..382200d 100644 --- a/main/groceries/views.py +++ b/main/groceries/views.py @@ -5,6 +5,8 @@ from django.shortcuts import get_object_or_404, redirect, render from django.views.decorators.http import require_POST from recipes.models import Recipe +from home_assistant.models import HomeAssistantConnection +from planner.demo_policy import is_demo_user from .models import GroceryItem from food.models import FoodProduct from .services import (GroceryError, add, add_purchased_to_inventory, apply_recipe, @@ -17,7 +19,9 @@ def _page(request, status=200): items = list(search(request.user, query=request.GET.get('q', ''), status=selected).order_by('category', 'status', 'name')) except GroceryError: selected, items = 'needed', list(search(request.user)) - context = {'items': items, 'selected_status': selected, 'query': request.GET.get('q', '')} + connection = None if is_demo_user(request.user) else HomeAssistantConnection.objects.filter(user=request.user).first() + context = {'items': items, 'selected_status': selected, 'query': request.GET.get('q', ''), + 'ha_grocery_sync_available': bool(connection and connection.grocery_entity_id)} template = 'groceries/partials/list.html' if request.headers.get('HX-Request') == 'true' else 'groceries/index.html' return render(request, template, context, status=status) diff --git a/main/home_assistant/README.md b/main/home_assistant/README.md new file mode 100644 index 0000000..287ed57 --- /dev/null +++ b/main/home_assistant/README.md @@ -0,0 +1,13 @@ +# Home Assistant integration + +This integration connects one user's TasksNest grocery list and, optionally, one planner task list to separate Home Assistant `todo.*` entities. The user configures the URL and long-lived access token at `/settings/home-assistant/`, tests the connection, discovers entities, chooses lists, and reviews signed previews before each list's first sync. A preview expires after ten minutes and is recalculated before writes. Later syncs can be manual or run every 5, 15, or 30 minutes after the grocery first sync is confirmed. + +Home Assistant is reached through its REST API: `todo.get_items` uses `POST /api/services/todo/get_items?return_response`; `todo.add_item` and `todo.update_item` use the corresponding service paths. Responses are parsed from `service_response[entity_id].items`, and mappings use the returned item UID. The selected entity's `supported_features` controls whether create and update actions are offered. See the [REST API](https://developers.home-assistant.io/docs/api/rest/), [get-items action](https://www.home-assistant.io/actions/todo.get_items/), and [to-do feature flags](https://developers.home-assistant.io/docs/core/entity/todo/). + +Grocery sync shares only item name and needed/purchased state. New Home Assistant items become one-piece groceries. TasksNest quantity, unit, category, notes, product, inventory link, and recipe provenance stay local. The optional task sync shares title, completion, description, and scheduled date when the chosen Home Assistant entity advertises support. Task exports require Home Assistant create and due-date support; imports without a due date are skipped because TasksNest tasks require a scheduled date. Only active, top-level, non-recurring planner tasks sync. Recurrence, subtasks, priority, weather rules, and deferrals stay in TasksNest. A missing item is reported; it is never deleted on the other side. Equal titles without a UID mapping are flagged for review and not merged automatically. The default conflict winner is Home Assistant when both mapped sides changed. One direction modes apply the chosen side's syncable fields. + +The sync uses per-user and per-entity UID mappings with local and remote fingerprints. A database lease prevents concurrent runs; at most twenty changes are applied per run. Automatic retries back off and stop after five consecutive failures; the user can re-enable them after reviewing the error. If an outbound add may have succeeded but its UID cannot be confirmed, its mapping remains pending. The next preview attempts UID reconciliation from a unique matching name but never blindly sends another add. A still-uncertain add can be reset only after the user confirms they checked the remote list. Recent run history and failure backoff are stored per connection. + +Tokens are encrypted at rest with a Fernet key derived from Django's deployment `SECRET_KEY`. Rotating that key requires users to replace their Home Assistant tokens. Requests validate the destination, pin a checked DNS result for the socket, verify TLS, reject redirects, and use five-second timeouts. Private-network HTTP is permitted; public destinations require HTTPS. No token value is rendered or logged. + +Automatic sync runs through `manage.py sync_home_assistant`; install `main/deploy/systemd/tasknest-home-assistant-sync.service` and `.timer` on the host to check for due syncs every minute. Automatic syncing is off by default. A live Home Assistant instance was not available during implementation, so API behavior is covered by mocked tests and a targeted browser smoke test with a fake server. diff --git a/main/home_assistant/__init__.py b/main/home_assistant/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/main/home_assistant/client.py b/main/home_assistant/client.py new file mode 100644 index 0000000..c180860 --- /dev/null +++ b/main/home_assistant/client.py @@ -0,0 +1,202 @@ +"""Bounded Home Assistant REST client with validated, pinned destinations.""" +import base64 +import hashlib +import http.client +import ipaddress +import json +import socket +import ssl +from urllib.parse import urlsplit, urlunsplit + +from cryptography.fernet import Fernet, InvalidToken +from django.conf import settings + + +class ConnectionError(ValueError): + pass + + +def _cipher(): + # Derive a distinct encryption key from Django's deployment secret. + key = hashlib.sha256(b'tasksnest-home-assistant-token-v1:' + settings.SECRET_KEY.encode()).digest() + return Fernet(base64.urlsafe_b64encode(key)) + + +def encrypt_token(token): + return _cipher().encrypt(token.encode()).decode() + + +def decrypt_token(value): + try: + return _cipher().decrypt(value.encode()).decode() + except (InvalidToken, UnicodeError): + raise ConnectionError('The saved Home Assistant token could not be read. Replace it in Settings.') from None + + +def _addresses(host, port): + addresses = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM) + if not addresses: + raise ValueError + ips = [ipaddress.ip_address(entry[4][0]) for entry in addresses] + if any(ip.is_loopback or ip.is_link_local or ip.is_multicast or ip.is_unspecified or ip.is_reserved for ip in ips): + raise ValueError + return ips + + +def normalize_url(raw): + try: + parts = urlsplit(raw.strip()) + if parts.scheme not in {'http', 'https'} or not parts.hostname or parts.username or parts.password: + raise ValueError + if parts.path not in {'', '/'} or parts.query or parts.fragment: + raise ValueError + port = parts.port + ips = _addresses(parts.hostname, port or (443 if parts.scheme == 'https' else 80)) + if parts.scheme == 'http' and not all(ip.is_private for ip in ips): + raise ConnectionError('HTTP is allowed only for private network addresses. Use HTTPS for public servers.') + return urlunsplit((parts.scheme, parts.netloc.lower(), '', '', '')) + except ConnectionError: + raise + except (ValueError, OverflowError, socket.gaierror): + raise ConnectionError('Enter a reachable Home Assistant URL without a path, query, or credentials.') from None + + +def _request_json(base_url, token, path, payload=None): + # Connect to the validated IP while retaining the hostname for Host and TLS SNI. + # This prevents a second DNS lookup from redirecting the socket to another address. + base_url = normalize_url(base_url) + parts = urlsplit(base_url) + port = parts.port or (443 if parts.scheme == 'https' else 80) + try: + ips = _addresses(parts.hostname, port) + if parts.scheme == 'http' and not all(address.is_private for address in ips): + raise ValueError + ip = str(ips[0]) + except (ValueError, socket.gaierror): + raise ConnectionError('Home Assistant could not be reached.') from None + connection_type = http.client.HTTPSConnection if parts.scheme == 'https' else http.client.HTTPConnection + kwargs = {'timeout': 5} + if parts.scheme == 'https': + kwargs['context'] = ssl.create_default_context() + connection = connection_type(parts.hostname, port, **kwargs) + connection._create_connection = lambda _address, timeout, source_address=None: socket.create_connection( + (ip, port), timeout, source_address) + try: + body = json.dumps(payload).encode() if payload is not None else None + headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json', 'Host': parts.netloc} + if body is not None: + headers['Content-Type'] = 'application/json' + connection.request('POST' if body is not None else 'GET', path, body=body, headers=headers) + response = connection.getresponse() + if response.status == 401: + raise ConnectionError('Token rejected (401). Check the long-lived access token.') from None + if response.status in {301, 302, 303, 307, 308}: + raise ConnectionError('The server redirected the request. Enter its final URL instead.') + if response.status != 200: + error_body = response.read(4096).decode('utf-8', 'replace').lower() + if response.status >= 500 and 'did not match any entities' in error_body: + raise ConnectionError( + 'The selected Home Assistant to-do list is unavailable. Refresh the discovered lists ' + 'and choose an available list in Home Assistant settings.' + ) from None + raise ConnectionError(f'Home Assistant returned HTTP {response.status}.') + response_body = response.read(1024 * 1024 + 1) + if len(response_body) > 1024 * 1024: + raise ConnectionError('Home Assistant returned too much data.') + return json.loads(response_body) + except (TimeoutError, OSError, ssl.SSLError) as exc: + reason = str(exc).lower() + if 'certificate' in reason or 'ssl' in reason: + raise ConnectionError('TLS certificate verification failed.') from None + if 'timed out' in reason: + raise ConnectionError('Connection timed out after five seconds.') from None + raise ConnectionError('Home Assistant could not be reached.') from None + except ConnectionError: + raise + except (ValueError, UnicodeError): + raise ConnectionError('Home Assistant returned invalid JSON.') from None + finally: + connection.close() + + +def _get_json(base_url, token, path): + return _request_json(base_url, token, path) + + +def test_connection(base_url, token): + data = _get_json(base_url, token, '/api/config') + if not isinstance(data, dict) or not data.get('version'): + raise ConnectionError('This does not look like a Home Assistant server.') + return str(data['version'])[:100] + + +def discover_entities(base_url, token): + data = _get_json(base_url, token, '/api/states') + if not isinstance(data, list): + raise ConnectionError('Home Assistant returned an invalid entity list.') + entities = [] + for state in data: + if not isinstance(state, dict) or not str(state.get('entity_id', '')).startswith('todo.'): + continue + entity_id = str(state['entity_id'])[:255] + attrs = state.get('attributes') or {} + name = str(attrs.get('friendly_name') or entity_id)[:255] if isinstance(attrs, dict) else entity_id + try: + features = int(attrs.get('supported_features', 0)) if isinstance(attrs, dict) else 0 + except (TypeError, ValueError): + features = 0 + entities.append({'id': entity_id, 'name': name, 'features': max(0, features), + 'available': isinstance(state.get('state'), str) and state['state'] != 'unavailable'}) + return sorted(entities, key=lambda entity: entity['name'].lower()) + + +def get_items(base_url, token, entity_id): + if not entity_id.startswith('todo.'): + raise ConnectionError('Choose a valid to-do list.') + response = _request_json(base_url, token, '/api/services/todo/get_items?return_response', + {'entity_id': entity_id, 'status': ['needs_action', 'completed']}) + try: + items = response['service_response'][entity_id]['items'] + if not isinstance(items, list): + raise KeyError + result = {} + for item in items: + uid = item['uid'] + summary = item['summary'] + status = item['status'] + if not isinstance(uid, str) or not uid or len(uid) > 255 or not isinstance(summary, str) or not summary.strip() or len(summary.strip()) > 180 or status not in {'needs_action', 'completed'} or uid in result: + raise KeyError + due = item.get('due') + if due is not None and (not isinstance(due, str) or len(due) > 64): + raise KeyError + description = item.get('description') or '' + if not isinstance(description, str) or len(description) > 10000: + raise KeyError + result[uid] = {'uid': uid, 'name': summary.strip(), 'status': status, + 'due': due, 'description': description} + return result + except (KeyError, TypeError, AttributeError): + raise ConnectionError('Home Assistant returned an incomplete to-do list. No changes were made.') from None + + +def add_item(base_url, token, entity_id, name, *, due_date=None, description=None): + data = {'entity_id': entity_id, 'item': name} + if due_date is not None: + data['due_date'] = due_date + if description: + data['description'] = description + return _request_json(base_url, token, '/api/services/todo/add_item', data) + + +def update_item(base_url, token, entity_id, uid, *, name=None, status=None, due_date=None, + description=None): + data = {'entity_id': entity_id, 'item': uid} + if name is not None: + data['rename'] = name + if status is not None: + data['status'] = status + if due_date is not None: + data['due_date'] = due_date + if description is not None: + data['description'] = description + return _request_json(base_url, token, '/api/services/todo/update_item', data) diff --git a/main/home_assistant/management/__init__.py b/main/home_assistant/management/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/main/home_assistant/management/__init__.py @@ -0,0 +1 @@ + diff --git a/main/home_assistant/management/commands/__init__.py b/main/home_assistant/management/commands/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/main/home_assistant/management/commands/__init__.py @@ -0,0 +1 @@ + diff --git a/main/home_assistant/management/commands/sync_home_assistant.py b/main/home_assistant/management/commands/sync_home_assistant.py new file mode 100644 index 0000000..93f3d23 --- /dev/null +++ b/main/home_assistant/management/commands/sync_home_assistant.py @@ -0,0 +1,14 @@ +from django.core.management.base import BaseCommand + +from home_assistant.sync import run_due_syncs + + +class Command(BaseCommand): + help = 'Run due Home Assistant grocery syncs.' + + def handle(self, *args, **options): + result = run_due_syncs() + self.stdout.write( + f"Home Assistant syncs: {result['succeeded']} succeeded, " + f"{result['failed']} failed, {result['attempted']} attempted." + ) diff --git a/main/home_assistant/migrations/0001_initial.py b/main/home_assistant/migrations/0001_initial.py new file mode 100644 index 0000000..0fca1e3 --- /dev/null +++ b/main/home_assistant/migrations/0001_initial.py @@ -0,0 +1,25 @@ +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + initial = True + dependencies = [migrations.swappable_dependency(settings.AUTH_USER_MODEL)] + operations = [ + migrations.CreateModel( + name='HomeAssistantConnection', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('base_url', models.URLField(max_length=500)), + ('encrypted_token', models.TextField()), + ('grocery_entity_id', models.CharField(blank=True, max_length=255)), + ('entities', models.JSONField(blank=True, default=list)), + ('version', models.CharField(blank=True, max_length=100)), + ('last_tested_at', models.DateTimeField(blank=True, null=True)), + ('last_error', models.CharField(blank=True, max_length=255)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='home_assistant_connection', to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/main/home_assistant/migrations/0002_sync.py b/main/home_assistant/migrations/0002_sync.py new file mode 100644 index 0000000..d706d9d --- /dev/null +++ b/main/home_assistant/migrations/0002_sync.py @@ -0,0 +1,28 @@ +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [('home_assistant', '0001_initial'), ('groceries', '0003_groceryitem_inventory_transfer_and_more')] + operations = [ + migrations.AddField(model_name='homeassistantconnection', name='direction', field=models.CharField(choices=[('both', 'Two-way'), ('from_ha', 'Home Assistant to TasksNest'), ('to_ha', 'TasksNest to Home Assistant')], default='both', max_length=10)), + migrations.AddField(model_name='homeassistantconnection', name='conflict_winner', field=models.CharField(choices=[('ha', 'Home Assistant'), ('tasksnest', 'TasksNest')], default='ha', max_length=10)), + migrations.AddField(model_name='homeassistantconnection', name='initial_sync_confirmed', field=models.BooleanField(default=False)), + migrations.AddField(model_name='homeassistantconnection', name='sync_lock_until', field=models.DateTimeField(blank=True, null=True)), + migrations.AddField(model_name='homeassistantconnection', name='last_synced_at', field=models.DateTimeField(blank=True, null=True)), + migrations.AddField(model_name='homeassistantconnection', name='last_sync_error', field=models.CharField(blank=True, max_length=255)), + migrations.CreateModel(name='HomeAssistantItemMap', fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('entity_id', models.CharField(max_length=255)), + ('ha_uid', models.CharField(blank=True, max_length=255)), + ('last_local_hash', models.CharField(blank=True, max_length=64)), + ('last_remote_hash', models.CharField(blank=True, max_length=64)), + ('pending_create', models.BooleanField(default=False)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('connection', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='item_maps', to='home_assistant.homeassistantconnection')), + ('grocery', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='groceries.groceryitem')), + ], options={'constraints': [ + models.UniqueConstraint(fields=('connection', 'entity_id', 'grocery'), name='ha_unique_grocery_mapping'), + models.UniqueConstraint(condition=~models.Q(ha_uid=''), fields=('connection', 'entity_id', 'ha_uid'), name='ha_unique_uid_mapping'), + ]}), + ] diff --git a/main/home_assistant/migrations/0003_background_history.py b/main/home_assistant/migrations/0003_background_history.py new file mode 100644 index 0000000..5127be3 --- /dev/null +++ b/main/home_assistant/migrations/0003_background_history.py @@ -0,0 +1,22 @@ +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [('home_assistant', '0002_sync')] + operations = [ + migrations.AddField(model_name='homeassistantconnection', name='auto_sync_enabled', field=models.BooleanField(default=False)), + migrations.AddField(model_name='homeassistantconnection', name='sync_interval_minutes', field=models.CharField(choices=[('manual', 'Manual only'), ('5', 'Every 5 minutes'), ('15', 'Every 15 minutes'), ('30', 'Every 30 minutes')], default='15', max_length=6)), + migrations.AddField(model_name='homeassistantconnection', name='next_sync_at', field=models.DateTimeField(blank=True, null=True)), + migrations.AddField(model_name='homeassistantconnection', name='consecutive_sync_failures', field=models.PositiveSmallIntegerField(default=0)), + migrations.CreateModel(name='HomeAssistantSyncRun', fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('succeeded', models.BooleanField(default=False)), + ('automatic', models.BooleanField(default=False)), + ('changes_applied', models.PositiveSmallIntegerField(default=0)), + ('summary', models.CharField(blank=True, max_length=255)), + ('error', models.CharField(blank=True, max_length=255)), + ('connection', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sync_runs', to='home_assistant.homeassistantconnection')), + ], options={'ordering': ('-created_at', '-pk')}), + ] diff --git a/main/home_assistant/migrations/0004_task_lists.py b/main/home_assistant/migrations/0004_task_lists.py new file mode 100644 index 0000000..3090ba3 --- /dev/null +++ b/main/home_assistant/migrations/0004_task_lists.py @@ -0,0 +1,20 @@ +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ('home_assistant', '0003_background_history'), + ('planner', '0006_assign_unambiguous_legacy_task_owners'), + ] + + operations = [ + migrations.AddField(model_name='homeassistantconnection', name='task_entity_id', + field=models.CharField(blank=True, max_length=255)), + migrations.AddField(model_name='homeassistantconnection', name='task_initial_sync_confirmed', + field=models.BooleanField(default=False)), + migrations.AddField(model_name='homeassistantitemmap', name='task', field=models.ForeignKey( + blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='planner.task')), + migrations.AddConstraint(model_name='homeassistantitemmap', constraint=models.UniqueConstraint( + fields=('connection', 'entity_id', 'task'), name='ha_unique_task_mapping')), + ] diff --git a/main/home_assistant/migrations/__init__.py b/main/home_assistant/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/main/home_assistant/models.py b/main/home_assistant/models.py new file mode 100644 index 0000000..307dc0d --- /dev/null +++ b/main/home_assistant/models.py @@ -0,0 +1,80 @@ +from django.conf import settings +from django.db import models + + +class HomeAssistantConnection(models.Model): + class Direction(models.TextChoices): + BOTH = 'both', 'Two-way' + FROM_HA = 'from_ha', 'Home Assistant to TasksNest' + TO_HA = 'to_ha', 'TasksNest to Home Assistant' + + class ConflictWinner(models.TextChoices): + HA = 'ha', 'Home Assistant' + TASKSNEST = 'tasksnest', 'TasksNest' + + class SyncInterval(models.TextChoices): + MANUAL = 'manual', 'Manual only' + FIVE = '5', 'Every 5 minutes' + FIFTEEN = '15', 'Every 15 minutes' + THIRTY = '30', 'Every 30 minutes' + + user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, + related_name='home_assistant_connection') + base_url = models.URLField(max_length=500) + encrypted_token = models.TextField() + grocery_entity_id = models.CharField(max_length=255, blank=True) + task_entity_id = models.CharField(max_length=255, blank=True) + task_initial_sync_confirmed = models.BooleanField(default=False) + direction = models.CharField(max_length=10, choices=Direction.choices, default=Direction.BOTH) + conflict_winner = models.CharField(max_length=10, choices=ConflictWinner.choices, default=ConflictWinner.HA) + auto_sync_enabled = models.BooleanField(default=False) + sync_interval_minutes = models.CharField(max_length=6, choices=SyncInterval.choices, default=SyncInterval.FIFTEEN) + initial_sync_confirmed = models.BooleanField(default=False) + sync_lock_until = models.DateTimeField(null=True, blank=True) + last_synced_at = models.DateTimeField(null=True, blank=True) + last_sync_error = models.CharField(max_length=255, blank=True) + next_sync_at = models.DateTimeField(null=True, blank=True) + consecutive_sync_failures = models.PositiveSmallIntegerField(default=0) + entities = models.JSONField(default=list, blank=True) + version = models.CharField(max_length=100, blank=True) + last_tested_at = models.DateTimeField(null=True, blank=True) + last_error = models.CharField(max_length=255, blank=True) + updated_at = models.DateTimeField(auto_now=True) + + def __str__(self): + return f'Home Assistant connection for user {self.user_id}' + + +class HomeAssistantSyncRun(models.Model): + connection = models.ForeignKey(HomeAssistantConnection, on_delete=models.CASCADE, related_name='sync_runs') + created_at = models.DateTimeField(auto_now_add=True) + succeeded = models.BooleanField(default=False) + automatic = models.BooleanField(default=False) + changes_applied = models.PositiveSmallIntegerField(default=0) + summary = models.CharField(max_length=255, blank=True) + error = models.CharField(max_length=255, blank=True) + + class Meta: + ordering = ('-created_at', '-pk') + + +class HomeAssistantItemMap(models.Model): + connection = models.ForeignKey(HomeAssistantConnection, on_delete=models.CASCADE, related_name='item_maps') + entity_id = models.CharField(max_length=255) + grocery = models.ForeignKey('groceries.GroceryItem', on_delete=models.SET_NULL, null=True, blank=True) + task = models.ForeignKey('planner.Task', on_delete=models.SET_NULL, null=True, blank=True) + ha_uid = models.CharField(max_length=255, blank=True) + last_local_hash = models.CharField(max_length=64, blank=True) + last_remote_hash = models.CharField(max_length=64, blank=True) + pending_create = models.BooleanField(default=False) + updated_at = models.DateTimeField(auto_now=True) + + class Meta: + constraints = [ + models.UniqueConstraint(fields=['connection', 'entity_id', 'grocery'], + name='ha_unique_grocery_mapping'), + models.UniqueConstraint(fields=['connection', 'entity_id', 'task'], + name='ha_unique_task_mapping'), + models.UniqueConstraint(fields=['connection', 'entity_id', 'ha_uid'], + condition=~models.Q(ha_uid=''), name='ha_unique_uid_mapping'), + ] diff --git a/main/home_assistant/sync.py b/main/home_assistant/sync.py new file mode 100644 index 0000000..9644477 --- /dev/null +++ b/main/home_assistant/sync.py @@ -0,0 +1,362 @@ +"""Conservative UID-based grocery reconciliation. Never deletes on either side.""" +import hashlib +import json +from datetime import timedelta + +from django.db import transaction +from django.db.models import Q +from django.utils import timezone + +from groceries.models import GroceryItem +from groceries.services import add as add_grocery, edit as edit_grocery + +from .client import ConnectionError, add_item, decrypt_token, get_items, update_item +from .models import HomeAssistantConnection, HomeAssistantItemMap, HomeAssistantSyncRun + + +def _fingerprint(name, status): + return hashlib.sha256(json.dumps([name, status], ensure_ascii=False).encode()).hexdigest() + + +def _local_state(item): + return {'name': item.name, 'status': 'completed' if item.status == GroceryItem.Status.PURCHASED else 'needs_action'} + + +def _hash_state(state): + return _fingerprint(state['name'], state['status']) + + +def _snapshot(connection): + if not connection.grocery_entity_id: + raise ConnectionError('Choose a Home Assistant grocery list first.') + token = decrypt_token(connection.encrypted_token) + remote = get_items(connection.base_url, token, connection.grocery_entity_id) + local = {str(item.pk): item for item in GroceryItem.objects.filter(user=connection.user)} + maps = list(HomeAssistantItemMap.objects.filter(connection=connection, + entity_id=connection.grocery_entity_id).select_related('grocery')) + return token, local, remote, maps + + +def build_plan(connection, local, remote, maps): + operations = [] + issues = [] + pending_recoveries = [] + mapped_local = {str(link.grocery_id) for link in maps if link.grocery_id} + mapped_remote = {link.ha_uid for link in maps if link.ha_uid} + can_pull = connection.direction in {'both', 'from_ha'} + can_push = connection.direction in {'both', 'to_ha'} + entity = next((entry for entry in connection.entities if entry.get('id') == connection.grocery_entity_id), {}) + features = int(entity.get('features') or 0) + can_create = bool(features & 1) + can_update = bool(features & 4) + + for link in sorted(maps, key=lambda item: item.pk): + item = local.get(str(link.grocery_id)) if link.grocery_id else None + ha = remote.get(link.ha_uid) if link.ha_uid else None + if link.pending_create: + if not item: + issues.append('An unfinished Home Assistant addition lost its TasksNest item; review it manually.') + continue + candidates = [value for uid, value in remote.items() if uid not in mapped_remote and value['name'].casefold() == item.name.casefold()] + if len(candidates) == 1: + operations.append({'kind': 'reconcile', 'local_id': str(item.pk), 'uid': candidates[0]['uid'], 'label': item.name}) + else: + issues.append(f'Addition of “{item.name}” is uncertain; review Home Assistant before retrying.') + if not candidates: + pending_recoveries.append({'mapping_id': link.pk, 'label': item.name}) + continue + if not item or not ha: + issues.append(f'Mapped item {link.ha_uid or "unknown"} is missing on one side; no deletion will occur.') + continue + local_state = _local_state(item) + local_hash = _hash_state(local_state) + remote_hash = _hash_state(ha) + if local_hash == remote_hash: + if link.last_local_hash != local_hash or link.last_remote_hash != remote_hash: + operations.append({'kind': 'baseline', 'local_id': str(item.pk), 'uid': link.ha_uid, 'label': item.name}) + continue + local_changed = local_hash != link.last_local_hash + remote_changed = remote_hash != link.last_remote_hash + if connection.direction == 'from_ha': + winner = 'ha' + elif connection.direction == 'to_ha': + winner = 'tasksnest' + elif local_changed and remote_changed: + winner = connection.conflict_winner + elif remote_changed: + winner = 'ha' + elif local_changed: + winner = 'tasksnest' + else: + # Inconsistent prior snapshots must be reviewed, not guessed. + issues.append(f'“{item.name}” has inconsistent sync history; no change was made.') + continue + if winner == 'ha' and can_pull: + operations.append({'kind': 'pull', 'local_id': str(item.pk), 'uid': link.ha_uid, 'label': ha['name']}) + elif winner == 'tasksnest' and can_push: + if can_update: + operations.append({'kind': 'push', 'local_id': str(item.pk), 'uid': link.ha_uid, 'label': item.name}) + else: + issues.append(f'“{item.name}” cannot be updated: this Home Assistant list is read-only.') + + reconciled_uids = {action['uid'] for action in operations if action['kind'] == 'reconcile'} + unmatched_local = [item for key, item in local.items() if key not in mapped_local] + unmatched_remote = [item for uid, item in remote.items() if uid not in mapped_remote and uid not in reconciled_uids] + local_names = {item.name.casefold() for item in local.values()} + remote_names = {item['name'].casefold() for item in remote.values()} + collisions = ({item.name.casefold() for item in unmatched_local} & remote_names) | ( + {item['name'].casefold() for item in unmatched_remote} & local_names) + for name in sorted(collisions): + issues.append(f'“{name}” exists on both lists without a UID mapping; review duplicates before syncing it.') + if can_push: + for item in sorted(unmatched_local, key=lambda row: str(row.pk)): + if item.name.casefold() not in collisions: + if can_create and (item.status == GroceryItem.Status.NEEDED or can_update): + operations.append({'kind': 'export', 'local_id': str(item.pk), 'label': item.name}) + else: + issues.append(f'“{item.name}” cannot be added: this Home Assistant list lacks create or update support.') + if can_pull: + for uid, item in sorted(remote.items()): + if uid not in mapped_remote and uid not in reconciled_uids and item['name'].casefold() not in collisions: + operations.append({'kind': 'import', 'uid': uid, 'label': item['name']}) + + state = { + 'connection': connection.pk, 'entity': connection.grocery_entity_id, + 'direction': connection.direction, 'winner': connection.conflict_winner, + 'features': features, + 'local': sorted((key, _hash_state(_local_state(item))) for key, item in local.items()), + 'remote': sorted((uid, _hash_state(item)) for uid, item in remote.items()), + 'maps': sorted((link.pk, str(link.grocery_id), link.ha_uid, link.last_local_hash, + link.last_remote_hash, link.pending_create) for link in maps), + } + digest = hashlib.sha256(json.dumps(state, sort_keys=True).encode()).hexdigest() + return {'operations': operations, 'issues': issues, 'pending_recoveries': pending_recoveries, 'digest': digest, + 'limited': len(operations) > 20} + + +def preview(connection): + _, local, remote, maps = _snapshot(connection) + return build_plan(connection, local, remote, maps) + + +def _mapping(connection, item, uid='', *, pending=False, local_hash='', remote_hash=''): + return HomeAssistantItemMap.objects.create( + connection=connection, entity_id=connection.grocery_entity_id, grocery=item, + ha_uid=uid, pending_create=pending, last_local_hash=local_hash, + last_remote_hash=remote_hash) + + +def _record_run(connection, *, succeeded, automatic, changes=0, summary='', error=''): + HomeAssistantSyncRun.objects.create(connection=connection, succeeded=succeeded, + automatic=automatic, changes_applied=changes, summary=str(summary)[:255], error=str(error)[:255]) + old_ids = list(connection.sync_runs.order_by('-created_at', '-pk').values_list('pk', flat=True)[20:]) + if old_ids: + HomeAssistantSyncRun.objects.filter(pk__in=old_ids).delete() + + +def _schedule_after_success(connection, now): + connection.consecutive_sync_failures = 0 + connection.last_synced_at = now + connection.last_sync_error = '' + if connection.auto_sync_enabled and connection.sync_interval_minutes != 'manual': + connection.next_sync_at = now + timedelta(minutes=int(connection.sync_interval_minutes)) + else: + connection.next_sync_at = None + + +def _schedule_after_failure(connection, error, automatic): + connection.last_sync_error = str(error)[:255] + connection.consecutive_sync_failures = min(connection.consecutive_sync_failures + 1, 16) + exhausted = connection.consecutive_sync_failures >= 5 + if exhausted: + connection.auto_sync_enabled = False + connection.next_sync_at = None + elif connection.auto_sync_enabled and connection.sync_interval_minutes != 'manual': + backoff = min(60, 5 * (2 ** min(connection.consecutive_sync_failures - 1, 4))) + connection.next_sync_at = timezone.now() + timedelta(minutes=backoff) + connection.save(update_fields=['last_sync_error', 'consecutive_sync_failures', 'next_sync_at', + 'auto_sync_enabled', 'updated_at']) + _record_run(connection, succeeded=False, automatic=automatic, error=error, + summary=('Automatic sync disabled after five consecutive failures.' if exhausted else + 'Automatic sync failed.' if automatic else 'Sync stopped after an error.')) + + +def _apply(connection, token, action, local, remote): + kind = action['kind'] + item = local.get(action.get('local_id')) + uid = action.get('uid', '') + ha = remote.get(uid) + link = HomeAssistantItemMap.objects.filter(connection=connection, + entity_id=connection.grocery_entity_id, grocery=item).first() if item else None + if kind == 'import': + if not ha: + raise ConnectionError('The Home Assistant list changed. Preview again.') + key = 'ha:' + hashlib.sha256(f'{connection.pk}:{connection.grocery_entity_id}:{uid}'.encode()).hexdigest()[:64] + with transaction.atomic(): + item = add_grocery(connection.user, name=ha['name'], quantity=1, unit='pieces', idempotency_key=key) + if ha['status'] == 'completed' and item.status != GroceryItem.Status.PURCHASED: + edit_grocery(connection.user, item.pk, {'status': GroceryItem.Status.PURCHASED}) + _mapping(connection, item, uid, local_hash=_hash_state(ha), remote_hash=_hash_state(ha)) + return + if not item: + raise ConnectionError('A TasksNest item changed. Preview again.') + local_state = _local_state(item) + if kind == 'export': + link = _mapping(connection, item, pending=True) + add_item(connection.base_url, token, connection.grocery_entity_id, item.name) + latest = get_items(connection.base_url, token, connection.grocery_entity_id) + candidates = [value for candidate_uid, value in latest.items() + if candidate_uid not in remote and value['name'] == item.name] + if len(candidates) != 1: + raise ConnectionError(f'Added “{item.name}”, but its new UID could not be identified. Review the list before retrying.') + uid = candidates[0]['uid'] + link.ha_uid = uid + link.pending_create = False + link.last_remote_hash = _hash_state(candidates[0]) + link.last_local_hash = _hash_state(candidates[0]) + link.save() + if local_state['status'] == 'completed': + update_item(connection.base_url, token, connection.grocery_entity_id, uid, status='completed') + verified = get_items(connection.base_url, token, connection.grocery_entity_id).get(uid) + if not verified or verified['status'] != 'completed': + raise ConnectionError(f'“{item.name}” was added but its completed state was not confirmed.') + link.last_local_hash = _hash_state(local_state) + link.last_remote_hash = _hash_state(verified) + link.save(update_fields=['last_local_hash', 'last_remote_hash', 'updated_at']) + return + if not link or not ha: + raise ConnectionError('A mapped item changed. Preview again.') + if kind == 'reconcile': + link.ha_uid = uid + link.pending_create = False + link.last_local_hash = _hash_state(ha) + link.last_remote_hash = _hash_state(ha) + link.save() + elif kind == 'pull': + changes = {} + if item.name != ha['name']: + changes['name'] = ha['name'] + new_status = GroceryItem.Status.PURCHASED if ha['status'] == 'completed' else GroceryItem.Status.NEEDED + if item.status != new_status: + changes['status'] = new_status + if changes: + edit_grocery(connection.user, item.pk, changes) + link.last_local_hash = _hash_state(ha) + link.last_remote_hash = _hash_state(ha) + link.save(update_fields=['last_local_hash', 'last_remote_hash', 'updated_at']) + elif kind == 'push': + update_item(connection.base_url, token, connection.grocery_entity_id, uid, + name=item.name if item.name != ha['name'] else None, + status=local_state['status'] if local_state['status'] != ha['status'] else None) + verified = get_items(connection.base_url, token, connection.grocery_entity_id).get(uid) + if not verified or _hash_state(verified) != _hash_state(local_state): + raise ConnectionError(f'Home Assistant did not confirm the update to “{item.name}”.') + link.last_local_hash = _hash_state(local_state) + link.last_remote_hash = _hash_state(verified) + link.save(update_fields=['last_local_hash', 'last_remote_hash', 'updated_at']) + elif kind == 'baseline': + link.last_local_hash = _hash_state(local_state) + link.last_remote_hash = _hash_state(ha) + link.save(update_fields=['last_local_hash', 'last_remote_hash', 'updated_at']) + + +def confirm_and_sync(connection, expected_digest, *, automatic=False): + now = timezone.now() + claimed = HomeAssistantConnection.objects.filter(pk=connection.pk).filter( + Q(sync_lock_until__isnull=True) | Q(sync_lock_until__lt=now)).update( + sync_lock_until=now + timedelta(minutes=3)) + if not claimed: + raise ConnectionError('A sync is already running. Try again shortly.') + try: + connection.refresh_from_db() + token, local, remote, maps = _snapshot(connection) + plan = build_plan(connection, local, remote, maps) + if plan['digest'] != expected_digest: + raise ConnectionError('The lists changed after the preview. Review a fresh preview before syncing.') + connection.initial_sync_confirmed = True + connection.save(update_fields=['initial_sync_confirmed', 'updated_at']) + done = 0 + for action in plan['operations'][:20]: + _apply(connection, token, action, local, remote) + done += 1 + # Refresh state so later actions see UIDs and items created in this run. + token, local, remote, _ = _snapshot(connection) + _schedule_after_success(connection, timezone.now()) + connection.save(update_fields=['last_synced_at', 'last_sync_error', 'next_sync_at', + 'consecutive_sync_failures', 'updated_at']) + _record_run(connection, succeeded=True, automatic=automatic, changes=done, + summary=f'Applied {done} change(s); {len(plan["issues"])} item(s) need review.') + return done, plan + except ConnectionError as exc: + _schedule_after_failure(connection, exc, automatic) + raise + finally: + HomeAssistantConnection.objects.filter(pk=connection.pk).update(sync_lock_until=None) + + +def reset_pending_create(connection, expected_digest, mapping_id): + now = timezone.now() + claimed = HomeAssistantConnection.objects.filter(pk=connection.pk).filter( + Q(sync_lock_until__isnull=True) | Q(sync_lock_until__lt=now)).update( + sync_lock_until=now + timedelta(minutes=3)) + if not claimed: + raise ConnectionError('A sync is already running. Try again shortly.') + try: + connection.refresh_from_db() + _, local, remote, maps = _snapshot(connection) + plan = build_plan(connection, local, remote, maps) + if plan['digest'] != expected_digest: + raise ConnectionError('The lists changed after the preview. Review a fresh preview.') + mapping = HomeAssistantItemMap.objects.filter(pk=mapping_id, connection=connection, + entity_id=connection.grocery_entity_id, pending_create=True).select_related('grocery').first() + if not mapping or not mapping.grocery_id: + raise ConnectionError('This pending addition can no longer be reset.') + if any(item['name'].casefold() == mapping.grocery.name.casefold() for item in remote.values()): + raise ConnectionError('A matching item is now visible in Home Assistant. Preview again to reconcile it.') + mapping.delete() + _record_run(connection, succeeded=True, automatic=False, + summary=f'Reset uncertain addition for “{mapping.grocery.name}”.') + finally: + HomeAssistantConnection.objects.filter(pk=connection.pk).update(sync_lock_until=None) + + +def run_due_syncs(): + now = timezone.now() + due = HomeAssistantConnection.objects.filter(auto_sync_enabled=True, + initial_sync_confirmed=True).exclude(sync_interval_minutes='manual').filter( + Q(next_sync_at__isnull=True) | Q(next_sync_at__lte=now)).order_by('next_sync_at', 'pk') + attempted = succeeded = failed = 0 + for connection in due.iterator(): + if connection.sync_lock_until and connection.sync_lock_until > now: + continue + attempted += 1 + grocery_failed = False + try: + plan = preview(connection) + confirm_and_sync(connection, plan['digest'], automatic=True) + succeeded += 1 + except ConnectionError as exc: + grocery_failed = True + failed += 1 + connection.refresh_from_db() + # Preview can fail before confirm_and_sync claims a lock and records history. + recent = connection.sync_runs.filter(created_at__gte=now).exists() + if not recent: + _schedule_after_failure(connection, exc, automatic=True) + # Optional task sync shares the connection's scheduler and lease, but has + # its own first-sync confirmation and item mapping namespace. + connection.refresh_from_db() + if not grocery_failed and connection.task_entity_id and connection.task_initial_sync_confirmed: + runs_before = connection.sync_runs.count() + try: + from .task_sync import confirm_task_sync, preview_tasks + task_plan = preview_tasks(connection) + confirm_task_sync(connection, task_plan['digest'], automatic=True) + succeeded += 1 + attempted += 1 + except ConnectionError as exc: + failed += 1 + connection.refresh_from_db() + if connection.sync_runs.count() == runs_before: + _schedule_after_failure(connection, exc, automatic=True) + return {'attempted': attempted, 'succeeded': succeeded, 'failed': failed} diff --git a/main/home_assistant/task_sync.py b/main/home_assistant/task_sync.py new file mode 100644 index 0000000..d4cf283 --- /dev/null +++ b/main/home_assistant/task_sync.py @@ -0,0 +1,357 @@ +"""Optional synchronization for simple, top-level TasksNest planner tasks.""" +import hashlib +import json +from datetime import date + +from django.db import transaction +from django.db.models import Q +from django.utils import timezone + +from planner.models import Task + +from .client import ConnectionError, add_item, decrypt_token, get_items, update_item +from .models import HomeAssistantConnection, HomeAssistantItemMap +from .sync import _record_run, _schedule_after_failure, _schedule_after_success + +FEATURE_CREATE = 1 +FEATURE_UPDATE = 4 +FEATURE_DUE_DATE = 16 +FEATURE_DESCRIPTION = 64 + + +def _due_date(value): + if not value: + return '' + try: + return date.fromisoformat(value[:10]).isoformat() + except (TypeError, ValueError): + raise ConnectionError('Home Assistant returned an invalid task due date.') from None + + +def _local_state(task): + return { + 'name': task.title, + 'status': 'completed' if task.is_completed else 'needs_action', + 'description': task.description, + 'due': task.scheduled_for.isoformat(), + } + + +def _remote_state(item): + return { + 'name': item['name'], + 'status': item['status'], + 'description': item.get('description', ''), + 'due': _due_date(item.get('due')), + } + + +def _hash(state): + raw = json.dumps(state, ensure_ascii=False, sort_keys=True, separators=(',', ':')) + return hashlib.sha256(raw.encode()).hexdigest() + + +def _snapshot(connection): + if not connection.task_entity_id: + raise ConnectionError('Choose a Home Assistant task list first.') + token = decrypt_token(connection.encrypted_token) + remote = get_items(connection.base_url, token, connection.task_entity_id) + tasks = list(Task.objects.filter(owner=connection.user)) + eligible = {str(task.pk): task for task in tasks + if task.parent_task_id is None and task.is_active and not task.is_recurring} + maps = list(HomeAssistantItemMap.objects.filter(connection=connection, + entity_id=connection.task_entity_id).select_related('task')) + return token, tasks, eligible, remote, maps + + +def build_task_plan(connection, tasks, eligible, remote, maps): + operations, issues, recoveries = [], [], [] + features = next((int(e.get('features') or 0) for e in connection.entities + if e.get('id') == connection.task_entity_id), 0) + can_create = bool(features & FEATURE_CREATE) + can_update = bool(features & FEATURE_UPDATE) + can_due = bool(features & FEATURE_DUE_DATE) + can_description = bool(features & FEATURE_DESCRIPTION) + mapped_local = {str(link.task_id) for link in maps if link.task_id} + mapped_remote = {link.ha_uid for link in maps if link.ha_uid} + + for task in tasks: + if task.parent_task_id is not None: + issues.append(f'“{task.title}” is a subtask and is not synced as a separate item.') + elif not task.is_active: + issues.append(f'“{task.title}” is inactive and is not synced.') + elif task.is_recurring: + issues.append(f'“{task.title}” recurs; recurring tasks are not synced as single list items.') + + for link in sorted(maps, key=lambda row: row.pk): + task = eligible.get(str(link.task_id)) if link.task_id else None + ha = remote.get(link.ha_uid) if link.ha_uid else None + if link.pending_create: + if not task: + issues.append('An unfinished Home Assistant task addition lost its TasksNest task; review it manually.') + continue + candidates = [row for uid, row in remote.items() + if uid not in mapped_remote and row['name'].casefold() == task.title.casefold()] + if len(candidates) == 1: + operations.append({'kind': 'task_reconcile', 'local_id': str(task.pk), + 'uid': candidates[0]['uid'], 'label': task.title}) + else: + issues.append(f'Addition of “{task.title}” is uncertain; review Home Assistant before retrying.') + if not candidates: + recoveries.append({'mapping_id': link.pk, 'label': task.title}) + continue + if not task or not ha: + issues.append(f'Mapped task {link.ha_uid or "unknown"} is missing on one side; no deletion will occur.') + continue + local_state, remote_state = _local_state(task), _remote_state(ha) + local_hash, remote_hash = _hash(local_state), _hash(remote_state) + if local_hash == remote_hash: + if link.last_local_hash != local_hash or link.last_remote_hash != remote_hash: + operations.append({'kind': 'task_baseline', 'local_id': str(task.pk), + 'uid': link.ha_uid, 'label': task.title}) + continue + local_changed, remote_changed = local_hash != link.last_local_hash, remote_hash != link.last_remote_hash + if connection.direction == 'from_ha': + winner = 'ha' + elif connection.direction == 'to_ha': + winner = 'tasksnest' + elif local_changed and remote_changed: + winner = connection.conflict_winner + elif remote_changed: + winner = 'ha' + elif local_changed: + winner = 'tasksnest' + else: + issues.append(f'“{task.title}” has inconsistent sync history; no change was made.') + continue + if winner == 'ha' and connection.direction != 'to_ha': + if not remote_state['due'] and local_state['due']: + issues.append(f'“{task.title}” has no Home Assistant due date; TasksNest requires a scheduled date, so it was left unchanged.') + elif remote_state['due'] and not can_due and remote_state['due'] != local_state['due']: + issues.append(f'“{task.title}” has a due date this Home Assistant list cannot edit; review it manually.') + elif remote_state['description'] and not can_description and remote_state['description'] != local_state['description']: + issues.append(f'“{task.title}” has a description this Home Assistant list cannot edit; review it manually.') + else: + operations.append({'kind': 'task_pull', 'local_id': str(task.pk), 'uid': link.ha_uid, + 'label': ha['name']}) + elif winner == 'tasksnest' and connection.direction != 'from_ha': + unsupported = ((local_state['due'] != remote_state['due'] and not can_due) + or (local_state['description'] != remote_state['description'] and not can_description)) + if not can_update: + issues.append(f'“{task.title}” cannot be updated: this Home Assistant list is read-only.') + elif unsupported: + issues.append(f'“{task.title}” has fields this Home Assistant list cannot update; no fields were changed.') + else: + operations.append({'kind': 'task_push', 'local_id': str(task.pk), 'uid': link.ha_uid, + 'label': task.title}) + + unmatched = [task for key, task in eligible.items() if key not in mapped_local] + unmatched_remote = [row for uid, row in remote.items() if uid not in mapped_remote] + local_names = {task.title.casefold() for task in eligible.values()} + remote_names = {row['name'].casefold() for row in remote.values()} + collisions = ({task.title.casefold() for task in unmatched} & remote_names) | ( + {row['name'].casefold() for row in unmatched_remote} & local_names) + for title in sorted(collisions): + issues.append(f'“{title}” exists on both lists without a UID mapping; review duplicates before syncing it.') + if connection.direction in {'both', 'to_ha'}: + for task in sorted(unmatched, key=lambda row: str(row.pk)): + state = _local_state(task) + if len(task.title) > 180: + issues.append(f'“{task.title[:80]}…” is longer than Home Assistant supports and was not exported.') + continue + unsupported = (not can_create or (task.is_completed and not can_update) + or (state['due'] and not can_due) + or (state['description'] and not can_description)) + if task.title.casefold() in collisions: + continue + if unsupported: + issues.append(f'“{task.title}” needs create, update, due-date, and description support on the selected list.') + else: + operations.append({'kind': 'task_export', 'local_id': str(task.pk), 'label': task.title}) + if connection.direction in {'both', 'from_ha'}: + for uid, item in sorted(remote.items()): + if uid in mapped_remote or item['name'].casefold() in collisions: + continue + state = _remote_state(item) + if not state['due']: + issues.append(f'“{item["name"]}” has no due date; TasksNest requires a scheduled date, so it was not imported.') + continue + if (state['due'] and not can_due) or (state['description'] and not can_description): + issues.append(f'“{item["name"]}” has fields this list cannot preserve in TasksNest; it was not imported.') + continue + operations.append({'kind': 'task_import', 'uid': uid, 'label': item['name']}) + + snapshot = { + 'connection': connection.pk, 'entity': connection.task_entity_id, + 'direction': connection.direction, 'winner': connection.conflict_winner, 'features': features, + 'local': sorted((key, _hash(_local_state(task))) for key, task in eligible.items()), + 'remote': sorted((uid, _hash(_remote_state(item))) for uid, item in remote.items()), + 'maps': sorted((row.pk, str(row.task_id), row.ha_uid, row.last_local_hash, + row.last_remote_hash, row.pending_create) for row in maps), + } + digest = hashlib.sha256(json.dumps(snapshot, sort_keys=True).encode()).hexdigest() + return {'operations': operations, 'issues': issues, 'pending_recoveries': recoveries, + 'digest': digest, 'limited': len(operations) > 20} + + +def preview_tasks(connection): + _, tasks, eligible, remote, maps = _snapshot(connection) + return build_task_plan(connection, tasks, eligible, remote, maps) + + +def _task_map(connection, task, uid='', *, pending=False, local_hash='', remote_hash=''): + return HomeAssistantItemMap.objects.create(connection=connection, entity_id=connection.task_entity_id, + task=task, ha_uid=uid, pending_create=pending, last_local_hash=local_hash, + last_remote_hash=remote_hash) + + +def _apply_task(connection, token, action, eligible, remote, features): + kind, uid = action['kind'], action.get('uid', '') + task = eligible.get(action.get('local_id')) + ha = remote.get(uid) + link = HomeAssistantItemMap.objects.filter(connection=connection, + entity_id=connection.task_entity_id, task=task).first() if task else None + if kind == 'task_import': + if not ha: + raise ConnectionError('The Home Assistant list changed. Preview again.') + state = _remote_state(ha) + with transaction.atomic(): + task = Task.objects.create(owner=connection.user, title=state['name'], + description=state['description'], scheduled_for=date.fromisoformat(state['due']) if state['due'] else timezone.localdate(), + is_completed=state['status'] == 'completed') + _task_map(connection, task, uid, local_hash=_hash(state), remote_hash=_hash(state)) + return + if not task: + raise ConnectionError('A TasksNest task changed. Preview again.') + state = _local_state(task) + if kind == 'task_export': + link = _task_map(connection, task, pending=True) + add_item(connection.base_url, token, connection.task_entity_id, task.title, + due_date=state['due'] if features & FEATURE_DUE_DATE else None, + description=state['description'] if features & FEATURE_DESCRIPTION else None) + latest = get_items(connection.base_url, token, connection.task_entity_id) + candidates = [row for candidate_uid, row in latest.items() + if candidate_uid not in remote and row['name'] == task.title] + if len(candidates) != 1: + raise ConnectionError(f'Added “{task.title}”, but its new UID could not be identified. Review the list before retrying.') + item = candidates[0] + uid = item['uid'] + link.ha_uid, link.pending_create = uid, False + link.last_local_hash = link.last_remote_hash = _hash(_remote_state(item)) + link.save() + if state['status'] == 'completed': + update_item(connection.base_url, token, connection.task_entity_id, uid, status='completed') + item = get_items(connection.base_url, token, connection.task_entity_id).get(uid) + if not item or item['status'] != 'completed': + raise ConnectionError(f'“{task.title}” was added but its completed state was not confirmed.') + remote_state = _remote_state(item) + link.last_local_hash = _hash(state) + link.last_remote_hash = _hash(remote_state) + link.save(update_fields=['last_local_hash', 'last_remote_hash', 'updated_at']) + return + if not link or not ha: + raise ConnectionError('A mapped task changed. Preview again.') + if kind == 'task_reconcile': + item = remote.get(uid) + if not item: + raise ConnectionError('The Home Assistant list changed. Preview again.') + remote_state = _remote_state(item) + link.ha_uid, link.pending_create = uid, False + link.last_local_hash = link.last_remote_hash = _hash(remote_state) + link.save() + elif kind == 'task_baseline': + task.refresh_from_db() + local_state, remote_state = _local_state(task), _remote_state(ha) + link.last_local_hash, link.last_remote_hash = _hash(local_state), _hash(remote_state) + link.save(update_fields=['last_local_hash', 'last_remote_hash', 'updated_at']) + elif kind == 'task_pull': + remote_state = _remote_state(ha) + if not remote_state['due']: + raise ConnectionError('Home Assistant removed a due date TasksNest requires. Review the task before syncing again.') + task.title = remote_state['name'] + task.is_completed = remote_state['status'] == 'completed' + task.description = remote_state['description'] + fields = ['title', 'is_completed', 'description', 'updated_at'] + if remote_state['due']: + task.scheduled_for = date.fromisoformat(remote_state['due']) + fields.append('scheduled_for') + task.save(update_fields=fields) + link.last_local_hash = link.last_remote_hash = _hash(remote_state) + link.save(update_fields=['last_local_hash', 'last_remote_hash', 'updated_at']) + elif kind == 'task_push': + remote_state = _remote_state(ha) + update_item(connection.base_url, token, connection.task_entity_id, uid, + name=state['name'] if state['name'] != remote_state['name'] else None, + status=state['status'] if state['status'] != remote_state['status'] else None, + due_date=state['due'] if state['due'] != remote_state['due'] else None, + description=state['description'] if state['description'] != remote_state['description'] else None) + item = get_items(connection.base_url, token, connection.task_entity_id).get(uid) + if not item or _hash(_remote_state(item)) != _hash(state): + raise ConnectionError(f'Home Assistant did not confirm the update to “{task.title}”.') + link.last_local_hash = link.last_remote_hash = _hash(state) + link.save(update_fields=['last_local_hash', 'last_remote_hash', 'updated_at']) + + +def confirm_task_sync(connection, expected_digest, *, automatic=False): + from datetime import timedelta + from django.db import transaction + now = timezone.now() + claimed = HomeAssistantConnection.objects.filter(pk=connection.pk).filter( + Q(sync_lock_until__isnull=True) | Q(sync_lock_until__lt=now)).update( + sync_lock_until=now + timedelta(minutes=3)) + if not claimed: + raise ConnectionError('A sync is already running. Try again shortly.') + try: + connection.refresh_from_db() + token, tasks, eligible, remote, maps = _snapshot(connection) + plan = build_task_plan(connection, tasks, eligible, remote, maps) + if plan['digest'] != expected_digest: + raise ConnectionError('The lists changed after the preview. Review a fresh preview before syncing.') + connection.task_initial_sync_confirmed = True + connection.save(update_fields=['task_initial_sync_confirmed', 'updated_at']) + done = 0 + features = next((int(e.get('features') or 0) for e in connection.entities + if e.get('id') == connection.task_entity_id), 0) + for action in plan['operations'][:20]: + _apply_task(connection, token, action, eligible, remote, features) + done += 1 + token, tasks, eligible, remote, maps = _snapshot(connection) + _schedule_after_success(connection, timezone.now()) + connection.save(update_fields=['last_synced_at', 'last_sync_error', 'next_sync_at', + 'consecutive_sync_failures', 'updated_at']) + _record_run(connection, succeeded=True, automatic=automatic, changes=done, + summary=f'Task sync applied {done} change(s); {len(plan["issues"])} item(s) need review.') + return done, plan + except ConnectionError as exc: + _schedule_after_failure(connection, exc, automatic) + raise + finally: + HomeAssistantConnection.objects.filter(pk=connection.pk).update(sync_lock_until=None) + + +def reset_task_pending(connection, expected_digest, mapping_id): + from datetime import timedelta + now = timezone.now() + claimed = HomeAssistantConnection.objects.filter(pk=connection.pk).filter( + Q(sync_lock_until__isnull=True) | Q(sync_lock_until__lt=now)).update( + sync_lock_until=now + timedelta(minutes=3)) + if not claimed: + raise ConnectionError('A sync is already running. Try again shortly.') + try: + connection.refresh_from_db() + _, tasks, eligible, remote, maps = _snapshot(connection) + plan = build_task_plan(connection, tasks, eligible, remote, maps) + if plan['digest'] != expected_digest: + raise ConnectionError('The lists changed after the preview. Review a fresh preview.') + mapping = HomeAssistantItemMap.objects.filter(pk=mapping_id, connection=connection, + entity_id=connection.task_entity_id, pending_create=True).select_related('task').first() + if not mapping or not mapping.task_id: + raise ConnectionError('This pending task addition can no longer be reset.') + if any(row['name'].casefold() == mapping.task.title.casefold() for row in remote.values()): + raise ConnectionError('A matching item is now visible in Home Assistant. Preview again to reconcile it.') + title = mapping.task.title + mapping.delete() + _record_run(connection, succeeded=True, automatic=False, + summary=f'Reset uncertain task addition for “{title}”.') + finally: + HomeAssistantConnection.objects.filter(pk=connection.pk).update(sync_lock_until=None) diff --git a/main/home_assistant/templates/home_assistant/preview.html b/main/home_assistant/templates/home_assistant/preview.html new file mode 100644 index 0000000..543ff51 --- /dev/null +++ b/main/home_assistant/templates/home_assistant/preview.html @@ -0,0 +1,17 @@ +{% extends 'planner/base.html' %} +{% block title %}Preview grocery sync · TasksNest{% endblock %} +{% block content %} +
+

Home Assistant

Preview grocery sync

+

← Home Assistant settings

+
+

{% if first_sync %}First sync review{% else %}Changes to sync{% endif %}

+

Review these changes before anything is written. This preview expires after 10 minutes and is checked again when you confirm. At most 20 changes run at once.

+ {% if plan.operations %}
    {% for action in plan.operations %}
  • {% if action.kind == 'export' %}Add to Home Assistant{% elif action.kind == 'import' %}Add to TasksNest{% elif action.kind == 'push' %}Update Home Assistant{% elif action.kind == 'pull' %}Update TasksNest{% elif action.kind == 'reconcile' %}Reconnect by UID{% else %}Record matching state{% endif %}: {{ action.label }}
  • {% endfor %}
{% else %}

No changes are ready to sync.

{% endif %} + {% if plan.limited %}

Only the first 20 changes will run. Preview again for the rest.

{% endif %} + {% if plan.issues %}

Needs review

    {% for issue in plan.issues %}
  • {{ issue }}
  • {% endfor %}
{% endif %} + {% if plan.pending_recoveries %}

Uncertain additions

Check the Home Assistant list first. Reset only if the item is absent; if it appears, preview again so it can be matched.

{% for recovery in plan.pending_recoveries %}
{% csrf_token %}
{% endfor %}{% endif %} + {% if plan.operations or first_sync %}
{% csrf_token %}
{% endif %} +
+
+{% endblock %} diff --git a/main/home_assistant/templates/home_assistant/settings.html b/main/home_assistant/templates/home_assistant/settings.html new file mode 100644 index 0000000..6bdff1d --- /dev/null +++ b/main/home_assistant/templates/home_assistant/settings.html @@ -0,0 +1,47 @@ +{% extends 'planner/base.html' %} +{% block title %}Home Assistant · TasksNest{% endblock %} +{% block content %} +
+

Integrations

Home Assistant

+

← Settings

+
+

Connection

+

In Home Assistant, open your profile → Security → Long-Lived Access Tokens. Create a token and paste it here. For private network addresses, HTTP is allowed; use HTTPS for public servers.

+
{% csrf_token %} +
+
{% if connection %}A token is saved. Leave blank to keep it; enter a new one to replace it.{% else %}The token is stored encrypted and is never shown again.{% endif %}
+ +
+ {% if connection %}

Connection: {% if connection.last_tested_at %}Tested {{ connection.last_tested_at }} · Home Assistant {{ connection.version }}{% else %}Not tested{% endif %}

+ {% if connection.last_error %}

Last test: {{ connection.last_error }}

{% endif %} +
{% csrf_token %}
+
{% csrf_token %}
+ {% endif %} +
+ {% if connection and connection.entities %}
+

Grocery list

Choose the Home Assistant to-do entity for your TasksNest groceries. Selecting a list does not sync or change any items.

+
{% csrf_token %}
+
{% endif %} + {% if connection and connection.entities %}
+

Optional task list

+

Syncs only top-level, active, non-recurring tasks. Titles, completion, descriptions, and scheduled dates are shared when the selected Home Assistant list supports those fields. Recurrence, subtasks, priority, weather rules, and deferrals stay in TasksNest. If you also use Apple Reminders through the Home Assistant Companion app, set its list sync direction to “Beide kanten op” and its conflict preference to Home Assistant; iOS background sync may be delayed.

+
{% csrf_token %}
+ {% if connection.task_entity_id %}

{% if connection.task_initial_sync_confirmed %}Task sync is enabled.{% else %}First task sync needs review and confirmation.{% endif %}

Preview task changes{% endif %} +
{% endif %} + {% if connection and connection.grocery_entity_id %}
+

Grocery sync

+

TasksNest syncs item names and purchased status. Quantities, units, categories, product links, and inventory stay in TasksNest. Items absent from either list are never deleted automatically.

+
{% csrf_token %} +
+
+
+
{% if not connection.initial_sync_confirmed %}Confirm the first sync before enabling automatic sync.{% endif %}
+ +
+

{% if connection.last_synced_at %}Last successful sync: {{ connection.last_synced_at }}{% else %}No sync has run yet.{% endif %}

+ {% if connection.last_sync_error %}

Last sync: {{ connection.last_sync_error }}

{% endif %} + {% if history %}

Recent sync activity

    {% for run in history %}
  • {{ run.created_at }} · {% if run.succeeded %}Succeeded{% else %}Failed{% endif %}{% if run.automatic %} (automatic){% else %} (manual){% endif %} · {{ run.summary }}{% if run.error %}: {{ run.error }}{% endif %}
  • {% endfor %}
{% endif %} + Preview changes +
{% endif %} +
+{% endblock %} diff --git a/main/home_assistant/templates/home_assistant/task_preview.html b/main/home_assistant/templates/home_assistant/task_preview.html new file mode 100644 index 0000000..9d32b70 --- /dev/null +++ b/main/home_assistant/templates/home_assistant/task_preview.html @@ -0,0 +1,17 @@ +{% extends 'planner/base.html' %} +{% block title %}Preview task sync · TasksNest{% endblock %} +{% block content %} +
+

Home Assistant

Preview task sync

+

← Home Assistant settings

+
+

{% if first_sync %}First task sync review{% else %}Task changes to sync{% endif %}

+

Review changes before writing. The preview expires after 10 minutes and is checked again at confirmation. At most 20 changes run at once.

+ {% if plan.operations %}
    {% for action in plan.operations %}
  • {% if action.kind == 'task_export' %}Add to Home Assistant{% elif action.kind == 'task_import' %}Add to TasksNest{% elif action.kind == 'task_push' %}Update Home Assistant{% elif action.kind == 'task_pull' %}Update TasksNest{% elif action.kind == 'task_reconcile' %}Reconnect by UID{% else %}Record matching state{% endif %}: {{ action.label }}
  • {% endfor %}
{% else %}

No changes are ready to sync.

{% endif %} + {% if plan.limited %}

Only the first 20 changes will run. Preview again for the rest.

{% endif %} + {% if plan.issues %}

Needs review

    {% for issue in plan.issues %}
  • {{ issue }}
  • {% endfor %}
{% endif %} + {% if plan.pending_recoveries %}

Uncertain additions

Check the Home Assistant list first. Reset only if the task is absent; if it appears, preview again so it can be matched.

{% for recovery in plan.pending_recoveries %}
{% csrf_token %}
{% endfor %}{% endif %} + {% if plan.operations or first_sync %}
{% csrf_token %}
{% endif %} +
+
+{% endblock %} diff --git a/main/home_assistant/tests.py b/main/home_assistant/tests.py new file mode 100644 index 0000000..1ff56ff --- /dev/null +++ b/main/home_assistant/tests.py @@ -0,0 +1,490 @@ +import hashlib +import json +from unittest.mock import MagicMock, patch + +from django.contrib.auth import get_user_model +from django.test import TestCase, override_settings +from django.urls import reverse + +from groceries.models import GroceryItem +from planner.models import Task +from .client import ConnectionError, _get_json, _request_json, decrypt_token, encrypt_token, get_items, normalize_url +from .models import HomeAssistantConnection, HomeAssistantItemMap, HomeAssistantSyncRun +from .sync import _schedule_after_failure, build_plan, confirm_and_sync, preview, run_due_syncs +from .task_sync import confirm_task_sync, preview_tasks + + +def dns_for(address): + return [(2, 1, 6, '', (address, 8123))] + + +@override_settings(SECURE_SSL_REDIRECT=False) +class ConnectionTests(TestCase): + def setUp(self): + self.user = get_user_model().objects.create_user(username='ha-user', password='example-password') + self.other = get_user_model().objects.create_user(username='other-user', password='example-password') + self.client.force_login(self.user) + self.url = reverse('home_assistant:settings') + + @patch('home_assistant.client.socket.getaddrinfo', return_value=dns_for('192.168.1.10')) + def test_token_is_encrypted_and_never_rendered(self, _dns): + response = self.client.post(self.url, {'action': 'save', 'base_url': 'http://ha.local:8123/', 'token': 'private-token'}) + self.assertEqual(response.status_code, 302) + connection = HomeAssistantConnection.objects.get(user=self.user) + self.assertEqual(connection.base_url, 'http://ha.local:8123') + self.assertNotIn('private-token', connection.encrypted_token) + self.assertEqual(decrypt_token(connection.encrypted_token), 'private-token') + self.assertNotContains(self.client.get(self.url), 'private-token') + + @patch('home_assistant.client.socket.getaddrinfo', return_value=dns_for('192.168.1.10')) + def test_blank_token_keeps_previous_value_and_owner_isolation(self, _dns): + HomeAssistantConnection.objects.create(user=self.user, base_url='http://ha.local:8123', encrypted_token=encrypt_token('old')) + HomeAssistantConnection.objects.create(user=self.other, base_url='https://other.example', encrypted_token=encrypt_token('other')) + self.client.post(self.url, {'action': 'save', 'base_url': 'http://ha.local:8123', 'token': ''}) + self.assertEqual(decrypt_token(HomeAssistantConnection.objects.get(user=self.user).encrypted_token), 'old') + self.assertEqual(decrypt_token(HomeAssistantConnection.objects.get(user=self.other).encrypted_token), 'other') + + @patch('home_assistant.client.socket.getaddrinfo', return_value=dns_for('192.168.1.10')) + @patch('home_assistant.views.test_connection', return_value='2026.10.0') + @patch('home_assistant.views.discover_entities', return_value=[{'id': 'todo.shopping', 'name': 'Shopping'}]) + def test_discover_and_select_only_known_entity(self, _discover, _test, _dns): + HomeAssistantConnection.objects.create(user=self.user, base_url='http://ha.local:8123', encrypted_token=encrypt_token('secret')) + self.client.post(self.url, {'action': 'discover'}) + connection = HomeAssistantConnection.objects.get(user=self.user) + self.assertEqual(connection.version, '2026.10.0') + self.assertEqual(connection.entities[0]['id'], 'todo.shopping') + self.client.post(self.url, {'action': 'select', 'grocery_entity_id': 'todo.foreign'}) + connection.refresh_from_db() + self.assertEqual(connection.grocery_entity_id, '') + self.client.post(self.url, {'action': 'select', 'grocery_entity_id': 'todo.shopping'}) + connection.refresh_from_db() + self.assertEqual(connection.grocery_entity_id, 'todo.shopping') + + def test_optional_task_entity_requires_discovery_and_can_be_disconnected(self): + connection = HomeAssistantConnection.objects.create(user=self.user, + base_url='https://ha.example', encrypted_token=encrypt_token('secret'), + grocery_entity_id='todo.shopping', entities=[ + {'id': 'todo.shopping', 'name': 'Shopping'}, + {'id': 'todo.tasks', 'name': 'Tasks'}, + ]) + self.client.post(self.url, {'action': 'select-task', 'task_entity_id': 'todo.unknown'}) + connection.refresh_from_db() + self.assertEqual(connection.task_entity_id, '') + self.client.post(self.url, {'action': 'select-task', 'task_entity_id': 'todo.tasks'}) + connection.refresh_from_db() + self.assertEqual(connection.task_entity_id, 'todo.tasks') + self.client.post(self.url, {'action': 'select-task', 'task_entity_id': ''}) + connection.refresh_from_db() + self.assertEqual(connection.task_entity_id, '') + self.assertEqual(connection.grocery_entity_id, 'todo.shopping') + + @patch('home_assistant.client.socket.getaddrinfo', return_value=dns_for('8.8.8.8')) + def test_public_http_rejected(self, _dns): + with self.assertRaises(ConnectionError): + normalize_url('http://example.com:8123') + + def test_url_credentials_and_paths_rejected(self): + for value in ('https://user:secret@example.com', 'https://example.com/api', 'file:///etc/passwd'): + with self.subTest(value=value), self.assertRaises(ConnectionError): + normalize_url(value) + + @patch('home_assistant.client.socket.getaddrinfo', return_value=dns_for('192.168.1.10')) + def test_changing_host_requires_new_token(self, _dns): + HomeAssistantConnection.objects.create(user=self.user, base_url='http://old.local:8123', encrypted_token=encrypt_token('old')) + self.client.post(self.url, {'action': 'save', 'base_url': 'http://new.local:8123', 'token': ''}) + connection = HomeAssistantConnection.objects.get(user=self.user) + self.assertEqual(connection.base_url, 'http://old.local:8123') + + @patch('home_assistant.views.test_connection', side_effect=ConnectionError('Token rejected (401).')) + def test_test_failure_is_sanitized_and_saved(self, _test): + HomeAssistantConnection.objects.create(user=self.user, base_url='https://ha.example', encrypted_token=encrypt_token('secret')) + self.client.post(self.url, {'action': 'test'}) + connection = HomeAssistantConnection.objects.get(user=self.user) + self.assertEqual(connection.last_error, 'Token rejected (401).') + self.assertNotIn('secret', connection.last_error) + self.assertIsNone(connection.last_tested_at) + + @patch('home_assistant.client.socket.getaddrinfo', return_value=dns_for('192.168.1.10')) + @patch('home_assistant.client.http.client.HTTPConnection') + def test_api_rejects_redirect_and_unauthorized(self, connection_class, _dns): + connection = connection_class.return_value + response = MagicMock() + connection.getresponse.return_value = response + for status, expected in ((302, 'redirected'), (401, 'Token rejected')): + response.status = status + with self.subTest(status=status), self.assertRaisesRegex(ConnectionError, expected): + _get_json('http://ha.local:8123', 'secret', '/api/config') + self.assertEqual(connection.request.call_count, 2) + self.assertEqual(connection.request.call_args.args[:2], ('GET', '/api/config')) + + @patch('home_assistant.client.socket.getaddrinfo', return_value=dns_for('192.168.1.10')) + @patch('home_assistant.client.http.client.HTTPConnection') + def test_unavailable_entity_error_is_actionable(self, connection_class, _dns): + response = connection_class.return_value.getresponse.return_value + response.status = 500 + response.read.return_value = b'{"message":"Service call requested response data but did not match any entities"}' + with self.assertRaisesRegex(ConnectionError, 'selected Home Assistant to-do list is unavailable'): + _request_json('http://ha.local:8123', 'secret', '/api/services/todo/get_items?return_response', {}) + + @patch('home_assistant.client._get_json', return_value=[ + {'entity_id': 'todo.active', 'state': '0', 'attributes': {'friendly_name': 'Active'}}, + {'entity_id': 'todo.offline', 'state': 'unavailable', 'attributes': {'friendly_name': 'Offline'}}, + ]) + def test_discovery_marks_unavailable_entities(self, _get): + from .client import discover_entities + found = discover_entities('https://ha.example', 'token') + self.assertTrue(next(entity for entity in found if entity['id'] == 'todo.active')['available']) + self.assertFalse(next(entity for entity in found if entity['id'] == 'todo.offline')['available']) + + @patch('home_assistant.client._request_json') + def test_get_items_parses_service_response_and_rejects_duplicate_uids(self, request): + request.return_value = {'changed_states': [], 'service_response': {'todo.shopping': {'items': [ + {'uid': 'one', 'summary': 'Milk', 'status': 'needs_action'}, + {'uid': 'two', 'summary': 'Bread', 'status': 'completed'}, + ]}}} + result = get_items('https://ha.example', 'token', 'todo.shopping') + self.assertEqual(set(result), {'one', 'two'}) + self.assertEqual(request.call_args.args[2], '/api/services/todo/get_items?return_response') + request.return_value['service_response']['todo.shopping']['items'][1]['uid'] = 'one' + with self.assertRaises(ConnectionError): + get_items('https://ha.example', 'token', 'todo.shopping') + + +class FakeHomeAssistant: + def __init__(self, items=None): + self.items = {item['uid']: item.copy() for item in (items or [])} + self.calls = [] + self.next_id = 1 + + def get(self, *_args): + return {uid: item.copy() for uid, item in self.items.items()} + + def add(self, _url, _token, _entity, name): + self.calls.append(('add', name)) + uid = f'new-{self.next_id}' + self.next_id += 1 + self.items[uid] = {'uid': uid, 'name': name, 'status': 'needs_action'} + + def update(self, _url, _token, _entity, uid, *, name=None, status=None): + self.calls.append(('update', uid, name, status)) + if name is not None: + self.items[uid]['name'] = name + if status is not None: + self.items[uid]['status'] = status + + +class FakeTaskHomeAssistant: + def __init__(self, items=None): + self.items = {item['uid']: item.copy() for item in (items or [])} + self.next_id = 1 + + def get(self, *_args): + return {uid: item.copy() for uid, item in self.items.items()} + + def add(self, _url, _token, _entity, name, *, due_date=None, description=None): + uid = f'task-{self.next_id}' + self.next_id += 1 + self.items[uid] = {'uid': uid, 'name': name, 'status': 'needs_action', + 'due': due_date, 'description': description or ''} + + def update(self, _url, _token, _entity, uid, *, name=None, status=None, + due_date=None, description=None): + item = self.items[uid] + if name is not None: + item['name'] = name + if status is not None: + item['status'] = status + if due_date is not None: + item['due'] = due_date + if description is not None: + item['description'] = description + + +@override_settings(SECURE_SSL_REDIRECT=False) +class GrocerySyncTests(TestCase): + def setUp(self): + self.user = get_user_model().objects.create_user(username='sync-user') + self.other = get_user_model().objects.create_user(username='sync-other') + self.connection = HomeAssistantConnection.objects.create( + user=self.user, base_url='https://ha.example', encrypted_token=encrypt_token('secret'), + grocery_entity_id='todo.shopping', + entities=[{'id': 'todo.shopping', 'name': 'Shopping', 'features': 5}]) + + def local(self, name, status='needed', user=None): + return GroceryItem.objects.create(user=user or self.user, name=name, quantity=1, + unit='pieces', status=status) + + def run_with(self, fake): + with patch('home_assistant.sync.get_items', side_effect=fake.get), \ + patch('home_assistant.sync.add_item', side_effect=fake.add), \ + patch('home_assistant.sync.update_item', side_effect=fake.update): + plan = preview(self.connection) + result = confirm_and_sync(self.connection, plan['digest']) + return plan, result + + def test_first_sync_import_export_and_repeat_is_duplicate_free(self): + local = self.local('Milk', 'purchased') + self.local('Private', user=self.other) + fake = FakeHomeAssistant([{'uid': 'ha-1', 'name': 'Bread', 'status': 'completed'}]) + plan, (done, _) = self.run_with(fake) + self.assertEqual({action['kind'] for action in plan['operations']}, {'import', 'export'}) + self.assertEqual(done, 2) + self.assertEqual(fake.items['new-1']['status'], 'completed') + self.assertEqual(HomeAssistantItemMap.objects.filter(connection=self.connection).count(), 2) + self.assertEqual(GroceryItem.objects.get(user=self.user, name='Bread').status, 'purchased') + self.assertEqual(GroceryItem.objects.get(pk=local.pk).name, 'Milk') + second, (done_again, _) = self.run_with(fake) + self.assertEqual(second['operations'], []) + self.assertEqual(done_again, 0) + self.assertEqual(fake.calls.count(('add', 'Milk')), 1) + self.assertFalse(GroceryItem.objects.filter(user=self.user, name='Private').exists()) + + def test_ambiguous_same_title_is_not_merged_or_duplicated(self): + self.local('Eggs') + fake = FakeHomeAssistant([{'uid': 'ha-1', 'name': 'Eggs', 'status': 'needs_action'}]) + plan, (done, _) = self.run_with(fake) + self.assertEqual(done, 0) + self.assertEqual(plan['operations'], []) + self.assertTrue(plan['issues']) + self.assertEqual(fake.calls, []) + + def test_remote_edit_and_completion_pull_into_local(self): + item = self.local('Tea') + fake = FakeHomeAssistant([{'uid': 'ha-1', 'name': 'Tea', 'status': 'needs_action'}]) + self.run_with(fake) + # Initial same-title items are deliberately not matched; create a known mapping. + HomeAssistantItemMap.objects.create(connection=self.connection, entity_id='todo.shopping', grocery=item, + ha_uid='ha-1', last_local_hash='x', last_remote_hash='x') + fake.items['ha-1'].update(name='Green tea', status='completed') + with patch('home_assistant.sync.get_items', side_effect=fake.get): + plan = preview(self.connection) + self.assertEqual(plan['operations'][0]['kind'], 'pull') + with patch('home_assistant.sync.get_items', side_effect=fake.get): + confirm_and_sync(self.connection, plan['digest']) + item.refresh_from_db() + self.assertEqual((item.name, item.status), ('Green tea', 'purchased')) + + def test_missing_remote_does_not_delete_local(self): + item = self.local('Rice') + HomeAssistantItemMap.objects.create(connection=self.connection, entity_id='todo.shopping', grocery=item, + ha_uid='gone', last_local_hash='x', last_remote_hash='x') + fake = FakeHomeAssistant() + plan, (done, _) = self.run_with(fake) + self.assertEqual(done, 0) + self.assertTrue(plan['issues']) + self.assertTrue(GroceryItem.objects.filter(pk=item.pk).exists()) + + def test_outbound_failure_stays_pending_and_does_not_retry_blindly(self): + self.local('Oats') + fake = FakeHomeAssistant() + with patch('home_assistant.sync.get_items', side_effect=fake.get), \ + patch('home_assistant.sync.add_item', side_effect=ConnectionError('Connection timed out.')): + plan = preview(self.connection) + with self.assertRaises(ConnectionError): + confirm_and_sync(self.connection, plan['digest']) + mapping = HomeAssistantItemMap.objects.get(connection=self.connection) + self.assertTrue(mapping.pending_create) + with patch('home_assistant.sync.get_items', side_effect=fake.get): + next_plan = preview(self.connection) + self.assertFalse(next_plan['operations']) + self.assertTrue(next_plan['issues']) + + def test_two_sided_conflict_defaults_to_home_assistant(self): + item = self.local('Original') + fake = FakeHomeAssistant([{'uid': 'ha-1', 'name': 'Home edit', 'status': 'completed'}]) + baseline = hashlib.sha256(json.dumps(['Original', 'needs_action'], ensure_ascii=False).encode()).hexdigest() + HomeAssistantItemMap.objects.create(connection=self.connection, entity_id='todo.shopping', grocery=item, + ha_uid='ha-1', last_local_hash=baseline, last_remote_hash=baseline) + item.name = 'TasksNest edit' + item.save() + plan, (done, _) = self.run_with(fake) + self.assertEqual(plan['operations'][0]['kind'], 'pull') + self.assertEqual(done, 1) + item.refresh_from_db() + self.assertEqual((item.name, item.status), ('Home edit', 'purchased')) + + def test_two_sided_conflict_can_choose_tasksnest(self): + item = self.local('Local edit') + fake = FakeHomeAssistant([{'uid': 'ha-1', 'name': 'Home edit', 'status': 'completed'}]) + HomeAssistantItemMap.objects.create(connection=self.connection, entity_id='todo.shopping', grocery=item, + ha_uid='ha-1', last_local_hash='old', last_remote_hash='old') + self.connection.conflict_winner = 'tasksnest' + self.connection.save() + plan, (done, _) = self.run_with(fake) + self.assertEqual(plan['operations'][0]['kind'], 'push') + self.assertEqual(done, 1) + self.assertEqual(fake.items['ha-1']['name'], 'Local edit') + self.assertEqual(fake.items['ha-1']['status'], 'needs_action') + + def test_direction_to_ha_pushes_and_unsupported_update_is_blocked(self): + item = self.local('Original') + fake = FakeHomeAssistant([{'uid': 'ha-1', 'name': 'Home edit', 'status': 'needs_action'}]) + HomeAssistantItemMap.objects.create(connection=self.connection, entity_id='todo.shopping', grocery=item, + ha_uid='ha-1', last_local_hash='previous', last_remote_hash='previous') + self.connection.direction = 'to_ha' + self.connection.save() + plan, (done, _) = self.run_with(fake) + self.assertEqual(plan['operations'][0]['kind'], 'push') + self.assertEqual(done, 1) + self.assertEqual(fake.items['ha-1']['name'], 'Original') + self.connection.entities = [{'id': 'todo.shopping', 'features': 1}] + self.connection.save() + item.name = 'Another edit' + item.save() + with patch('home_assistant.sync.get_items', side_effect=fake.get): + blocked = preview(self.connection) + self.assertFalse(blocked['operations']) + self.assertTrue(blocked['issues']) + + def test_stale_preview_cannot_write(self): + item = self.local('Apples') + fake = FakeHomeAssistant() + with patch('home_assistant.sync.get_items', side_effect=fake.get), \ + patch('home_assistant.sync.add_item', side_effect=fake.add): + digest = preview(self.connection)['digest'] + item.name = 'Oranges' + item.save() + with self.assertRaisesRegex(ConnectionError, 'changed after the preview'): + confirm_and_sync(self.connection, digest) + self.assertEqual(fake.calls, []) + + def test_preview_page_requires_signed_confirmation(self): + self.client.force_login(self.user) + self.local('Bananas') + fake = FakeHomeAssistant() + with patch('home_assistant.sync.get_items', side_effect=fake.get): + response = self.client.get(reverse('home_assistant:preview')) + self.assertEqual(response.status_code, 200) + self.assertContains(response, 'Bananas') + bad = self.client.post(reverse('home_assistant:preview'), {'preview_token': 'invalid'}) + self.assertEqual(bad.status_code, 302) + self.assertEqual(fake.calls, []) + + def test_pending_addition_can_only_be_reset_after_checked_absent(self): + item = self.local('Oats') + mapping = HomeAssistantItemMap.objects.create(connection=self.connection, + entity_id='todo.shopping', grocery=item, pending_create=True) + self.client.force_login(self.user) + with patch('home_assistant.sync.get_items', return_value={}): + response = self.client.get(reverse('home_assistant:preview')) + token = response.context['preview_token'] + refused = self.client.post(reverse('home_assistant:preview'), { + 'action': 'reset-pending', 'mapping_id': mapping.pk, 'preview_token': token, + }) + self.assertEqual(refused.status_code, 302) + self.assertTrue(HomeAssistantItemMap.objects.filter(pk=mapping.pk).exists()) + reset = self.client.post(reverse('home_assistant:preview'), { + 'action': 'reset-pending', 'mapping_id': mapping.pk, + 'checked_absent': 'yes', 'preview_token': token, + }) + self.assertEqual(reset.status_code, 302) + self.assertFalse(HomeAssistantItemMap.objects.filter(pk=mapping.pk).exists()) + + def test_auto_sync_preferences_and_due_run_are_recorded(self): + self.client.force_login(self.user) + self.connection.initial_sync_confirmed = True + self.connection.save(update_fields=['initial_sync_confirmed', 'updated_at']) + self.client.post(reverse('home_assistant:settings'), { + 'action': 'sync-options', 'direction': 'both', 'conflict_winner': 'ha', + 'sync_interval_minutes': '5', 'auto_sync_enabled': 'on', + }) + self.connection.refresh_from_db() + self.assertTrue(self.connection.auto_sync_enabled) + self.assertEqual(self.connection.sync_interval_minutes, '5') + self.connection.task_entity_id = 'todo.tasks' + self.connection.task_initial_sync_confirmed = True + self.connection.entities.append({'id': 'todo.tasks', 'features': 85}) + self.connection.save(update_fields=['task_entity_id', 'task_initial_sync_confirmed', 'entities', 'updated_at']) + fake = FakeHomeAssistant() + with patch('home_assistant.sync.get_items', side_effect=fake.get), \ + patch('home_assistant.task_sync.get_items', side_effect=fake.get): + result = run_due_syncs() + self.assertEqual(result, {'attempted': 2, 'succeeded': 2, 'failed': 0}) + runs = HomeAssistantSyncRun.objects.filter(connection=self.connection) + self.assertEqual(runs.count(), 2) + self.assertTrue(all(run.succeeded and run.automatic for run in runs)) + self.connection.refresh_from_db() + self.assertIsNotNone(self.connection.next_sync_at) + self.assertEqual(self.connection.consecutive_sync_failures, 0) + + def test_automatic_retries_stop_after_five_consecutive_failures(self): + self.connection.auto_sync_enabled = True + self.connection.sync_interval_minutes = '5' + self.connection.consecutive_sync_failures = 4 + self.connection.save() + _schedule_after_failure(self.connection, ConnectionError('Home Assistant is unreachable.'), True) + self.connection.refresh_from_db() + self.assertFalse(self.connection.auto_sync_enabled) + self.assertIsNone(self.connection.next_sync_at) + self.assertEqual(self.connection.consecutive_sync_failures, 5) + run = self.connection.sync_runs.first() + self.assertIn('disabled after five', run.summary) + + def test_task_sync_maps_due_description_and_completion_without_losing_planner_fields(self): + local = Task.objects.create(owner=self.user, title='Read a book', description='Chapter 4', + scheduled_for='2026-11-02', priority=Task.Priority.HIGH, is_completed=True, + is_outdoor=True, weather_profile=Task.WeatherProfile.SUNNY) + self.connection.task_entity_id = 'todo.tasks' + self.connection.entities.append({'id': 'todo.tasks', 'features': 85}) + self.connection.save() + remote = FakeTaskHomeAssistant([{'uid': 'remote-1', 'name': 'Pay a bill', + 'status': 'completed', 'due': '2026-11-03', 'description': 'Online'}]) + with patch('home_assistant.task_sync.get_items', side_effect=remote.get), \ + patch('home_assistant.task_sync.add_item', side_effect=remote.add), \ + patch('home_assistant.task_sync.update_item', side_effect=remote.update): + plan = preview_tasks(self.connection) + self.assertEqual({item['kind'] for item in plan['operations']}, {'task_export', 'task_import'}) + done, _ = confirm_task_sync(self.connection, plan['digest']) + self.assertEqual(done, 2) + next_plan = preview_tasks(self.connection) + self.assertFalse(next_plan['operations']) + local.refresh_from_db() + self.assertEqual((local.title, local.description, local.priority, local.is_outdoor, + local.weather_profile, local.is_completed), + ('Read a book', 'Chapter 4', Task.Priority.HIGH, True, + Task.WeatherProfile.SUNNY, True)) + imported = Task.objects.get(owner=self.user, title='Pay a bill') + self.assertEqual((imported.description, imported.scheduled_for.isoformat(), imported.is_completed), + ('Online', '2026-11-03', True)) + self.assertEqual(remote.items['task-1']['status'], 'completed') + + def test_task_sync_skips_recurring_and_remote_tasks_without_due_dates(self): + Task.objects.create(owner=self.user, title='Daily journal', scheduled_for='2026-10-22', is_recurring=True) + self.connection.task_entity_id = 'todo.tasks' + self.connection.entities.append({'id': 'todo.tasks', 'features': 85}) + self.connection.save() + remote = FakeTaskHomeAssistant([{'uid': 'remote-1', 'name': 'Unscheduled', + 'status': 'needs_action', 'due': None, 'description': ''}]) + with patch('home_assistant.task_sync.get_items', side_effect=remote.get): + plan = preview_tasks(self.connection) + self.assertFalse(plan['operations']) + self.assertTrue(any('recurs' in issue for issue in plan['issues'])) + self.assertTrue(any('requires a scheduled date' in issue for issue in plan['issues'])) + + def test_reopening_and_local_completion_both_propagate(self): + fake = FakeHomeAssistant([{'uid': 'ha-1', 'name': 'Coffee', 'status': 'completed'}]) + self.run_with(fake) + item = GroceryItem.objects.get(user=self.user, name='Coffee') + self.assertEqual(item.status, 'purchased') + fake.items['ha-1']['status'] = 'needs_action' + plan, _ = self.run_with(fake) + self.assertEqual(plan['operations'][0]['kind'], 'pull') + item.refresh_from_db() + self.assertEqual(item.status, 'needed') + item.status = 'purchased' + item.save() + plan, _ = self.run_with(fake) + self.assertEqual(plan['operations'][0]['kind'], 'push') + self.assertEqual(fake.items['ha-1']['status'], 'completed') + + def test_uncertain_addition_reconciles_uid_without_duplicate(self): + item = self.local('Yoghurt') + HomeAssistantItemMap.objects.create(connection=self.connection, entity_id='todo.shopping', + grocery=item, pending_create=True) + fake = FakeHomeAssistant([{'uid': 'ha-created', 'name': 'Yoghurt', 'status': 'needs_action'}]) + plan, (done, _) = self.run_with(fake) + self.assertEqual([action['kind'] for action in plan['operations']], ['reconcile']) + self.assertEqual(done, 1) + mapping = HomeAssistantItemMap.objects.get(connection=self.connection) + self.assertEqual(mapping.ha_uid, 'ha-created') + self.assertEqual(fake.calls, []) diff --git a/main/home_assistant/urls.py b/main/home_assistant/urls.py new file mode 100644 index 0000000..162a75a --- /dev/null +++ b/main/home_assistant/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from . import views + +app_name = 'home_assistant' +urlpatterns = [ + path('', views.connection_settings, name='settings'), + path('preview/', views.grocery_preview, name='preview'), + path('tasks/preview/', views.task_preview, name='task-preview'), +] diff --git a/main/home_assistant/views.py b/main/home_assistant/views.py new file mode 100644 index 0000000..7185963 --- /dev/null +++ b/main/home_assistant/views.py @@ -0,0 +1,219 @@ +from django.contrib import messages +from django.contrib.auth.decorators import login_required +from django.core import signing +from django.shortcuts import redirect, render +from django.utils import timezone +from django.views.decorators.http import require_http_methods + +from planner.demo_policy import is_demo_user +from .client import ConnectionError, decrypt_token, discover_entities, encrypt_token, normalize_url, test_connection +from .models import HomeAssistantConnection +from .sync import confirm_and_sync, preview, reset_pending_create +from .task_sync import confirm_task_sync, preview_tasks, reset_task_pending + + +@login_required +@require_http_methods(['GET', 'POST']) +def connection_settings(request): + if is_demo_user(request.user): + from django.http import HttpResponseForbidden + return HttpResponseForbidden('This action is unavailable in the demo account.') + connection = HomeAssistantConnection.objects.filter(user=request.user).first() + if request.method == 'POST': + action = request.POST.get('action') + if action == 'save': + raw_token = request.POST.get('token', '').strip() + try: + url = normalize_url(request.POST.get('base_url', '')) + if not raw_token and not connection: + raise ConnectionError('Enter a long-lived access token.') + if connection and url != connection.base_url and not raw_token: + raise ConnectionError('Enter a new token when changing the Home Assistant URL.') + if len(raw_token) > 4096: + raise ConnectionError('The access token is too long.') + connection = connection or HomeAssistantConnection(user=request.user) + if url != connection.base_url: + if connection.pk: + connection.item_maps.all().delete() + connection.entities = [] + connection.grocery_entity_id = '' + connection.task_entity_id = '' + connection.initial_sync_confirmed = False + connection.task_initial_sync_confirmed = False + connection.base_url = url + if raw_token: + connection.encrypted_token = encrypt_token(raw_token) + connection.last_tested_at = None + connection.last_error = '' + connection.version = '' + connection.save() + messages.success(request, 'Connection details saved. Test the connection before choosing a list.') + return redirect('home_assistant:settings') + except ConnectionError as exc: + messages.error(request, str(exc)) + elif action == 'disconnect' and connection: + connection.delete() + messages.success(request, 'Home Assistant disconnected. Your TasksNest groceries were kept.') + return redirect('home_assistant:settings') + elif connection and action in {'test', 'discover'}: + try: + token = decrypt_token(connection.encrypted_token) + version = test_connection(connection.base_url, token) + connection.version = version + connection.last_tested_at = timezone.now() + connection.last_error = '' + if action == 'discover': + connection.entities = discover_entities(connection.base_url, token) + if connection.grocery_entity_id not in {item['id'] for item in connection.entities}: + connection.grocery_entity_id = '' + connection.initial_sync_confirmed = False + if connection.task_entity_id not in {item['id'] for item in connection.entities}: + connection.task_entity_id = '' + connection.task_initial_sync_confirmed = False + messages.success(request, f'Found {len(connection.entities)} to-do lists.') + else: + messages.success(request, f'Connected to Home Assistant {version}.') + connection.save() + except ConnectionError as exc: + connection.last_error = str(exc) + connection.save(update_fields=['last_error', 'updated_at']) + messages.error(request, str(exc)) + return redirect('home_assistant:settings') + elif connection and action == 'select': + entity_id = request.POST.get('grocery_entity_id', '') + known_entities = {item['id']: item for item in connection.entities} + if (entity_id and entity_id != connection.task_entity_id and entity_id in known_entities + and known_entities[entity_id].get('available', True)): + if entity_id != connection.grocery_entity_id: + connection.initial_sync_confirmed = False + connection.grocery_entity_id = entity_id + connection.save(update_fields=['grocery_entity_id', 'initial_sync_confirmed', 'updated_at']) + messages.success(request, 'Grocery list selected. Preview changes before syncing.') + else: + messages.error(request, 'Choose a discovered to-do list different from the selected task list.') + return redirect('home_assistant:settings') + elif connection and action == 'select-task': + entity_id = request.POST.get('task_entity_id', '') + if not entity_id: + connection.task_entity_id = '' + connection.task_initial_sync_confirmed = False + connection.save(update_fields=['task_entity_id', 'task_initial_sync_confirmed', 'updated_at']) + messages.success(request, 'Optional task list disconnected. TasksNest tasks were kept.') + elif (entity_id != connection.grocery_entity_id + and entity_id in {item['id'] for item in connection.entities} + and next(item for item in connection.entities if item['id'] == entity_id).get('available', True)): + if entity_id != connection.task_entity_id: + connection.task_initial_sync_confirmed = False + connection.task_entity_id = entity_id + connection.save(update_fields=['task_entity_id', 'task_initial_sync_confirmed', 'updated_at']) + messages.success(request, 'Task list selected. Review a preview before its first sync.') + else: + messages.error(request, 'Choose a discovered to-do list different from your grocery list.') + return redirect('home_assistant:settings') + elif connection and action == 'sync-options': + direction = request.POST.get('direction') + winner = request.POST.get('conflict_winner') + interval = request.POST.get('sync_interval_minutes') + auto_enabled = request.POST.get('auto_sync_enabled') == 'on' + if (direction in HomeAssistantConnection.Direction.values + and winner in HomeAssistantConnection.ConflictWinner.values + and interval in HomeAssistantConnection.SyncInterval.values + and (not auto_enabled or connection.initial_sync_confirmed)): + connection.direction = direction + connection.conflict_winner = winner + connection.sync_interval_minutes = interval + connection.auto_sync_enabled = auto_enabled and interval != HomeAssistantConnection.SyncInterval.MANUAL + connection.next_sync_at = timezone.now() if connection.auto_sync_enabled else None + connection.save(update_fields=['direction', 'conflict_winner', 'sync_interval_minutes', + 'auto_sync_enabled', 'next_sync_at', 'updated_at']) + messages.success(request, 'Sync preferences saved. Preview before syncing.') + else: + messages.error(request, 'Choose valid sync preferences and confirm the first sync before enabling automatic sync.') + return redirect('home_assistant:settings') + else: + messages.error(request, 'Save a connection before using that action.') + history = connection.sync_runs.all()[:10] if connection else [] + return render(request, 'home_assistant/settings.html', {'connection': connection, 'history': history}) + + +@login_required +@require_http_methods(['GET', 'POST']) +def grocery_preview(request): + if is_demo_user(request.user): + from django.http import HttpResponseForbidden + return HttpResponseForbidden('This action is unavailable in the demo account.') + connection = HomeAssistantConnection.objects.filter(user=request.user).first() + if not connection or not connection.grocery_entity_id: + messages.error(request, 'Connect Home Assistant and choose a to-do list first.') + return redirect('home_assistant:settings') + if request.method == 'POST': + try: + digest = signing.loads(request.POST.get('preview_token', ''), salt='ha-grocery-sync', max_age=600) + if request.POST.get('action') == 'reset-pending': + if request.POST.get('checked_absent') != 'yes': + raise ConnectionError('Confirm that you checked Home Assistant before resetting this addition.') + reset_pending_create(connection, digest, request.POST.get('mapping_id')) + messages.success(request, 'Pending addition reset. Preview again before syncing.') + return redirect('home_assistant:preview') + done, plan = confirm_and_sync(connection, digest) + remaining = max(0, len(plan['operations']) - done) + messages.success(request, f'Synced {done} changes.' + (f' Preview again for {remaining} more.' if remaining else '')) + return redirect('home_assistant:settings') + except signing.BadSignature: + messages.error(request, 'The preview expired or was invalid. Review it again.') + return redirect('home_assistant:preview') + except ConnectionError as exc: + messages.error(request, str(exc)) + return redirect('home_assistant:preview') + try: + plan = preview(connection) + token = signing.dumps(plan['digest'], salt='ha-grocery-sync') + except ConnectionError as exc: + messages.error(request, str(exc)) + return redirect('home_assistant:settings') + return render(request, 'home_assistant/preview.html', { + 'connection': connection, 'plan': plan, 'preview_token': token, + 'first_sync': not connection.initial_sync_confirmed, + }) + + +@login_required +@require_http_methods(['GET', 'POST']) +def task_preview(request): + if is_demo_user(request.user): + from django.http import HttpResponseForbidden + return HttpResponseForbidden('This action is unavailable in the demo account.') + connection = HomeAssistantConnection.objects.filter(user=request.user).first() + if not connection or not connection.task_entity_id: + messages.error(request, 'Connect Home Assistant and choose a task list first.') + return redirect('home_assistant:settings') + if request.method == 'POST': + try: + digest = signing.loads(request.POST.get('preview_token', ''), salt='ha-task-sync', max_age=600) + if request.POST.get('action') == 'reset-pending': + if request.POST.get('checked_absent') != 'yes': + raise ConnectionError('Confirm that you checked Home Assistant before resetting this addition.') + reset_task_pending(connection, digest, request.POST.get('mapping_id')) + messages.success(request, 'Pending task addition reset. Preview again before syncing.') + return redirect('home_assistant:task-preview') + done, plan = confirm_task_sync(connection, digest) + remaining = max(0, len(plan['operations']) - done) + messages.success(request, f'Synced {done} task changes.' + + (f' Preview again for {remaining} more.' if remaining else '')) + return redirect('home_assistant:settings') + except signing.BadSignature: + messages.error(request, 'The preview expired or was invalid. Review it again.') + return redirect('home_assistant:task-preview') + except ConnectionError as exc: + messages.error(request, str(exc)) + return redirect('home_assistant:task-preview') + try: + plan = preview_tasks(connection) + token = signing.dumps(plan['digest'], salt='ha-task-sync') + except ConnectionError as exc: + messages.error(request, str(exc)) + return redirect('home_assistant:settings') + return render(request, 'home_assistant/task_preview.html', { + 'connection': connection, 'plan': plan, 'preview_token': token, + 'first_sync': not connection.task_initial_sync_confirmed, + }) diff --git a/main/mcp_server/server.py b/main/mcp_server/server.py index 038e11f..e09f176 100644 --- a/main/mcp_server/server.py +++ b/main/mcp_server/server.py @@ -196,6 +196,30 @@ class RecipeAnalysisInput(BaseModel): substitutions: dict[str, str] = Field(default_factory=dict) +class RecipeComponentInput(BaseModel): + recipe_id: str + servings: Decimal = Field(gt=0, le=Decimal('99999.99')) + + +class RecipeCompositionInput(BaseModel): + title: str = Field(min_length=1, max_length=180) + default_servings: Decimal = Field(default=Decimal('2'), gt=0, le=Decimal('99999.99')) + components: list[RecipeComponentInput] = Field(min_length=1, max_length=10) + + +class RecipeLinkInput(BaseModel): + recipe_id: str + relationship: Literal['goes_well_with', 'try_with', 'alternative_to'] = 'goes_well_with' + note: str = Field(default='', max_length=255) + reciprocal: bool = True + + +class RecipeLinkChanges(BaseModel): + relationship: Literal['goes_well_with', 'try_with', 'alternative_to'] | None = None + note: str | None = Field(default=None, max_length=255) + reciprocal: bool | None = None + + class GroceryItemInput(BaseModel): name: str quantity: Decimal = Field(gt=0) @@ -539,7 +563,7 @@ def create_server() -> MCPServer: return {'success': False, 'error': item['error']} return {'success': True, 'item': _inventory(item)} - @server.tool(structured_output=True, description='Preview or update up to 20 owned inventory rows. Uses inventory_update field rules. Partial success is default; atomic=true rolls back all updates if any item fails. Actual execution requires a unique idempotency_key; reuse the same key and payload on retry. Optionally supply expected_updated_at per item to reject concurrent edits.') + @server.tool(structured_output=True, description='Preview or update up to 50 owned inventory rows. Uses inventory_update field rules and returns per-item before/after values and counts. Dry run is default and writes nothing. Partial success is default for compatibility; atomic=true rolls back all updates if any item fails. Actual execution requires an idempotency_key; reuse the same key and payload on retry. Supply expected_updated_at per item to reject concurrent edits.') async def inventory_bulk_update(items: list[InventoryBulkUpdateEntry], idempotency_key: str | None = None, dry_run: bool = True, atomic: bool = False) -> dict[str, Any]: if not dry_run and not idempotency_key: @@ -634,6 +658,41 @@ def create_server() -> MCPServer: async def recipe_get(recipe_id: str) -> dict[str, Any]: return await api_request('GET', f'recipes/{recipe_id}/') + @server.tool(structured_output=True, description='List up to 100 linked recipe suggestions for an owned recipe. Includes incoming suggestions only when the other owner-visible link is reciprocal. Suggestions are not recipe ingredients.') + async def recipe_link_list(recipe_id: str) -> dict[str, Any]: + return await api_request('GET', f'recipes/{recipe_id}/links/') + + @server.tool(structured_output=True, description='Create a one-way or reciprocal suggestion between two owned recipes. This does not add ingredients or change nutrition. Reuse idempotency_key on retries.') + async def recipe_link_create(recipe_id: str, link: RecipeLinkInput, + idempotency_key: str | None = None) -> dict[str, Any]: + key = _key(idempotency_key) + data = await api_request('POST', f'recipes/{recipe_id}/links/', + payload=link.model_dump(mode='json'), idempotency_key=key) + return {**data, 'idempotency_key': key} + + @server.tool(structured_output=True, description='Edit the relationship, note, or reciprocal visibility of an owned recipe link.') + async def recipe_link_update(recipe_id: str, link_id: str, + changes: RecipeLinkChanges) -> dict[str, Any]: + return await api_request('PATCH', f'recipes/{recipe_id}/links/{link_id}/', + payload=changes.model_dump(mode='json', exclude_unset=True)) + + @server.tool(structured_output=True, description='Remove an owned suggestion link. The linked recipe remains unchanged.') + async def recipe_link_remove(recipe_id: str, link_id: str) -> dict[str, Any]: + return await api_request('DELETE', f'recipes/{recipe_id}/links/{link_id}/') + + @server.tool(structured_output=True, description='Preview an explicitly selected set of owned component recipes. Returns ingredient details, nutrition completeness, duplicate ingredient warnings, and issues. Suggestion links are excluded; this does not create a recipe, log a meal, or deduct inventory.') + async def recipe_composition_preview(components: list[RecipeComponentInput]) -> dict[str, Any]: + return await api_request('POST', 'recipes/compositions/preview/', + payload={'components': [item.model_dump(mode='json') for item in components]}) + + @server.tool(structured_output=True, description='Create a new draft recipe that explicitly includes selected owned component recipes. Existing source recipes remain intact. This does not log a meal or deduct inventory. Reuse idempotency_key on retries.') + async def recipe_composition_create(composition: RecipeCompositionInput, + idempotency_key: str | None = None) -> dict[str, Any]: + key = _key(idempotency_key) + data = await api_request('POST', 'recipes/compositions/', + payload=composition.model_dump(mode='json'), idempotency_key=key) + return {'recipe': data, 'idempotency_key': key} + @server.tool(structured_output=True, description='Save one owned recipe to your Cook Later list. Idempotent; does not log a meal or use inventory.') async def recipe_cook_later_add(recipe_id: str) -> dict[str, Any]: return await api_request('POST', 'recipes/cook-later/', payload={'recipe_id': recipe_id}) diff --git a/main/mcp_server/tests.py b/main/mcp_server/tests.py index 52e5753..3276fec 100644 --- a/main/mcp_server/tests.py +++ b/main/mcp_server/tests.py @@ -246,6 +246,21 @@ class McpHttpTests(LiveServerTestCase): asyncio.run(self._client(self.secret, check)) self.assertEqual(KitchenInventoryItem.objects.filter(pk__in=[item.pk for item in items], archived_at__isnull=False).count(), 2) + def test_inventory_bulk_update_handles_twenty_three_rows_in_one_call(self): + location = InventoryLocation.objects.create(user=self.a, name='Pantry') + items = [KitchenInventoryItem.objects.create(user=self.a, product=self.a_product, location=location) + for _ in range(23)] + + async def check(client): + request = {'items': [{'item_id': str(item.api_id), 'changes': {'notes': 'Audited'}} for item in items], + 'dry_run': False, 'atomic': True, 'idempotency_key': 'mcp-bulk-23'} + result = (await client.call_tool('inventory_bulk_update', request)).structured_content + self.assertEqual(result['counts']['changed'], 23) + self.assertEqual(result['status'], 'success') + + asyncio.run(self._client(self.secret, check)) + self.assertEqual(KitchenInventoryItem.objects.filter(user=self.a, notes='Audited').count(), 23) + def test_oauth_issued_pat_authenticates_mcp_with_journal_upgrade(self): application = Application.objects.create(user=self.a, name='Test ChatGPT style client', client_type=Application.CLIENT_PUBLIC, authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE, diff --git a/main/planner/middleware_demo.py b/main/planner/middleware_demo.py index 065d430..7508e69 100644 --- a/main/planner/middleware_demo.py +++ b/main/planner/middleware_demo.py @@ -53,7 +53,7 @@ class DemoAccountPolicyMiddleware: response = HttpResponse('Demo request limit reached. Try again in a minute.', status=429) response['Retry-After'] = '60' return response - forbidden = (path.startswith(('/o/', '/admin/', '/food/settings/tokens/')) or + forbidden = (path.startswith(('/o/', '/admin/', '/food/settings/tokens/', '/settings/home-assistant/')) or path == '/settings/notifications/ntfy/test/' or (request.method == 'POST' and path in { '/food/search/openfoodfacts/', '/food/import/openfoodfacts/', diff --git a/main/planner/templates/planner/user_settings.html b/main/planner/templates/planner/user_settings.html index 32ff231..692f538 100644 --- a/main/planner/templates/planner/user_settings.html +++ b/main/planner/templates/planner/user_settings.html @@ -9,7 +9,7 @@ {% endif %}

Sick Day Mode

Temporarily defer routine work while you recover.

{% csrf_token %}{% for field in sick_form %}{% if field.name == 'enabled' %}
{{ field.errors }}
{% else %}
{{ field.label_tag }}{{ field }}{% if field.help_text %}{{ field.help_text }}{% endif %}{{ field.errors }}
{% endif %}{% endfor %}

Hydration

Set your daily fluid goal and how drinks are counted.

{% csrf_token %}{% for field in hydration_goal_form %}
{{ field.label_tag }}{{ field }}{{ field.errors }}
{% endfor %}

Food

Set optional daily nutrition goals. Exercise calories do not change these goals.

{% csrf_token %}{% for field in food_goal_form %}
{{ field.label_tag }}{{ field }}{{ field.errors }}
{% endfor %}
- {% if not is_demo_account %}

API access

Create scoped tokens for apps that need access to your food log or kitchen inventory.

Manage API tokens
+ {% if not is_demo_account %}

Home Assistant

Connect a to-do list to your groceries.

Manage Home Assistant

API access

Create scoped tokens for apps that need access to your food log or kitchen inventory.

Manage API tokens

Notifications / ntfy

Enter your private topic URL, test it, then enable notifications.

diff --git a/main/recipes/api.py b/main/recipes/api.py index 1419cc2..e371f98 100644 --- a/main/recipes/api.py +++ b/main/recipes/api.py @@ -10,7 +10,7 @@ from django.http import JsonResponse from food.api import (_date, _decimal, _idempotent, _json, _time, _uuid, api_view, error) from food.models import FoodProduct, MealType -from .models import CookLater, Recipe, RecipeIngredient, RecipeSubrecipe, RecipeUse +from .models import CookLater, Recipe, RecipeIngredient, RecipeLink, RecipeSubrecipe, RecipeUse from .services import cook_later from .services.analysis import AnalysisError, analyze from .services.cooking import CookingError, complete, make_completion @@ -82,10 +82,26 @@ def _detail(recipe): 'unit': item.unit, 'note': item.note, 'display_order': item.display_order} for item in recipe.subrecipes.all()], variations=[{'id': str(item.api_id), 'title': item.title} - for item in recipe.variations.all()]) + for item in recipe.variations.all()], + linked_recipes=_link_data(recipe)) return data +def _link_data(recipe): + links = RecipeLink.objects.filter(owner=recipe.owner, source=recipe, + target__owner=recipe.owner).select_related('target') + incoming = RecipeLink.objects.filter(owner=recipe.owner, target=recipe, reciprocal=True, + source__owner=recipe.owner).select_related('source') + return ([{'link_id': str(link.id), 'recipe_id': str(link.target.api_id), + 'title': link.target.title, 'relationship': link.relationship, + 'note': link.note, 'reciprocal': link.reciprocal, 'direction': 'outgoing'} + for link in links[:50]] + + [{'link_id': str(link.id), 'recipe_id': str(link.source.api_id), + 'title': link.source.title, 'relationship': link.relationship, + 'note': link.note, 'reciprocal': True, 'direction': 'incoming'} + for link in incoming[:50]]) + + def _set_recipe(request, recipe, data): _unknown(data, RECIPE_FIELDS) for field, value in data.items(): @@ -198,6 +214,167 @@ def recipe_create(request): return _idempotent(request, data, action) +def _link_payload(link, direction='outgoing'): + other = link.target if direction == 'outgoing' else link.source + return {'link_id': str(link.id), 'recipe_id': str(other.api_id), 'title': other.title, + 'relationship': link.relationship, 'note': link.note, + 'reciprocal': link.reciprocal, 'direction': direction} + + +@api_view({'GET': 'food:read', 'POST': 'food:write'}, ('GET', 'POST')) +def recipe_links(request, identifier): + recipe = _owned(request, identifier) + if recipe is None: + return error('recipe_not_found', 'Recipe not found.', 404) + if request.method == 'GET': + return JsonResponse({'links': _link_data(recipe)}) + data = _json(request) + try: + _unknown(data, {'recipe_id', 'relationship', 'note', 'reciprocal'}) + target = _owned(request, _uuid(data.get('recipe_id'))) + if target is None: + return error('recipe_not_found', 'Linked recipe not found.', 404) + if target.pk == recipe.pk: + raise ValueError('A recipe cannot link to itself.') + relationship = data.get('relationship', RecipeLink.Relationship.GOES_WELL_WITH) + note = data.get('note', '') + reciprocal = data.get('reciprocal', True) + if relationship not in RecipeLink.Relationship.values: + raise ValueError('Choose a supported relationship.') + if not isinstance(note, str) or len(note) > 255: + raise ValueError('note must be at most 255 characters.') + if not isinstance(reciprocal, bool): + raise ValueError('reciprocal must be a boolean.') + if RecipeLink.objects.filter(owner=request.api_user, source=target, target=recipe).exists(): + raise ValueError('A reverse link already exists; edit that link instead.') + def action(): + try: + link = RecipeLink(owner=request.api_user, source=recipe, target=target, + relationship=relationship, note=note.strip(), reciprocal=reciprocal) + link.full_clean() + link.save() + except (ValidationError, IntegrityError): + return error('duplicate_link', 'This recipe link already exists.', 409) + return JsonResponse(_link_payload(link), status=201) + return _idempotent(request, data, action) + except (ValueError, ValidationError) as exc: + return error('invalid_recipe_link', str(exc)) + + +@api_view({'PATCH': 'food:write', 'DELETE': 'food:write'}, ('PATCH', 'DELETE')) +def recipe_link_item(request, identifier, link_id): + link = RecipeLink.objects.filter(owner=request.api_user, id=link_id).select_related('source', 'target').first() + if link is None or str(link.source.api_id) != str(identifier): + return error('recipe_link_not_found', 'Recipe link not found.', 404) + if request.method == 'DELETE': + link.delete() + return JsonResponse({'deleted': True, 'link_id': str(link_id)}) + data = _json(request) + try: + _unknown(data, {'relationship', 'note', 'reciprocal'}) + for field, value in data.items(): + if field == 'relationship' and value not in RecipeLink.Relationship.values: + raise ValueError('Choose a supported relationship.') + if field == 'note' and (not isinstance(value, str) or len(value) > 255): + raise ValueError('note must be at most 255 characters.') + if field == 'reciprocal' and not isinstance(value, bool): + raise ValueError('reciprocal must be a boolean.') + setattr(link, field, value.strip() if field == 'note' else value) + link.full_clean() + link.save() + except (ValueError, ValidationError) as exc: + return error('invalid_recipe_link', str(exc)) + return JsonResponse(_link_payload(link)) + + +def _composition(request, data): + components = data.get('components') + if not isinstance(components, list) or not 1 <= len(components) <= 10: + raise ValueError('Choose between one and ten component recipes.') + resolved, seen = [], set() + for row in components: + if not isinstance(row, dict) or set(row) != {'recipe_id', 'servings'}: + raise ValueError('Each component needs recipe_id and servings.') + component = _owned(request, _uuid(row['recipe_id'])) + if component is None: + raise ValueError('A selected component recipe was not found.') + if component.pk in seen: + raise ValueError('Choose each component recipe only once.') + seen.add(component.pk) + servings = _decimal(row['servings'], 'servings', positive=True) + if servings > Decimal('99999.99') or servings != servings.quantize(Decimal('.01')): + raise ValueError('Servings must be between 0.01 and 99,999.99 with at most two decimal places.') + resolved.append((component, servings)) + return resolved + + +@api_view('food:read', ('POST',)) +def recipe_composition_preview(request): + try: + resolved = _composition(request, _json(request)) + component_results, ingredient_counts = [], {} + nutrition_totals = {} + nutrition_completeness = {} + for component, servings in resolved: + result = analyze(component, request.api_user, servings) + for key, value in result['nutrition'].items(): + nutrition_completeness[key] = nutrition_completeness.get(key, True) and value['complete'] + if value['total'] is not None: + nutrition_totals[key] = nutrition_totals.get(key, Decimal(0)) + value['total'] + ingredients = [] + for line in result['lines']: + key = (line['name'].strip().casefold(), line['unit']) + ingredient_counts[key] = ingredient_counts.get(key, 0) + 1 + ingredients.append({'name': line['name'], 'quantity': str(line['quantity']), + 'unit': line['unit'], 'source': line['source']}) + component_results.append({'recipe_id': str(component.api_id), 'title': component.title, + 'servings': str(servings), 'ingredients': ingredients, + 'nutrition': {key: {'total': str(val['total']) if val['total'] is not None else None, + 'complete': val['complete']} for key, val in result['nutrition'].items()}, + 'issues': result['issues']}) + duplicates = [{'name': name, 'unit': unit} for (name, unit), count in ingredient_counts.items() if count > 1] + return JsonResponse({'components': component_results, 'duplicate_ingredients': duplicates, + 'nutrition': {key: {'total': str(nutrition_totals[key]) if key in nutrition_totals else None, + 'complete': nutrition_completeness[key] and key in nutrition_totals} + for key in nutrition_completeness}, + 'nutrition_complete': bool(nutrition_completeness) and all( + nutrition_completeness[key] and key in nutrition_totals for key in nutrition_completeness), + 'note': 'Preview only. Suggestions are excluded; inventory and diary are unchanged.'}) + except (ValueError, ValidationError, AnalysisError) as exc: + return error('invalid_composition', str(exc)) + + +@api_view('food:write', ('POST',)) +def recipe_composition_create(request): + data = _json(request) + try: + _unknown(data, {'title', 'default_servings', 'components'}) + title = data.get('title') + if not isinstance(title, str) or not title.strip() or len(title.strip()) > 180: + raise ValueError('Provide a title of at most 180 characters.') + root_servings = _decimal(data.get('default_servings', '2'), 'default_servings', positive=True) + if root_servings > Decimal('99999.99') or root_servings != root_servings.quantize(Decimal('.01')): + raise ValueError('default_servings must be between 0.01 and 99,999.99 with at most two decimal places.') + resolved = _composition(request, data) + except (ValueError, ValidationError) as exc: + return error('invalid_composition', str(exc)) + def action(): + try: + with transaction.atomic(): + recipe = Recipe(owner=request.api_user, title=title.strip(), default_servings=root_servings) + recipe.full_clean() + recipe.save() + for index, (component, servings) in enumerate(resolved): + relation = RecipeSubrecipe(recipe=recipe, component=component, + quantity=servings, unit='servings', display_order=index) + relation.full_clean() + relation.save() + except (ValidationError, IntegrityError) as exc: + return error('invalid_composition', str(exc)) + return JsonResponse(_detail(recipe), status=201) + return _idempotent(request, data, action) + + @api_view({'GET': 'food:read', 'PATCH': 'food:write', 'DELETE': 'food:write'}, ('GET', 'PATCH', 'DELETE')) def recipe_item(request, identifier): diff --git a/main/recipes/migrations/0007_recipelink.py b/main/recipes/migrations/0007_recipelink.py new file mode 100644 index 0000000..a1eb156 --- /dev/null +++ b/main/recipes/migrations/0007_recipelink.py @@ -0,0 +1,35 @@ +# Generated by Django 6.1.1 on 2026-10-23 11:53 + +import django.db.models.deletion +import uuid +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('recipes', '0006_cooklater'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='RecipeLink', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('relationship', models.CharField(choices=[('goes_well_with', 'Goes well with'), ('try_with', 'Try with'), ('alternative_to', 'Alternative to')], default='goes_well_with', max_length=24)), + ('note', models.CharField(blank=True, max_length=255)), + ('reciprocal', models.BooleanField(default=True)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='recipe_links', to=settings.AUTH_USER_MODEL)), + ('source', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='outgoing_links', to='recipes.recipe')), + ('target', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='incoming_links', to='recipes.recipe')), + ], + options={ + 'ordering': ('created_at', 'id'), + 'constraints': [models.UniqueConstraint(fields=('owner', 'source', 'target'), name='recipes_unique_link_direction'), models.CheckConstraint(condition=models.Q(('source', models.F('target')), _negated=True), name='recipes_link_not_self')], + }, + ), + ] diff --git a/main/recipes/models.py b/main/recipes/models.py index 26481a3..03187b5 100644 --- a/main/recipes/models.py +++ b/main/recipes/models.py @@ -187,6 +187,44 @@ class RecipeSubrecipe(models.Model): return f'{self.recipe} includes {self.component}' +class RecipeLink(models.Model): + """A non-compositional suggestion between two recipes owned by one user.""" + + class Relationship(models.TextChoices): + GOES_WELL_WITH = 'goes_well_with', 'Goes well with' + TRY_WITH = 'try_with', 'Try with' + ALTERNATIVE_TO = 'alternative_to', 'Alternative to' + + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, + related_name='recipe_links') + source = models.ForeignKey(Recipe, on_delete=models.CASCADE, related_name='outgoing_links') + target = models.ForeignKey(Recipe, on_delete=models.CASCADE, related_name='incoming_links') + relationship = models.CharField(max_length=24, choices=Relationship.choices, + default=Relationship.GOES_WELL_WITH) + note = models.CharField(max_length=255, blank=True) + reciprocal = models.BooleanField(default=True) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta: + ordering = ('created_at', 'id') + constraints = [ + models.UniqueConstraint(fields=('owner', 'source', 'target'), name='recipes_unique_link_direction'), + models.CheckConstraint(condition=~Q(source=models.F('target')), name='recipes_link_not_self'), + ] + + def clean(self): + super().clean() + if self.source_id and self.target_id: + if self.source_id == self.target_id: + raise ValidationError({'target': 'A recipe cannot link to itself.'}) + if self.source.owner_id != self.owner_id or self.target.owner_id != self.owner_id: + raise ValidationError({'target': 'Choose one of your own recipes.'}) + if self.relationship not in self.Relationship.values: + raise ValidationError({'relationship': 'Choose a supported relationship.'}) + + class RecipeIngredientSubstitution(models.Model): ingredient = models.ForeignKey(RecipeIngredient, on_delete=models.CASCADE, related_name='substitutions') diff --git a/main/recipes/static/recipes/recipe_picker.js b/main/recipes/static/recipes/recipe_picker.js new file mode 100644 index 0000000..1fbf55a --- /dev/null +++ b/main/recipes/static/recipes/recipe_picker.js @@ -0,0 +1,33 @@ +(() => { + const form = document.querySelector('[data-recipe-link-picker]'); + if (!form) return; + const search = form.querySelector('[data-recipe-search], [data-component-search]'); + const empty = form.querySelector('[data-picker-empty]'); + const clear = form.querySelector('[data-clear-search]'); + if (!search) return; + + const filter = () => { + const query = search.value.trim().toLocaleLowerCase(); + let matches = 0; + const select = form.querySelector('#linked-recipe'); + if (select) { + [...select.options].slice(1).forEach(option => { + const visible = option.textContent.toLocaleLowerCase().includes(query); + option.hidden = !visible; + if (visible) matches += 1; + }); + } + form.querySelectorAll('[data-recipe-name]').forEach(card => { + const visible = card.dataset.recipeName.includes(query); + card.hidden = !visible; + if (visible) matches += 1; + }); + if (empty) empty.hidden = !query || matches > 0; + }; + search.addEventListener('input', filter); + clear?.addEventListener('click', () => { + search.value = ''; + filter(); + search.focus(); + }); +})(); diff --git a/main/recipes/static/recipes/recipes.css b/main/recipes/static/recipes/recipes.css index a178b8f..caaab0c 100644 --- a/main/recipes/static/recipes/recipes.css +++ b/main/recipes/static/recipes/recipes.css @@ -46,3 +46,6 @@ body[data-theme=dark] .cook-later{--recipe-ink:#eaf0ea;--recipe-muted:#afbbb2;-- .recipes-field .recipe-photo-editor__zone{display:grid;margin:0;align-items:center;justify-items:center} .recipes-field label.recipe-photo-editor__zone{display:grid;margin:0;align-items:center;justify-items:center} .recipe-photo-editor__prompt small,body[data-theme=dark] .recipe-photo-editor__prompt small{color:#415c4b} + +.recipes-linked{margin-top:18px}.recipes-linked__heading{display:flex;align-items:center;justify-content:space-between;gap:14px}.recipes-linked__heading h3{margin:0}.recipes-linked__heading p{margin:5px 0 0}.recipes-linked__heading>div:last-child{display:flex;flex-wrap:wrap;gap:8px}.recipes-linked-card{display:flex;align-items:center;justify-content:space-between;gap:14px;padding:14px 0;border-top:1px solid var(--recipe-line)}.recipes-linked-card>div{display:grid;gap:4px}.recipes-linked-actions{display:flex;gap:8px}.recipes-linked-actions a,.recipes-linked-actions button{display:inline-flex;align-items:center;padding:8px 13px;border:1px solid var(--recipe-line);border-radius:8px;color:var(--recipe-ink);background:transparent;text-decoration:none}.recipes-linked-card small{color:var(--recipe-muted)}.recipes-linked-card p{margin:3px 0 0}.recipes-linked-card a{color:var(--recipe-accent)}.recipes-linked-card button,.recipes-linked__heading a,.recipes-link-form button,.recipes-compose button{min-height:44px}.recipes-link-form,.recipes-compose{max-width:900px}.recipes-link-form .recipes-edit-card>section,.recipes-compose .recipes-edit-card>section{padding:20px}.recipes-compose-list{display:grid;gap:8px}.recipes-compose-choice{display:grid;grid-template-columns:22px minmax(0,1fr) minmax(150px,220px);align-items:center;gap:12px;padding:12px;border:1px solid var(--recipe-line);border-radius:10px;background:var(--recipe-surface)}.recipes-compose-choice>span:nth-child(2){display:grid;gap:4px}.recipes-compose-choice small,.recipes-compose-choice .recipes-field>span{color:var(--recipe-muted);font-size:.8rem}.recipes-compose-choice input[type=checkbox]{width:20px;height:20px;accent-color:var(--recipe-accent)}.recipes-compose-choice input[type=number],.recipes-link-form select,.recipes-link-form input:not([type=checkbox]){width:100%;min-height:44px;padding:8px;border:1px solid var(--recipe-line);border-radius:8px;background:var(--recipe-surface);color:var(--recipe-ink)}.recipes-compose-preview{margin-top:18px}.recipes-compose-preview h2{margin:0 0 10px}.recipes-compose-preview article{padding:12px 0;border-top:1px solid var(--recipe-line)}.recipes-compose-preview h3{margin:0;font-size:1rem}.recipes-compose-preview ul{margin-bottom:0}.recipes-linked :is(a,button):focus-visible,.recipes-compose :is(a,button,input,select):focus-visible{outline:3px solid #a44cbe;outline-offset:3px} +@media(max-width:767px){.recipes-linked__heading{align-items:flex-start;flex-direction:column}.recipes-linked__heading>div:last-child{width:100%}.recipes-linked__heading a{flex:1;text-align:center}.recipes-linked-card{align-items:flex-start}.recipes-linked-actions{flex-wrap:wrap}.recipes-compose-choice{grid-template-columns:22px minmax(0,1fr)}.recipes-compose-choice .recipes-field{grid-column:2}.recipes-compose .recipes-form-actions>*{width:100%;text-align:center}} diff --git a/main/recipes/templates/recipes/compose.html b/main/recipes/templates/recipes/compose.html new file mode 100644 index 0000000..f48a270 --- /dev/null +++ b/main/recipes/templates/recipes/compose.html @@ -0,0 +1,3 @@ +{% extends 'planner/base.html' %}{% load static %} +{% block title %}Build a recipe · Tasknest{% endblock %} +{% block content %}
← Back to recipes

Build a recipe

Create a new recipe from explicitly selected components. Linked suggestions are never included.

{% if error %}{% endif %}
{% csrf_token %}

New recipe

Choose components

Select up to ten recipes and set the servings to include from each.

{% for item in available %}{% empty %}

Create a recipe first to use it as a component.

{% endfor %}
{% if preview %}

Component preview

Preview only. Creating this recipe will preserve each source recipe and add explicit component references.

Combined nutrition estimate

{% for row in nutrition_preview %}{{ row.label }}: {% if row.total is not None %}{{ row.total|floatformat:1 }} {{ row.unit }}{% else %}unknown{% endif %}{% if not row.complete %} · incomplete{% endif %}{% if not forloop.last %} · {% endif %}{% endfor %}

{% if duplicates %}

Ingredient overlap: {% for name in duplicates %}{{ name }}{% if not forloop.last %}, {% endif %}{% endfor %}. Check that these components should both be included.

{% endif %}{% for component, result in preview %}

{{ component.title }} · {{ result.servings }} servings

    {% for line in result.lines %}
  • {{ line.quantity|floatformat:-3 }} {{ line.unit }} {{ line.name }}
  • {% empty %}
  • No structured ingredients yet.
  • {% endfor %}

{% for row in result.nutrition_rows %}{{ row.label }}: {% if row.total is not None %}{{ row.total|floatformat:1 }} {{ row.unit }}{% else %}unknown{% endif %}{% if not row.complete %} · incomplete{% endif %}{% if not forloop.last %} · {% endif %}{% endfor %}

{% if result.issues %}

Nutrition or inventory data may be incomplete.

{% endif %}
{% endfor %}
{% endif %}
{% endblock %} diff --git a/main/recipes/templates/recipes/library.html b/main/recipes/templates/recipes/library.html index 0b5a4bd..cea1449 100644 --- a/main/recipes/templates/recipes/library.html +++ b/main/recipes/templates/recipes/library.html @@ -51,6 +51,10 @@

Ingredients

    {% for line in analysis.lines %}
  • {{ line.name }}{% if line.source %}From {{ line.source }}{% endif %}{% if line.ingredient.preparation_note %}{{ line.ingredient.preparation_note }}{% endif %}{{ line.status|capfirst }}{% if line.reason %}: {{ line.reason }}{% endif %}{{ line.quantity|floatformat:-3 }} {{ line.unit }}{% if line.optional %}Optional{% if not analysis.include_optional %} · excluded{% endif %}{% endif %}
  • {% empty %}
  • No ingredients added yet.
  • {% endfor %}

Instructions

{% if selected.instructions_markdown %}
{{ selected.instructions_markdown|recipe_markdown }}
{% else %}

No instructions added yet.

{% endif %}
{% if selected.notes_markdown %}

Notes

{{ selected.notes_markdown|recipe_markdown }}
{% endif %}
+
+

Linked recipes

Suggestions stay separate from this recipe’s ingredients and nutrition.

+ {% for link, other, direction in linked_recipes %}
{{ other.title }}{{ link.get_relationship_display }}{% if direction == 'incoming' %} · shared by {{ link.source.title }}{% endif %}{% if link.note %}

{{ link.note }}

{% endif %}
Edit
{% csrf_token %}
{% empty %}

Link your first recipe to keep a pairing or alternative close by.

{% endfor %} +
{% else %}

Make room for something delicious.

Save a recipe to see its ingredients and instructions here.

Create your first recipe
{% endif %} diff --git a/main/recipes/templates/recipes/link_form.html b/main/recipes/templates/recipes/link_form.html new file mode 100644 index 0000000..e70caee --- /dev/null +++ b/main/recipes/templates/recipes/link_form.html @@ -0,0 +1,3 @@ +{% extends 'planner/base.html' %}{% load static %} +{% block title %}Link a recipe · Tasknest{% endblock %} +{% block content %}{% endblock %} diff --git a/main/recipes/test_api.py b/main/recipes/test_api.py index 77ed9ff..756527e 100644 --- a/main/recipes/test_api.py +++ b/main/recipes/test_api.py @@ -182,3 +182,37 @@ class RecipeApiTests(TestCase): {'consumed_servings': '1', 'meal': 'lunch'}, key='meal-with-shortage').status_code, 201) self.package.refresh_from_db() self.assertEqual(self.package.remaining_amount, Decimal('300')) + + def test_recipe_suggestion_and_explicit_composition_api(self): + base_id = self.create() + fries = self.call('post', 'recipes/create/', {'title': 'Fries'}, key='fries').json()['id'] + linked = self.call('post', f'recipes/{base_id}/links/', { + 'recipe_id': fries, 'relationship': 'goes_well_with', 'note': 'Serve hot', + 'reciprocal': True}, key='link-fries') + self.assertEqual(linked.status_code, 201, linked.content) + replay = self.call('post', f'recipes/{base_id}/links/', { + 'recipe_id': fries, 'relationship': 'goes_well_with', 'note': 'Serve hot', + 'reciprocal': True}, key='link-fries') + self.assertEqual(replay.status_code, 200) + self.assertEqual(replay.json()['link_id'], linked.json()['link_id']) + self.assertEqual(self.call('get', f'recipes/{fries}/').json()['linked_recipes'][0]['recipe_id'], base_id) + edited = self.call('patch', f'recipes/{base_id}/links/{linked.json()["link_id"]}/', + {'note': 'Serve warm', 'reciprocal': False}) + self.assertEqual(edited.json()['note'], 'Serve warm') + self.assertEqual(self.call('get', f'recipes/{fries}/').json()['linked_recipes'], []) + self.assertEqual(self.call('post', f'recipes/{base_id}/links/', { + 'recipe_id': str(self.other_recipe.api_id)}).status_code, 404) + self.assertEqual(self.call('post', 'recipes/compositions/preview/', { + 'components': [{'recipe_id': base_id, 'servings': '2'}, + {'recipe_id': fries, 'servings': '1'}]}).status_code, 200) + composed = self.call('post', 'recipes/compositions/', { + 'title': 'Meal plate', 'default_servings': '2', 'components': [ + {'recipe_id': base_id, 'servings': '2'}, {'recipe_id': fries, 'servings': '1'}]}, + key='meal-plate') + self.assertEqual(composed.status_code, 201, composed.content) + self.assertEqual(len(composed.json()['subrecipes']), 2) + self.assertEqual(self.call('post', 'recipes/compositions/', { + 'title': 'bad', 'components': [{'recipe_id': str(self.other_recipe.api_id), 'servings': '1'}] + }).status_code, 400) + self.assertEqual(self.call('delete', f'recipes/{base_id}/links/{linked.json()["link_id"]}/').status_code, 200) + self.assertEqual(self.call('get', f'recipes/{base_id}/').json()['linked_recipes'], []) diff --git a/main/recipes/tests.py b/main/recipes/tests.py index a1325a0..1825257 100644 --- a/main/recipes/tests.py +++ b/main/recipes/tests.py @@ -11,7 +11,7 @@ from django.test.utils import override_settings from django.urls import reverse from PIL import Image -from .models import Recipe, RecipeIngredient, RecipeSubrecipe +from .models import Recipe, RecipeIngredient, RecipeLink, RecipeSubrecipe from .templatetags.recipe_markdown import recipe_markdown @@ -86,6 +86,42 @@ class RecipeWebTests(TestCase): with self.assertRaises(ValidationError): RecipeIngredient(recipe=a, name='Flour', quantity=Decimal('-1'), unit='g').full_clean() + def test_linked_recipe_and_composition_web_flows(self): + main = Recipe.objects.create(owner=self.user, title='Main') + side = Recipe.objects.create(owner=self.user, title='Side') + other = Recipe.objects.create(owner=self.other, title='Private') + response = self.client.post(reverse('recipes:link', args=[main.api_id]), { + 'recipe_id': str(side.api_id), 'relationship': 'goes_well_with', + 'note': 'Serve together', 'reciprocal': '1'}) + self.assertRedirects(response, reverse('recipes:detail', args=[main.api_id])) + link = RecipeLink.objects.get() + edit_url = reverse('recipes:link-edit', args=[main.api_id, link.id]) + self.assertContains(self.client.get(edit_url), 'Edit linked recipe') + self.assertEqual(self.client.post(edit_url, { + 'relationship': 'alternative_to', 'note': 'Different texture', + 'reciprocal': ''}).status_code, 302) + link.refresh_from_db() + self.assertEqual((link.relationship, link.note, link.reciprocal), + ('alternative_to', 'Different texture', False)) + self.assertContains(self.client.get(reverse('recipes:detail', args=[side.api_id])), 'Main') + response = self.client.post(reverse('recipes:link', args=[main.api_id]), { + 'recipe_id': str(other.api_id), 'relationship': 'goes_well_with'}) + self.assertEqual(response.status_code, 404) + preview = self.client.post(reverse('recipes:compose'), { + 'component': [str(main.api_id), str(side.api_id)], + f'servings_{main.api_id}': '2', f'servings_{side.api_id}': '1', + 'action': 'preview'}) + self.assertEqual(preview.status_code, 200) + self.assertContains(preview, 'Component preview') + created = self.client.post(reverse('recipes:compose'), { + 'title': 'Combined', 'default_servings': '2', + 'component': [str(main.api_id), str(side.api_id)], + f'servings_{main.api_id}': '2', f'servings_{side.api_id}': '1', + 'action': 'create'}) + self.assertEqual(created.status_code, 302) + combined = Recipe.objects.get(title='Combined') + self.assertEqual(set(combined.subrecipes.values_list('component_id', flat=True)), {main.pk, side.pk}) + def test_photo_upload_is_private(self): image = BytesIO() Image.new('RGB', (2, 2), '#217857').save(image, format='PNG') diff --git a/main/recipes/urls.py b/main/recipes/urls.py index 4ab05f5..83a100d 100644 --- a/main/recipes/urls.py +++ b/main/recipes/urls.py @@ -15,6 +15,10 @@ urlpatterns = [ path('match//photo/', views.match_photo, name='match-photo'), path('new/', views.create, name='create'), path('new/autosave/', views.autosave_new, name='autosave-new'), + path('compose/', views.compose_recipe, name='compose'), + path('/links/new/', views.link_recipe, name='link'), + path('/links//edit/', views.link_recipe, name='link-edit'), + path('/links//remove/', views.unlink_recipe, name='unlink'), path('/', views.detail, name='detail'), path('/edit/', views.edit, name='edit'), path('/photo/upload/', views.photo_upload, name='photo-upload'), diff --git a/main/recipes/views.py b/main/recipes/views.py index e846b06..51c3a3c 100644 --- a/main/recipes/views.py +++ b/main/recipes/views.py @@ -20,7 +20,7 @@ from PIL import Image from .forms import (RecipeForm, RecipeIngredientFormSet, RecipeSubrecipeFormSet, RecipeSubstitutionForm, validate_recipe_photo) from .models import (CookLater, Recipe, RecipeIngredient, RecipeIngredientSubstitution, - RecipeSelection, RecipeSubrecipe, RecipeUse) + RecipeLink, RecipeSelection, RecipeSubrecipe, RecipeUse) from .services import cook_later from .services.analysis import AnalysisError, analyze from .services.cooking import CookingError, complete, instruction_steps, make_completion @@ -153,12 +153,19 @@ def _library(request, selected=None): .values_list('category', flat=True).distinct()) tags = sorted({tag for values in _owned(request.user).values_list('tags', flat=True) for tag in values if isinstance(tag, str)}) + linked = [] + if selected: + linked.extend((link, link.target, 'outgoing') for link in RecipeLink.objects.filter( + owner=request.user, source=selected, target__owner=request.user).select_related('target')) + linked.extend((link, link.source, 'incoming') for link in RecipeLink.objects.filter( + owner=request.user, target=selected, reciprocal=True, + source__owner=request.user).select_related('source')) return render(request, 'recipes/library.html', { 'recipes': recipes, 'selected': selected, 'query': query, 'category': category, 'tag': tag, 'favourite': favourite, 'categories': categories, 'tags': tags, 'explicit_detail': explicit_detail, 'analysis': analysis, 'analysis_error': analysis_error, 'last_use': last_use, - 'saved_ids': saved_ids, + 'saved_ids': saved_ids, 'linked_recipes': linked, }) @@ -174,6 +181,121 @@ def detail(request, identifier): return _library(request, recipe) +@login_required +@require_http_methods(['GET', 'POST']) +def link_recipe(request, identifier, link_id=None): + recipe = get_object_or_404(_owned(request.user), api_id=identifier) + existing_link = get_object_or_404(RecipeLink, owner=request.user, source=recipe, id=link_id) if link_id else None + if request.method == 'POST': + target = existing_link.target if existing_link else get_object_or_404( + _owned(request.user), api_id=request.POST.get('recipe_id')) + if target.pk == recipe.pk: + messages.error(request, 'A recipe cannot link to itself.') + elif not existing_link and RecipeLink.objects.filter(owner=request.user, source=target, target=recipe).exists(): + messages.error(request, 'A reverse link already exists. Edit that link instead.') + else: + link = existing_link or RecipeLink(owner=request.user, source=recipe, target=target) + link.relationship = request.POST.get('relationship', RecipeLink.Relationship.GOES_WELL_WITH) + link.note = request.POST.get('note', '').strip()[:255] + link.reciprocal = request.POST.get('reciprocal') == '1' + try: + link.full_clean() + link.save() + messages.success(request, f'Updated the link to {target.title}.' if existing_link + else f'Linked {target.title} to {recipe.title}.') + return redirect('recipes:detail', identifier=identifier) + except ValidationError as exc: + messages.error(request, '; '.join(exc.messages)) + return render(request, 'recipes/link_form.html', {'recipe': recipe, + 'choices': RecipeLink.Relationship.choices, + 'available': _owned(request.user).exclude(pk=recipe.pk).order_by('title'), + 'existing_link': existing_link, + 'form_data': request.POST if request.method == 'POST' else None}) + + +@login_required +@require_POST +def unlink_recipe(request, identifier, link_id): + recipe = get_object_or_404(_owned(request.user), api_id=identifier) + link = get_object_or_404(RecipeLink, owner=request.user, source=recipe, id=link_id) + title = link.target.title + link.delete() + messages.success(request, f'Removed {title} from linked recipes.') + return redirect('recipes:detail', identifier=identifier) + + +@login_required +@require_http_methods(['GET', 'POST']) +def compose_recipe(request): + available = list(_owned(request.user).order_by('title')) + preview = None + error = '' + duplicates = [] + nutrition_preview = [] + selected_rows = [] + if request.method == 'POST': + ids = request.POST.getlist('component')[:10] + selected_rows = [(value, request.POST.get(f'servings_{value}', '')) for value in ids] + if request.POST.get('action') == 'create': + title = request.POST.get('title', '').strip() + if not title or len(title) > 180 or not ids: + error = 'Enter a title and choose at least one component recipe.' + else: + try: + with transaction.atomic(): + root = Recipe(owner=request.user, title=title, + default_servings=request.POST.get('default_servings', '2')) + root.full_clean() + root.save() + for index, (api_id, servings) in enumerate(selected_rows): + component = get_object_or_404(_owned(request.user), api_id=api_id) + relation = RecipeSubrecipe(recipe=root, component=component, + quantity=servings, unit='servings', display_order=index) + relation.full_clean() + relation.save() + messages.success(request, 'Created a new recipe from the selected components.') + return redirect('recipes:detail', identifier=root.api_id) + except (ValidationError, ValueError) as exc: + error = '; '.join(getattr(exc, 'messages', [str(exc)])) + else: + try: + component_results = [] + for api_id, servings in selected_rows: + component = get_object_or_404(_owned(request.user), api_id=api_id) + result = analyze(component, request.user, servings) + component_results.append((component, result)) + preview = component_results + ingredient_counts = {} + for component, result in component_results: + for line in result['lines']: + key = (line['name'].strip().casefold(), line['unit']) + if key in ingredient_counts: + ingredient_counts[key][1] += 1 + else: + ingredient_counts[key] = [line['name'], 1] + duplicates = [name for name, count in ingredient_counts.values() if count > 1] + fields = {} + for component, result in component_results: + for row in result['nutrition_rows']: + current = fields.setdefault(row['label'], { + 'label': row['label'], 'unit': row['unit'], 'total': 0, + 'known': False, 'complete': True}) + if row['total'] is not None: + current['total'] += row['total'] + current['known'] = True + current['complete'] = current['complete'] and row['complete'] + nutrition_preview = [{**row, 'total': row['total'] if row['known'] else None, + 'complete': row['complete'] and row['known']} + for row in fields.values()] + if not selected_rows: + error = 'Choose at least one component recipe to preview.' + except (AnalysisError, ValueError) as exc: + error = str(exc) + return render(request, 'recipes/compose.html', {'available': available, + 'selected_rows': selected_rows, 'preview': preview, 'duplicates': duplicates, + 'nutrition_preview': nutrition_preview, 'error': error}) + + def _cooking_analysis(request, recipe): selected = {key[4:]: value for key, value in request.GET.items() if key.startswith('sub_') and value} diff --git a/main/requirements.txt b/main/requirements.txt index dd68e12..1aa6ea8 100644 --- a/main/requirements.txt +++ b/main/requirements.txt @@ -1,6 +1,7 @@ asarPy==1.0.1 asgiref==3.12.1 Django==6.1.1 +cryptography==50.0.2 whitenoise==6.12.0 Pillow==12.3.0 django-oauth-toolkit==3.4.1 diff --git a/main/tasksnest/settings.py b/main/tasksnest/settings.py index 87200b5..adb5559 100644 --- a/main/tasksnest/settings.py +++ b/main/tasksnest/settings.py @@ -63,6 +63,7 @@ INSTALLED_APPS = [ 'food.apps.FoodConfig', 'recipes', 'groceries', + 'home_assistant', 'stress', 'dreams', ] diff --git a/main/tasksnest/urls.py b/main/tasksnest/urls.py index 634e40e..fde3732 100644 --- a/main/tasksnest/urls.py +++ b/main/tasksnest/urls.py @@ -28,6 +28,7 @@ urlpatterns = [ path('food/', include('food.urls', namespace='food')), path('recipes/', include('recipes.urls', namespace='recipes')), path('groceries/', include('groceries.urls', namespace='groceries')), + path('settings/home-assistant/', include('home_assistant.urls', namespace='home_assistant')), path('stress/', include('stress.urls', namespace='stress')), path('dreams/', include('dreams.urls', namespace='dreams')), path('', include('planner.urls')),