Browse Source

Add private.json view

It's required to have staff privillege to access that resource.
Jocelyn Delande 9 years ago
parent
commit
5b7d1f6334

+ 14 - 0
wifiwithme/apps/contribmap/tests.py

@@ -1,5 +1,6 @@
 import json
 
+from django.contrib.auth.models import User
 from django.test import TestCase, Client
 
 from contribmap.models import Contrib
@@ -85,6 +86,19 @@ class TestViews(APITestCase):
         self.assertEqual(response.status_code, 200)
         self.assertEqual(len(response.data['features']), 1)
 
+    def test_private_json(self):
+        self.client.force_login(
+            User.objects.create(username='foo', is_staff=False))
+
+        response = self.client.get('/map/private.json')
+        self.assertEqual(response.status_code, 403)
+
+    def test_private_json_staff(self):
+        self.client.force_login(
+            User.objects.create(username='foo', is_staff=True))
+        response = self.client.get('/map/private.json')
+        self.assertEqual(response.status_code, 200)
+
 
 class TestDataImport(TestCase):
     fixtures = ['bottle_data.yaml']

+ 2 - 1
wifiwithme/apps/contribmap/urls.py

@@ -1,7 +1,8 @@
 from django.conf.urls import url
 
-from .views import PublicJSON
+from .views import PublicJSON, PrivateJSON
 
 urlpatterns = [
     url(r'^public.json$', PublicJSON.as_view(), name='public_json'),
+    url(r'^private.json$', PrivateJSON.as_view(), name='private_json'),
 ]

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

@@ -1,10 +1,10 @@
-from django.http import JsonResponse
+from django.http import JsonResponse, HttpResponseForbidden
 from django.views.generic import View
 
 from .models import Contrib
 
 
-class JSONView(View):
+class JSONContribView(View):
     def get(self, request):
         return JsonResponse({
             "id": self.ID,
@@ -12,15 +12,16 @@ class JSONView(View):
             "features": self.get_features(),
         })
 
+    PLACE_PROPERTIES = [
+        'floor', 'angles', 'orientations', 'roof', 'floor', 'floor_total']
+
 
-class PublicJSON(JSONView):
+class PublicJSON(JSONContribView):
     ID = 'public'
     LICENSE = {
         "type": "ODC-BY-1.0",
         "url": "http:\/\/opendatacommons.org\/licenses\/by\/1.0\/"
     }
-    PLACE_PROPERTIES = [
-        'floor', 'angles', 'orientations', 'roof', 'floor', 'floor_total']
 
     def get_features(self):
         contribs = Contrib.objects.all()
@@ -41,6 +42,7 @@ class PublicJSON(JSONView):
                     "type": "Point",
                 },
                 "properties": {
+                    "contrib_type": i.contrib_type,
                     "name": i.get_public_field('name'),
                     "place": {
                         k: i.get_public_field(k) for k in self.PLACE_PROPERTIES
@@ -49,3 +51,47 @@ class PublicJSON(JSONView):
                 }
             })
         return data
+
+
+class PrivateJSON(JSONContribView):
+    ID = 'private'
+    LICENSE = {
+        "type": "Copyright",
+    }
+
+    def dispatch(self, request, *args, **kwargs):
+        if hasattr(request, 'user') and request.user.is_staff:
+            return super().dispatch(request, *args, **kwargs)
+        else:
+            return HttpResponseForbidden('Need staff access')
+
+    def get_features(self):
+        contribs = Contrib.objects.all()
+
+        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": {
+                    "contrib_type": i.contrib_type,
+                    "name": i.name,
+                    "place": {
+                        k: getattr(i, k) for k in self.PLACE_PROPERTIES
+                    },
+                    "comment": i.comment,
+                    "phone": i.phone,
+                    "email": i.email
+                }
+            })
+        return data