diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html index 57fec3c86..ec0ae237e 100644 --- a/cookbook/templates/url_import.html +++ b/cookbook/templates/url_import.html @@ -113,7 +113,12 @@

- {% trans 'Import all Keywords not only the ones already existing.' %} + {% trans 'Import all Keywords not only the ones already existing.' %} +
+ +
+

@@ -158,13 +163,42 @@ console.log(err) }) }, + importRecipe: function () { + + let recipe_keywords = [] + for (k of this.recipe_data.keywords) { + if (k.id !== "null") { + recipe_keywords.push(Number.parseInt(k.id)) + } + //TODO create non existent if checked + } + + let recipe = { + name: this.recipe_data.name, + instructions: this.recipe_data.recipeInstructions, + keywords: recipe_keywords, + created_by: {{ request.user.pk }}, + } + + this.$http.post(`{% url 'api:recipe-list' %}`, recipe).then((response) => { + let entry = response.data + console.log(entry) + + //TODO create some kind of endpoint for ingredients, units and recipe ingredients creation + + location.href = "{% url 'view_recipe' 12345 %}".replace(/12345/, entry.id) + }).catch((err) => { + console.log("dragChanged create error", err); + }) + + }, getKeywords: function () { this.$http.get("{% url 'dal_keyword' %}").then((response) => { this.keywords = response.data.results; }).catch((err) => { console.log(err) }) - } + }, } }); diff --git a/cookbook/urls.py b/cookbook/urls.py index 3bb392e0a..d3715d714 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -11,6 +11,8 @@ from cookbook.helper import dal router = routers.DefaultRouter() router.register(r'user-preference', api.UserPreferenceViewSet) router.register(r'recipe', api.RecipeViewSet) +router.register(r'ingredient', api.IngredientViewSet) +router.register(r'recipe-ingredient', api.RecipeIngredientSerializer) router.register(r'meal-plan', api.MealPlanViewSet) router.register(r'meal-type', api.MealTypeViewSet) router.register(r'view-log', api.ViewLogViewSet) diff --git a/cookbook/views/api.py b/cookbook/views/api.py index a3b8d61f4..32a2b4e02 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -18,12 +18,12 @@ from rest_framework import viewsets, permissions from rest_framework.exceptions import APIException from rest_framework.mixins import RetrieveModelMixin, UpdateModelMixin, ListModelMixin -from cookbook.helper.permission_helper import group_required, CustomIsOwner, CustomIsAdmin +from cookbook.helper.permission_helper import group_required, CustomIsOwner, CustomIsAdmin, CustomIsUser from cookbook.helper.recipe_url_import import find_recipe_json -from cookbook.models import Recipe, Sync, Storage, CookLog, MealPlan, MealType, ViewLog, UserPreference, RecipeBook, Keyword +from cookbook.models import Recipe, Sync, Storage, CookLog, MealPlan, MealType, ViewLog, UserPreference, RecipeBook, Keyword, RecipeIngredient, Ingredient from cookbook.provider.dropbox import Dropbox from cookbook.provider.nextcloud import Nextcloud -from cookbook.serializer import MealPlanSerializer, MealTypeSerializer, RecipeSerializer, ViewLogSerializer, UserNameSerializer, UserPreferenceSerializer, RecipeBookSerializer +from cookbook.serializer import MealPlanSerializer, MealTypeSerializer, RecipeSerializer, ViewLogSerializer, UserNameSerializer, UserPreferenceSerializer, RecipeBookSerializer, RecipeIngredientSerializer, IngredientSerializer class UserNameViewSet(viewsets.ModelViewSet): @@ -122,7 +122,7 @@ class RecipeViewSet(viewsets.ModelViewSet): """ queryset = Recipe.objects.all() serializer_class = RecipeSerializer - permission_classes = [permissions.IsAuthenticated] + permission_classes = [permissions.IsAuthenticated] # TODO split read and write permission for meal plan guest def get_queryset(self): queryset = Recipe.objects.all() @@ -136,6 +136,18 @@ class RecipeViewSet(viewsets.ModelViewSet): return queryset +class RecipeIngredientViewSet(viewsets.ModelViewSet): + queryset = RecipeIngredient.objects.all() + serializer_class = RecipeIngredientSerializer + permission_classes = [CustomIsUser] + + +class IngredientViewSet(viewsets.ModelViewSet): + queryset = Ingredient.objects.all() + serializer_class = IngredientSerializer + permission_classes = [CustomIsUser] + + class ViewLogViewSet(viewsets.ModelViewSet): queryset = ViewLog.objects.all() serializer_class = ViewLogSerializer