Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Remove parent from category form
Marcin Gębala5bcc09eb4f0e
diff --git a/saleor/dashboard/category/forms.py b/saleor/dashboard/category/forms.pyindex 75fb4b060c..f18871cd51 100644--- a/saleor/dashboard/category/forms.py+++ b/saleor/dashboard/category/forms.py@@ -1,17 +1,15 @@from django import forms+from django.shortcuts import get_object_or_404from django.utils.text import slugify-from django.utils.translation import ugettext_lazy as _-from mptt.forms import TreeNodeChoiceFieldfrom unidecode import unidecodefrom ...product.models import Categoryclass CategoryForm(forms.ModelForm):- parent = TreeNodeChoiceField(queryset=Category.objects.all(),- required=False, widget=forms.HiddenInput())def __init__(self, *args, **kwargs):+ self.parent_pk = kwargs.pop('parent_pk')super(CategoryForm, self).__init__(*args, **kwargs)if self.instance.parent and self.instance.parent.hidden:self.fields['hidden'].widget.attrs['disabled'] = True@@ -20,17 +18,10 @@ class CategoryForm(forms.ModelForm):model = Categoryexclude = ['slug']- def clean_parent(self):- parent = self.cleaned_data['parent']- if parent:- if parent == self.instance:- raise forms.ValidationError(_('A category may not be made a child of itself'))- if self.instance in parent.get_ancestors():- raise forms.ValidationError(_('A category may not be made a child of any of its descendants.'))- return parent-def save(self, commit=True):self.instance.slug = slugify(unidecode(self.instance.name))+ if self.parent_pk:+ self.instance.parent = get_object_or_404(Category, pk=self.parent_pk)if self.instance.parent and self.instance.parent.hidden:self.instance.hidden = Truesuper(CategoryForm, self).save(commit=commit)diff --git a/saleor/dashboard/category/views.py b/saleor/dashboard/category/views.pyindex 692aad6287..6e1ac0f6b5 100644--- a/saleor/dashboard/category/views.py+++ b/saleor/dashboard/category/views.py@@ -24,7 +24,7 @@ def category_list(request, root_pk=None):@staff_member_requireddef category_create(request, root_pk=None):category = Category()- form = CategoryForm(request.POST or None, initial={'parent': root_pk})+ form = CategoryForm(request.POST or None, parent_pk=root_pk)if form.is_valid():category = form.save()messages.success(request, _('Added category %s') % category)@@ -40,15 +40,15 @@ def category_create(request, root_pk=None):def category_edit(request, root_pk=None):category = get_object_or_404(Category, pk=root_pk)form = CategoryForm(request.POST or None, instance=category,- initial={'parent': category.parent_id})+ parent_pk=category.parent_id)status = 200if form.is_valid():category = form.save()messages.success(request, _('Added category %s') % category)if root_pk:- return redirect('dashboard:category-children-list', root_pk=root_pk)+ return redirect('dashboard:category-list', root_pk=root_pk)else:- return redirect('dashboard:category-root-list')+ return redirect('dashboard:category-list')elif form.errors:status = 400ctx = {'category': category, 'form': form, 'status': status}diff --git a/saleor/dashboard/templates/dashboard/category/detail.html b/saleor/dashboard/templates/dashboard/category/detail.htmlindex aab091da47..4c7d42a709 100644--- a/saleor/dashboard/templates/dashboard/category/detail.html+++ b/saleor/dashboard/templates/dashboard/category/detail.html@@ -29,7 +29,6 @@<div class="row"><div class="col l8"><div class="row">- {{ form.parent }}{{ form.name|materializecss:"s8 m10" }}{{ form.hidden|materializecss:"s4 m2" }}{{ form.description|materializecss }}