Skip to content

Commit

Permalink
Fix ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
ale-rt committed Oct 4, 2018
1 parent 13231bd commit 40e6974
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CHANGES
0.7.1 (unreleased)
==================

- Python 3 compatibility: use the implementer decorator
- Python 3 compatibility: use the implementer decorator and fix ordering
[ale-rt]

- Python 3 compatibility: Make ``RelationValue`` hashable. [sallner]
Expand Down
18 changes: 14 additions & 4 deletions src/z3c/relationfield/relation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import total_ordering
from persistent import Persistent
from z3c.objpath.interfaces import IObjectPath
from z3c.relationfield.interfaces import IRelationValue
Expand All @@ -10,6 +11,7 @@


@implementer(IRelationValue)
@total_ordering
class RelationValue(Persistent):

_broken_to_path = None
Expand Down Expand Up @@ -77,10 +79,18 @@ def __eq__(self, other):
def __ne__(self, other):
return not self.__eq__(other)

def __cmp__(self, other):
if other is None:
return cmp(self._sort_key(), None)
return cmp(self._sort_key(), other._sort_key())
def __lt__(self, other):
''' Relations are sorted by default on a combination of the relation name,
the path of the object the relation is one and the path of the object
the relation is pointing to.
'''
if (self.from_attribute or '') < (other.from_attribute or ''):
return True
if (self.from_path or '') < (other.from_path or ''):
return True
if (self.to_path or '') < (other.to_path or ''):
return True
return False

def _sort_key(self):
return (self.from_attribute, self.from_path, self.to_path)
Expand Down

0 comments on commit 40e6974

Please sign in to comment.