From e794ee5a82bba287b8691c82c66aac57d5c35c7d Mon Sep 17 00:00:00 2001 From: Samuel Spencer Date: Mon, 20 Jul 2015 21:18:44 +0000 Subject: [PATCH] fixes failed travis tests includes django1.6 way of doing jsonresponse --- notifications/tests/tests.py | 16 ++++++++-------- notifications/views.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index 2ae3ca3..a090d21 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -218,20 +218,20 @@ class NotificationTestPages(TestCase): self.login('to', 'pwd') response = self.client.get(reverse('notifications:live_unread_notification_count')) - data = json.loads(response.content) + data = json.loads(str(response.content)) self.assertEqual(data.keys(),['unread_count']) self.assertEqual(data['unread_count'],10) Notification.objects.filter(recipient=self.to_user).mark_all_as_read() response = self.client.get(reverse('notifications:live_unread_notification_count')) - data = json.loads(response.content) + data = json.loads(str(response.content)) self.assertEqual(data.keys(),['unread_count']) self.assertEqual(data['unread_count'],0) notify.send(self.from_user, recipient=self.to_user, verb='commented', action_object=self.from_user) response = self.client.get(reverse('notifications:live_unread_notification_count')) - data = json.loads(response.content) + data = json.loads(str(response.content)) self.assertEqual(data.keys(),['unread_count']) self.assertEqual(data['unread_count'],1) @@ -239,34 +239,34 @@ class NotificationTestPages(TestCase): self.login('to', 'pwd') response = self.client.get(reverse('notifications:live_unread_notification_list')) - data = json.loads(response.content) + data = json.loads(str(response.content)) self.assertEqual(data.keys(),['unread_count','unread_list']) self.assertEqual(data['unread_count'],10) self.assertEqual(len(data['unread_list']),5) response = self.client.get(reverse('notifications:live_unread_notification_list')+"?max=12") - data = json.loads(response.content) + data = json.loads(str(response.content)) self.assertEqual(data.keys(),['unread_count','unread_list']) self.assertEqual(data['unread_count'],10) self.assertEqual(len(data['unread_list']),10) # Test with a bad 'max' value response = self.client.get(reverse('notifications:live_unread_notification_list')+"?max=this_is_wrong") - data = json.loads(response.content) + data = json.loads(str(response.content)) self.assertEqual(data.keys(),['unread_count','unread_list']) self.assertEqual(data['unread_count'],10) self.assertEqual(len(data['unread_list']),5) Notification.objects.filter(recipient=self.to_user).mark_all_as_read() response = self.client.get(reverse('notifications:live_unread_notification_list')) - data = json.loads(response.content) + data = json.loads(str(response.content)) self.assertEqual(data.keys(),['unread_count','unread_list']) self.assertEqual(data['unread_count'],0) self.assertEqual(len(data['unread_list']),0) notify.send(self.from_user, recipient=self.to_user, verb='commented', action_object=self.from_user) response = self.client.get(reverse('notifications:live_unread_notification_list')) - data = json.loads(response.content) + data = json.loads(str(response.content)) self.assertEqual(data.keys(),['unread_count','unread_list']) self.assertEqual(data['unread_count'],1) self.assertEqual(len(data['unread_list']),1) diff --git a/notifications/views.py b/notifications/views.py index 580de05..aa93d7d 100644 --- a/notifications/views.py +++ b/notifications/views.py @@ -3,13 +3,22 @@ from django.conf import settings from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.forms import model_to_dict -from django.http import JsonResponse from django.shortcuts import get_object_or_404, render, redirect from django.template.context import RequestContext from .utils import slug2id from .models import Notification +from django import get_version +from distutils.version import StrictVersion +if StrictVersion(get_version()) >= StrictVersion('1.7.0'): + from django.http import JsonResponse +else: + # Django 1.6 doesn't have a proper JsonResponse + import json + def JsonResponse(data): + return HttpResponse(json.dumps(data), content_type="application/json") + @login_required def all(request): """