def setup(app, url_path_prefix):
    """Set up routes as resources so we can use the `Index:get` notation for URL lookup."""

    combined_routes = [
        *extra_routes,
        *employee_routes,
        *static_routes,
        *saml_routes,
        *downloads_routes,
        *microservices_handler_routes,
        *customsql_handler_routes,
        *customsql_choice_handler_routes,
    ]

    module = ('app.handler')
    for route in combined_routes:
        prefix = url_path_prefix if route.kwargs.get('use_prefix',
                                                     True) else ''
        if route.method == "*":
            with add_resource_context(app, module=module,
                                      url_prefix=prefix) as new_resource:
                new_resource(route.path, route.handler())
        else:
            with add_route_context(app, module=module,
                                   url_prefix=prefix) as new_route:
                new_route(route.method, route.path, route.handler)
Exemplo n.º 2
0
    def test_add_resource_context_passing_classes(self, app):
        with add_resource_context(app) as route:
            route('/articles/', views.ArticleResource())
            route('/articles/{pk}', views.ArticleList())

        assert app.router['ArticleResource:get'].url() == '/articles/'
        assert app.router['ArticleResource:post'].url() == '/articles/'
Exemplo n.º 3
0
    def test_add_resource_context_passing_classes(self, app):
        with add_resource_context(app) as route:
            route('/articles/', views.ArticleResource())
            route('/articles/{pk}', views.ArticleList())

        assert str(app.router['ArticleResource:get'].url_for()) == '/articles/'
        assert str(app.router['ArticleResource:post'].url_for()) == '/articles/'
Exemplo n.º 4
0
    def test_add_resource_context_passing_classes_with_prefix(self, app):
        with add_resource_context(app, name_prefix='articles') as route:
            route('/articles/', views.ArticleResource())
            route('/articles/{pk}', views.ArticleList())

        assert app.router['articles.ArticleResource:get'].url() == '/articles/'
        assert app.router['articles.ArticleResource:post'].url() == '/articles/'
        assert app.router['articles.ArticleList:post'].url(parts={'pk': 42}) == '/articles/42'
Exemplo n.º 5
0
    def test_passing_module_and_resources_as_strings(self, app):
        with add_resource_context(app, module='tests.views') as route:
            route('/articles/', 'ArticleResource')
            route('/articles/{pk}', 'ArticleList')

        assert app.router['ArticleResource:get'].url() == '/articles/'
        assert app.router['ArticleResource:post'].url() == '/articles/'
        assert app.router['ArticleList:post'].url(parts={'pk': 42}) == '/articles/42'
Exemplo n.º 6
0
    def test_add_resource_context_basic(self, app):
        with add_resource_context(app, views) as route:
            route('/articles/', 'ArticleResource')
            route('/articles/{pk}', 'ArticleList')

        assert app.router['ArticleResource:get'].url() == '/articles/'
        assert app.router['ArticleResource:post'].url() == '/articles/'
        assert app.router['ArticleList:post'].url(parts={'pk': 42}) == '/articles/42'
Exemplo n.º 7
0
    def test_add_resource_context_with_name_prefix_and_override(self, app):
        with add_resource_context(app, views, name_prefix='api') as route:
            route('/articles/',
                  'ArticleResource',
                  names={'get': 'list_articles'})

        assert app.router['api.list_articles'].url() == '/articles/'
        assert app.router['api.ArticleResource:post'].url() == '/articles/'
Exemplo n.º 8
0
    def test_passing_module_and_resources_as_strings(self, app):
        with add_resource_context(app, module='tests.views') as route:
            route('/articles/', 'ArticleResource')
            route('/articles/{pk}', 'ArticleList')

        assert str(app.router['ArticleResource:get'].url_for()) == '/articles/'
        assert str(app.router['ArticleResource:post'].url_for()) == '/articles/'
        assert str(app.router['ArticleList:post'].url_for(
            pk='42')) == '/articles/42'
Exemplo n.º 9
0
    def test_add_resource_context_passing_classes_with_prefix(self, app):
        with add_resource_context(app, name_prefix='articles') as route:
            route('/articles/', views.ArticleResource())
            route('/articles/{pk}', views.ArticleList())

        assert str(app.router['articles.ArticleResource:get'].url_for()) == '/articles/'
        assert str(app.router['articles.ArticleResource:post'].url_for()) == '/articles/'
        assert str(app.router['articles.ArticleList:post'].url_for(
            pk='42')) == '/articles/42'  # noqa
Exemplo n.º 10
0
    def test_add_resource_context_basic(self, app):
        with add_resource_context(app, views) as route:
            route('/articles/', 'ArticleResource')
            route('/articles/{pk}', 'ArticleList')

        assert str(app.router['ArticleResource:get'].url_for()) == '/articles/'
        assert str(app.router['ArticleResource:post'].url_for()) == '/articles/'
        assert str(app.router['ArticleList:post'].url_for(
            pk='42')) == '/articles/42'
Exemplo n.º 11
0
    def test_add_resource_context_basic(self, app):
        with add_resource_context(app, views) as route:
            route('/articles/', 'ArticleResource')
            route('/articles/{pk}', 'ArticleList')

        assert app.router['ArticleResource:get'].url() == '/articles/'
        assert app.router['ArticleResource:post'].url() == '/articles/'
        assert app.router['ArticleList:post'].url(
            parts={'pk': 42}) == '/articles/42'
Exemplo n.º 12
0
    def test_add_resource_context_passing_classes_with_prefix(self, app):
        with add_resource_context(app, name_prefix='articles') as route:
            route('/articles/', views.ArticleResource())
            route('/articles/{pk}', views.ArticleList())

        assert app.router['articles.ArticleResource:get'].url() == '/articles/'
        assert app.router['articles.ArticleResource:post'].url(
        ) == '/articles/'
        assert app.router['articles.ArticleList:post'].url(
            parts={'pk': 42}) == '/articles/42'
Exemplo n.º 13
0
def setup(app, url_path_prefix):
    """Set up routes as resources so we can use the `Index:get` notation for URL lookup."""

    combined_routes = [*request_routes, *start_routes, *static_routes, *web_form_routes,
                       *webchat_routes, *common_routes, *support_centre_routes]

    for route in combined_routes:
        use_prefix = route.kwargs.get('use_prefix', True)
        prefix = url_path_prefix if use_prefix else ''
        with add_resource_context(app,
                                  module='app.handlers',
                                  url_prefix=prefix) as new_route:
            new_route(route.path, route.handler())
Exemplo n.º 14
0
 def test_make_resource_override_on_context_manager(self, app):
     db = {}
     with add_resource_context(app, module='tests.views',
                               make_resource=lambda cls: cls(db=db)) as route:
         route('/authors/', 'AuthorList')
def setup(app, url_path_prefix):
    """Set up routes as resources so we can use the `Index:get` notation for URL lookup."""
    for route in routes:
        prefix = url_path_prefix if route.kwargs.get('use_prefix', True) else ''
        with add_resource_context(app, module='app.handlers', url_prefix=prefix) as new_route:
            new_route(route.path, route.handler())
Exemplo n.º 16
0
    def test_add_resource_context_with_name_prefix(self, app):
        with add_resource_context(app, views, name_prefix='api') as route:
            route('/articles/', 'ArticleResource')

        assert str(app.router['api.ArticleResource:get'].url_for()) == '/articles/'
        assert str(app.router['api.ArticleResource:post'].url_for()) == '/articles/'
Exemplo n.º 17
0
 def test_make_resource_override_on_context_manager(self, app):
     db = {}
     with add_resource_context(app, module='tests.views',
                               make_resource=lambda cls: cls(db=db)) as route:
         route('/authors/', 'AuthorList')
Exemplo n.º 18
0
@coroutine
def index(request):
    return Response('Welcome!')

class HelloResource:

    @coroutine
    def get(self, request):
        return Response({
            'message': 'Welcome to the API!'
        })


with routing.add_route_context(app) as route:
    route('GET', '/', index)

with routing.add_resource_context(app, url_prefix='/api/') as route:
    route('/', HelloResource())

negotiation.setup(app)
path_norm.setup(app)

if __name__ == "__main__":
    run(
        app,
        app_uri="examples.kitchen_sink:app",
        reload=True,
        port=8000
    )
Exemplo n.º 19
0
    def test_add_resource_context_with_name_prefix(self, app):
        with add_resource_context(app, views, name_prefix='api') as route:
            route('/articles/', 'ArticleResource')

        assert app.router['api.ArticleResource:get'].url() == '/articles/'
        assert app.router['api.ArticleResource:post'].url() == '/articles/'
Exemplo n.º 20
0
    def test_add_resource_context_with_url_prefix(self, app):
        with add_resource_context(app, views, url_prefix='/api/') as route:
            route('/articles/', 'ArticleResource')

        assert app.router['ArticleResource:get'].url() == '/api/articles/'
        assert app.router['ArticleResource:post'].url() == '/api/articles/'
Exemplo n.º 21
0
    def test_add_resource_context_with_name_prefix_and_override(self, app):
        with add_resource_context(app, views, name_prefix='api') as route:
            route('/articles/', 'ArticleResource', names={'get': 'list_articles'})

        assert app.router['api.list_articles'].url() == '/articles/'
        assert app.router['api.ArticleResource:post'].url() == '/articles/'
Exemplo n.º 22
0
from asyncio import coroutine

from aiohttp import web
from aiohttp_utils import Response, routing, negotiation, run, path_norm

app = web.Application(router=routing.ResourceRouter())


@coroutine
def index(request):
    return Response('Welcome!')


class HelloResource:
    @coroutine
    def get(self, request):
        return Response({'message': 'Welcome to the API!'})


with routing.add_route_context(app) as route:
    route('GET', '/', index)

with routing.add_resource_context(app, url_prefix='/api/') as route:
    route('/', HelloResource())

negotiation.setup(app)
path_norm.setup(app)

if __name__ == "__main__":
    run(app, app_uri="examples.kitchen_sink:app", reload=True, port=8000)