Skip to content

Commit

Permalink
Merge pull request #191 from level12/179-no-default-route
Browse files Browse the repository at this point in the history
**BC BREAK** no longer add default route when at least one rule present
  • Loading branch information
guruofgentoo committed Nov 1, 2022
2 parents b09d55e + d4e7bf4 commit 15b003e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
22 changes: 22 additions & 0 deletions docs/source/readme/installation.rst
Expand Up @@ -2,3 +2,25 @@ Installation
============

``pip install keg``


Upgrade Notes
=============

While we attempt to preserve backward compatibility, some Keg versions do introduce
breaking changes. This list should provide information on needed app changes.

- 0.10.0
- ``rule``: default class view route no longer generated when any rules are present

- Absolute route had been provided automatically from the class name, but in some situations
this would not be desired. Views that still need that route can use a couple of solutions:

- Provide an absolute route rule: ``rule('/my-route')``
- Use an empty relative route rule: ``rule()``

- All of an app's routes may be shown on CLI with the ``<app> develop routes`` command

- Removed ``keg`` blueprint along with ``ping`` and ``exception-test`` routes
- DB manager ``prep_empty`` method no longer called (had been deprecated)
- Python 2 support removed
15 changes: 15 additions & 0 deletions keg/tests/test_view_routing.py
Expand Up @@ -54,6 +54,7 @@ def test_route_endpoints(self):
'routing.cars:list',
'routing.explicit-route',
'routing.hello-req',
'routing.hello-req2',
'routing.hello-world',
'routing.hw-rule-default',
'routing.misc',
Expand Down Expand Up @@ -210,6 +211,20 @@ def test_route_with_required_rule(self):
assert rule.methods == {'GET', 'HEAD', 'OPTIONS'}
assert rule.endpoint == 'routing.hello-req'

def test_route_no_absolute_single_endpoint(self):
self.testapp.get('/hello-req2', status=404)

resp = self.testapp.get('/hello-req2/foo')
assert resp.text == 'Hello foo'

rules = list(self.app.url_map.iter_rules(endpoint='routing.hello-req2'))
assert len(rules) == 1
rule = rules.pop()

assert rule.rule == '/hello-req2/<name>'
assert rule.methods == {'GET', 'HEAD', 'OPTIONS'}
assert rule.endpoint == 'routing.hello-req2'

def test_route_plain(self):
resp = self.testapp.get('/cars/list')
assert resp.text == 'list'
Expand Down
8 changes: 4 additions & 4 deletions keg/web.py
Expand Up @@ -315,16 +315,16 @@ def init_blueprint(cls, rules):
str_endpoint = str(endpoint)
view_func = cls.as_view(str_endpoint)

absolute_found = False
for rule, options in rules:
if rule.startswith('/'):
absolute_found = True
if rule and rule.startswith('/'):
class_url = rule
elif not rule:
rule = class_url
else:
rule = '{}/{}'.format(class_url, rule)
cls.blueprint.add_url_rule(rule, endpoint=endpoint, view_func=view_func, **options)

if not absolute_found:
if not rules:
cls.blueprint.add_url_rule(class_url, endpoint=endpoint, view_func=view_func)

for rule, options in cls.url_rules:
Expand Down
18 changes: 15 additions & 3 deletions keg_apps/web/views/routing.py
Expand Up @@ -60,10 +60,10 @@ def get(self):

class HelloWorld(BaseView):
"""
/hello -> 'Hello World'
/hello/foo -> 'Hello Foo'
/hello-world -> 'Hello World'
/hello-world/foo -> 'Hello Foo'
"""
# relative URL indicates this route should be appended to the default rule for the class
rule()
rule('<name>')

def get(self, name='World'):
Expand Down Expand Up @@ -101,6 +101,18 @@ def get(self, name):
return _('Hello {name}', name=name)


class HelloReq2(BaseView):
"""
/hello-req2 -> 404
/hello-req2/foo -> 'Hello Foo'
"""
# no absolute rule, but only one endpoint to use
rule('<name>')

def get(self, name):
return _('Hello {name}', name=name)


class Cars(BaseView):
"""
CRUD for a model/entity
Expand Down

0 comments on commit 15b003e

Please sign in to comment.