~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/publisher/__init__.py

  • Committer: William Grant
  • Date: 2010-07-03 08:18:50 UTC
  • Revision ID: grantw@unimelb.edu.au-20100703081850-ygl8vp5nyznf3dnk
Trailing slashes in URLs can now be detected by views accepting subpaths.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
       >>> _segment_path('/path/to/something')
48
48
       ['path', 'to', 'something']
49
49
       >>> _segment_path('/path/to/something/')
50
 
       ['path', 'to', 'something']
 
50
       ['path', 'to', 'something', '']
51
51
       >>> _segment_path('/')
52
 
       []
 
52
       ['']
53
53
    """
54
54
 
55
 
    segments = os.path.normpath(path).split('/')
 
55
    segments = os.path.normpath(path).split(os.sep)
56
56
 
57
 
    # Remove empty segments caused by leading and trailing seperators.
 
57
    # Remove empty segment caused by a leading seperator.
58
58
    if segments[0] == '':
59
59
        segments.pop(0)
60
 
    if segments[-1] == '':
61
 
        segments.pop()
 
60
 
 
61
    # normpath removes trailing separators. But trailing seperators are
 
62
    # meaningful to browsers, so we add an empty segment.
 
63
    if path.endswith(os.sep) and len(segments) > 0 and segments[-1] != '':
 
64
        segments.append('')
 
65
 
62
66
    return segments
63
67
 
64
68
class Publisher(object):
266
270
            # Attempt views first, then routes.
267
271
            if type(obj) in self.vmap and \
268
272
               viewset in self.vmap[type(obj)]:
269
 
                # If there are no segments left, attempt the default view.
270
 
                # Otherwise, look for a view with the name in the first
271
 
                # remaining path segment.
272
273
                vnames = self.vmap[type(obj)][viewset]
 
274
                # If there is a view with no name, it overrides
 
275
                # everything. Regardless of what comes after, we take it
 
276
                # and use all remaining segments as the subpath.
273
277
                if None in vnames:
274
278
                    view = vnames[None]
275
279
                    remove = 0
 
280
                # If there are no segments or only the empty segment left,
 
281
                # attempt the default view. Otherwise, look for a view with
 
282
                # the name in the first remaining path segment.
276
283
                else:
277
 
                    view = vnames.get(
278
 
                        self.default if len(todo) == 0 else todo[0])
 
284
                    if todo in ([], ['']):
 
285
                        view = vnames.get(self.default)
 
286
                    else:
 
287
                        view = vnames.get(todo[0])
279
288
                    remove = 1
280
289
 
281
290
                if view is not None:
297
306
                        view = vnames.get(tuple(todo[:x]))
298
307
                        if view is not None:
299
308
                            return (obj, view, tuple(todo[x:]))
300
 
                    view = vnames.get(tuple(todo + [self.default]))
 
309
                    # If we have an empty final segment (indicating a
 
310
                    # trailing slash), replace it with the default view.
 
311
                    # Otherwise try just adding the default view name.
 
312
                    prefix = list(todo)
 
313
                    if prefix[-1] == '':
 
314
                        prefix.pop()
 
315
 
 
316
                    view = vnames.get(tuple(prefix + [self.default]))
301
317
                    if view is not None:
302
318
                        return (obj, view, tuple())
303
319