Skip to content

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

Refactor categories

Marcin Gębala285d20a8c84c
diff --git a/saleor/dashboard/category/forms.py b/saleor/dashboard/category/forms.py
index 9827f43167..fdbf03dc77 100644
--- a/saleor/dashboard/category/forms.py
+++ b/saleor/dashboard/category/forms.py
@@ -1,6 +1,8 @@
from django import forms
+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
@@ -11,7 +13,7 @@ class CategoryForm(forms.ModelForm):
class Meta:
model = Category
- exclude = []
+ exclude = ['slug']
def clean_parent(self):
parent = self.cleaned_data['parent']
@@ -21,3 +23,7 @@ class CategoryForm(forms.ModelForm):
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))
+ return super(CategoryForm, self).save(commit=commit)
diff --git a/saleor/dashboard/category/urls.py b/saleor/dashboard/category/urls.py
index dc9a72a155..c255d7ddbf 100644
--- a/saleor/dashboard/category/urls.py
+++ b/saleor/dashboard/category/urls.py
@@ -4,15 +4,15 @@ from . import views
urlpatterns = [
url(r'^$',
- views.category_list, name='categories'),
- url(r'^(?P<root>[0-9]+)/$',
- views.category_list, name='categories'),
+ views.category_list, name='category-list'),
+ url(r'^(?P<root_pk>[0-9]+)/$',
+ views.category_list, name='category-list'),
url(r'^add/$',
- views.category_details, name='category-add'),
- url(r'^(?P<parent_pk>[0-9]+)/add/$',
- views.category_details, name='category-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_details, name='category-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 b2ec6bfa41..767e479311 100644
--- a/saleor/dashboard/category/views.py
+++ b/saleor/dashboard/category/views.py
@@ -9,43 +9,53 @@ from .forms import CategoryForm
@staff_member_required
-def category_list(request, root=None):
- ctx = {}
- categories = Category.objects.all()
- if root:
- current_node = get_object_or_404(Category, pk=root)
- categories = current_node.get_descendants()
- ctx['current_node'] = current_node
- ctx['category_breadcrumbs'] = current_node.get_ancestors(include_self=True)
- min_level = categories[0].get_level() if categories else 0
- max_level = min_level + 1
- categories = categories.filter(level__gte=min_level, level__lte=max_level)
- ctx['categories'] = categories
- ctx['min_level'] = min_level
+def category_list(request, root_pk=None):
+ root = None
+ if root_pk:
+ root = get_object_or_404(Category, pk=root_pk)
+ categories = root.get_children()
+ else:
+ categories = Category.tree.root_nodes()
+ path = root.get_ancestors(include_self=True) if root else []
+ ctx = {'categories': categories, 'path': path, 'root': root}
return TemplateResponse(request, 'dashboard/category/list.html', ctx)
@staff_member_required
-def category_details(request, pk=None, parent_pk=None):
- if pk:
- category = get_object_or_404(Category.objects.all(), pk=pk)
- title = category.name
- else:
- category = Category()
- title = _('Add new category')
- form = CategoryForm(request.POST or None, instance=category,
- initial={'parent': parent_pk})
+def category_create(request, root_pk=None):
+ category = Category()
+ title = _('Add new category')
+ form = CategoryForm(request.POST or None, initial={'parent': root_pk})
if form.is_valid():
- form.save()
- if pk:
- msg = _('Updated category %s') % category
+ category = form.save()
+ messages.success(request, _('Added category %s') % category)
+ if root_pk:
+ return redirect('dashboard:category-list', root_pk=root_pk)
else:
- msg = _('Added category %s') % category
- messages.success(request, msg)
- return redirect('dashboard:categories')
- else:
- if form.errors:
- messages.error(request, _('Failed to save category'))
+ return redirect('dashboard:category-list')
+ elif form.errors:
+ messages.error(request, _('Failed to save category'))
+ ctx = {'category': category, 'form': form, 'title': title}
+ 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)
+ title = category.name
+ form = CategoryForm(request.POST or None,
+ instance=category,
+ initial={'parent': category.parent})
+ if form.is_valid():
+ category = form.save()
+ messages.success(request, _('Updated category %s') % category)
+ if category.parent:
+ return redirect('dashboard:category-list',
+ root_pk=category.parent.pk)
+ else:
+ return redirect('dashboard:category-list')
+ elif form.errors:
+ messages.error(request, _('Failed to save category'))
ctx = {'category': category, 'form': form, 'title': title}
return TemplateResponse(request, 'dashboard/category/detail.html', ctx)
@@ -56,7 +66,10 @@ def category_delete(request, pk):
if request.method == 'POST':
category.delete()
messages.success(request, _('Deleted category %s') % category)
- return redirect('dashboard:categories')
+ if category.parent:
+ return redirect('dashboard:category-list', root_pk=category.parent.pk)
+ else:
+ return redirect('dashboard:category-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 8fb087dd95..fd8c162587 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:categories' %}">{% trans "Categories" %}</a></li>
+ <li class="subnav-categories"><a href="{% url 'dashboard:category-list' %}">{% trans "Categories" %}</a></li>
</ul>
</div>
</li>
diff --git a/saleor/dashboard/templates/dashboard/category/detail.html b/saleor/dashboard/templates/dashboard/category/detail.html
index e89961471d..9aea5ef019 100644
--- a/saleor/dashboard/templates/dashboard/category/detail.html
+++ b/saleor/dashboard/templates/dashboard/category/detail.html
@@ -9,6 +9,20 @@
{% block page_title %}{{ title }}{% endblock %}
+{% block breadcrumbs %}
+ <ul class="breadcrumbs breadcrumbs--with-menu">
+ <li><a href="{% url 'dashboard:category-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>
+ {% endfor %}
+ {% endif %}
+ <li>{{ title }}</li>
+ </ul>
+{% endblock %}
+
+{% block menu_catalogue_class %} active{% endblock %}
+
{% block content %}
<form method="post" id="category-form" enctype="multipart/form-data">
<div class="row">
@@ -22,9 +36,9 @@
<div class="row">
<div class="col l8 offset-l2 right-align">
{% if category.parent %}
- <a href="{% url 'dashboard:categories' root=category.parent.pk %}" class="btn-flat"><i class="mdi-content-clear"></i> {% trans "Cancel" %}</a>
+ <a href="{% url 'dashboard:category-list' root_pk=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>
+ <a href="{% url 'dashboard:category-list' %}" class="btn-flat"><i class="mdi-content-clear"></i> {% trans "Cancel" %}</a>
{% endif %}
{% if category.pk %}
<button type="submit" class="btn"><i class="mdi-content-save"></i> {% trans "Update" %}</button>
diff --git a/saleor/dashboard/templates/dashboard/category/list.html b/saleor/dashboard/templates/dashboard/category/list.html
index 5d7acb18ab..1c38e09f79 100644
--- a/saleor/dashboard/templates/dashboard/category/list.html
+++ b/saleor/dashboard/templates/dashboard/category/list.html
@@ -10,44 +10,51 @@
{% block header_extra %}
<a class="btn-fab btn-floating btn-large waves-effect waves-light teal" title="{% trans "Add category" %}"
- href="{% if current_node %}{% url 'dashboard:category-add' parent_pk=current_node.pk %}{% else %}{% url 'dashboard:category-add' %}{% endif %}">
+ href="{% if root %}{% url 'dashboard:category-add' root_pk=root.pk %}{% else %}{% url 'dashboard:category-add' %}{% endif %}">
<i class="mdi-content-add"></i>
</a>
{% endblock %}
-{% block content %}
- <div class="row">
- <div class="col s12">
- {% for item in category_breadcrumbs %}
- {% if forloop.last %}
- <span>{% if not forloop.first %} / {% endif %}{{ item.name }}</span>
- {% else %}
- <span>{% if not forloop.first %} / {% endif %}<a href="{% url 'dashboard:categories' root=item.pk %}">{{ item.name }}</a></span>
- {% endif %}
- {% endfor %}
+{% block breadcrumbs %}
+ <ul class="breadcrumbs breadcrumbs--with-menu">
+ <li>{% if path %}<a href="{% url 'dashboard:category-list' %}">{% trans "Categories" %}</a>{% else %}{% trans "Categories" %}{% endif %}</li>
+ {% if path %}
+ {% for node in path %}
+ {% if forloop.last %}
+ <li>{{ node.name }}</li>
+ {% else %}
+ <li><a href="{% url 'dashboard:category-list' root_pk=node.pk %}">{{ node.name }}</a></li>
+ {% endif %}
+ {% endfor %}
+ {% endif %}
+ </ul>
+{% endblock %}
- <ul class="collection category-list">
- {% recursetree categories %}
- <li class="collection-item category-list-item">
- {% if node.level == min_level %}<a href="{% url 'dashboard:categories' root=node.pk %}">{{ node.name }}</a>{% else %}{{ node.name }}{% endif %}
+{% block menu_catalogue_class %} active{% endblock %}
- <div class="right category-list-item-actions">
- <a href="{% url 'dashboard:category-add' parent_pk=node.pk %}" title="{% trans "Add child" %}">
- <i class="mdi-content-add teal-text"></i>
- </a>
- <a href="{% url 'dashboard:category-update' pk=node.pk %}" title="{% trans "Edit category" %}">
- <i class="mdi-content-create light-blue-text lighten-2"></i>
- </a>
- <a href="{% url 'dashboard:category-delete' pk=node.pk %}" title="{% trans "Delete category" %}">
- <i class="mdi-action-delete materialize-red-text"></i>
- </a>
- </div>
- </li>
- {% if not node.is_leaf_node %}
- {{ children }}
- {% endif %}
- {% endrecursetree %}
- </ul>
- </div>
- </div>
-{% endblock %}{% if current_node %}: {{ current_node }}{% endif %}
+{% block content %}
+ <div class="row">
+ <div class="col s12">
+ <ul class="collection category-list">
+ {% if categories %}
+ {% for node in categories %}
+ <li class="collection-item category-list-item">
+ <a href="{% url 'dashboard:category-list' root_pk=node.pk %}">{{ node.name }}</a>
+ <div class="right category-list-item-actions">
+ <a href="{% url 'dashboard:category-add' root_pk=node.pk %}" title="{% trans "Add child" %}">
+ <i class="mdi-content-add teal-text"></i>
+ </a>
+ <a href="{% url 'dashboard:category-update' pk=node.pk %}" title="{% trans "Edit category" %}"><i class="mdi-content-create light-blue-text lighten-2"></i></a>
+ <a href="{% url 'dashboard:category-delete' pk=node.pk %}" title="{% trans "Delete category" %}">
+ <i class="mdi-action-delete materialize-red-text"></i>
+ </a>
+ </div>
+ </li>
+ {% endfor %}
+ {% else %}
+ <p>{% trans "No subcategories" %}</p>
+ {% endif %}
+ </ul>
+ </div>
+ </div>
+{% endblock %}
diff --git a/saleor/product/models/base.py b/saleor/product/models/base.py
index b0bcd21af3..ed7cb604ad 100644
--- a/saleor/product/models/base.py
+++ b/saleor/product/models/base.py
@@ -5,6 +5,7 @@ from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.core.validators import MinValueValidator, RegexValidator
+from django.db.models import Manager
from django.utils.encoding import python_2_unicode_compatible, smart_text
from django.db import models
from django.utils.text import slugify
@@ -12,6 +13,7 @@ from django.utils.translation import pgettext_lazy
from django_prices.models import PriceField
from jsonfield import JSONField
from model_utils.managers import InheritanceManager
+from mptt.managers import TreeManager
from mptt.models import MPTTModel
from satchless.item import ItemRange, Item
from unidecode import unidecode
@@ -32,6 +34,9 @@ class Category(MPTTModel):
'self', null=True, blank=True, related_name='children',
verbose_name=pgettext_lazy('Category field', 'parent'))
+ objects = Manager()
+ tree = TreeManager()
+
def __str__(self):
return self.name