Browse Source

initial drf-ification, with two resources : panorama and refpoints

Jocelyn Delande 10 years ago
parent
commit
f7f9ec615e
9 changed files with 54 additions and 18 deletions
  1. 0 0
      api/__init__.py
  2. 19 0
      api/serializers.py
  3. 3 0
      api/tests.py
  4. 12 0
      api/urls.py
  5. 14 0
      api/views.py
  6. 0 18
      panorama/models.py
  7. 3 0
      requirements.txt
  8. 2 0
      ztulec/settings.py
  9. 1 0
      ztulec/urls.py

+ 0 - 0
api/__init__.py


+ 19 - 0
api/serializers.py

@@ -0,0 +1,19 @@
+from rest_framework import serializers
+
+from panorama.models import Panorama, ReferencePoint
+
+class PanoramaSerializer(serializers.HyperlinkedModelSerializer):
+    # fixme : return absolute URL for tiles_url
+    class Meta:
+        model = Panorama
+        fields = ("url", "name", "loop",
+                  "latitude", "longitude", "altitude",
+                  "tiles_url")
+
+class ReferencePointSerializer(serializers.HyperlinkedModelSerializer):
+    # fixme : return absolute URL for tiles_url
+    class Meta:
+        model = ReferencePoint
+        fields = ("url", "name",
+                  "latitude", "longitude", "altitude")
+

+ 3 - 0
api/tests.py

@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.

+ 12 - 0
api/urls.py

@@ -0,0 +1,12 @@
+from django.conf.urls import url, include
+from rest_framework import routers
+from . import views
+
+router = routers.DefaultRouter()
+router.register(r'panoramas', views.PanoramaViewSet)
+router.register(r'refpoints', views.ReferencePointViewSet)
+
+urlpatterns = [
+    url(r'^', include(router.urls)),
+#    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
+]

+ 14 - 0
api/views.py

@@ -0,0 +1,14 @@
+from rest_framework import viewsets
+
+from panorama.models import Panorama, ReferencePoint
+from .serializers import *
+
+class PanoramaViewSet(viewsets.ModelViewSet):
+    queryset = Panorama.objects.all()
+    serializer_class = PanoramaSerializer
+
+class ReferencePointViewSet(viewsets.ModelViewSet):
+    queryset = ReferencePoint.objects.all()
+    serializer_class = ReferencePointSerializer
+
+

+ 0 - 18
panorama/models.py

@@ -71,16 +71,6 @@ class Panorama(Point):
         return os.path.join(settings.MEDIA_URL, settings.PANORAMA_TILES_DIR,
                             str(self.pk))
 
-    def to_dict(self):
-        """Useful to pass information to the javascript code as JSON"""
-        return {"id": self.id,
-                "name": self.name,
-                "loop": self.loop,
-                "latitude": self.latitude,
-                "longitude": self.longitude,
-                "altitude": self.altitude,
-                "tiles_url": self.tiles_url()}
-
     def generate_tiles(self):
         # The trailing slash is necessary for the shell script.
         tiles_dir = self.tiles_dir()  + "/"
@@ -101,14 +91,6 @@ class ReferencePoint(Point):
     name = models.CharField(verbose_name="name", max_length=255,
                             help_text="Name of the reference point")
 
-    def to_dict(self):
-        """Useful to pass information to the javascript code as JSON"""
-        return {"id": self.id,
-                "name": self.name,
-                "latitude": self.latitude,
-                "longitude": self.longitude,
-                "altitude": self.altitude}
-
     def to_dict_extended(self, point):
         """Same as above, but also includes information relative
         to the given point: bearing, azimuth, distance."""

+ 3 - 0
requirements.txt

@@ -0,0 +1,3 @@
+Django==1.7.4
+djangorestframework==3.0.5
+Pillow==2.7.0

+ 2 - 0
ztulec/settings.py

@@ -36,7 +36,9 @@ INSTALLED_APPS = (
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
+    'rest_framework',
     'panorama',
+    'api'
 )
 
 MIDDLEWARE_CLASSES = (

+ 1 - 0
ztulec/urls.py

@@ -8,5 +8,6 @@ urlpatterns = patterns('',
     # url(r'^$', 'ztulec.views.home', name='home'),
     url(r'^admin/', include(admin.site.urls)),
     url(r'^', include('panorama.urls', namespace="panorama")),
+    url(r'^api/v1/', include('api.urls')),
 # In debug mode, serve tiles
 ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)