Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Remove parent from category form

Marcin Gębala5bcc09eb4f0e
diff --git a/saleor/dashboard/category/forms.py b/saleor/dashboard/category/forms.py
index 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_404
from django.utils.text import slugify
-from django.utils.translation import ugettext_lazy as _
-from mptt.forms import TreeNodeChoiceField
from unidecode import unidecode
from ...product.models import Category
class 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 = Category
exclude = ['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 = True
super(CategoryForm, self).save(commit=commit)
diff --git a/saleor/dashboard/category/views.py b/saleor/dashboard/category/views.py
index 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_required
def 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 = 200
if 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 = 400
ctx = {'category': category, 'form': form, 'status': status}
diff --git a/saleor/dashboard/templates/dashboard/category/detail.html b/saleor/dashboard/templates/dashboard/category/detail.html
index 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 }}