コード例 #1
0
ファイル: crud.py プロジェクト: pascalbertrand/hypr
    def post(self):

        model = self.__model__
        bulk = request.args.get('_bulk', '').lower() in ['true', 'on', '1']
        json = yield from request.json()

        if json is None:
            abort(400)

        if not model.__validate__(json):
            abort(400)

        if bulk:
            # TODO: implement bulk POST
            raise NotImplementedError()
        else:

            try:
                rv = model(**json)
            except TypeError:
                abort(400)
            else:
                rv.save(commit=False)

        # commit changes

        try:
            model.commit()
        except ModelConflictException:
            model.rollback()
            abort(409)
        else:
            return rv, 201
コード例 #2
0
ファイル: crud.py プロジェクト: project-hypr/hypr
    def put(self, _data=None, **kwargs):
        """Update an existing instance of the model.

        put() acts as the UPDATE of the CRUD functions. It modifies an existing
        instance of the model with the JSON payload of the request. The data
        can be validated with the help of jsonschema.

        Args:
            _data: override the payload of the request.

        Docs:
            Update an existing instance of ``{model}``.

            Request
            -------

            Headers
            =======
            The request should have the Content-Type application/json.

            Body
            ====
            The data should comply with the following schema::

                {model.schema}
        """
        model = self.__model__

        if _data is None:
            _data = yield from request.json()
        if _data is None:
            abort(400)

        bulk = request.args.get('_bulk', '').lower() in ['true', 'on', '1']
        if bulk and not kwargs:
            # TODO: implement bulk PUT
            raise NotImplementedError()
        elif kwargs:
            rv = model.one(*self._uid(**kwargs)) or abort(404)
            model.__validate__(_data) or abort(400)
            for k, v in _data.items():
                if hasattr(rv, k):
                    setattr(rv, k, v)
                else:
                    model.rollback()
                    abort(400)
            rv.save(commit=False)
        else:
            abort(400)

        try:
            model.commit()
        except ModelConflictException:
            model.rollback()
            abort(409)
        else:
            return rv
コード例 #3
0
ファイル: crud.py プロジェクト: pascalbertrand/hypr
    def put(self, **kwargs):

        model = self.__model__
        bulk = request.args.get('_bulk', '').lower() in ['true', 'on', '1']
        json = yield from request.json()

        if json is None:
            abort(400)

        if not model.__validate__(json):
            abort(400)

        if bulk and not kwargs:
            # TODO: implement bulk PUT
            raise NotImplementedError()

        elif kwargs:

            rv = model.one(*self._uid(**kwargs)) or abort(404)

            for k, v in json.items():
                if hasattr(rv, k):
                    setattr(rv, k, v)
                else:
                    model.rollback()
                    abort(400)

            rv.save(commit=False)

        else:
            abort(400)

        # commit changes

        try:
            model.commit()
        except ModelConflictException:
            model.rollback()
            abort(409)
        else:
            return rv
コード例 #4
0
ファイル: crud.py プロジェクト: project-hypr/hypr
    def post(self, _data=None):
        """Create a new instance of the model.

        post() acts as the CREATE of the CRUD functions. It creates a new
        instance of the model from the JSON payload of the request. It supports
        bulk insert along with single instance creation and the data can be
        validated with the help of jsonschema.

        Args:
            _data: override the payload of the request.

        Docs:
            Create a new instance of ``{model}``.

            Request
            -------

            Headers
            =======
            The request should have the Content-Type application/json.

            Body
            ====
            The data should comply with the following schema::

                {model.schema}

            Responses
            ---------

                **201 Created**
        """
        model = self.__model__

        if _data is None:
            _data = yield from request.json()
        if _data is None:
            abort(400)

        bulk = request.args.get('_bulk', '').lower() in ['true', 'on', '1']
        if bulk:
            # TODO: implement bulk POST
            raise NotImplementedError()
        else:
            model.__validate__(_data) or abort(400)
            try:
                rv = model(**_data)
            except TypeError:
                abort(400)
            else:
                rv.save(commit=False)

        # commit changes and rollback on error.
        try:
            model.commit()
        except ModelConflictException as e:
            model.rollback()
            request.app.logger.debug(e.msg)
            abort(409)
        else:
            return rv, 201