views.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import json
  2. from datetime import datetime
  3. from django.shortcuts import render
  4. from django.http import JsonResponse, HttpResponse
  5. from django.core.serializers.json import DjangoJSONEncoder
  6. from django.views import View
  7. from django.core.cache import cache
  8. from django.contrib.auth.decorators import permission_required
  9. from django.contrib.auth.mixins import PermissionRequiredMixin
  10. from django.contrib.gis.geos import Point
  11. from .models import *
  12. def index(request):
  13. return render(request, 'gps_logger/index.html')
  14. @permission_required("gps-logger.markers.change")
  15. def upload(request):
  16. return render(request, 'gps_logger/upload.html')
  17. class MarkerCreateView(PermissionRequiredMixin, View):
  18. permission_required = ("gps-logger.markers.change")
  19. def post(self, request):
  20. insert_count = censor_count = replace_count = 0
  21. data = json.loads(request.body)
  22. for file in data:
  23. if len(data[file]) == 0:
  24. continue
  25. objects = [Marker(
  26. timestamp=datetime.strptime(x['timestamp'].replace("Z","-0000"), r"%Y-%m-%dT%H:%M:%S.%f%z"),
  27. location = Point(x['lng'], x['lat'], x['alt']),
  28. hdop = x.get('hdop', None),
  29. speed = x.get('speed', None)
  30. ) for x in data[file]]
  31. start_date = objects[0].timestamp
  32. end_date = objects[-1].timestamp
  33. oldMarkers = Marker.objects.filter(timestamp__range=(start_date, end_date)).all()
  34. identical = len(oldMarkers) == len(objects)
  35. if identical:
  36. for i, marker in enumerate(oldMarkers):
  37. if marker.timestamp != objects[i].timestamp:
  38. identical = False
  39. break
  40. if identical:
  41. for i, marker in enumerate(oldMarkers):
  42. objects[i].id = marker.id
  43. Marker.objects.bulk_update(objects, ['timestamp', 'location', 'hdop', 'speed'])
  44. replace_count += len(oldMarkers)
  45. else:
  46. insert_count += len(objects) - len(oldMarkers)
  47. oldMarkers.delete()
  48. Marker.objects.bulk_create(objects)
  49. censor_count = censor()
  50. return JsonResponse({
  51. 'censored': censor_count,
  52. 'replaced': replace_count,
  53. 'inserted': insert_count - censor_count
  54. }, status=201)
  55. def marker_view(request):
  56. data = cache.get('markers')
  57. if not data:
  58. values = list(Marker.objects.values())
  59. for marker in values:
  60. location = marker['location']
  61. if location:
  62. marker['lng'] = location.x
  63. marker['lat'] = location.y
  64. marker['alt'] = location.z
  65. del marker['location']
  66. data = json.dumps(values, cls=DjangoJSONEncoder)
  67. cache.set('markers', data, 3600*24)
  68. return HttpResponse(data, content_type='application/json')
  69. class DeleteCensoredView(PermissionRequiredMixin, View):
  70. permission_required = ("gps-logger.markers.change",)
  71. def get(self, request):
  72. counter = censor()
  73. return JsonResponse({'censored': counter})
  74. def censor() -> int:
  75. counter = 0
  76. locations = CensoredLocation.objects.all()
  77. for location in locations:
  78. counter += location.delete_markers()
  79. if(cache.get('markers')):
  80. cache.delete('markers')
  81. return counter