{ "info": { "author": "UNKNOWN", "author_email": "UNKNOWN", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Environment :: Web Environment", "Framework :: Django", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Utilities" ], "description": ".. image:: http://www.mylittledjango.com/media/pony_parts/pony.png\r\n :alt: AJAX requests for Ponies\u2122\r\n :align: right\r\n\r\nHow many times you found yourself writing some views in Django to handle an AJAX request? And how many times you just copy-pasted a view written for synchronous requests and edited them to return a JSON object? And so, how many times you forgot that that ``@login_required`` will actually redirect the request to the login page in case of anonymous users? If this happened to you as many times as it happened to us, you may be start considering using django-ajaxutils.\r\n\r\nDjango-ajaxutils allows you to define a view as an AJAX view that will return a JSON object and that will handle correctly errors such as user not authenticated and invalid requests. Everything through a simple decorator!\r\n\r\n.. image:: https://secure.travis-ci.org/ahref/django-ajaxutils.png?branch=master\r\n\r\nInstallation\r\n============\r\n\r\nJust go for::\r\n\r\n pip install django-ajaxutils\r\n\r\nto do the magic.\r\n\r\nUsage\r\n=====\r\n\r\nIf you want to define a view for handling an AJAX request, you have just to decorate it with ``@ajax`` and return a dictionary representing the object to be returned as JSON. If you lack of imagination, check this out::\r\n\r\n from ajaxutils.decorators import ajax\r\n\r\n\r\n @ajax()\r\n def check_for_some_task(request):\r\n exit_status = get_status_of_some_task()\r\n if exit_status is None:\r\n return {\r\n 'status': 'pending'\r\n }\r\n\r\n return {\r\n 'status': 'completed',\r\n 'exit_status': exit_status\r\n }\r\n\r\n\r\nRequiring authentication\r\n------------------------\r\n\r\nIf your view requires the user to be authenticated, just write it::\r\n\r\n @ajax(login_required=True)\r\n def some_very_private_view(request):\r\n data = perform_something_private()\r\n return {\r\n 'data': data\r\n }\r\n\r\n\r\nIn case of an unauthenticated request, a ``401: Unauthorized`` response containing the following JSON object will be returned::\r\n\r\n {\r\n 'status': 'error',\r\n 'error': 'Unauthorized',\r\n }\r\n\r\n\r\nRequiring GET / POST\r\n--------------------\r\n\r\n``@ajax`` also allows you a quick way to require a particular method for requesting the view. For example, if your view will edit some server-side data, you may accept only POST requests. With ``@ajax`` this is as easy as remembering the first two decimal digits of PI (which are ``1`` and ``4``, btw)::\r\n\r\n @ajax(login_required=True, require_POST=True)\r\n def submit_my_data(request):\r\n new_obj = save_my_data()\r\n return {\r\n 'id': new_obj.pk\r\n }\r\n\r\nThis will return a ``405: Method not allowed`` response with the following JSON object in case of illegal requests::\r\n\r\n {\r\n 'status': 'error',\r\n 'error': 'Method not allowed',\r\n }\r\n\r\nYou can of course set ``require_GET=True`` for GET requests.\r\n\r\nYou can also use this alternative syntax::\r\n\r\n @ajax(require=\"GET\")\r\n def my_cool_view(request):\r\n return {\r\n 'hello': 'world!'\r\n }\r\n\r\n\r\nCustom Status Codes\r\n-------------------\r\nWhat if you don't want to return an HTTP 200? Do you want to return a 404? Write::\r\n\r\n from django.http import Http404\r\n\r\n @ajax()\r\n def my_cool_view(request):\r\n raise Http404\r\n\r\nThis returns::\r\n\r\n {\r\n 'status': 'error',\r\n 'error': 'Not found',\r\n }\r\n\r\nOr maybe a 400 - Bad Request::\r\n\r\n from django.http import HttpResponseBadRequest\r\n\r\n @ajax()\r\n def my_cool_view(request):\r\n return HttpResponseBadRequest('My error message')\r\n\r\nThis returns::\r\n\r\n {\r\n 'status': 'error',\r\n 'error': 'My error message',\r\n }\r\n\r\nand the HTTP response has status code 400.\r\n\r\nAnother syntax, more Flask-ish::\r\n\r\n @ajax()\r\n def my_cool_view(request):\r\n return {\r\n \"i'm a\": 'teapot'\r\n }, 418\r\n\r\n\r\nFrom infinity import json\r\n-------------------------\r\n\r\nTired of writing infinite import statements to choose the best json module? Let ajaxutils do it for you::\r\n\r\n from ajaxutils import json\r\n\r\nAt the moment, ajaxutils prefers simplejson over the stdlib json. No other json module is used. In the future we will probably provide support to ujson using a Django setting.\r\n\r\nChangelog\r\n=========\r\n\r\nv0.2\r\n----\r\n\r\n* Moved JsonResponse to ajaxutils.http\r\n* Added Custom Status Codes handling\r\n* Added documentation for @ajax(require=METHOD)\r\n* Added \"from ajaxutils import json\"", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/ahref/django-ajaxutils", "keywords": "", "license": "BSD", "maintainer": "", "maintainer_email": "", "name": "django-ajaxutils", "package_url": "https://pypi.org/project/django-ajaxutils/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/django-ajaxutils/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://github.com/ahref/django-ajaxutils" }, "release_url": "https://pypi.org/project/django-ajaxutils/0.2/", "requires_dist": null, "requires_python": null, "summary": "Ajax requests for Ponies", "version": "0.2" }, "last_serial": 789077, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "4332553069f80ddf5c4e88a08b0f6b72", "sha256": "ecf89f443d66feda6cac00909cc86decc1cadb85a95ac0268fda8c44dda1c976" }, "downloads": -1, "filename": "django-ajaxutils-0.1.0.tar.gz", "has_sig": false, "md5_digest": "4332553069f80ddf5c4e88a08b0f6b72", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 1759, "upload_time": "2012-02-03T16:11:55", "url": "https://files.pythonhosted.org/packages/8c/16/fc854f579482731911abd1220f59fc0879315e6b0fbd3184cb147cb496eb/django-ajaxutils-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "23bfb47860d1e9e83d4a5b19b2342d5e", "sha256": "0ab4918c8e28672a7c5c9d89f411e7d4ccb598d49b4d7572538bc9c41709fe1c" }, "downloads": -1, "filename": "django-ajaxutils-0.1.1.tar.gz", "has_sig": false, "md5_digest": "23bfb47860d1e9e83d4a5b19b2342d5e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3177, "upload_time": "2012-02-03T16:17:41", "url": "https://files.pythonhosted.org/packages/2e/bb/f53ca7f015dc6c5f6d9cc000debf1028c765baac88b2ade02bafdd461d06/django-ajaxutils-0.1.1.tar.gz" } ], "0.1.2": [ { "comment_text": "", "digests": { "md5": "565eb0d6a9e08f0fff0ee1d2bdbf02d4", "sha256": "8e3fd977a2462961c7055e6c3feab08e5706a746a54c1dd0caa81c37bbeba547" }, "downloads": -1, "filename": "django-ajaxutils-0.1.2.tar.gz", "has_sig": false, "md5_digest": "565eb0d6a9e08f0fff0ee1d2bdbf02d4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3233, "upload_time": "2012-02-24T16:53:31", "url": "https://files.pythonhosted.org/packages/02/91/f0d44dd07e82ddf819f8757482ae895b49d546923e8e1861509933d7ffff/django-ajaxutils-0.1.2.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "039f0072d7939c65eef4649c7dadebba", "sha256": "0a9a7ced6342c09eb4a339af6734e172fa0ba994b60a8ce53ef99e16e1b52df5" }, "downloads": -1, "filename": "django-ajaxutils-0.2.tar.gz", "has_sig": false, "md5_digest": "039f0072d7939c65eef4649c7dadebba", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4451, "upload_time": "2012-08-21T06:19:05", "url": "https://files.pythonhosted.org/packages/8b/57/afc9f562dff5ee0c5aa6120d79575c624adb3daf606b2337c7090223a834/django-ajaxutils-0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "039f0072d7939c65eef4649c7dadebba", "sha256": "0a9a7ced6342c09eb4a339af6734e172fa0ba994b60a8ce53ef99e16e1b52df5" }, "downloads": -1, "filename": "django-ajaxutils-0.2.tar.gz", "has_sig": false, "md5_digest": "039f0072d7939c65eef4649c7dadebba", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4451, "upload_time": "2012-08-21T06:19:05", "url": "https://files.pythonhosted.org/packages/8b/57/afc9f562dff5ee0c5aa6120d79575c624adb3daf606b2337c7090223a834/django-ajaxutils-0.2.tar.gz" } ] }