Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add name option for route building #901

Merged
merged 3 commits into from
Aug 31, 2017
Merged

add name option for route building #901

merged 3 commits into from
Aug 31, 2017

Conversation

lixxu
Copy link
Contributor

@lixxu lixxu commented Aug 21, 2017

No description provided.

@r0fls
Copy link
Contributor

r0fls commented Aug 21, 2017

This may also solve #895

@lixxu
Copy link
Contributor Author

lixxu commented Aug 21, 2017

no, it can not. the later name will be ignored as sanic use url as key that caused only first name will be the winner.

app = Sanic('test_dynamic_route')

@app.route('/overload', methods=['GET'], name='route_first')
async def handler1(request):
    return text('OK1')

@app.route('/overload', methods=['POST', 'PUT'], name='name_will_be_ignored')
async def handler2(request):
    return text('OK2')

@lixxu
Copy link
Contributor Author

lixxu commented Aug 22, 2017

Not sure if this makes sense.

# sanic/router.py

class RouteNameExists(Exception):
    pass


class Router:
    # other codes
    def __init__(self):
        # other codes
        self.routes_names = {}
        
    def _add(self, uri, methods, handler, host=None, name=None):
        # other codes
        if handler_name in self.routes_names:
            raise RouteNameExists(
                "Route name already exists: {}".format(handler_name))
            
        self.routes_names[handler_name] = (uri, route)

    def remove(self, uri, clean_cache=True, host=None):
        if host is not None:
            uri = host + uri
        try:
            route = self.routes_all.pop(uri)
            for handler_name, pairs in self.routes_names.items():
                if pairs[0] == uri:
                    self.routes_names.pop(handler_name)
                    break
                
        except KeyError:
            raise RouteDoesNotExist("Route was not registered: {}".format(uri))

        # other codes

    @lru_cache(maxsize=ROUTER_CACHE_SIZE)
    def find_route_by_view_name(self, view_name):
        # ...
        return self.routes_names.get(view_name, (None, None))

@lixxu
Copy link
Contributor Author

lixxu commented Aug 22, 2017

I tried, seems work.

@r0fls r0fls merged commit 158da09 into sanic-org:master Aug 31, 2017
@seemethere seemethere added this to the 0.6.1 milestone Oct 13, 2017
@r0fls
Copy link
Contributor

r0fls commented Nov 6, 2017

@lixxu do you mean that it should be ok to close #895 ?

@lixxu
Copy link
Contributor Author

lixxu commented Nov 6, 2017

yes, it's ok to close. but seems there's a bug that when specify the name that same as the handler name for first function (don't know why). like this:

# below will not work
@bp.get('/<post_id>', name='show')
async def show(request, post_id):
    pass

# below is ok
@bp.get('/<post_id>', name='show')
async def get(request, post_id):
    pass

@seemethere seemethere modified the milestones: 0.6.1, 0.7.0 Nov 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants