From 50cd44d13b6397a2cb8fec8c836bbcc199ead99a Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Wed, 13 Nov 2019 23:22:26 +0100 Subject: [PATCH] internal recipe stuff --- .../templates/forms/edit_internal_recipe.html | 24 +++++++++++++++ cookbook/urls.py | 2 +- cookbook/views/edit.py | 30 +++++++++++++++++-- cookbook/views/new.py | 2 +- 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 cookbook/templates/forms/edit_internal_recipe.html diff --git a/cookbook/templates/forms/edit_internal_recipe.html b/cookbook/templates/forms/edit_internal_recipe.html new file mode 100644 index 00000000..a858a085 --- /dev/null +++ b/cookbook/templates/forms/edit_internal_recipe.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} +{% load crispy_forms_tags %} +{% load i18n %} + +{% block title %}{% trans 'Edit Recipe' %}{% endblock %} + +{% block content %} + +

{% trans 'Edit Recipe' %}

+ +
+ {% csrf_token %} + {{ form|crispy }} + +
+ + +{% endblock %} \ No newline at end of file diff --git a/cookbook/urls.py b/cookbook/urls.py index 11ec8534..62ad7348 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -9,7 +9,7 @@ urlpatterns = [ path('test', views.test, name='test'), path('new/recipe/', new.RecipeCreate.as_view(), name='new_recipe'), - path('new/recipe_import//', new.create_new_recipe, name='new_recipe_import'), + path('new/recipe_import//', new.create_new_external_recipe, name='new_recipe_import'), path('new/keyword/', new.KeywordCreate.as_view(), name='new_keyword'), path('new/storage/', new.StorageCreate.as_view(), name='new_storage'), diff --git a/cookbook/views/edit.py b/cookbook/views/edit.py index 7f6853d8..7e3c0b8d 100644 --- a/cookbook/views/edit.py +++ b/cookbook/views/edit.py @@ -1,14 +1,40 @@ from django.contrib import messages +from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin -from django.shortcuts import redirect +from django.shortcuts import redirect, get_object_or_404, render from django.urls import reverse_lazy, reverse from django.utils.translation import gettext as _ from django.views.generic import UpdateView, DeleteView -from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm +from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage +@login_required +def edit_internal_recipe(request, pk): + recipe_instance = get_object_or_404(Recipe, pk=pk) + + if request.method == "POST": + form = InternalRecipeForm(request.POST) + if form.is_valid(): + recipe = Recipe() + recipe.name = form.cleaned_data['name'] + recipe.instructions = form.cleaned_data['instructions'] + + recipe.save() + + recipe.keywords.set(form.cleaned_data['keywords']) + + messages.add_message(request, messages.SUCCESS, _('Recipe saved!')) + return redirect('index') + else: + messages.add_message(request, messages.ERROR, _('There was an error importing this recipe!')) + else: + form = InternalRecipeForm(recipe_instance) + + return render(request, 'forms/edit_internal_recipe.html', {'form': form}) + + class SyncUpdate(LoginRequiredMixin, UpdateView): template_name = "generic/edit_template.html" model = Sync diff --git a/cookbook/views/new.py b/cookbook/views/new.py index fcc4341b..ae5ba927 100644 --- a/cookbook/views/new.py +++ b/cookbook/views/new.py @@ -47,7 +47,7 @@ class StorageCreate(LoginRequiredMixin, CreateView): @login_required -def create_new_recipe(request, import_id): +def create_new_external_recipe(request, import_id): if request.method == "POST": form = ImportRecipeForm(request.POST) if form.is_valid():