Browse Source

Switch public.json to class-based view

Jocelyn Delande 9 years ago
parent
commit
a190150b1f
2 changed files with 46 additions and 47 deletions
  1. 2 2
      wifiwithme/apps/contribmap/urls.py
  2. 44 45
      wifiwithme/apps/contribmap/views.py

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

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

+ 44 - 45
wifiwithme/apps/contribmap/views.py

@@ -1,52 +1,51 @@
 from django.http import JsonResponse
+from django.views.generic import View
 
 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": i.get_public_field('angles'),
-                    "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'),
+class JSONView(View):
+    def get(self, request):
+        return JsonResponse({
+            "id": self.ID,
+            "license": self.LICENSE,
+            "features": self.get_features(),
+        })
+
+
+class PublicJSON(JSONView):
+    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()
+
+        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": {
+                        k: i.get_public_field(k) for k in self.PLACE_PROPERTIES
+                    },
                     "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,
-    })
+            })
+        return data