Skip to main content

declaraive de/serialization of python structures for pyramid

Project description

You will simply get a request method request.objective to ensure your request objective.

from pyramid import view_config

import objective


def includeme(config):
    # include config somewhere
    config.include("pyramid_objective")
    config.add_route("foobar", "/foobar")


class FooObjective(objective.BunchMapping)
    @objective.Item()
    class body(objective.Mapping):
        foo = objective.Item(objective.Unicode)
        bar = objective.Item(objective.Number)


@view_config(route_name="foobar", request_method="POST")
def view(request):

    validated = request.objective(FooObjective)

    assert isinstance(validated.body['foo'], unicode)
    assert isinstance(validated.body['bar'], (int, float))

The default objective subject is implemented like this:

class DefaultObjectiveSubject(dict):

    """Default adapter to build our objective subject."""

    implements(IObjectiveSubject)
    adapts(IRequest)

    def __init__(self, request):
        super(DefaultObjectiveSubject, self).__init__(
            match=request.matchdict,
            params=request.params
        )

        body = self._find_body(request)

        if body:
            self['body'] = body

    @staticmethod
    def _find_body(request):
        # TODO maybe inspect content-type?
        try:
            body = request.json_body
            return body

        except ValueError:
            if request.POST:
                return request.POST

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyramid_objective-0.0.1.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

pyramid_objective-0.0.1-py2.py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page