Use Djangos JsonResponse class when returning JSON from view

pull/1634/head
Karl Hobley 2015-08-21 14:59:16 +01:00
rodzic 6d802303de
commit ae3c08ad69
4 zmienionych plików z 18 dodań i 34 usunięć

Wyświetl plik

@ -1,8 +1,6 @@
import json
from taggit.models import Tag
from django.http import HttpResponse
from django.http import JsonResponse
def autocomplete(request):
@ -12,6 +10,4 @@ def autocomplete(request):
else:
tags = Tag.objects.none()
response = json.dumps([tag.name for tag in tags])
return HttpResponse(response, content_type='text/javascript')
return JsonResponse([tag.name for tag in tags])

Wyświetl plik

@ -1,5 +1,4 @@
import os
import json
from django.shortcuts import render, redirect, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
@ -7,7 +6,7 @@ from django.core.exceptions import PermissionDenied
from django.utils.translation import ugettext as _
from django.views.decorators.vary import vary_on_headers
from django.core.urlresolvers import reverse, NoReverseMatch
from django.http import HttpResponse
from django.http import HttpResponse, JsonResponse
from wagtail.wagtailcore.models import Site
from wagtail.wagtailadmin.forms import SearchForm
@ -162,23 +161,19 @@ def url_generator(request, image_id):
})
def json_response(document, status=200):
return HttpResponse(json.dumps(document), content_type='application/json', status=status)
def generate_url(request, image_id, filter_spec):
# Get the image
Image = get_image_model()
try:
image = Image.objects.get(id=image_id)
except Image.DoesNotExist:
return json_response({
return JsonResponse({
'error': "Cannot find image."
}, status=404)
# Check if this user has edit permission on this image
if not image.is_editable_by_user(request.user):
return json_response({
return JsonResponse({
'error': "You do not have permission to generate a URL for this image."
}, status=403)
@ -186,7 +181,7 @@ def generate_url(request, image_id, filter_spec):
try:
Filter(spec=filter_spec).operations
except InvalidFilterSpecError:
return json_response({
return JsonResponse({
'error': "Invalid filter spec."
}, status=400)
@ -203,7 +198,7 @@ def generate_url(request, image_id, filter_spec):
# Generate preview url
preview_url = reverse('wagtailimages:preview', args=(image_id, filter_spec))
return json_response({'url': site_root_url + url, 'preview_url': preview_url}, status=200)
return JsonResponse({'url': site_root_url + url, 'preview_url': preview_url}, status=200)
def preview(request, image_id, filter_spec):

Wyświetl plik

@ -1,10 +1,8 @@
import json
from django.shortcuts import render, get_object_or_404
from django.views.decorators.http import require_POST
from django.core.exceptions import PermissionDenied
from django.views.decorators.vary import vary_on_headers
from django.http import HttpResponse, HttpResponseBadRequest
from django.http import JsonResponse, HttpResponseBadRequest
from django.utils.encoding import force_text
from wagtail.wagtailadmin.utils import permission_required
@ -17,10 +15,6 @@ from wagtail.wagtailimages.fields import ALLOWED_EXTENSIONS
from wagtail.utils.compat import render_to_string
def json_response(document):
return HttpResponse(json.dumps(document), content_type='application/json')
def get_image_edit_form(ImageModel):
ImageForm = get_image_form(ImageModel)
@ -67,7 +61,7 @@ def add(request):
image.save()
# Success! Send back an edit form for this image to the user
return json_response({
return JsonResponse({
'success': True,
'image_id': int(image.id),
'form': render_to_string('wagtailimages/multiple/edit_form.html', {
@ -77,7 +71,7 @@ def add(request):
})
else:
# Validation error
return json_response({
return JsonResponse({
'success': False,
# https://github.com/django/django/blob/stable/1.6.x/django/forms/util.py#L45
@ -117,12 +111,12 @@ def edit(request, image_id, callback=None):
for backend in get_search_backends():
backend.add(image)
return json_response({
return JsonResponse({
'success': True,
'image_id': int(image_id),
})
else:
return json_response({
return JsonResponse({
'success': False,
'image_id': int(image_id),
'form': render_to_string('wagtailimages/multiple/edit_form.html', {
@ -144,7 +138,7 @@ def delete(request, image_id):
image.delete()
return json_response({
return JsonResponse({
'success': True,
'image_id': int(image_id),
})

Wyświetl plik

@ -1,8 +1,6 @@
import json
from django.conf import settings
from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from wagtail.wagtailcore import models
@ -66,7 +64,8 @@ def search(
query = None
search_results = None
if use_json: # Return a json response
if use_json:
# Return a json response
if search_results:
search_results_json = []
for result in search_results:
@ -78,9 +77,9 @@ def search(
if hasattr(result_specific, attr)
))
return HttpResponse(json.dumps(search_results_json))
return JsonResponse(search_results_json)
else:
return HttpResponse('[]')
return JsonResponse([], safe=False)
else: # Render a template
if request.is_ajax() and template_ajax:
template = template_ajax