|
@@ -2,14 +2,15 @@ import json
|
|
|
from datetime import datetime
|
|
|
from django.shortcuts import render
|
|
|
from django.http import JsonResponse, HttpResponse
|
|
|
+from django.core.serializers.json import DjangoJSONEncoder
|
|
|
from django.views import View
|
|
|
+from django.core.cache import cache
|
|
|
from django.contrib.auth.decorators import permission_required
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
|
|
+from django.contrib.gis.geos import Point
|
|
|
|
|
|
from .models import *
|
|
|
|
|
|
-from pprint import pprint
|
|
|
-
|
|
|
def index(request):
|
|
|
return render(request, 'gps_logger/index.html')
|
|
|
|
|
@@ -21,16 +22,14 @@ class MarkerCreateView(View, PermissionRequiredMixin):
|
|
|
permission_required = ("gps-logger.markers.change")
|
|
|
|
|
|
def post(self, request):
|
|
|
- status_code = 204
|
|
|
+ insert_count = censor_count = replace_count = 0
|
|
|
data = json.loads(request.body)
|
|
|
for file in data:
|
|
|
if len(data[file]) == 0:
|
|
|
continue
|
|
|
objects = [Marker(
|
|
|
timestamp=datetime.strptime(x['timestamp'].replace("Z","-0000"), r"%Y-%m-%dT%H:%M:%S.%f%z"),
|
|
|
- lat = x['lat'],
|
|
|
- lng = x['lng'],
|
|
|
- alt = x['alt'],
|
|
|
+ location = Point(x['lng'], x['lat'], x['alt']),
|
|
|
hdop = x.get('hdop', None),
|
|
|
speed = x.get('speed', None)
|
|
|
) for x in data[file]]
|
|
@@ -38,21 +37,58 @@ class MarkerCreateView(View, PermissionRequiredMixin):
|
|
|
end_date = objects[-1].timestamp
|
|
|
oldMarkers = Marker.objects.filter(timestamp__range=(start_date, end_date)).all()
|
|
|
identical = len(oldMarkers) == len(objects)
|
|
|
+
|
|
|
if identical:
|
|
|
for i, marker in enumerate(oldMarkers):
|
|
|
if marker.timestamp != objects[i].timestamp:
|
|
|
- print(type(marker.timestamp), type(objects[i].timestamp))
|
|
|
identical = False
|
|
|
break
|
|
|
|
|
|
- print(len(oldMarkers), len(objects), identical)
|
|
|
- if not identical:
|
|
|
+ if identical:
|
|
|
+ for i, marker in enumerate(oldMarkers):
|
|
|
+ objects[i].id = marker.id
|
|
|
+ Marker.objects.bulk_update(objects, ['timestamp', 'location', 'hdop', 'speed'])
|
|
|
+ replace_count += len(oldMarkers)
|
|
|
+ else:
|
|
|
+ insert_count += len(objects) - len(oldMarkers)
|
|
|
oldMarkers.delete()
|
|
|
Marker.objects.bulk_create(objects)
|
|
|
- status_code = 201
|
|
|
- return HttpResponse(status=status_code)
|
|
|
+
|
|
|
+ locations = CensoredLocation.objects.all()
|
|
|
+ for location in locations:
|
|
|
+ censor_count += location.delete_markers()
|
|
|
|
|
|
+ if(cache.get('markers')):
|
|
|
+ cache.delete('markers')
|
|
|
|
|
|
-class MarkerView(View):
|
|
|
+ return JsonResponse({
|
|
|
+ 'censored': censor_count,
|
|
|
+ 'replaced': replace_count,
|
|
|
+ 'inserted': insert_count - censor_count
|
|
|
+ }, status=201)
|
|
|
+
|
|
|
+def marker_view(request):
|
|
|
+ data = cache.get('markers')
|
|
|
+ if not data:
|
|
|
+ values = list(Marker.objects.values())
|
|
|
+ for marker in values:
|
|
|
+ location = marker['location']
|
|
|
+ if location:
|
|
|
+ marker['lng'] = location.x
|
|
|
+ marker['lat'] = location.y
|
|
|
+ marker['alt'] = location.z
|
|
|
+ del marker['location']
|
|
|
+ data = json.dumps(values, cls=DjangoJSONEncoder)
|
|
|
+ cache.set('markers', data, 3600*24)
|
|
|
+ return HttpResponse(data, content_type='application/json')
|
|
|
+
|
|
|
+class DeleteCensoredView(View, PermissionRequiredMixin):
|
|
|
+ permission_required = ("gps-logger.markers.change")
|
|
|
def get(self, request):
|
|
|
- return JsonResponse(list(Marker.objects.values()), safe=False)
|
|
|
+ counter = 0
|
|
|
+ locations = CensoredLocation.objects.all()
|
|
|
+ for location in locations:
|
|
|
+ counter += location.delete_markers()
|
|
|
+ if(cache.get('markers')):
|
|
|
+ cache.delete('markers')
|
|
|
+ return JsonResponse({'censored': counter})
|