From c73dcb39985b444bf77e53dcd2d170c36a686858 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Thu, 8 Feb 2018 23:18:56 +0100 Subject: [PATCH] edit recipe --- .idea/workspace.xml | 262 ++++++++++-------- cookbook/forms.py | 16 +- .../{new_recipe.html => edit/recipe.html} | 0 .../{new_category.html => new/category.html} | 0 .../{new_keyword.html => new/keyword.html} | 0 cookbook/templates/new/recipe.html | 6 + cookbook/views/edit.py | 19 +- cookbook/views/new.py | 6 +- 8 files changed, 175 insertions(+), 134 deletions(-) rename cookbook/templates/{new_recipe.html => edit/recipe.html} (100%) rename cookbook/templates/{new_category.html => new/category.html} (100%) rename cookbook/templates/{new_keyword.html => new/keyword.html} (100%) create mode 100644 cookbook/templates/new/recipe.html diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9e9433fb4..7e5ff0f56 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,12 +2,14 @@ - + - - + + + + - + @@ -256,6 +259,20 @@ + + + + + + + + + + + + + + @@ -287,11 +304,12 @@ - + + @@ -367,7 +385,7 @@ - + @@ -393,7 +411,7 @@ - @@ -442,13 +460,6 @@ - - - - - - - @@ -496,7 +507,7 @@ - + @@ -554,13 +565,6 @@ - - - - - - - @@ -596,20 +600,6 @@ - - - - - - - - - - - - - - @@ -667,24 +657,6 @@ - - - - - - - - - - - - - - - - - - @@ -705,16 +677,6 @@ - - - - - - - - - - @@ -723,28 +685,90 @@ - + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cookbook/forms.py b/cookbook/forms.py index 6aca2b0c6..244411118 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -22,9 +22,6 @@ class RecipeForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(RecipeForm, self).__init__(*args, **kwargs) - self.fields['name'].widget.attrs.update({'class': 'form-control'}) - self.fields['category'].widget.attrs.update({'class': 'form-control'}) - self.fields['keywords'].widget.attrs.update({'class': 'form-control'}) self.helper = FormHelper() self.helper.form_method = 'post' self.helper.add_input(Submit('save', _('Save'), css_class='btn-primary')) @@ -67,14 +64,25 @@ class KeywordForm(forms.ModelForm): class EditRecipeForm(forms.ModelForm): class Meta: model = Recipe - fields = ('name', 'category', 'keywords') + fields = ('name', 'category', 'keywords','path') labels = { 'name': _('Name'), 'category': _('Category'), 'keywords': _('Keywords'), + 'path': _('Path'), } + help_texts = { + 'keywords': _('Ctrl+Click to select multiple keywords'), + } + + def __init__(self, *args, **kwargs): + super(EditRecipeForm, self).__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.form_method = 'post' + self.helper.add_input(Submit('save', _('Save'), css_class='btn-primary')) + class ImportForm(forms.Form): path = forms.CharField(label=_('Path')) diff --git a/cookbook/templates/new_recipe.html b/cookbook/templates/edit/recipe.html similarity index 100% rename from cookbook/templates/new_recipe.html rename to cookbook/templates/edit/recipe.html diff --git a/cookbook/templates/new_category.html b/cookbook/templates/new/category.html similarity index 100% rename from cookbook/templates/new_category.html rename to cookbook/templates/new/category.html diff --git a/cookbook/templates/new_keyword.html b/cookbook/templates/new/keyword.html similarity index 100% rename from cookbook/templates/new_keyword.html rename to cookbook/templates/new/keyword.html diff --git a/cookbook/templates/new/recipe.html b/cookbook/templates/new/recipe.html new file mode 100644 index 000000000..b4c6839a9 --- /dev/null +++ b/cookbook/templates/new/recipe.html @@ -0,0 +1,6 @@ +{% extends "base.html" %} +{% load crispy_forms_tags %} + +{% block content %} + {% crispy form %} +{% endblock %} \ No newline at end of file diff --git a/cookbook/views/edit.py b/cookbook/views/edit.py index a655287d6..0686c96cb 100644 --- a/cookbook/views/edit.py +++ b/cookbook/views/edit.py @@ -1,25 +1,28 @@ from django.contrib.auth.decorators import login_required from django.shortcuts import redirect, render +from django.urls import reverse from cookbook.forms import RecipeForm, EditRecipeForm -from cookbook.models import Recipe +from cookbook.models import Recipe, Category @login_required def recipe(request, recipe_id): + recipe_obj = Recipe.objects.get(id=recipe_id) if request.method == "POST": - form = RecipeForm(request.POST) + form = EditRecipeForm(request.POST) if form.is_valid(): - recipe_obj = form.save(commit=False) - recipe_obj.created_by = request.user.id + recipe_obj.name = form.cleaned_data['name'] + recipe_obj.path = form.cleaned_data['path'] + recipe_obj.category = Category.objects.get(name=form.cleaned_data['category']) + recipe_obj.keywords.clear() + recipe_obj.keywords.add(*list(form.cleaned_data['keywords'])) recipe_obj.save() - form.save_m2m() - return redirect('edit_recipe/' + recipe_id) + return redirect(reverse('edit_recipe', args=[recipe_id])) else: - recipe_obj = Recipe.objects.get(id=recipe_id) form = EditRecipeForm(instance=recipe_obj) - return render(request, 'new_recipe.html', {'from': form}) + return render(request, 'edit/recipe.html', {'form': form}) @login_required diff --git a/cookbook/views/new.py b/cookbook/views/new.py index d289b829e..1ee92c99b 100644 --- a/cookbook/views/new.py +++ b/cookbook/views/new.py @@ -20,7 +20,7 @@ def recipe(request): else: form = RecipeForm() - return render(request, 'new_recipe.html', {'form': form}) + return render(request, 'new/recipe.html', {'form': form}) @login_required @@ -38,7 +38,7 @@ def category(request): table = CategoryTable(Category.objects.all()) RequestConfig(request, paginate={'per_page': 25}).configure(table) - return render(request, 'new_category.html', {'form': form, 'table': table}) + return render(request, 'new/category.html', {'form': form, 'table': table}) @login_required @@ -56,4 +56,4 @@ def keyword(request): table = KeywordTable(Keyword.objects.all()) RequestConfig(request, paginate={'per_page': 25}).configure(table) - return render(request, 'new_keyword.html', {'form': form, 'table': table}) + return render(request, 'new/keyword.html', {'form': form, 'table': table})