Skip to content

Commit

Permalink
Reworked categories to enfore uniqueness at python-level.
Browse files Browse the repository at this point in the history
To work around MySQL bug with fields of > 255 chars not being allowed to
have unique indexes.
  • Loading branch information
codeinthehole committed Jul 9, 2012
1 parent c117c43 commit 166d7be
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 173 deletions.
12 changes: 11 additions & 1 deletion oscar/apps/catalogue/abstract_models.py
Expand Up @@ -56,7 +56,7 @@ class AbstractCategory(MP_Node):
name = models.CharField(max_length=255, db_index=True)
description = models.TextField(blank=True, null=True)
image = models.ImageField(upload_to='categories', blank=True, null=True)
slug = models.SlugField(max_length=1024, db_index=True, editable=False, unique=True)
slug = models.SlugField(max_length=1024, db_index=True, editable=False)
full_name = models.CharField(max_length=1024, db_index=True, editable=False)

_slug_separator = '/'
Expand All @@ -76,6 +76,16 @@ def save(self, update_slugs=True, *args, **kwargs):
else:
self.slug = slug
self.full_name = self.name

# Enforce slug uniqueness here as MySQL can't handle a unique index on
# the slug field
try:
match = self.__class__.objects.get(slug=self.slug)
except self.__class__.DoesNotExist:
pass
else:
if match.id != self.id:
raise ValidationError(_("A category with slug '%(slug)s' already exists") % {'slug': self.slug})
super(AbstractCategory, self).save(*args, **kwargs)

def move(self, target, pos=None):
Expand Down
161 changes: 0 additions & 161 deletions oscar/apps/catalogue/migrations/0005_auto__add_unique_category_slug.py

This file was deleted.

20 changes: 9 additions & 11 deletions oscar/templates/dashboard/catalogue/category_list.html
Expand Up @@ -24,17 +24,15 @@ <h1>Category management</h1>

{% block dashboard_content %}

<div class="well well-info">
<p>You are editing:
<a href="{% url dashboard:catalogue-category-list %}">Home</a>
{% if ancestors %}
&gt;
{% for ancestor in ancestors %}
<a href="{% url dashboard:catalogue-category-detail-list pk=ancestor.pk %}">{{ ancestor.name }}</a>{% if not forloop.last %} > {% endif %}
{% endfor %}
{% endif %}
</p>
</div>
<p>You are editing:
<a href="{% url dashboard:catalogue-category-list %}">Home</a>
{% if ancestors %}
&gt;
{% for ancestor in ancestors %}
<a href="{% url dashboard:catalogue-category-detail-list pk=ancestor.pk %}">{{ ancestor.name }}</a>{% if not forloop.last %} > {% endif %}
{% endfor %}
{% endif %}
</p>

<table class="table table-striped table-bordered">
<thead>
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/catalogue_tests.py
Expand Up @@ -124,6 +124,12 @@ def test_supports_has_children_method(self):
root.add_child(name="Books")
self.assertTrue(root.has_children())

def test_enforces_slug_uniqueness(self):
root = Category.add_root(name="Products")
root.add_child(name="Books")
with self.assertRaises(ValidationError):
root.add_child(name="Books")


class ProductTests(TestCase):

Expand Down

0 comments on commit 166d7be

Please sign in to comment.