From f9d3e99dbf49517ee903a9d110871626427c6a7a Mon Sep 17 00:00:00 2001 From: David Fischer Date: Wed, 18 Apr 2018 10:25:01 -0700 Subject: [PATCH] Fix bookmark app lint failures --- readthedocs/bookmarks/views.py | 56 +++++++++------------------------- 1 file changed, 14 insertions(+), 42 deletions(-) diff --git a/readthedocs/bookmarks/views.py b/readthedocs/bookmarks/views.py index 9a790c3ffbe..b1861d44e86 100644 --- a/readthedocs/bookmarks/views.py +++ b/readthedocs/bookmarks/views.py @@ -9,8 +9,7 @@ from django.contrib.auth.decorators import login_required from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse -from django.http import ( - HttpResponse, HttpResponseBadRequest, HttpResponseRedirect) +from django.http import HttpResponseRedirect, JsonResponse from django.shortcuts import get_object_or_404, render from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt @@ -32,9 +31,8 @@ def dispatch(self, request, *args, **kwargs): self).dispatch(request, *args, **kwargs) def get(self, request): - return HttpResponse( - content=json.dumps({'error': 'You must POST!'}), - content_type='application/json', + return JsonResponse( + {'error': 'You must POST!'}, status=405, ) @@ -52,9 +50,7 @@ def post(self, request, *args, **kwargs): version = post_json['version'] page = post_json['page'] except KeyError: - return HttpResponseBadRequest( - content=json.dumps({'error': 'Invalid parameters'}), - ) + return JsonResponse({'error': 'Invalid parameters'}, status=400) try: Bookmark.objects.get( project__slug=project, @@ -62,17 +58,9 @@ def post(self, request, *args, **kwargs): page=page, ) except ObjectDoesNotExist: - return HttpResponse( - content=json.dumps({'exists': False}), - status=404, - content_type='application/json', - ) + return JsonResponse({'exists': False}, status=404) - return HttpResponse( - content=json.dumps({'exists': True}), - status=200, - content_type='application/json', - ) + return JsonResponse({'exists': True}) class BookmarkListView(ListView): @@ -99,11 +87,7 @@ def dispatch(self, request, *args, **kwargs): return super(BookmarkAddView, self).dispatch(request, *args, **kwargs) def get(self, request): - return HttpResponse( - content=json.dumps({'error': 'You must POST!'}), - content_type='application/json', - status=405, - ) + return JsonResponse({'error': 'You must POST!'}, status=405) def post(self, request, *args, **kwargs): """ @@ -118,17 +102,15 @@ def post(self, request, *args, **kwargs): page_slug = post_json['page'] url = post_json['url'] except KeyError: - return HttpResponseBadRequest( - content=json.dumps({'error': 'Invalid parameters'}), - ) + return JsonResponse({'error': 'Invalid parameters'}, status=400) try: project = Project.objects.get(slug=project_slug) version = project.versions.get(slug=version_slug) except ObjectDoesNotExist: - return HttpResponseBadRequest( - content=json.dumps( - {'error': 'Project or Version does not exist'}), + return JsonResponse( + {'error': 'Project or Version does not exist'}, + status=400, ) Bookmark.objects.get_or_create( @@ -138,11 +120,7 @@ def post(self, request, *args, **kwargs): version=version, page=page_slug, ) - return HttpResponse( - json.dumps({'added': True}), - status=201, - content_type='application/json', - ) + return JsonResponse({'added': True}, status=201) class BookmarkRemoveView(View): @@ -179,9 +157,7 @@ def post(self, request, *args, **kwargs): url = post_json['url'] page = post_json['page'] except KeyError: - return HttpResponseBadRequest( - json.dumps({'error': 'Invalid parameters'}), - ) + return JsonResponse({'error': 'Invalid parameters'}, status=400) bookmark = get_object_or_404( Bookmark, @@ -193,8 +169,4 @@ def post(self, request, *args, **kwargs): ) bookmark.delete() - return HttpResponse( - json.dumps({'removed': True}), - status=200, - content_type='application/json', - ) + return JsonResponse({'removed': True})