Skip to content

Commit

Permalink
Make unique_together work more seamlessly.
Browse files Browse the repository at this point in the history
Fixes #7

Add Orderable.validate_unique to exclude `sort_order` when it is
unique_together with something..
  • Loading branch information
maxpeterson committed Mar 7, 2019
1 parent fa5e986 commit ac132b8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
UPCOIMING
======

* Add Orderable.validate_unique to exclude `sort_order` when it is unique_together with something.

v6.0.1
======

Expand Down
17 changes: 17 additions & 0 deletions orderable/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ def prev(self):

return self.get_filtered_manager().before(self)

def validate_unique(self, exclude=None):
if self._is_sort_order_unique_together_with_something():
exclude = exclude or []
if 'sort_order' not in exclude:
exclude.append('sort_order')
return super().validate_unique(exclude=exclude)

def _is_sort_order_unique_together_with_something(self):
"""
Is the sort_order field unique_together with something
"""
unique_together = self._meta.unique_together
for fields in unique_together:
if 'sort_order' in fields and len(fields) > 1:
return True
return False

@staticmethod
def _update(qs):
"""
Expand Down
38 changes: 38 additions & 0 deletions orderable/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.exceptions import ValidationError
from hypothesis import example, given
from hypothesis.extra.django import TestCase
from hypothesis.strategies import integers, lists
Expand Down Expand Up @@ -237,3 +238,40 @@ def test_save_subtask_no_errors(self, sort_orders):

subtask.sort_order = 2
subtask.save()

def test_validate_unique(self):
"""validate_unique should validate when sort_order conflicts."""
task = Task.objects.create()
subtask = SubTask.objects.create(task=task, sort_order=1)
SubTask.objects.create(task=task, sort_order=2)

subtask.sort_order = 2
try:
subtask.validate_unique()
except ValidationError:
self.fail("SubTask.clean() raised ValidationError unexpectedly!")

def test_validate_unique_exclude_sort_order(self):
"""validate_unique should validate when sort_order excluded."""
task = Task.objects.create()
subtask = SubTask.objects.create(task=task, sort_order=1)
SubTask.objects.create(task=task, sort_order=2)

subtask.sort_order = 2
try:
subtask.validate_unique(exclude=('sort_order',))
except ValidationError:
self.fail("SubTask.clean() raised ValidationError unexpectedly!")

def test_validate_unique_not_unique_sort_order(self):
"""
validate_unique should validate when sort_order not in unique_together.
"""
task = Task.objects.create(sort_order=1)
Task.objects.create(sort_order=2)

task.sort_order = 1
try:
task.validate_unique()
except ValidationError:
self.fail("Task.clean() raised ValidationError unexpectedly!")

0 comments on commit ac132b8

Please sign in to comment.