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']),
  28. alt = x['alt'],
  29. hdop = x.get('hdop', None),
  30. speed = x.get('speed', None)
  31. ) for x in data[file]]
  32. start_date = objects[0].timestamp
  33. end_date = objects[-1].timestamp
  34. oldMarkers = Marker.objects.filter(timestamp__range=(start_date, end_date)).all()
  35. identical = len(oldMarkers) == len(objects)
  36. if identical:
  37. for i, marker in enumerate(oldMarkers):
  38. if marker.timestamp != objects[i].timestamp:
  39. identical = False
  40. break
  41. if identical:
  42. for i, marker in enumerate(oldMarkers):
  43. objects[i].id = marker.id
  44. Marker.objects.bulk_update(objects, ['location', 'alt', 'hdop', 'speed'])
  45. replace_count += len(oldMarkers)
  46. else:
  47. insert_count += len(objects) - len(oldMarkers)
  48. oldMarkers.delete()
  49. Marker.objects.bulk_create(objects)
  50. censor_count = censor()
  51. return JsonResponse({
  52. 'censored': censor_count,
  53. 'replaced': replace_count,
  54. 'inserted': insert_count - censor_count
  55. }, status=201)
  56. def marker_view(request):
  57. data = cache.get('markers')
  58. if not data:
  59. values = list(Marker.objects.values())
  60. for marker in values:
  61. location = marker['location']
  62. if location:
  63. marker['lng'] = location.x
  64. marker['lat'] = location.y
  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