Skip to content

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

Refactor category views

Marcin Gębalae155ee66ee25
diff --git a/saleor/dashboard/category/urls.py b/saleor/dashboard/category/urls.py
index c255d7ddbf..739557ef0b 100644
--- a/saleor/dashboard/category/urls.py
+++ b/saleor/dashboard/category/urls.py
@@ -4,15 +4,13 @@ from . import views
urlpatterns = [
url(r'^$',
- views.category_list, name='category-list'),
+ views.category_root_nodes_list, name='category-root-list'),
url(r'^(?P<root_pk>[0-9]+)/$',
- views.category_list, name='category-list'),
+ views.category_children_nodes_list, name='category-children-list'),
url(r'^add/$',
views.category_create, name='category-add'),
url(r'^(?P<root_pk>[0-9]+)/add/$',
views.category_create, name='category-add'),
- url(r'^(?P<pk>[0-9]+)/update/$',
- views.category_edit, name='category-update'),
url(r'^(?P<pk>[0-9]+)/delete/$',
views.category_delete, name='category-delete')
]
diff --git a/saleor/dashboard/category/views.py b/saleor/dashboard/category/views.py
index a66757cf1f..272dfc2c31 100644
--- a/saleor/dashboard/category/views.py
+++ b/saleor/dashboard/category/views.py
@@ -9,28 +9,28 @@ from .forms import CategoryForm
@staff_member_required
-def category_list(request, root_pk=None):
- root = None
- form = None
- if root_pk:
- root = get_object_or_404(Category, pk=root_pk)
- category = None
- if root.parent:
- category = get_object_or_404(Category, pk=root.parent.pk)
- categories = root.get_children()
- form = CategoryForm(request.POST or None,
- instance=root,
+def category_root_nodes_list(request):
+ categories = Category.tree.root_nodes()
+ ctx = {'categories': categories, 'path': [], 'root': None, 'form': None}
+ return TemplateResponse(request, 'dashboard/category/list.html', ctx)
+
+
+@staff_member_required
+def category_children_nodes_list(request, root_pk):
+ root = get_object_or_404(Category, pk=root_pk)
+ category = get_object_or_404(Category,
+ pk=root.parent.pk) if root.parent else None
+ form = CategoryForm(request.POST or None, instance=root,
initial={'parent': category})
- if form.is_valid():
- category = form.save()
- messages.success(request, _('Updated category %s') % category)
- if root:
- return redirect('dashboard:category-list', root_pk=root.pk)
- else:
- return redirect('dashboard:category-list')
- else:
- categories = Category.tree.root_nodes()
+ if form.is_valid():
+ category = form.save()
+ messages.success(request, _('Updated category %s') % category)
+ if root:
+ return redirect('dashboard:category-children-list', root_pk=root.pk)
+ else:
+ return redirect('dashboard:category-root-list')
path = root.get_ancestors(include_self=True) if root else []
+ categories = root.get_children()
ctx = {'categories': categories, 'path': path, 'root': root, 'form': form}
return TemplateResponse(request, 'dashboard/category/list.html', ctx)
@@ -43,27 +43,9 @@ def category_create(request, root_pk=None):
category = form.save()
messages.success(request, _('Added category %s') % category)
if root_pk:
- return redirect('dashboard:category-list', root_pk=root_pk)
- else:
- return redirect('dashboard:category-list')
- ctx = {'category': category, 'form': form}
- return TemplateResponse(request, 'dashboard/category/detail.html', ctx)
-
-
-@staff_member_required
-def category_edit(request, pk=None):
- category = get_object_or_404(Category, pk=pk)
- root = category.parent
- form = CategoryForm(request.POST or None,
- instance=category,
- initial={'parent': root})
- if form.is_valid():
- category = form.save()
- messages.success(request, _('Updated category %s') % category)
- if root:
- return redirect('dashboard:category-list', root_pk=root.pk)
+ return redirect('dashboard:category-children-list', root_pk=root_pk)
else:
- return redirect('dashboard:category-list')
+ return redirect('dashboard:category-root-list')
ctx = {'category': category, 'form': form}
return TemplateResponse(request, 'dashboard/category/detail.html', ctx)
@@ -75,9 +57,9 @@ def category_delete(request, pk):
category.delete()
messages.success(request, _('Deleted category %s') % category)
if category.parent:
- return redirect('dashboard:category-list', root_pk=category.parent.pk)
+ return redirect('dashboard:category-children-list', root_pk=category.parent.pk)
else:
- return redirect('dashboard:category-list')
+ return redirect('dashboard:category-root-list')
ctx = {'category': category,
'descendants': list(category.get_descendants()),
'products_count': len(category.products.all())}
diff --git a/saleor/dashboard/templates/dashboard/base.html b/saleor/dashboard/templates/dashboard/base.html
index d2dab9ca17..5f24400590 100644
--- a/saleor/dashboard/templates/dashboard/base.html
+++ b/saleor/dashboard/templates/dashboard/base.html
@@ -61,7 +61,7 @@
<ul>
<li class="subnav-products"><a href="{% url 'dashboard:product-list' %}">{% trans "Products" %}</a></li>
<li class="subnav-attributes"><a href="{% url 'dashboard:product-attributes' %}">{% trans "Attributes" %}</a></li>
- <li class="subnav-categories"><a href="{% url 'dashboard:category-list' %}">{% trans "Categories" %}</a></li>
+ <li class="subnav-categories"><a href="{% url 'dashboard:category-root-list' %}">{% trans "Categories" %}</a></li>
</ul>
</div>
</li>
diff --git a/saleor/dashboard/templates/dashboard/category/category_confirm_delete.html b/saleor/dashboard/templates/dashboard/category/category_confirm_delete.html
index 65ac115204..9b7e8cef98 100644
--- a/saleor/dashboard/templates/dashboard/category/category_confirm_delete.html
+++ b/saleor/dashboard/templates/dashboard/category/category_confirm_delete.html
@@ -50,7 +50,7 @@
<div class="form-group">
<div class="col-md-6">
<button class="btn btn-danger" type="submit"><i class="glyphicon glyphicon-trash"></i> {% trans "Delete" %}</button>
- <a class="btn btn-default" href="{% url 'dashboard:category-update' pk=category.pk %}"><i class="glyphicon glyphicon-remove"></i> {% trans "Cancel" %}</a>
+ <a class="btn btn-default" href="{% url 'dashboard:category-children-list' root_pk=category.pk %}"><i class="glyphicon glyphicon-remove"></i> {% trans "Cancel" %}</a>
</div>
</div>
</form>
diff --git a/saleor/dashboard/templates/dashboard/category/detail.html b/saleor/dashboard/templates/dashboard/category/detail.html
index de23a836fc..f884730d1a 100644
--- a/saleor/dashboard/templates/dashboard/category/detail.html
+++ b/saleor/dashboard/templates/dashboard/category/detail.html
@@ -11,10 +11,10 @@
{% block breadcrumbs %}
<ul class="breadcrumbs breadcrumbs--with-menu">
- <li><a href="{% url 'dashboard:category-list' %}">{% trans "Categories" %}</a></li>
+ <li><a href="{% url 'dashboard:category-root-list' %}">{% trans "Categories" %}</a></li>
{% if path %}
{% for node in path %}
- <li><a href="{% url 'dashboard:category-list' root_pk=node.pk %}">{{ node.name }}</a></li>
+ <li><a href="{% url 'dashboard:category-children-list' root_pk=node.pk %}">{{ node.name }}</a></li>
{% endfor %}
{% endif %}
<li>{% if category.pk %}{{ category }}{% else %}{% trans 'Add new category' %}{% endif %}</li>
@@ -36,9 +36,9 @@
<div class="row">
<div class="col s12 right-align">
{% if category.parent %}
- <a href="{% url 'dashboard:category-list' root_pk=category.parent.pk %}" class="btn-flat">{% trans "Cancel" %}</a>
+ <a href="{% url 'dashboard:category-children-list' root_pk=category.parent.pk %}" class="btn-flat">{% trans "Cancel" %}</a>
{% else %}
- <a href="{% url 'dashboard:category-list' %}" class="btn-flat">{% trans "Cancel" %}</a>
+ <a href="{% url 'dashboard:category-root-list' %}" class="btn-flat">{% trans "Cancel" %}</a>
{% endif %}
{% if category.pk %}
<button type="submit" class="btn">{% trans "Update" %}</button>
diff --git a/saleor/dashboard/templates/dashboard/category/list.html b/saleor/dashboard/templates/dashboard/category/list.html
index 192d6d388a..7ec2453bf0 100644
--- a/saleor/dashboard/templates/dashboard/category/list.html
+++ b/saleor/dashboard/templates/dashboard/category/list.html
@@ -10,9 +10,9 @@
{% block header_menu %}
{% if root %}
{% if root.parent %}
- {% url 'dashboard:category-list' root_pk=root.parent.pk as url %}
+ {% url 'dashboard:category-children-list' root_pk=root.parent.pk as url %}
{% else %}
- {% url 'dashboard:category-list' as url %}
+ {% url 'dashboard:category-root-list' as url %}
{% endif %}
{% include "dashboard/includes/_back-link.html" with url=url %}
{% else %}
@@ -44,14 +44,14 @@
{% block breadcrumbs %}
<ul class="breadcrumbs breadcrumbs--with-menu breadcrumbs--ellipsed">
- <li>{% if path %}<a href="{% url 'dashboard:category-list' %}">{% trans "Categories" %}</a>{% else %}{% trans "Categories" %}{% endif %}</li>
+ <li>{% if path %}<a href="{% url 'dashboard:category-root-list' %}">{% trans "Categories" %}</a>{% else %}{% trans "Categories" %}{% endif %}</li>
{% if path %}
{% for node in path %}
{% if forloop.last %}
<li title="{{ node.name }}"><span class="breadcrumbs--ellipsed-item">{{ node.name }}</span></li>
{% else %}
<li>
- <a title="{{ node.name }}" href="{% url 'dashboard:category-list' root_pk=node.pk %}">
+ <a title="{{ node.name }}" href="{% url 'dashboard:category-children-list' root_pk=node.pk %}">
<span class="breadcrumbs--ellipsed-item">{{ node.name }}</span>
</a>
</li>
@@ -91,7 +91,7 @@
{% if categories %}
{% for node in categories %}
<li class="collection-item list-item list-item--two-lines{% if node.hidden %} list-item--hidden{% endif %}">
- <a class="list-item-name" href="{% url 'dashboard:category-list' root_pk=node.pk %}">
+ <a class="list-item-name" href="{% url 'dashboard:category-children-list' root_pk=node.pk %}">
{% if node.hidden %}<span class="list-item-prefix">({% trans "Hidden" %}) </span>{% endif %}{{ node.name }}
</a>
<p class="list-item-desc"><span>{{ node.description }}</span></p>