diff options
author | mikepk <mikepk@smarterer.com> | 2012-08-08 15:45:31 -0400 |
---|---|---|
committer | mikepk <mikepk@smarterer.com> | 2015-08-18 18:13:34 -0400 |
commit | 0a417004be7e2d950bdcd629ccf24cf9f56ef817 (patch) | |
tree | 0956af3d7c42cc8211e57e4286fc3b72c0ef3a9c | |
parent | 439529937f2014e0e117e0f38394eb5061d00554 (diff) | |
download | routes-0a417004be7e2d950bdcd629ccf24cf9f56ef817.tar.gz |
Modify submapping behavior to avoid concatenation
Allow name_prefixes to concatenate in submapper nesting. Also fix the
case where the concatenation behavior was being applied to all
non-dictionary arguments in the submapper. This had the effect of
concat'ing args like controller definition together rather than allowing
submapper definitions to override parent definitions for arguments.
Fix mapper so that collection_name can be None.
For collections with no collection_name, adds a logic branch to handle
the case cleanly and provide a reasonable path_prefix.
Remove special controller case
This change makes routes treat path_prefix and name_prefix as special
instead of 'controller' as special. This allows any argument to be
overridden in child submappers and allows the 'prefix' args to
concatenate as you would expect.
Add extended call signature to add_action, missing from previous commits
-rw-r--r-- | routes/mapper.py | 48 |
1 files changed, 37 insertions, 11 deletions
diff --git a/routes/mapper.py b/routes/mapper.py index 583309e..7ca9ee9 100644 --- a/routes/mapper.py +++ b/routes/mapper.py @@ -116,7 +116,19 @@ class SubMapperParent(object): controller = resource_name or collection_name if path_prefix is None: - path_prefix = '/' + collection_name + if collection_name is None: + path_prefix_str = '' + else: + path_prefix_str = '/{collection_name}' + else: + if collection_name is None: + path_prefix_str = "{pre}" + else: + path_prefix_str = "{pre}/{collection_name}" + + # generate what will be the path prefix for the collection + path_prefix = path_prefix_str.format(pre=path_prefix, + collection_name=collection_name) collection = SubMapper(self, collection_name=collection_name, resource_name=resource_name, @@ -148,30 +160,44 @@ class SubMapper(SubMapperParent): self.formatted = getattr(obj, 'formatted', None) if self.formatted is None: self.formatted = True - - self.add_actions(actions or []) + self.add_actions(actions or [], **kwargs) def connect(self, *args, **kwargs): newkargs = {} - newargs = args + # newargs = args + routename, path = args for key, value in six.iteritems(self.kwargs): if key == 'path_prefix': if len(args) > 1: - newargs = (args[0], self.kwargs[key] + args[1]) + # if there's a name_prefix, add it to the route name + # and if there's a path_prefix + path = ''.join((self.kwargs[key], args[1])) else: - newargs = (self.kwargs[key] + args[0],) + path = ''.join((self.kwargs[key], args[0])) + elif key == 'name_prefix': + if len(args) > 1: + # if there's a name_prefix, add it to the route name + # and if there's a path_prefix + routename = ''.join((self.kwargs[key], args[0])) + else: + routename = None elif key in kwargs: if isinstance(value, dict): newkargs[key] = dict(value, **kwargs[key]) # merge dicts - elif key == 'controller': - newkargs[key] = kwargs[key] else: - newkargs[key] = value + kwargs[key] + # Originally used this form: + # newkargs[key] = value + kwargs[key] + # New version avoids the inheritance concatenation issue + # with submappers. Only prefixes concatenate, everything + # else overrides in submappers. + newkargs[key] = kwargs[key] else: newkargs[key] = self.kwargs[key] for key in kwargs: if key not in self.kwargs: newkargs[key] = kwargs[key] + + newargs = (routename, path) return self.obj.connect(*newargs, **newkargs) def link(self, rel=None, name=None, action=None, method='GET', @@ -263,8 +289,8 @@ class SubMapper(SubMapperParent): """Generates the "delete" action for a collection member submapper.""" return self.action(action='delete', method='DELETE', **kwargs) - def add_actions(self, actions): - [getattr(self, action)() for action in actions] + def add_actions(self, actions, **kwargs): + [getattr(self, action)(**kwargs) for action in actions] # Provided for those who prefer using the 'with' syntax in Python 2.5+ def __enter__(self): |