Commit ff275ddb authored by Yohan Boniface's avatar Yohan Boniface

Django 1.7 WIP

parent b2fe42ed
......@@ -3,5 +3,6 @@ from django.contrib.gis.db import models
class PublicManager(models.GeoManager):
def get_query_set(self):
return super(PublicManager, self).get_query_set().filter(share_status=self.model.PUBLIC)
def get_queryset(self):
return super(PublicManager, self).get_queryset().filter(
share_status=self.model.PUBLIC)
from django.utils import simplejson
import json
from django import template
from django.conf import settings
......@@ -54,7 +55,7 @@ def map_fragment(map_instance, **kwargs):
})
map_settings['properties'].update(kwargs)
return {
"map_settings": simplejson.dumps(map_settings),
"map_settings": json.dumps(map_settings),
"map": map_instance
}
......
import json
from django.test import TestCase
from django.contrib.auth import get_user_model
from django.utils import simplejson
from django.core.urlresolvers import reverse
from django.core.files import temp
from django.test.client import RequestFactory
......@@ -115,16 +116,16 @@ class BaseTest(TestCase):
def assertLoginRequired(self, response):
self.assertEqual(response.status_code, 200)
json = simplejson.loads(response.content)
self.assertIn("login_required", json)
j = json.loads(response.content)
self.assertIn("login_required", j)
redirect_url = reverse('login')
self.assertEqual(json['login_required'], redirect_url)
self.assertEqual(j['login_required'], redirect_url)
def assertHasForm(self, response):
self.assertEqual(response.status_code, 200)
json = simplejson.loads(response.content)
self.assertIn("html", json)
self.assertIn("form", json['html'])
j = json.loads(response.content)
self.assertIn("html", j)
self.assertIn("form", j['html'])
def temp_file(self, content):
tdir = temp.gettempdir()
......
# -*- coding: utf-8 -*-
from django.utils import simplejson
from django.core.urlresolvers import reverse
import json
from django.conf import settings
from django.contrib.auth import get_user_model
from django.test.utils import override_settings
from django.core.signing import get_cookie_signer
from django.core.urlresolvers import reverse
from django.test.utils import override_settings
from leaflet_storage.models import Map, DataLayer
......@@ -27,9 +29,9 @@ class MapViews(BaseTest):
self.client.login(username=self.user.username, password="123123")
response = self.client.post(url, post_data)
self.assertEqual(response.status_code, 200)
json = simplejson.loads(response.content)
j = json.loads(response.content)
created_map = Map.objects.latest('pk')
self.assertEqual(json['id'], created_map.pk)
self.assertEqual(j['id'], created_map.pk)
self.assertEqual(created_map.name, name)
def test_update(self):
......@@ -44,10 +46,10 @@ class MapViews(BaseTest):
self.client.login(username=self.user.username, password="123123")
response = self.client.post(url, post_data)
self.assertEqual(response.status_code, 200)
json = simplejson.loads(response.content)
self.assertNotIn("html", json)
j = json.loads(response.content)
self.assertNotIn("html", j)
updated_map = Map.objects.get(pk=self.map.pk)
self.assertEqual(json['id'], updated_map.pk)
self.assertEqual(j['id'], updated_map.pk)
self.assertEqual(updated_map.name, new_name)
def test_delete(self):
......@@ -60,8 +62,8 @@ class MapViews(BaseTest):
# Check that user has not been impacted
self.assertEqual(User.objects.filter(pk=self.user.pk).count(), 1)
# Test response is a json
json = simplejson.loads(response.content)
self.assertIn("redirect", json)
j = json.loads(response.content)
self.assertIn("redirect", j)
def test_wrong_slug_should_redirect_to_canonical(self):
url = reverse('map', kwargs={'pk': self.map.pk, 'slug': 'wrong-slug'})
......@@ -154,9 +156,9 @@ class MapViews(BaseTest):
self.client.login(username=self.user.username, password="123123")
response = self.client.post(url, post_data)
self.assertEqual(response.status_code, 200)
json = simplejson.loads(response.content)
j = json.loads(response.content)
created_map = Map.objects.latest('pk')
self.assertEqual(json['id'], created_map.pk)
self.assertEqual(j['id'], created_map.pk)
self.assertEqual(created_map.name, name)
# Lower case of the russian original name
# self.assertEqual(created_map.slug, u"академический")
......@@ -214,8 +216,8 @@ class MapViews(BaseTest):
def test_map_geojson_view(self):
url = reverse('map_geojson', args=(self.map.pk, ))
response = self.client.get(url)
json = simplejson.loads(response.content)
self.assertIn('type', json)
j = json.loads(response.content)
self.assertIn('type', j)
@override_settings(LEAFLET_STORAGE_ALLOW_ANONYMOUS=True)
......@@ -242,9 +244,9 @@ class AnonymousMapViews(BaseTest):
}
response = self.client.post(url, post_data)
self.assertEqual(response.status_code, 200)
json = simplejson.loads(response.content)
j = json.loads(response.content)
created_map = Map.objects.latest('pk')
self.assertEqual(json['id'], created_map.pk)
self.assertEqual(j['id'], created_map.pk)
self.assertEqual(created_map.name, name)
key, value = created_map.signed_cookie_elements
self.assertIn(key, self.client.cookies)
......@@ -273,9 +275,9 @@ class AnonymousMapViews(BaseTest):
}
response = self.client.post(url, post_data)
self.assertEqual(response.status_code, 200)
json = simplejson.loads(response.content)
j = json.loads(response.content)
updated_map = Map.objects.get(pk=self.anonymous_map.pk)
self.assertEqual(json['id'], updated_map.pk)
self.assertEqual(j['id'], updated_map.pk)
def test_delete(self):
url = reverse('map_delete', args=(self.anonymous_map.pk, ))
......@@ -284,8 +286,8 @@ class AnonymousMapViews(BaseTest):
self.assertEqual(response.status_code, 200)
self.assertEqual(Map.objects.filter(pk=self.anonymous_map.pk).count(), 0)
# Test response is a json
json = simplejson.loads(response.content)
self.assertIn("redirect", json)
j = json.loads(response.content)
self.assertIn("redirect", j)
def test_no_cookie_cant_delete(self):
url = reverse('map_delete', args=(self.anonymous_map.pk, ))
......@@ -327,9 +329,9 @@ class AnonymousMapViews(BaseTest):
}
response = self.client.post(url, post_data)
self.assertEqual(response.status_code, 200)
json = simplejson.loads(response.content)
j = json.loads(response.content)
updated_map = Map.objects.get(pk=self.anonymous_map.pk)
self.assertEqual(json['id'], updated_map.pk)
self.assertEqual(j['id'], updated_map.pk)
self.assertEqual(updated_map.owner.pk, self.user.pk)
def test_clone_map_should_not_be_possible_if_user_is_not_allowed(self):
......@@ -449,14 +451,15 @@ class DataLayerViews(BaseTest):
def test_get(self):
url = reverse('datalayer_view', args=(self.datalayer.pk, ))
response = self.client.get(url)
self.assertIsNotNone(response['ETag'])
if getattr(settings, 'LEAFLET_STORAGE_XSENDFILE_HEADER'):
self.assertIsNotNone(response['ETag'])
self.assertIsNotNone(response['Last-Modified'])
self.assertIsNotNone(response['Cache-Control'])
self.assertNotIn('Content-Encoding', response)
json = simplejson.loads(response.content)
self.assertIn('_storage', json)
self.assertIn('features', json)
self.assertEquals(json['type'], 'FeatureCollection')
j = json.loads(response.content)
self.assertIn('_storage', j)
self.assertIn('features', j)
self.assertEquals(j['type'], 'FeatureCollection')
def test_update(self):
url = reverse('datalayer_update', args=(self.map.pk, self.datalayer.pk))
......@@ -472,9 +475,9 @@ class DataLayerViews(BaseTest):
modified_datalayer = DataLayer.objects.get(pk=self.datalayer.pk)
self.assertEqual(modified_datalayer.name, name)
# Test response is a json
json = simplejson.loads(response.content)
self.assertIn("id", json)
self.assertEqual(self.datalayer.pk, json['id'])
j = json.loads(response.content)
self.assertIn("id", j)
self.assertEqual(self.datalayer.pk, j['id'])
def test_should_not_be_possible_to_update_with_wrong_map_id_in_url(self):
other_map = MapFactory(owner=self.user)
......@@ -500,8 +503,8 @@ class DataLayerViews(BaseTest):
# Check that map has not been impacted
self.assertEqual(Map.objects.filter(pk=self.map.pk).count(), 1)
# Test response is a json
json = simplejson.loads(response.content)
self.assertIn("info", json)
j = json.loads(response.content)
self.assertIn("info", j)
def test_should_not_be_possible_to_delete_with_wrong_map_id_in_url(self):
other_map = MapFactory(owner=self.user)
......@@ -514,7 +517,8 @@ class DataLayerViews(BaseTest):
def test_get_gzipped(self):
url = reverse('datalayer_view', args=(self.datalayer.pk, ))
response = self.client.get(url, HTTP_ACCEPT_ENCODING="gzip")
self.assertIsNotNone(response['ETag'])
if getattr(settings, 'LEAFLET_STORAGE_XSENDFILE_HEADER'):
self.assertIsNotNone(response['ETag'])
self.assertIsNotNone(response['Last-Modified'])
self.assertIsNotNone(response['Cache-Control'])
self.assertIn('Content-Encoding', response)
......
......@@ -2,6 +2,7 @@
import os
import hashlib
import json
from django.conf import settings
from django.contrib import messages
......@@ -10,12 +11,11 @@ from django.contrib.auth import get_user_model
from django.core.signing import Signer, BadSignature
from django.core.urlresolvers import reverse_lazy, reverse
from django.http import (HttpResponse, HttpResponseForbidden,
HttpResponseRedirect, CompatibleStreamingHttpResponse,
HttpResponseRedirect, StreamingHttpResponse,
HttpResponsePermanentRedirect)
from django.shortcuts import get_object_or_404
from django.template import RequestContext
from django.template.loader import render_to_string
from django.utils import simplejson
from django.utils.translation import ugettext as _
from django.views.generic import View
from django.views.generic import DetailView
......@@ -63,14 +63,14 @@ def render_to_json(templates, response_kwargs, context, request):
response_kwargs,
RequestContext(request, context)
)
_json = simplejson.dumps({
_json = json.dumps({
"html": html
})
return HttpResponse(_json)
def simple_json_response(**kwargs):
return HttpResponse(simplejson.dumps(kwargs))
return HttpResponse(json.dumps(kwargs))
# ############## #
......@@ -125,7 +125,7 @@ class MapDetailMixin(object):
if not "properties" in map_settings:
map_settings['properties'] = {}
map_settings['properties'].update(properties)
context['map_settings'] = simplejson.dumps(map_settings, indent=settings.DEBUG)
context['map_settings'] = json.dumps(map_settings, indent=settings.DEBUG)
return context
def get_tilelayers(self):
......@@ -437,17 +437,17 @@ class DataLayerView(GZipMixin, BaseDetailView):
if getattr(settings, 'LEAFLET_STORAGE_XSENDFILE_HEADER', None):
response = HttpResponse()
path = path.replace(settings.MEDIA_ROOT, '/internal')
response[settings.LEAFLET_STORAGE_XSENDFILE_HEADER] = path
else:
# TODO IMS
statobj = os.stat(path)
response = CompatibleStreamingHttpResponse(
response = StreamingHttpResponse(
open(path, 'rb'),
content_type='application/json'
)
response["Last-Modified"] = http_date(statobj.st_mtime)
response['ETag'] = '%s' % hashlib.md5(response.content).hexdigest()
response['Content-Length'] = str(len(response.content))
# response['Content-Length'] = str(len(response.streaming_content))
if path.endswith(self.EXT):
response['Content-Encoding'] = 'gzip'
return response
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment