Browse Source

Add public.json view

Jocelyn Delande 9 years ago
parent
commit
fc01bc0cca
3 changed files with 88 additions and 19 deletions
  1. 35 1
      wifiwithme/apps/contribmap/tests.py
  2. 51 2
      wifiwithme/apps/contribmap/views.py
  3. 2 16
      wifiwithme/core/urls.py

+ 35 - 1
wifiwithme/apps/contribmap/tests.py

@@ -1,8 +1,25 @@
-from django.test import TestCase
+import json
+
+from django.test import TestCase, Client
 
 from contribmap.models import Contrib
 
 
+class APITestClient(Client):
+    def json_get(self, *args, **kwargs):
+        """ Annotate the response with a .data containing parsed JSON
+        """
+        response = super().get(*args, **kwargs)
+        response.data = json.loads(response.content.decode('utf-8'))
+        return response
+
+
+class APITestCase(TestCase):
+    def setUp(self):
+        super().setUp()
+        self.client = APITestClient()
+
+
 class TestContrib(TestCase):
     def test_comma_separatedcharfield(self):
         co = Contrib(name='foo', orientations=['SO', 'NE'])
@@ -42,6 +59,23 @@ class TestContribPrivacy(TestCase):
         self.assertEqual(c.get_public_field('name'), None)
 
 
+class TestViews(APITestCase):
+    def test_public_json(self):
+        response = self.client.json_get('/map/public.json')
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(len(response.data['features']), 0)
+
+        Contrib.objects.create(
+            name='John',
+            phone='010101010101',
+            contrib_type=Contrib.CONTRIB_CONNECT,
+            privacy_coordinates=False,
+        )
+        response = self.client.json_get('/map/public.json')
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(len(response.data['features']), 1)
+
+
 class TestDataImport(TestCase):
     fixtures = ['bottle_data.yaml']
 

+ 51 - 2
wifiwithme/apps/contribmap/views.py

@@ -1,3 +1,52 @@
-from django.shortcuts import render
+from django.http import JsonResponse
 
-# Create your views here.
+from .models import Contrib
+
+
+def public_json(request):
+    contribs = Contrib.objects.all()
+
+    properties = [
+        ('name', 'privacy_name'),
+        ('comment', 'privacy_comment'),
+        ('place_details', 'privacy_place_details'),
+        ('contrib_type', True),
+        (''),
+    ]
+
+    data = []
+    for i in contribs:
+        if not i.is_public():
+            continue
+
+        data.append({
+            "id": i.pk,
+            "type": "Feature",
+            "geometry": {
+                "coordinates": [
+                    i.latitude,
+                    i.longitude,
+                ],
+                "type": "Point",
+            },
+            "properties": {
+                "name": i.get_public_field('name'),
+                "place": {
+                    "floor": i.get_public_field('floor'),
+                    # "angles" : TODO
+                    "orientations": i.get_public_field('orientations'),
+                    "roof": i.get_public_field('roof'),
+                    "floor": i.get_public_field('floor'),
+                    "floor_total": i.get_public_field('floor_total'),
+                    "comment": i.get_public_field('comment'),
+                }
+            }
+        })
+    return JsonResponse({
+        "id": "public",
+        "license": {
+            "type": "ODC-BY-1.0",
+            "url": "http:\/\/opendatacommons.org\/licenses\/by\/1.0\/"
+        },
+        "features": data,
+    })

+ 2 - 16
wifiwithme/core/urls.py

@@ -1,21 +1,7 @@
-"""wifiwithme URL Configuration
-
-The `urlpatterns` list routes URLs to views. For more information please see:
-    https://docs.djangoproject.com/en/1.9/topics/http/urls/
-Examples:
-Function views
-    1. Add an import:  from my_app import views
-    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
-Class-based views
-    1. Add an import:  from other_app.views import Home
-    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
-Including another URLconf
-    1. Import the include() function: from django.conf.urls import url, include
-    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
-"""
-from django.conf.urls import url
+from django.conf.urls import url, include
 from django.contrib import admin
 
 urlpatterns = [
     url(r'^admin/', admin.site.urls),
+    url(r'^map/', include('contribmap.urls')),
 ]