X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/d8ba4e197c35a8e30f10112fcf53740e0711dc98..2f90757a89424650440d684e08cbf53d98cec8fd:/models/base.py diff --git a/models/base.py b/models/base.py index dabe491..ae15d16 100644 --- a/models/base.py +++ b/models/base.py @@ -291,6 +291,11 @@ class TreeManager(models.Manager): """ segments = path.split(pathsep) + # Check for a trailing pathsep so we can restore it later. + trailing_pathsep = False + if segments[-1] == '': + trailing_pathsep = True + # Clean out blank segments. Handles multiple consecutive pathseps. while True: try: @@ -318,6 +323,12 @@ class TreeManager(models.Manager): kwargs[prefix[:-2]] = root return kwargs + def build_path(segments): + path = pathsep.join(segments) + if trailing_pathsep and segments and segments[-1] != '': + path += pathsep + return path + def find_obj(segments, depth, deepest_found): try: obj = self.get(**make_query_kwargs(segments[:depth])) @@ -341,13 +352,13 @@ class TreeManager(models.Manager): depth = (len(segments) + depth)/2 if deepest_found == depth: - return obj, pathsep.join(segments[deepest_found:]) or None + return obj, build_path(segments[deepest_found:]) or None try: return find_obj(segments, depth, deepest_found) except self.model.DoesNotExist: # Then the deepest one was already found. - return obj, pathsep.join(segments[deepest_found:]) + return obj, build_path(segments[deepest_found:]) return find_obj(segments, len(segments), 0)