38 lines
1.6 KiB
Python
38 lines
1.6 KiB
Python
"""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
|