{ "info": { "author": "Henri Hulski", "author_email": "henri.hulski@gazeta.pl", "bugtrack_url": null, "classifiers": [ "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5" ], "description": "more.body_model: ``load_json`` infrastructure for Morepath\n==========================================================\n\nThe idea is to recognize on an application-level what kind of JSON content\nis being posted, and convert it into a Python object. You can use this\napplication object with ``request.body_obj``. With ``body_model`` you can\nthen write views that specifically match that kind model.\n\nTo use it you have to subclass your application from\n``more.body_model.BodyModelApp``:\n\n.. code-block:: python\n\n from more.body_model import BodyModelApp\n\n class App(BodyModelApp):\n pass\n\n.. note:: If you want to use body_model on a mounted App, make sure that both,\nthe base App and the mounted App are a subclass from\n``more.body_model.BodyModelApp``. Otherwise it will not work.\n\n\nload_json\n---------\n\nThe ``App.load_json`` directive lets you define a function that turns\nincoming JSON into a Python object. This behavior is shared by all views in the\napplication. We detect JSON with the type field ``Item`` and interpret it as an\n``Item`` instance, and pass through everything else:\n\n.. code-block:: python\n\n @App.load_json()\n def load_json(json, request):\n if json.get('type') != 'Item':\n return json\n return Item(json['x'])\n\nWhen you write a ``json`` view you automatically get the ``Item``\ninstance as the ``body_obj`` attribute of the ``request``:\n\n.. code-block:: python\n\n @App.json(model=Collection, request_method='POST')\n def collection_post(self, request):\n collection.add(request.body_obj)\n return \"success!\"\n\nYou can write views that match on the class of ``body_obj`` by specifying\n``body_model``:\n\n.. code-block:: python\n\n @App.json(model=Collection, request_method='POST', body_model=Item)\n def collection_post_item(self, request):\n collection.add(request.body_obj)\n return \"success!\"\n\n\nbody_model\n----------\n\nTo define JSON body conversion code generally for an application we can use\n``App.load_json``:\n\n.. code-block:: python\n\n @App.load_json()\n def load_json(json, request):\n if is_valid_document_json(json):\n return Document(title=json['title'],\n author=json['author'],\n content=json['content'])\n # fallback, just return plain JSON\n return json\n\nNow we get a ``Document`` instance in ``Request.body_obj``, so\nwe can simplify ``document_collection_post``:\n\n.. code-block:: python\n\n @App.json(model=DocumentCollection, request_method='POST')\n def document_collection_post(self, request):\n if not isinstance(request.body_obj, Document):\n raise webob.exc.HTTPUnprocessableEntity()\n result = self.add(request.body_obj)\n return request.view(result)\n\nTo only match if ``body_obj`` is an instance of ``Document`` we can\nuse ``body_model`` on the view instead:\n\n.. code-block:: python\n\n @App.json(model=DocumentCollection, request_method='POST', body_model=Document)\n def document_collection_post(self, request):\n result = self.add(request.body_obj)\n return request.view(result)\n\nNow you get the ``422`` error for free if no matching ``body_model``\ncan be found. You can also create additional ``POST`` views for\n``DocumentCollection`` that handle other types of JSON content this\nway.\n\n\nCHANGES\n=======\n\n0.1 (2017-03-17)\n----------------\n\n* initial public release.", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/morepath/more.body_model", "keywords": "morepath validation", "license": "BSD", "maintainer": "", "maintainer_email": "", "name": "more.body-model", "package_url": "https://pypi.org/project/more.body-model/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/more.body-model/", "project_urls": { "Homepage": "https://github.com/morepath/more.body_model" }, "release_url": "https://pypi.org/project/more.body-model/0.1/", "requires_dist": null, "requires_python": "", "summary": "load_json infrastructure for Morepath", "version": "0.1" }, "last_serial": 2713590, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "f0a5daf1c36e4537d24aca2992c58987", "sha256": "1ef87f3f07fea57060bc8741a9530c9cc572f8530f4fd494671455dd1b53c71d" }, "downloads": -1, "filename": "more.body_model-0.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "f0a5daf1c36e4537d24aca2992c58987", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 8747, "upload_time": "2017-03-17T18:47:35", "url": "https://files.pythonhosted.org/packages/7a/a3/f58feffa2904b34b2ec4b3d75442b1824721b9e543d9398b6057f6dfeb32/more.body_model-0.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "6c15dd38f6f9564e033fe95a6e0fda5c", "sha256": "ddc1938f1f0ae92346902be45e6633e19029c77a4fce7a825ceb806bb47ad477" }, "downloads": -1, "filename": "more.body_model-0.1.tar.gz", "has_sig": false, "md5_digest": "6c15dd38f6f9564e033fe95a6e0fda5c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6440, "upload_time": "2017-03-17T18:47:30", "url": "https://files.pythonhosted.org/packages/cc/1b/ca10b705c8474554c81121019f6d7b665b52aaafd7d3bfae0e0d35091f42/more.body_model-0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "f0a5daf1c36e4537d24aca2992c58987", "sha256": "1ef87f3f07fea57060bc8741a9530c9cc572f8530f4fd494671455dd1b53c71d" }, "downloads": -1, "filename": "more.body_model-0.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "f0a5daf1c36e4537d24aca2992c58987", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 8747, "upload_time": "2017-03-17T18:47:35", "url": "https://files.pythonhosted.org/packages/7a/a3/f58feffa2904b34b2ec4b3d75442b1824721b9e543d9398b6057f6dfeb32/more.body_model-0.1-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "6c15dd38f6f9564e033fe95a6e0fda5c", "sha256": "ddc1938f1f0ae92346902be45e6633e19029c77a4fce7a825ceb806bb47ad477" }, "downloads": -1, "filename": "more.body_model-0.1.tar.gz", "has_sig": false, "md5_digest": "6c15dd38f6f9564e033fe95a6e0fda5c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6440, "upload_time": "2017-03-17T18:47:30", "url": "https://files.pythonhosted.org/packages/cc/1b/ca10b705c8474554c81121019f6d7b665b52aaafd7d3bfae0e0d35091f42/more.body_model-0.1.tar.gz" } ] }