How to use the muffin.Handler function in muffin

To help you get started, we’ve selected a few muffin examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github klen / muffin / tests / test_handlers.py View on Github external
async def test_handler(aiohttp_client):
    """Test class-based handlers.

    Actually everything in Muffin is class-based handlers.
    Because view-functions will convert to Handler classes.
    """
    from muffin.handler import register

    app = muffin.Application('test')

    @app.register('/res(/{res})?/?')
    @app.register('/res/{res}')
    class Resource(muffin.Handler):

        def get(self, request):
            return request.match_info

        async def post(self, request):
            data = await self.parse(request)
            return dict(data)

        @register('/res/lama/rama', methods=['GET', 'POST', 'PATCH'])
        def lama(self, request):
            return 'LAMA'

        @register('/res/rama/lama')
        def rama(self, request):
            return 'RAMA'
github klen / muffin / tests / test_handlers.py View on Github external
async def test_deffered(aiohttp_client):

    class Resource3(muffin.Handler):

        methods = 'get',

        def get(self, request):
            return 'Resource3'

    @Resource3.register('/dummy')
    def dummy(handler, request):
        return 'dummy here'

    app = muffin.Application('test')
    app.register('/resource-3')(Resource3)

    client = await aiohttp_client(app)
    async with client.get('/resource-3') as resp:
        assert resp.status == 200
github klen / muffin / tests / test_handlers.py View on Github external
data = await self.parse(request)
            return dict(data)

        @register('/res/lama/rama', methods=['GET', 'POST', 'PATCH'])
        def lama(self, request):
            return 'LAMA'

        @register('/res/rama/lama')
        def rama(self, request):
            return 'RAMA'

    assert set(Resource.methods) == set(['GET', 'POST'])
    assert asyncio.iscoroutinefunction(Resource.get)

    @app.register('/res2(/{res2})?/?')
    class Resource2(muffin.Handler):

        methods = 'get',

        def get(self, request):
            return 'OK'

        def put(self, request):
            raise Exception('Must not be called')

    @Resource2.register('/connect')
    def connect_(handler, request):
        return handler.app.name

    assert 'resource' in app.router
    assert 'resource.lama' in app.router
    assert 'resource.rama' in app.router
github klen / muffin-admin / muffin_admin / handler.py View on Github external
"""Implement Admin interfaces."""
import muffin
import copy

from aiohttp.web import StreamResponse
from muffin import Handler
from muffin.utils import json
from wtforms import Form

from .formatters import format_value
from .filters import default_converter, PREFIX as FILTERS_PREFIX, DEFAULT


class AdminHandlerMeta(type(Handler)):
    """Prepare admin handler."""

    def __new__(mcs, name, bases, params):
        """Copy columns formatters to created class."""
        cls = super(AdminHandlerMeta, mcs).__new__(mcs, name, bases, params)
        cls.actions = []
        cls.global_actions = []
        cls.name = cls.name.replace(' ', '_')
        cls.columns_formatters = copy.copy(cls.columns_formatters)
        return cls


class AdminHandler(Handler, metaclass=AdminHandlerMeta):
    """Base admin handler. Inherit from this class any other implementation."""

    # List of columns
github klen / muffin / example / views.py View on Github external
@app.register('/404')
def raise404(request):
    """ Raise HTTP exceptions. """
    raise muffin.HTTPNotFound


@app.register(muffin.HTTPNotFound)
def handle404(request):
    """ Handle HTTP exceptions. """
    return muffin.Response(text='Custom 404 Page', status=404)


@app.register('/api/example', '/api/example/{example}')
class Example(muffin.Handler):

    """ Custom handler in REST-like style. """

    def get(self, request):
        return {'simple': 'rest', 'example': request.match_info.get('example')}

    def post(self, request):
        return [1, 2, 3]


@app.register('/')
def index(request):
    """ Get a current logged user and render a template to HTML. """
    user = yield from app.ps.session.load_user(request)
    return app.ps.jinja2.render('index.html', user=user, view='index')
github klen / muffin-admin / muffin_admin / handler.py View on Github external
class AdminHandlerMeta(type(Handler)):
    """Prepare admin handler."""

    def __new__(mcs, name, bases, params):
        """Copy columns formatters to created class."""
        cls = super(AdminHandlerMeta, mcs).__new__(mcs, name, bases, params)
        cls.actions = []
        cls.global_actions = []
        cls.name = cls.name.replace(' ', '_')
        cls.columns_formatters = copy.copy(cls.columns_formatters)
        return cls


class AdminHandler(Handler, metaclass=AdminHandlerMeta):
    """Base admin handler. Inherit from this class any other implementation."""

    # List of columns
    columns = 'id',
    columns_labels = {}
    columns_formatters = {}
    columns_formatters_csv = {}
    columns_filters = ()
    columns_sort = None
    columns_csv = None

    # WTF form class
    form = None

    # Templates
    template_list = None