Skip to content

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

Small improvements in categories views

Marcin Gębalac4ce4ff3c5bb
diff --git a/saleor/dashboard/category/forms.py b/saleor/dashboard/category/forms.py
index bfe1150370..9827f43167 100644
--- a/saleor/dashboard/category/forms.py
+++ b/saleor/dashboard/category/forms.py
@@ -6,14 +6,13 @@ from ...product.models import Category
class CategoryForm(forms.ModelForm):
+ parent = TreeNodeChoiceField(queryset=Category.objects.all(),
+ required=False)
+
class Meta:
model = Category
exclude = []
- def __init__(self, *args, **kwargs):
- super(CategoryForm, self).__init__(*args, **kwargs)
- self.fields['parent'] = TreeNodeChoiceField(queryset=Category.objects.all(), required=False)
-
def clean_parent(self):
parent = self.cleaned_data['parent']
if parent:
diff --git a/saleor/dashboard/category/views.py b/saleor/dashboard/category/views.py
index 050103272d..b2ec6bfa41 100644
--- a/saleor/dashboard/category/views.py
+++ b/saleor/dashboard/category/views.py
@@ -27,18 +27,14 @@ def category_list(request, root=None):
@staff_member_required
def category_details(request, pk=None, parent_pk=None):
- ctx = {}
if pk:
category = get_object_or_404(Category.objects.all(), pk=pk)
title = category.name
else:
category = Category()
title = _('Add new category')
- initial = {}
- if parent_pk:
- initial['parent'] = parent_pk
- ctx['parent_pk'] = parent_pk
- form = CategoryForm(request.POST or None, instance=category, initial=initial)
+ form = CategoryForm(request.POST or None, instance=category,
+ initial={'parent': parent_pk})
if form.is_valid():
form.save()
if pk:
@@ -50,7 +46,7 @@ def category_details(request, pk=None, parent_pk=None):
else:
if form.errors:
messages.error(request, _('Failed to save category'))
- ctx.update({'category': category, 'form': form, 'title': title})
+ ctx = {'category': category, 'form': form, 'title': title}
return TemplateResponse(request, 'dashboard/category/detail.html', ctx)
diff --git a/saleor/dashboard/templates/dashboard/category/detail.html b/saleor/dashboard/templates/dashboard/category/detail.html
index fda92848e4..e89961471d 100644
--- a/saleor/dashboard/templates/dashboard/category/detail.html
+++ b/saleor/dashboard/templates/dashboard/category/detail.html
@@ -21,10 +21,8 @@
</div>
<div class="row">
<div class="col l8 offset-l2 right-align">
- {% if parent_pk %}
- <a href="{% url 'dashboard:categories' root=parent_pk %}" class="btn-flat"><i class="mdi-content-clear"></i> {% trans "Cancel" %}</a>
-{# {% elif category.pk %}#}
-{# <a href="{% url 'dashboard:categories' root=category.pk %}" class="btn-flat"><i class="mdi-content-clear"></i> {% trans "Cancel" %}</a>#}
+ {% if category.parent %}
+ <a href="{% url 'dashboard:categories' root=category.parent.pk %}" class="btn-flat"><i class="mdi-content-clear"></i> {% trans "Cancel" %}</a>
{% else %}
<a href="{% url 'dashboard:categories' %}" class="btn-flat"><i class="mdi-content-clear"></i> {% trans "Cancel" %}</a>
{% endif %}