Add MCP nutrition ingredient import and classification
This commit is contained in:
+12
@@ -139,3 +139,15 @@ The OAuth consent page displays the requested scopes. OAuth access tokens are us
|
||||
## Verify
|
||||
|
||||
Run `python manage.py test food.test_oauth_mcp mcp_server.tests` from `main/`. These tests exercise OAuth issuance and revocation, the live HTTP MCP protocol, scopes, user isolation, image upload, idempotency, and tool results. A real external ChatGPT connection still requires a public HTTPS deployment and a client sign-in test.
|
||||
|
||||
## Structured nutrition import (v1 working slice)
|
||||
|
||||
A standard MCP client can submit PDF-extracted JSON to `nutrition_import_preview(payload)` and then, after user review, `nutrition_import_commit(payload, confirmation, idempotency_key)`. The payload is provider independent. The client must calculate the SHA-256 of the original PDF bytes; the server stores its hash and filename, not a public file URL. The server currently accepts up to 100 rows per document.
|
||||
|
||||
```json
|
||||
{"source":{"programme":"Body Support","document_name":"shopping.pdf","sha256":"<64 hex digits>"},"ingredients":[{"row_key":"page1-broccoli","original_label":"Broccoli vers","canonical_name":"broccoli","quantity":"250","unit":"g","page":1,"preparation":"raw","category":"vegetable","subcategory":"cruciferous","confidence":"0.9"}]}
|
||||
```
|
||||
|
||||
Each row may also include `form`, `aliases`, `nutrition` (energy_kcal, protein_g, carbohydrates_g, fat_g, fibre_g), and `uncertainty`. Omitted nutrition stays unknown. Supported culinary categories are vegetable, fruit, herb, spice, bread, grain, meat, fish, dairy, legume, nuts_seeds, oil_fat, sauce, beverage, other and unresolved. Preparation is raw, cooked, dry, prepared or unknown. Preview flags unresolved category or preparation and unsupported units. Commit rejects unresolved rows, requires a confirmation no older than ten minutes and an idempotency key, and preserves original wording and source page. A repeated key returns the prior result; a different key for the same PDF is rejected for review as a possible amendment.
|
||||
|
||||
`nutrition_ingredients_search` returns canonical IDs, classifications, source labels/pages and read-only exact-name stock candidates. Filter by category, subcategory, programme, form, verification, query, or inventory status, with bounded pagination. An exact name candidate does not establish equivalent nutrition or sufficient stock. `nutrition_ingredient_correct` lets the owner manually correct category, subcategory or aliases while leaving source rows unchanged. All operations use existing food read/write scopes and per-user ownership. Import never writes inventory, groceries, recipes or diary. Recipe review, planned meals, source revisions, richer matching and optional search enrichment remain later card scope.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from django.urls import path
|
||||
from . import api, api_schema
|
||||
from . import api, api_schema, nutrition_api
|
||||
from recipes import api as recipe_api
|
||||
from stress import api as stress_api
|
||||
from dreams import api as dream_api
|
||||
@@ -23,6 +23,10 @@ urlpatterns = [
|
||||
path('food/attachments/<uuid:identifier>/file/', api.attachment_file),
|
||||
path('food/attachments/<uuid:identifier>/', api.attachment_delete),
|
||||
path('kitchen/items/', api.inventory_collection),
|
||||
path('nutrition/preview/', nutrition_api.preview),
|
||||
path('nutrition/commit/', nutrition_api.commit),
|
||||
path('nutrition/ingredients/', nutrition_api.ingredients),
|
||||
path('nutrition/ingredients/<uuid:identifier>/', nutrition_api.ingredient_correct),
|
||||
path('kitchen/import-groceries/', api.inventory_import_groceries),
|
||||
path('kitchen/bulk-update/', api.inventory_bulk_update),
|
||||
path('kitchen/bulk-delete/', api.inventory_bulk_delete),
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
# Generated by Django 6.1.1 on 2026-11-02 10:10
|
||||
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('food', '0016_inventoryscanrun_food_one_active_scan_run'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='CanonicalIngredient',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('name', models.CharField(max_length=180)),
|
||||
('normalized_name', models.CharField(max_length=180)),
|
||||
('category', models.CharField(max_length=40)),
|
||||
('subcategory', models.CharField(blank=True, max_length=80)),
|
||||
('form', models.CharField(blank=True, max_length=80)),
|
||||
('preparation', models.CharField(default='unknown', max_length=20)),
|
||||
('aliases', models.JSONField(default=list)),
|
||||
('attributes', models.JSONField(default=dict)),
|
||||
('confidence', models.DecimalField(decimal_places=3, max_digits=4)),
|
||||
('provenance', models.CharField(default='source_inference', max_length=40)),
|
||||
('classification_version', models.PositiveIntegerField(default=1)),
|
||||
('verified', models.BooleanField(default=False)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='NutritionSource',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('programme', models.CharField(max_length=80)),
|
||||
('document_name', models.CharField(max_length=255)),
|
||||
('sha256', models.CharField(max_length=64)),
|
||||
('week_start', models.DateField(blank=True, null=True)),
|
||||
('week_end', models.DateField(blank=True, null=True)),
|
||||
('payload_hash', models.CharField(max_length=64)),
|
||||
('idempotency_key', models.CharField(max_length=128)),
|
||||
('imported_at', models.DateTimeField(auto_now_add=True)),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='SuggestedIngredient',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('row_key', models.CharField(max_length=100)),
|
||||
('original_label', models.CharField(max_length=255)),
|
||||
('quantity', models.DecimalField(decimal_places=3, max_digits=10)),
|
||||
('unit', models.CharField(max_length=20)),
|
||||
('preparation', models.CharField(max_length=20)),
|
||||
('page', models.PositiveIntegerField()),
|
||||
('nutrition', models.JSONField(default=dict)),
|
||||
('uncertainty', models.JSONField(default=list)),
|
||||
('canonical', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='suggestions', to='food.canonicalingredient')),
|
||||
('source', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ingredients', to='food.nutritionsource')),
|
||||
],
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='canonicalingredient',
|
||||
constraint=models.UniqueConstraint(fields=('user', 'normalized_name', 'form', 'preparation'), name='food_unique_canonical_identity'),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='nutritionsource',
|
||||
constraint=models.UniqueConstraint(fields=('user', 'sha256'), name='food_unique_nutrition_document'),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='nutritionsource',
|
||||
constraint=models.UniqueConstraint(fields=('user', 'idempotency_key'), name='food_unique_nutrition_key'),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='suggestedingredient',
|
||||
constraint=models.UniqueConstraint(fields=('source', 'row_key'), name='food_unique_nutrition_row'),
|
||||
),
|
||||
]
|
||||
@@ -180,6 +180,59 @@ class InventoryCategory(models.Model):
|
||||
return self.name
|
||||
|
||||
|
||||
class CanonicalIngredient(models.Model):
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
name = models.CharField(max_length=180)
|
||||
normalized_name = models.CharField(max_length=180)
|
||||
category = models.CharField(max_length=40)
|
||||
subcategory = models.CharField(max_length=80, blank=True)
|
||||
form = models.CharField(max_length=80, blank=True)
|
||||
preparation = models.CharField(max_length=20, default='unknown')
|
||||
aliases = models.JSONField(default=list)
|
||||
attributes = models.JSONField(default=dict)
|
||||
confidence = models.DecimalField(max_digits=4, decimal_places=3)
|
||||
provenance = models.CharField(max_length=40, default='source_inference')
|
||||
classification_version = models.PositiveIntegerField(default=1)
|
||||
verified = models.BooleanField(default=False)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [models.UniqueConstraint(fields=('user', 'normalized_name', 'form', 'preparation'), name='food_unique_canonical_identity')]
|
||||
|
||||
|
||||
class NutritionSource(models.Model):
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
programme = models.CharField(max_length=80)
|
||||
document_name = models.CharField(max_length=255)
|
||||
sha256 = models.CharField(max_length=64)
|
||||
week_start = models.DateField(null=True, blank=True)
|
||||
week_end = models.DateField(null=True, blank=True)
|
||||
payload_hash = models.CharField(max_length=64)
|
||||
idempotency_key = models.CharField(max_length=128)
|
||||
imported_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [models.UniqueConstraint(fields=('user', 'sha256'), name='food_unique_nutrition_document'), models.UniqueConstraint(fields=('user', 'idempotency_key'), name='food_unique_nutrition_key')]
|
||||
|
||||
|
||||
class SuggestedIngredient(models.Model):
|
||||
source = models.ForeignKey(NutritionSource, on_delete=models.CASCADE, related_name='ingredients')
|
||||
canonical = models.ForeignKey(CanonicalIngredient, on_delete=models.PROTECT, related_name='suggestions')
|
||||
row_key = models.CharField(max_length=100)
|
||||
original_label = models.CharField(max_length=255)
|
||||
quantity = models.DecimalField(max_digits=10, decimal_places=3)
|
||||
unit = models.CharField(max_length=20)
|
||||
preparation = models.CharField(max_length=20)
|
||||
page = models.PositiveIntegerField()
|
||||
nutrition = models.JSONField(default=dict)
|
||||
uncertainty = models.JSONField(default=list)
|
||||
|
||||
class Meta:
|
||||
constraints = [models.UniqueConstraint(fields=('source', 'row_key'), name='food_unique_nutrition_row')]
|
||||
|
||||
|
||||
class KitchenInventoryItem(models.Model):
|
||||
api_id = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='kitchen_inventory')
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
"""Portable, owner-scoped structured nutrition import and lookup."""
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
from datetime import date
|
||||
from decimal import Decimal, InvalidOperation
|
||||
|
||||
from django.core import signing
|
||||
from django.db import transaction
|
||||
from django.db.models import Exists, OuterRef, Q
|
||||
|
||||
from .models import CanonicalIngredient, KitchenInventoryItem, NutritionSource, SuggestedIngredient
|
||||
|
||||
CATEGORIES = {'vegetable', 'fruit', 'herb', 'spice', 'bread', 'grain', 'meat', 'fish', 'dairy', 'legume', 'nuts_seeds', 'oil_fat', 'sauce', 'beverage', 'other', 'unresolved'}
|
||||
UNITS = {'g', 'kg', 'ml', 'L', 'pieces', 'tsp', 'tbsp', 'portions'}
|
||||
PREPARATIONS = {'raw', 'cooked', 'dry', 'prepared', 'unknown'}
|
||||
MAX_ROWS = 100
|
||||
|
||||
|
||||
def normalized(value):
|
||||
return re.sub(r'\s+', ' ', value.strip()).casefold()
|
||||
|
||||
|
||||
def digest(value):
|
||||
return hashlib.sha256(json.dumps(value, sort_keys=True, ensure_ascii=False, separators=(',', ':')).encode()).hexdigest()
|
||||
|
||||
|
||||
def validate(user, payload):
|
||||
if not isinstance(payload, dict):
|
||||
raise ValueError('Provide a structured import payload.')
|
||||
source = payload.get('source')
|
||||
rows = payload.get('ingredients')
|
||||
if not isinstance(source, dict) or not isinstance(rows, list) or not 1 <= len(rows) <= MAX_ROWS:
|
||||
raise ValueError('Provide source and 1–100 ingredient rows.')
|
||||
programme = str(source.get('programme', '')).strip()
|
||||
name = str(source.get('document_name', '')).strip()
|
||||
sha = str(source.get('sha256', '')).lower()
|
||||
if not programme or len(programme) > 80 or not name or len(name) > 255 or not re.fullmatch(r'[0-9a-f]{64}', sha):
|
||||
raise ValueError('Source needs programme, document_name and a PDF SHA-256 hash.')
|
||||
for field in ('week_start', 'week_end'):
|
||||
if source.get(field):
|
||||
date.fromisoformat(source[field])
|
||||
existing_source = NutritionSource.objects.filter(user=user, sha256=sha).first()
|
||||
seen = set()
|
||||
output = []
|
||||
blocking = []
|
||||
for index, row in enumerate(rows):
|
||||
if not isinstance(row, dict):
|
||||
raise ValueError(f'Row {index + 1} must be an object.')
|
||||
label = str(row.get('original_label', '')).strip()
|
||||
canonical_name = str(row.get('canonical_name', '')).strip()
|
||||
form = str(row.get('form', '')).strip().lower()
|
||||
prep = str(row.get('preparation', 'unknown')).lower()
|
||||
category = str(row.get('category', 'unresolved')).lower()
|
||||
unit = str(row.get('unit', '')).strip()
|
||||
key = str(row.get('row_key', index + 1)).strip()
|
||||
page = row.get('page')
|
||||
try:
|
||||
quantity = Decimal(str(row.get('quantity')))
|
||||
except (InvalidOperation, TypeError):
|
||||
raise ValueError(f'Row {index + 1}: invalid quantity.') from None
|
||||
if not label or not canonical_name or len(label) > 255 or len(canonical_name) > 180 or not key or len(key) > 100 or quantity <= 0 or not quantity.is_finite() or quantity.as_tuple().exponent < -3 or quantity >= 10000000 or not isinstance(page, int) or isinstance(page, bool) or page < 1:
|
||||
raise ValueError(f'Row {index + 1}: label, name, positive quantity, row key and page are required.')
|
||||
if key in seen:
|
||||
blocking.append(f'row {index + 1}: duplicate row_key {key}')
|
||||
seen.add(key)
|
||||
if unit not in UNITS or prep == 'unknown' or category == 'unresolved':
|
||||
blocking.append(f'row {index + 1}: review unit, preparation or classification')
|
||||
if prep not in PREPARATIONS or category not in CATEGORIES or len(form) > 80:
|
||||
raise ValueError(f'Row {index + 1}: invalid preparation, category or form.')
|
||||
try:
|
||||
confidence = Decimal(str(row.get('confidence', '0.5')))
|
||||
except (InvalidOperation, TypeError):
|
||||
raise ValueError(f'Row {index + 1}: invalid confidence.') from None
|
||||
if not confidence.is_finite() or not 0 <= confidence <= 1:
|
||||
raise ValueError(f'Row {index + 1}: confidence must be 0–1.')
|
||||
nutrition = row.get('nutrition') or {}
|
||||
if not isinstance(nutrition, dict) or any(k not in {'energy_kcal', 'protein_g', 'carbohydrates_g', 'fat_g', 'fibre_g'} for k in nutrition):
|
||||
raise ValueError(f'Row {index + 1}: invalid nutrition fields.')
|
||||
for field, value in nutrition.items():
|
||||
if value is not None:
|
||||
try:
|
||||
nutrient = Decimal(str(value))
|
||||
except (InvalidOperation, TypeError):
|
||||
raise ValueError(f'Row {index + 1}: invalid {field}.') from None
|
||||
if not nutrient.is_finite() or nutrient < 0:
|
||||
raise ValueError(f'Row {index + 1}: invalid {field}.')
|
||||
candidates = list(CanonicalIngredient.objects.filter(user=user, normalized_name=normalized(canonical_name), form=form, preparation=prep).values('id', 'name', 'category')[:2])
|
||||
aliases = row.get('aliases') or []
|
||||
if not isinstance(aliases, list) or len(aliases) > 20 or any(not isinstance(a, str) or len(a) > 180 for a in aliases):
|
||||
raise ValueError(f'Row {index + 1}: invalid aliases.')
|
||||
uncertainty = row.get('uncertainty') or []
|
||||
if not isinstance(uncertainty, list) or len(uncertainty) > 20 or any(not isinstance(note, str) or len(note) > 255 for note in uncertainty):
|
||||
raise ValueError(f'Row {index + 1}: invalid uncertainty notes.')
|
||||
output.append({'row_key': key, 'original_label': label, 'canonical_name': canonical_name, 'quantity': str(quantity), 'unit': unit, 'preparation': prep, 'category': category, 'subcategory': str(row.get('subcategory', ''))[:80], 'form': form, 'page': page, 'confidence': str(confidence), 'aliases': aliases, 'nutrition': {k: str(v) if v is not None else None for k, v in nutrition.items()}, 'uncertainty': uncertainty, 'candidates': [{'id': str(c['id']), 'name': c['name'], 'category': c['category']} for c in candidates]})
|
||||
if candidates and candidates[0]['category'] != category:
|
||||
blocking.append(f'row {index + 1}: existing canonical classification differs')
|
||||
return {'source': {'programme': programme, 'document_name': name, 'sha256': sha, 'week_start': source.get('week_start'), 'week_end': source.get('week_end')}, 'ingredients': output, 'existing_source_id': str(existing_source.pk) if existing_source else None, 'blocking': blocking}
|
||||
|
||||
|
||||
def preview(user, payload):
|
||||
result = validate(user, payload)
|
||||
result['version'] = 1
|
||||
result['payload_hash'] = digest(payload)
|
||||
result['confirmation'] = signing.dumps({'user': user.pk, 'hash': result['payload_hash']}, salt='nutrition-import')
|
||||
return result
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def commit(user, payload, confirmation, idempotency_key):
|
||||
if not isinstance(payload, dict):
|
||||
raise ValueError('Provide a structured import payload.')
|
||||
if not idempotency_key or len(idempotency_key) > 128:
|
||||
raise ValueError('Provide an idempotency key.')
|
||||
try:
|
||||
signed = signing.loads(confirmation, salt='nutrition-import', max_age=600)
|
||||
except signing.BadSignature:
|
||||
raise ValueError('Preview confirmation expired or invalid.') from None
|
||||
if signed != {'user': user.pk, 'hash': digest(payload)}:
|
||||
raise ValueError('Preview does not match this user and payload.')
|
||||
prior = NutritionSource.objects.filter(user=user, idempotency_key=idempotency_key).first()
|
||||
if prior:
|
||||
if prior.payload_hash != digest(payload):
|
||||
raise ValueError('Idempotency key reused with different payload.')
|
||||
return {'source_id': str(prior.pk), 'created': 0, 'skipped': prior.ingredients.count(), 'idempotent': True}
|
||||
result = validate(user, payload)
|
||||
if result['blocking']:
|
||||
raise ValueError('Resolve preview blockers before commit: ' + '; '.join(result['blocking']))
|
||||
if result['existing_source_id']:
|
||||
raise ValueError('This PDF was already imported; amended extraction requires a reviewed revision.')
|
||||
source = NutritionSource.objects.create(user=user, payload_hash=digest(payload), idempotency_key=idempotency_key, **result['source'])
|
||||
for row in result['ingredients']:
|
||||
canonical, _ = CanonicalIngredient.objects.get_or_create(user=user, normalized_name=normalized(row['canonical_name']), form=row['form'], preparation=row['preparation'], defaults={'name': row['canonical_name'], 'category': row['category'], 'subcategory': row['subcategory'], 'aliases': row['aliases'], 'confidence': row['confidence']})
|
||||
SuggestedIngredient.objects.create(source=source, canonical=canonical, row_key=row['row_key'], original_label=row['original_label'], quantity=row['quantity'], unit=row['unit'], preparation=row['preparation'], page=row['page'], nutrition=row['nutrition'], uncertainty=row['uncertainty'])
|
||||
return {'source_id': str(source.pk), 'created': len(result['ingredients']), 'skipped': 0, 'idempotent': False}
|
||||
|
||||
|
||||
def search(user, *, category='', subcategory='', programme='', query='', form='', verified=None, inventory_status='', limit=20, offset=0):
|
||||
if inventory_status not in ('', 'in_stock', 'not_found'):
|
||||
raise ValueError('Invalid inventory_status.')
|
||||
qs = CanonicalIngredient.objects.filter(user=user)
|
||||
if category: qs = qs.filter(category=category)
|
||||
if subcategory: qs = qs.filter(subcategory=subcategory)
|
||||
if form: qs = qs.filter(form=form)
|
||||
if verified is not None: qs = qs.filter(verified=verified)
|
||||
if programme: qs = qs.filter(suggestions__source__programme__iexact=programme)
|
||||
if query: qs = qs.filter(Q(name__icontains=query) | Q(aliases__icontains=query))
|
||||
stock_query = KitchenInventoryItem.objects.filter(user=user, archived_at__isnull=True, product__product_name__iexact=OuterRef('name'))
|
||||
qs = qs.annotate(has_stock_name=Exists(stock_query))
|
||||
if inventory_status == 'in_stock': qs = qs.filter(has_stock_name=True)
|
||||
if inventory_status == 'not_found': qs = qs.filter(has_stock_name=False)
|
||||
qs = qs.distinct().order_by('name', 'pk')
|
||||
results = []
|
||||
for ingredient in qs[offset:offset + limit]:
|
||||
stock = list(KitchenInventoryItem.objects.filter(user=user, archived_at__isnull=True, product__product_name__iexact=ingredient.name).values('api_id', 'product__product_name')[:10])
|
||||
sources = list(SuggestedIngredient.objects.filter(canonical=ingredient).select_related('source').order_by('-source__imported_at')[:5])
|
||||
results.append({'id': str(ingredient.pk), 'name': ingredient.name, 'category': ingredient.category, 'subcategory': ingredient.subcategory, 'form': ingredient.form, 'preparation': ingredient.preparation, 'aliases': ingredient.aliases, 'confidence': str(ingredient.confidence), 'provenance': ingredient.provenance, 'verified': ingredient.verified, 'sources': [{'programme': s.source.programme, 'document_name': s.source.document_name, 'source_id': str(s.source_id), 'page': s.page, 'original_label': s.original_label, 'quantity': str(s.quantity), 'unit': s.unit, 'nutrition': s.nutrition} for s in sources], 'inventory': {'status': 'exact_name_candidate' if stock else 'not_found', 'items': [{'id': str(s['api_id']), 'product_name': s['product__product_name']} for s in stock], 'note': 'Name match requires product and quantity review; no nutritional equivalence is inferred.'}})
|
||||
return {'count': qs.count(), 'limit': limit, 'offset': offset, 'results': results}
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def correct_classification(user, identifier, changes):
|
||||
ingredient = CanonicalIngredient.objects.select_for_update().get(pk=identifier, user=user)
|
||||
allowed = {'category', 'subcategory', 'aliases'}
|
||||
if not isinstance(changes, dict) or not changes or set(changes) - allowed:
|
||||
raise ValueError('Provide category, subcategory or aliases only.')
|
||||
if 'category' in changes and changes['category'] not in CATEGORIES:
|
||||
raise ValueError('Invalid category.')
|
||||
if 'aliases' in changes and (not isinstance(changes['aliases'], list) or len(changes['aliases']) > 20 or any(not isinstance(x, str) or len(x) > 180 for x in changes['aliases'])):
|
||||
raise ValueError('Invalid aliases.')
|
||||
for field in ('subcategory',):
|
||||
if field in changes and (not isinstance(changes[field], str) or len(changes[field]) > 80):
|
||||
raise ValueError(f'Invalid {field}.')
|
||||
for field, value in changes.items():
|
||||
setattr(ingredient, field, value)
|
||||
ingredient.verified = True
|
||||
ingredient.provenance = 'manual_correction'
|
||||
ingredient.save(update_fields=[*changes, 'verified', 'provenance'])
|
||||
return {'id': str(ingredient.pk), 'category': ingredient.category, 'subcategory': ingredient.subcategory, 'aliases': ingredient.aliases, 'form': ingredient.form, 'verified': ingredient.verified, 'provenance': ingredient.provenance}
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Authenticated nutrition import contract."""
|
||||
from django.http import JsonResponse
|
||||
from django.http import Http404
|
||||
from .api import _json, api_view
|
||||
from . import nutrition
|
||||
|
||||
|
||||
@api_view('food:write', ('POST',))
|
||||
def preview(request):
|
||||
return JsonResponse(nutrition.preview(request.api_user, _json(request)))
|
||||
|
||||
|
||||
@api_view('food:write', ('POST',))
|
||||
def commit(request):
|
||||
body = _json(request)
|
||||
return JsonResponse(nutrition.commit(request.api_user, body.get('payload'), body.get('confirmation', ''), request.headers.get('Idempotency-Key', '')))
|
||||
|
||||
|
||||
@api_view('food:read', ('GET',))
|
||||
def ingredients(request):
|
||||
params = request.GET
|
||||
limit = int(params.get('limit', 20))
|
||||
offset = int(params.get('offset', 0))
|
||||
if not 1 <= limit <= 50 or not 0 <= offset <= 10000:
|
||||
raise ValueError('Pagination out of range.')
|
||||
verified = params.get('verified')
|
||||
if verified not in (None, 'true', 'false'):
|
||||
raise ValueError('verified must be true or false.')
|
||||
return JsonResponse(nutrition.search(request.api_user, category=params.get('category', ''), subcategory=params.get('subcategory', ''), programme=params.get('programme', ''), query=params.get('query', ''), form=params.get('form', ''), verified=None if verified is None else verified == 'true', inventory_status=params.get('inventory_status', ''), limit=limit, offset=offset))
|
||||
|
||||
|
||||
@api_view('food:write', ('PATCH',))
|
||||
def ingredient_correct(request, identifier):
|
||||
try:
|
||||
return JsonResponse(nutrition.correct_classification(request.api_user, identifier, _json(request)))
|
||||
except nutrition.CanonicalIngredient.DoesNotExist:
|
||||
raise Http404
|
||||
@@ -0,0 +1,72 @@
|
||||
import hashlib
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
from .models import CanonicalIngredient, FoodLogEntry, FoodProduct, InventoryLocation, KitchenInventoryItem, NutritionSource, SuggestedIngredient
|
||||
from .nutrition import preview, commit, search
|
||||
|
||||
|
||||
class NutritionImportTests(TestCase):
|
||||
def setUp(self):
|
||||
self.user = get_user_model().objects.create_user(username='nutrition-owner')
|
||||
self.other = get_user_model().objects.create_user(username='nutrition-other')
|
||||
self.payload = {'source': {'programme': 'Body Support', 'document_name': 'shopping.pdf', 'sha256': hashlib.sha256(b'pdf').hexdigest()}, 'ingredients': [
|
||||
{'row_key': 'p1-broccoli', 'original_label': 'Broccoli vers', 'canonical_name': 'broccoli', 'quantity': '250', 'unit': 'g', 'page': 1, 'preparation': 'raw', 'category': 'vegetable', 'subcategory': 'cruciferous', 'confidence': '0.9'},
|
||||
{'row_key': 'p1-paprika', 'original_label': 'Paprikapoeder', 'canonical_name': 'paprika powder', 'quantity': '5', 'unit': 'g', 'page': 1, 'preparation': 'dry', 'category': 'spice', 'confidence': '0.9'}]}
|
||||
|
||||
def test_preview_commit_and_repeat_are_read_only_to_stock_and_diary(self):
|
||||
result = preview(self.user, self.payload)
|
||||
self.assertEqual(SuggestedIngredient.objects.count(), 0)
|
||||
self.assertEqual(result['blocking'], [])
|
||||
committed = commit(self.user, self.payload, result['confirmation'], 'key-one')
|
||||
self.assertEqual(committed['created'], 2)
|
||||
self.assertTrue(commit(self.user, self.payload, result['confirmation'], 'key-one')['idempotent'])
|
||||
self.assertEqual(NutritionSource.objects.count(), 1)
|
||||
self.assertEqual(SuggestedIngredient.objects.count(), 2)
|
||||
self.assertEqual(KitchenInventoryItem.objects.count(), 0)
|
||||
self.assertEqual(FoodLogEntry.objects.count(), 0)
|
||||
self.assertEqual(search(self.user, category='vegetable', programme='Body Support')['count'], 1)
|
||||
self.assertEqual(search(self.other, category='vegetable')['count'], 0)
|
||||
|
||||
def test_ambiguous_preparation_blocks_commit(self):
|
||||
self.payload['ingredients'][0]['preparation'] = 'unknown'
|
||||
result = preview(self.user, self.payload)
|
||||
self.assertTrue(result['blocking'])
|
||||
with self.assertRaises(ValueError):
|
||||
commit(self.user, self.payload, result['confirmation'], 'key-two')
|
||||
self.assertEqual(CanonicalIngredient.objects.count(), 0)
|
||||
|
||||
def test_raw_and_cooked_are_distinct(self):
|
||||
result = preview(self.user, self.payload)
|
||||
commit(self.user, self.payload, result['confirmation'], 'key-one')
|
||||
self.payload['source']['sha256'] = hashlib.sha256(b'pdf2').hexdigest()
|
||||
self.payload['ingredients'] = [{**self.payload['ingredients'][0], 'preparation': 'cooked'}]
|
||||
result = preview(self.user, self.payload)
|
||||
commit(self.user, self.payload, result['confirmation'], 'key-two')
|
||||
self.assertEqual(CanonicalIngredient.objects.filter(name='broccoli').count(), 2)
|
||||
|
||||
def test_attached_body_support_pdf_row_preview(self):
|
||||
# Page 1 of the card attachment boodschappenlijst-17923863104217.pdf.
|
||||
self.payload['source']['sha256'] = '7afcefff2e7661b324bbfcd717adfb75274e0ad8b76254b65f11a0d8982f7d0c'
|
||||
self.payload['source']['document_name'] = 'boodschappenlijst-17923863104217.pdf'
|
||||
self.payload['ingredients'] = [
|
||||
{'row_key': 'page1-broccoli', 'original_label': 'Broccoli vers', 'canonical_name': 'broccoli', 'quantity': '250', 'unit': 'g', 'page': 1, 'preparation': 'raw', 'category': 'vegetable', 'subcategory': 'cruciferous'},
|
||||
{'row_key': 'page1-pasta', 'original_label': 'Pasta volkoren gekookt', 'canonical_name': 'wholegrain pasta', 'quantity': '150', 'unit': 'g', 'page': 1, 'preparation': 'cooked', 'category': 'grain', 'subcategory': 'wholegrain pasta'},
|
||||
]
|
||||
result = preview(self.user, self.payload)
|
||||
self.assertEqual(result['blocking'], [])
|
||||
self.assertEqual([row['category'] for row in result['ingredients']], ['vegetable', 'grain'])
|
||||
self.assertEqual(result['ingredients'][1]['preparation'], 'cooked')
|
||||
self.assertEqual(SuggestedIngredient.objects.count(), 0)
|
||||
|
||||
def test_inventory_filter_is_read_only_and_counted_before_pagination(self):
|
||||
result = preview(self.user, self.payload)
|
||||
commit(self.user, self.payload, result['confirmation'], 'stock-key')
|
||||
product = FoodProduct.objects.create(owner=self.user, product_name='broccoli')
|
||||
location = InventoryLocation.objects.create(user=self.user, name='Fridge')
|
||||
KitchenInventoryItem.objects.create(user=self.user, product=product, location=location)
|
||||
found = search(self.user, inventory_status='in_stock', limit=1)
|
||||
self.assertEqual(found['count'], 1)
|
||||
self.assertEqual(found['results'][0]['name'], 'broccoli')
|
||||
self.assertEqual(found['results'][0]['inventory']['status'], 'exact_name_candidate')
|
||||
self.assertEqual(search(self.user, inventory_status='not_found')['count'], 1)
|
||||
self.assertEqual(KitchenInventoryItem.objects.count(), 1)
|
||||
@@ -547,6 +547,25 @@ def create_server() -> MCPServer:
|
||||
result['attachment_error'] = str(exc)
|
||||
return result
|
||||
|
||||
@server.tool(structured_output=True, description='Preview 1–100 structured PDF-extracted ingredient rows. Requires source programme, filename and PDF SHA-256 plus original labels, quantities, pages, preparation and reviewable culinary categories. Read only; returns blockers and a short-lived confirmation. Unknown nutrition stays unknown.')
|
||||
async def nutrition_import_preview(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return await api_request('POST', 'nutrition/preview/', payload=payload)
|
||||
|
||||
@server.tool(structured_output=True, description='Commit exactly the previously previewed structured PDF extraction after explicit user approval. Requires the preview confirmation and an idempotency key. Writes suggestions and canonical ingredients only; never changes inventory, shopping or Food Diary.')
|
||||
async def nutrition_import_commit(payload: dict[str, Any], confirmation: str, idempotency_key: str) -> dict[str, Any]:
|
||||
return await api_request('POST', 'nutrition/commit/', payload={'payload': payload, 'confirmation': confirmation}, idempotency_key=idempotency_key)
|
||||
|
||||
@server.tool(structured_output=True, description='Search your source suggested canonical ingredients by culinary category, subcategory, programme, form, verification and actual inventory name candidates. For vegetables suggested by Body Support set category=vegetable and programme=Body Support. Stock matches are candidates only; no nutrition equivalence is inferred.')
|
||||
async def nutrition_ingredients_search(category: str = '', subcategory: str = '', programme: str = '', query: str = '', form: str = '', verified: bool | None = None, inventory_status: str = '', limit: int = 20, offset: int = 0) -> dict[str, Any]:
|
||||
params = {'category': category, 'subcategory': subcategory, 'programme': programme, 'query': query, 'form': form, 'inventory_status': inventory_status, 'limit': limit, 'offset': offset}
|
||||
if verified is not None:
|
||||
params['verified'] = str(verified).lower()
|
||||
return await api_request('GET', 'nutrition/ingredients/', params=params)
|
||||
|
||||
@server.tool(structured_output=True, description='Manually correct the category, subcategory or aliases of one owned canonical ingredient after review. Preserves the source PDF wording and marks the classification as verified.')
|
||||
async def nutrition_ingredient_correct(ingredient_id: str, changes: dict[str, Any]) -> dict[str, Any]:
|
||||
return await api_request('PATCH', f'nutrition/ingredients/{ingredient_id}/', payload=changes)
|
||||
|
||||
@server.tool(structured_output=True, description='List a bounded page of the authenticated user’s current kitchen inventory. Does not change data.')
|
||||
async def inventory_list(limit: int = 30, offset: int = 0) -> dict[str, Any]:
|
||||
data = await api_request('GET', 'kitchen/items/', params={'limit': min(max(limit, 1), 100), 'offset': max(offset, 0)})
|
||||
|
||||
@@ -99,6 +99,45 @@ class McpHttpTests(LiveServerTestCase):
|
||||
async with Client(streamable_http_client(self.mcp_url, http_client=http)) as client:
|
||||
return await operation(client)
|
||||
|
||||
def test_nutrition_tools_discover_and_preview(self):
|
||||
import hashlib
|
||||
payload = {'source': {'programme': 'Body Support', 'document_name': 'sample.pdf',
|
||||
'sha256': hashlib.sha256(b'sample').hexdigest()},
|
||||
'ingredients': [{'row_key': 'p1-1', 'original_label': 'Broccoli vers',
|
||||
'canonical_name': 'broccoli', 'quantity': '250', 'unit': 'g',
|
||||
'page': 1, 'preparation': 'raw', 'category': 'vegetable'}]}
|
||||
|
||||
async def check(client):
|
||||
names = {tool.name for tool in (await client.list_tools()).tools}
|
||||
self.assertTrue({'nutrition_import_preview', 'nutrition_import_commit',
|
||||
'nutrition_ingredients_search', 'nutrition_ingredient_correct'} <= names)
|
||||
result = (await client.call_tool('nutrition_import_preview', {'payload': payload})).structured_content
|
||||
self.assertEqual(result['blocking'], [])
|
||||
self.assertEqual(result['ingredients'][0]['category'], 'vegetable')
|
||||
self.assertFalse(result['existing_source_id'])
|
||||
committed = (await client.call_tool('nutrition_import_commit', {
|
||||
'payload': payload, 'confirmation': result['confirmation'], 'idempotency_key': 'nutrition-mcp-1'})).structured_content
|
||||
self.assertEqual(committed['created'], 1)
|
||||
repeated = (await client.call_tool('nutrition_import_commit', {
|
||||
'payload': payload, 'confirmation': result['confirmation'], 'idempotency_key': 'nutrition-mcp-1'})).structured_content
|
||||
self.assertTrue(repeated['idempotent'])
|
||||
found = (await client.call_tool('nutrition_ingredients_search', {
|
||||
'category': 'vegetable', 'programme': 'Body Support'})).structured_content
|
||||
self.assertEqual(found['count'], 1)
|
||||
self.assertEqual(found['results'][0]['sources'][0]['page'], 1)
|
||||
corrected = (await client.call_tool('nutrition_ingredient_correct', {
|
||||
'ingredient_id': found['results'][0]['id'], 'changes': {'subcategory': 'cruciferous'}})).structured_content
|
||||
self.assertTrue(corrected['verified'])
|
||||
|
||||
asyncio.run(self._client(self.secret, check))
|
||||
|
||||
async def isolated(client):
|
||||
found = (await client.call_tool('nutrition_ingredients_search', {'category': 'vegetable'})).structured_content
|
||||
self.assertEqual(found['count'], 0)
|
||||
denied = await client.call_tool('nutrition_import_preview', {'payload': payload})
|
||||
self.assertTrue(denied.is_error)
|
||||
asyncio.run(self._client(self.b_secret, isolated))
|
||||
|
||||
@override_settings(SECURE_SSL_REDIRECT=True)
|
||||
def test_authentication_and_scope_boundaries(self):
|
||||
async def check():
|
||||
|
||||
Reference in New Issue
Block a user