From ac18f31d9a4bdc3cb928a46f88502a526acbe575 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 9 Sep 2022 16:59:13 +0000 Subject: [PATCH] Start development of gpslog module --- gpslog/models.py | 40 ++++++++++++++++++++++++++++++++++++++-- gpslog/urls.py | 9 +++++++++ gpslog/views.py | 38 ++++++++++++++++++++++++++++++++++++-- kumify/settings.py | 6 ++++-- kumify/urls.py | 1 + requirements.txt | 5 ++--- 6 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 gpslog/urls.py diff --git a/gpslog/models.py b/gpslog/models.py index 71a8362..bfdf44b 100644 --- a/gpslog/models.py +++ b/gpslog/models.py @@ -1,3 +1,39 @@ -from django.db import models +from django.contrib.gis.db import models +from django.contrib.auth import get_user_model -# Create your models here. +from uuid import uuid4 + + +class GPSTrack(models.Model): + id = models.UUIDField(default=uuid4, primary_key=True) + user = models.ForeignKey(get_user_model(), models.CASCADE) + + +class GPSToken(models.Model): + name = models.CharField(max_length=128, null=True, blank=True) + track = models.ForeignKey(GPSTrack, models.CASCADE) + token = models.UUIDField(default=uuid4) + + # Permissions + read = models.BooleanField(default=False) + write = models.BooleanField(default=False) + history = models.BooleanField(default=False) + + +class GPSPoint(models.Model): + track = models.ForeignKey(GPSTrack, models.CASCADE) + point = models.PointField() + timestamp = models.DateTimeField() + token = models.ForeignKey(GPSToken, models.SET_NULL, null=True) + + # Optional additional information + battery = models.DecimalField( + max_digits=5, decimal_places=2, null=True, blank=True) + accuracy = models.DecimalField( + max_digits=64, decimal_places=32, null=True, blank=True) + speed = models.DecimalField( + max_digits=64, decimal_places=32, null=True, blank=True) + bearing = models.DecimalField( + max_digits=64, decimal_places=61, null=True, blank=True) + satellites = models.IntegerField(null=True, blank=True) + user_agent = models.TextField(null=True, blank=True) diff --git a/gpslog/urls.py b/gpslog/urls.py new file mode 100644 index 0000000..2fd8efa --- /dev/null +++ b/gpslog/urls.py @@ -0,0 +1,9 @@ +from .views import GPSLogView + +from django.urls import path + +app_name = "gpslog" + +urlpatterns = [ + path('//', GPSLogView.as_view(), name="gpslog"), +] \ No newline at end of file diff --git a/gpslog/views.py b/gpslog/views.py index 91ea44a..019d7dd 100644 --- a/gpslog/views.py +++ b/gpslog/views.py @@ -1,3 +1,37 @@ -from django.shortcuts import render +from django.views import View +from django.contrib.auth.mixins import LoginRequiredMixin +from django.shortcuts import get_object_or_404 +from django.utils import timezone +from django.contrib.gis.geos import Point -# Create your views here. +from .models import GPSTrack, GPSToken, GPSPoint + + +class GPSLogView(LoginRequiredMixin, View): + def dispatch(self, request, *args, **kwargs): + self.gps_track = get_object_or_404(GPSTrack, id=self.kwargs["track"]) + self.gps_token = get_object_or_404( + GPSToken, track=self.gps_track, token=self.kwargs["token"]) + + if request.method == "POST" and not self.gps_token.write: + return self.http_method_not_allowed(request, *args, **kwargs) + + return super().dispatch(request, *args, **kwargs) + +# ?lat=47.07047834&lon=15.45140181&alt=427.04833984375&tst=1043419092&batt=23.0&acc=14.692631721496582&spd=0.0&dir=&sat=6 + + def get(self, request, *args, **kwargs): + if self.gps_token.write and all(field in request.GET for field in ("lat", "lon")): + lat = request.GET["lat"] + lon = request.GET["lon"] + + alt = request.GET.get("alt", None) + + point = Point(lat, lon, alt) + + tst = request.GET.get("tst", timezone.now().timestamp()) + + + + def post(self, request, *args, **kwargs): + pass diff --git a/kumify/settings.py b/kumify/settings.py index e9204b6..6e834d2 100644 --- a/kumify/settings.py +++ b/kumify/settings.py @@ -22,6 +22,7 @@ except NameError: 'friends', 'habits', 'mood', + 'gpslog', ] CORE_MODULES = [ @@ -79,10 +80,10 @@ WSGI_APPLICATION = 'kumify.wsgi.application' DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', + 'ENGINE': 'django.contrib.gis.db.backends.spatialite', 'NAME': BASE_DIR / 'db.sqlite3', } if not DB_HOST else { - 'ENGINE': 'django.db.backends.mysql', + 'ENGINE': 'django.contrib.gis.db.backends.mysql', 'NAME': DB_NAME, 'USER': DB_USER, 'PASSWORD': DB_PASS, @@ -132,6 +133,7 @@ USE_L10N = True USE_TZ = True +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.1/howto/static-files/ diff --git a/kumify/urls.py b/kumify/urls.py index cd5cc14..2cb2f3d 100644 --- a/kumify/urls.py +++ b/kumify/urls.py @@ -26,4 +26,5 @@ urlpatterns = [ path('cron/', include("cronhandler.urls", "cron")), path('webhooks/telegram/', TelegramWebhookView.as_view()), path('dreams/', include("dreams.urls", "dreams")), + path('gpslog/', include("gpslog.urls")) ] diff --git a/requirements.txt b/requirements.txt index c40eb29..464c36f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +1,10 @@ -django django[argon2] -django[mysql] django-colorfield django-multiupload django-storages django-polymorphic git+https://kumig.it/kumitterer/django-multiselectfield.git -git+https://github.com/Chive/django-multiupload.git#egg=django-multiupload +git+https://github.com/Chive/django-multiupload.git dbsettings django-filtersignals mysqlclient @@ -20,3 +18,4 @@ bokeh panel scipy channels +django-autosecretkey \ No newline at end of file