forked from orbit-oss/flask
Added JSON Support and started working on jQuery docs
This commit is contained in:
parent
c64a4e0bef
commit
6e2be6a0b3
7 changed files with 254 additions and 9 deletions
58
flask.py
58
flask.py
|
|
@ -15,11 +15,23 @@ import sys
|
|||
|
||||
from jinja2 import Environment, PackageLoader, FileSystemLoader
|
||||
from werkzeug import Request as RequestBase, Response as ResponseBase, \
|
||||
LocalStack, LocalProxy, create_environ, SharedDataMiddleware
|
||||
LocalStack, LocalProxy, create_environ, SharedDataMiddleware, \
|
||||
cached_property
|
||||
from werkzeug.routing import Map, Rule
|
||||
from werkzeug.exceptions import HTTPException
|
||||
from werkzeug.contrib.securecookie import SecureCookie
|
||||
|
||||
# try to load the best simplejson implementation available. If JSON
|
||||
# is not installed, we add a failing class.
|
||||
json_available = True
|
||||
try:
|
||||
import simplejson as json
|
||||
except ImportError:
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
json_available = False
|
||||
|
||||
# utilities we import from Werkzeug and Jinja2 that are unused
|
||||
# in the module but are exported as public interface.
|
||||
from werkzeug import abort, redirect
|
||||
|
|
@ -49,6 +61,16 @@ class Request(RequestBase):
|
|||
self.endpoint = None
|
||||
self.view_args = None
|
||||
|
||||
@cached_property
|
||||
def json(self):
|
||||
"""If the mimetype is `application/json` this will contain the
|
||||
parsed JSON data.
|
||||
"""
|
||||
if not json_available:
|
||||
raise AttributeError('simplejson not available')
|
||||
if self.mimetype == 'application/json':
|
||||
return json.loads(self.data)
|
||||
|
||||
|
||||
class Response(ResponseBase):
|
||||
"""The response object that is used by default in flask. Works like the
|
||||
|
|
@ -81,7 +103,6 @@ class _NullSession(SecureCookie):
|
|||
del _fail
|
||||
|
||||
|
||||
|
||||
class _RequestContext(object):
|
||||
"""The request context contains all request relevant information. It is
|
||||
created at the beginning of the request and pushed to the
|
||||
|
|
@ -133,6 +154,8 @@ def get_template_attribute(template_name, attribute):
|
|||
hello = get_template_attribute('_foo.html', 'hello')
|
||||
return hello('World')
|
||||
|
||||
.. versionadded:: 0.2
|
||||
|
||||
:param template_name: the name of the template
|
||||
:param attribute: the name of the variable of macro to acccess
|
||||
"""
|
||||
|
|
@ -162,6 +185,35 @@ def get_flashed_messages():
|
|||
return flashes
|
||||
|
||||
|
||||
def jsonify(*args, **kwargs):
|
||||
"""Creates a :class:`~flask.Response` with the JSON representation of
|
||||
the given arguments with an `application/json` mimetype. The arguments
|
||||
to this function are the same as to the :class:`dict` constructor.
|
||||
|
||||
Example usage::
|
||||
|
||||
@app.route('/_get_current_user')
|
||||
def get_current_user():
|
||||
return jsonify(username=g.user.username,
|
||||
email=g.user.email,
|
||||
id=g.user.id)
|
||||
|
||||
This will send a JSON response like this to the browser::
|
||||
|
||||
{
|
||||
"username": "admin",
|
||||
"email": "admin@localhost",
|
||||
"id": 42
|
||||
}
|
||||
|
||||
This requires Python 2.6 or an installed version of simplejson.
|
||||
|
||||
.. versionadded:: 0.2
|
||||
"""
|
||||
return current_app.response_class(json.dumps(dict(*args, **kwargs),
|
||||
indent=None if request.is_xhr else 2), mimetype='application/json')
|
||||
|
||||
|
||||
def render_template(template_name, **context):
|
||||
"""Renders a template from the template folder with the given
|
||||
context.
|
||||
|
|
@ -326,6 +378,8 @@ class Flask(object):
|
|||
url_for=url_for,
|
||||
get_flashed_messages=get_flashed_messages
|
||||
)
|
||||
if json_available:
|
||||
self.jinja_env.filters['tojson'] = json.dumps
|
||||
|
||||
def create_jinja_loader(self):
|
||||
"""Creates the Jinja loader. By default just a package loader for
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue