Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bookmark app lint failures #3969

Merged
merged 1 commit into from
Apr 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 14 additions & 42 deletions readthedocs/bookmarks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)

Expand All @@ -52,27 +50,17 @@ 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,
version__slug=version,
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):
Expand All @@ -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):
"""
Expand All @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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})