Sfoglia il codice sorgente

add marker rest api

subDesTagesMitExtraKaese 2 anni fa
parent
commit
f7dc3f8ec3
3 ha cambiato i file con 40 aggiunte e 4 eliminazioni
  1. 15 0
      serializers.py
  2. 3 0
      urls.py
  3. 22 4
      views.py

+ 15 - 0
serializers.py

@@ -0,0 +1,15 @@
+from django.db import transaction
+from rest_framework import serializers
+from .models import *
+
+class MarkerSerializer(serializers.ModelSerializer):
+
+  def create(self, validated_data):
+    instance = Marker(**validated_data)
+    instance.save()
+    return instance
+
+  class Meta:
+    model = Marker
+    fields = ("id", "timestamp", "lat", "lng", "alt", "hdop", "speed")
+    read_only_fields = ("id",)

+ 3 - 0
urls.py

@@ -4,4 +4,7 @@ from . import views
 
 urlpatterns = [
   path('', views.index, name='index'),
+  path('upload', views.upload, name='upload'),
+  path('markers/create', views.MarkerCreateView.as_view(), name='marker-create'),
+  path('markers', views.MarkerView.as_view(), name='marker')
 ]

+ 22 - 4
views.py

@@ -1,10 +1,28 @@
 from django.shortcuts import render
+from django.contrib.auth.decorators import permission_required
+from django.contrib.auth.mixins import PermissionRequiredMixin
+from rest_framework import generics
+
 from .models import *
+from .serializers import MarkerSerializer
 
 
 def index(request):
+  return render(request, 'gps_logger/index.html')
+
+@permission_required("gps-logger.markers.change")
+def upload(request):
+  return render(request, 'gps_logger/upload.html')
+
+class MarkerCreateView(generics.CreateAPIView, PermissionRequiredMixin):
+  serializer_class = MarkerSerializer
+  permission_required = ("gps-logger.markers.change")
+  def get_serializer(self, *args, **kwargs):
+    if isinstance(kwargs.get("data", {}), list):
+        kwargs["many"] = True
+
+    return super(MarkerCreateView, self).get_serializer(*args, **kwargs)
 
-  context = {
-    
-  }
-  return render(request, 'gps_logger/index.html', context)
+class MarkerView(generics.ListAPIView):
+  serializer_class = MarkerSerializer
+  queryset = Marker.objects