You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.3 KiB

from couchpotato.core.helpers.encoding import toUnicode
14 years ago
from couchpotato.core.helpers.variable import natcmp
14 years ago
from flask.globals import current_app
13 years ago
from flask.helpers import json, make_response
from libs.werkzeug.urls import url_decode
from urllib import unquote
14 years ago
import flask
import re
14 years ago
def getParams():
params = url_decode(getattr(flask.request, 'environ').get('QUERY_STRING', ''))
reg = re.compile('^[a-z0-9_\.]+$')
current = temp = {}
for param, value in sorted(params.iteritems()):
nest = re.split("([\[\]]+)", param)
if len(nest) > 1:
nested = []
for key in nest:
if reg.match(key):
nested.append(key)
current = temp
for item in nested:
if item is nested[-1]:
current[item] = toUnicode(unquote(value))
else:
try:
current[item]
except:
current[item] = {}
current = current[item]
else:
temp[param] = toUnicode(unquote(value))
return dictToList(temp)
def dictToList(params):
if type(params) is dict:
new = {}
for x, value in params.iteritems():
try:
14 years ago
new_value = [dictToList(value[k]) for k in sorted(value.iterkeys(), cmp = natcmp)]
except:
new_value = value
new[x] = new_value
else:
new = params
return new
14 years ago
def getParam(attr, default = None):
try:
return toUnicode(unquote(getattr(flask.request, 'args').get(attr, default)))
except:
return default
14 years ago
def padded_jsonify(callback, *args, **kwargs):
content = str(callback) + '(' + json.dumps(dict(*args, **kwargs)) + ')'
return getattr(current_app, 'response_class')(content, mimetype = 'text/javascript')
def jsonify(mimetype, *args, **kwargs):
content = json.dumps(dict(*args, **kwargs))
return getattr(current_app, 'response_class')(content, mimetype = mimetype)
14 years ago
def jsonified(*args, **kwargs):
13 years ago
callback = getParam('callback_func', None)
14 years ago
if callback:
13 years ago
content = padded_jsonify(callback, *args, **kwargs)
14 years ago
else:
13 years ago
content = jsonify('application/json', *args, **kwargs)
response = make_response(content)
response.cache_control.no_cache = True
return response