Moves json_response into tools/json.py
This commit is contained in:
parent
53d78991d7
commit
04e08d422a
@ -51,30 +51,6 @@ class Auth(object):
|
|||||||
def __call__(self, request, *args, **kw):
|
def __call__(self, request, *args, **kw):
|
||||||
raise NotImplemented()
|
raise NotImplemented()
|
||||||
|
|
||||||
|
|
||||||
def json_response(serializable, _disable_cors=False, *args, **kw):
|
|
||||||
'''
|
|
||||||
Serializes a json objects and returns a werkzeug Response object with the
|
|
||||||
serialized value as the response body and Content-Type: application/json.
|
|
||||||
|
|
||||||
:param serializable: A json-serializable object
|
|
||||||
|
|
||||||
Any extra arguments and keyword arguments are passed to the
|
|
||||||
Response.__init__ method.
|
|
||||||
'''
|
|
||||||
response = Response(json.dumps(serializable), *args, content_type='application/json', **kw)
|
|
||||||
|
|
||||||
if not _disable_cors:
|
|
||||||
cors_headers = {
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
|
|
||||||
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With'}
|
|
||||||
for key, value in cors_headers.iteritems():
|
|
||||||
response.headers.set(key, value)
|
|
||||||
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
def get_entry_serializable(entry, urlgen):
|
def get_entry_serializable(entry, urlgen):
|
||||||
'''
|
'''
|
||||||
Returns a serializable dict() of a MediaEntry instance.
|
Returns a serializable dict() of a MediaEntry instance.
|
||||||
|
@ -21,11 +21,11 @@ from os.path import splitext
|
|||||||
from werkzeug.exceptions import BadRequest, Forbidden
|
from werkzeug.exceptions import BadRequest, Forbidden
|
||||||
from werkzeug.wrappers import Response
|
from werkzeug.wrappers import Response
|
||||||
|
|
||||||
|
from mediagoblin.tools.json import json_response
|
||||||
from mediagoblin.decorators import require_active_login
|
from mediagoblin.decorators import require_active_login
|
||||||
from mediagoblin.meddleware.csrf import csrf_exempt
|
from mediagoblin.meddleware.csrf import csrf_exempt
|
||||||
from mediagoblin.media_types import sniff_media
|
from mediagoblin.media_types import sniff_media
|
||||||
from mediagoblin.plugins.api.tools import api_auth, get_entry_serializable, \
|
from mediagoblin.plugins.api.tools import api_auth, get_entry_serializable
|
||||||
json_response
|
|
||||||
from mediagoblin.submit.lib import check_file_field, prepare_queue_task, \
|
from mediagoblin.submit.lib import check_file_field, prepare_queue_task, \
|
||||||
run_process_media, new_upload_entry
|
run_process_media, new_upload_entry
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
from mediagoblin.plugins.api.tools import json_response
|
from mediagoblin.tools.json import json_response
|
||||||
|
|
||||||
|
|
||||||
def require_client_auth(controller):
|
def require_client_auth(controller):
|
||||||
|
@ -22,6 +22,7 @@ from urllib import urlencode
|
|||||||
from werkzeug.exceptions import BadRequest
|
from werkzeug.exceptions import BadRequest
|
||||||
|
|
||||||
from mediagoblin.tools.response import render_to_response, redirect
|
from mediagoblin.tools.response import render_to_response, redirect
|
||||||
|
from mediagoblin.tools.json import json_response
|
||||||
from mediagoblin.decorators import require_active_login
|
from mediagoblin.decorators import require_active_login
|
||||||
from mediagoblin.messages import add_message, SUCCESS
|
from mediagoblin.messages import add_message, SUCCESS
|
||||||
from mediagoblin.tools.translate import pass_to_ugettext as _
|
from mediagoblin.tools.translate import pass_to_ugettext as _
|
||||||
@ -31,7 +32,6 @@ from mediagoblin.plugins.oauth.forms import ClientRegistrationForm, \
|
|||||||
AuthorizationForm
|
AuthorizationForm
|
||||||
from mediagoblin.plugins.oauth.tools import require_client_auth, \
|
from mediagoblin.plugins.oauth.tools import require_client_auth, \
|
||||||
create_token
|
create_token
|
||||||
from mediagoblin.plugins.api.tools import json_response
|
|
||||||
|
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
41
mediagoblin/tools/json.py
Normal file
41
mediagoblin/tools/json.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# GNU MediaGoblin -- federated, autonomous media hosting
|
||||||
|
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
from werkzeug.wrappers import Response
|
||||||
|
|
||||||
|
def json_response(serializable, _disable_cors=False, *args, **kw):
|
||||||
|
'''
|
||||||
|
Serializes a json objects and returns a werkzeug Response object with the
|
||||||
|
serialized value as the response body and Content-Type: application/json.
|
||||||
|
|
||||||
|
:param serializable: A json-serializable object
|
||||||
|
|
||||||
|
Any extra arguments and keyword arguments are passed to the
|
||||||
|
Response.__init__ method.
|
||||||
|
'''
|
||||||
|
response = Response(json.dumps(serializable), *args, content_type='application/json', **kw)
|
||||||
|
|
||||||
|
if not _disable_cors:
|
||||||
|
cors_headers = {
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
|
||||||
|
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With'}
|
||||||
|
for key, value in cors_headers.iteritems():
|
||||||
|
response.headers.set(key, value)
|
||||||
|
|
||||||
|
return response
|
Loading…
x
Reference in New Issue
Block a user