From 166d586fc21625f495fc681f4a7899542b40914f Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Mon, 10 Jul 2023 17:06:43 +0800 Subject: [PATCH] Use leaflet for handling of locations in django admin --- Pipfile | 1 + Pipfile.lock | 12 +++++++++-- stude/api/urls.py | 3 ++- stude/config/settings.py | 10 ++++++++++ stude/landmarks/__init__.py | 0 stude/landmarks/admin.py | 5 +++++ stude/landmarks/apps.py | 6 ++++++ stude/landmarks/migrations/0001_initial.py | 23 ++++++++++++++++++++++ stude/landmarks/migrations/__init__.py | 0 stude/landmarks/models.py | 11 +++++++++++ stude/landmarks/serializers.py | 8 ++++++++ stude/landmarks/tests.py | 3 +++ stude/landmarks/urls.py | 6 ++++++ stude/landmarks/views.py | 8 ++++++++ stude/student_status/admin.py | 3 ++- 15 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 stude/landmarks/__init__.py create mode 100644 stude/landmarks/admin.py create mode 100644 stude/landmarks/apps.py create mode 100644 stude/landmarks/migrations/0001_initial.py create mode 100644 stude/landmarks/migrations/__init__.py create mode 100644 stude/landmarks/models.py create mode 100644 stude/landmarks/serializers.py create mode 100644 stude/landmarks/tests.py create mode 100644 stude/landmarks/urls.py create mode 100644 stude/landmarks/views.py diff --git a/Pipfile b/Pipfile index d8df1cc..dc192fe 100644 --- a/Pipfile +++ b/Pipfile @@ -14,6 +14,7 @@ djangochannelsrestframework = "*" daphne = "*" psycopg2 = "*" gdal = {path = "./packages/GDAL-3.4.3-cp311-cp311-win_amd64.whl"} +django-leaflet = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 956ec20..564bee3 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "37bbd0140027c832ff7382532bd60c4def5a0a2a06d7fae418e89a7de6f109e1" + "sha256": "efd1134f98df71c3c1209f70d5d962b6609fe23b044b57f80bba89db477b549f" }, "pipfile-spec": 6, "requires": { @@ -268,6 +268,14 @@ "index": "pypi", "version": "==4.2.3" }, + "django-leaflet": { + "hashes": [ + "sha256:2f6dc8c7187fd22e62b6f2b7b42eed86920f81bec312aa654981ebe5bc8e0866", + "sha256:ea35244539fe298156feafc7f1e2ce63067c8caa2ac6f8344069796b19d1ce9b" + ], + "index": "pypi", + "version": "==0.29.0" + }, "django-templated-mail": { "hashes": [ "sha256:8db807effebb42a532622e2d142dfd453dafcd0d7794c4c3332acb90656315f9", @@ -311,7 +319,7 @@ "hashes": [ "sha256:f78861fb5115d5c2f8cf3c52a492ff548da9e1256dc84088947379f90e77e5b6" ], - "path": "E:/Downloads/GDAL-3.4.3-cp311-cp311-win_amd64.whl", + "path": "./packages/GDAL-3.4.3-cp311-cp311-win_amd64.whl", "version": "==3.4.3" }, "hyperlink": { diff --git a/stude/api/urls.py b/stude/api/urls.py index 6942309..9cce2fa 100644 --- a/stude/api/urls.py +++ b/stude/api/urls.py @@ -9,5 +9,6 @@ urlpatterns = [ path('semesters/', include('semesters.urls')), path('subjects/', include('subjects.urls')), path('study_groups/', include('study_groups.urls')), - path('messages/', include('studygroup_messages.urls')) + path('messages/', include('studygroup_messages.urls')), + path('landmarks/', include('landmarks.urls')), ] diff --git a/stude/config/settings.py b/stude/config/settings.py index 07a352f..06a6af9 100644 --- a/stude/config/settings.py +++ b/stude/config/settings.py @@ -87,6 +87,8 @@ INSTALLED_APPS = [ 'subjects', 'study_groups', 'studygroup_messages', + 'leaflet', + 'landmarks', ] MIDDLEWARE = [ @@ -225,3 +227,11 @@ SITE_NAME = 'Stud-E' JWT_TOKEN_LIFETIME = 10800 ACCESS_TOKEN_LIFETIME = JWT_TOKEN_LIFETIME REFRESH_TOKEN_LIFETIME = 24 * JWT_TOKEN_LIFETIME + +LEAFLET_CONFIG = { + 'DEFAULT_CENTER': (8.4803, 124.6498), + 'DEFAULT_ZOOM': 24, + 'MAX_ZOOM': 20, + 'MIN_ZOOM': 3, + 'SCALE': 'both' +} diff --git a/stude/landmarks/__init__.py b/stude/landmarks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stude/landmarks/admin.py b/stude/landmarks/admin.py new file mode 100644 index 0000000..950b566 --- /dev/null +++ b/stude/landmarks/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from leaflet.admin import LeafletGeoAdmin +from .models import Landmark + +admin.site.register(Landmark, LeafletGeoAdmin) diff --git a/stude/landmarks/apps.py b/stude/landmarks/apps.py new file mode 100644 index 0000000..a076cf5 --- /dev/null +++ b/stude/landmarks/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class LandmarksConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'landmarks' diff --git a/stude/landmarks/migrations/0001_initial.py b/stude/landmarks/migrations/0001_initial.py new file mode 100644 index 0000000..ed22085 --- /dev/null +++ b/stude/landmarks/migrations/0001_initial.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.3 on 2023-07-10 07:37 + +import django.contrib.gis.db.models.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Landmark', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=64)), + ('location', django.contrib.gis.db.models.fields.PolygonField(srid=4326)), + ], + ), + ] diff --git a/stude/landmarks/migrations/__init__.py b/stude/landmarks/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stude/landmarks/models.py b/stude/landmarks/models.py new file mode 100644 index 0000000..f3f2fde --- /dev/null +++ b/stude/landmarks/models.py @@ -0,0 +1,11 @@ +from django.db import models +from django.contrib.gis.db import models as gis_models +# Create your models here. + + +class Landmark(models.Model): + name = models.CharField(max_length=64) + location = gis_models.PolygonField() + + def __str__(self): + return self.name diff --git a/stude/landmarks/serializers.py b/stude/landmarks/serializers.py new file mode 100644 index 0000000..20796ac --- /dev/null +++ b/stude/landmarks/serializers.py @@ -0,0 +1,8 @@ +from .models import Landmark +from rest_framework import serializers + + +class LandmarkSerializer(serializers.ModelSerializer): + class Meta: + model = Landmark + fields = '__all__' diff --git a/stude/landmarks/tests.py b/stude/landmarks/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/stude/landmarks/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/stude/landmarks/urls.py b/stude/landmarks/urls.py new file mode 100644 index 0000000..7917063 --- /dev/null +++ b/stude/landmarks/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from .views import LandmarkListView + +urlpatterns = [ + path('', LandmarkListView.as_view()), +] diff --git a/stude/landmarks/views.py b/stude/landmarks/views.py new file mode 100644 index 0000000..084bbf0 --- /dev/null +++ b/stude/landmarks/views.py @@ -0,0 +1,8 @@ +from rest_framework import generics +from rest_framework.permissions import IsAuthenticated +from .serializers import LandmarkSerializer + + +class LandmarkListView(generics.ListAPIView): + serializer_class = LandmarkSerializer + permission_classes = [IsAuthenticated] diff --git a/stude/student_status/admin.py b/stude/student_status/admin.py index ba4cb5f..5ed1467 100644 --- a/stude/student_status/admin.py +++ b/stude/student_status/admin.py @@ -1,4 +1,5 @@ from django.contrib import admin from .models import StudentStatus +from leaflet.admin import LeafletGeoAdmin -admin.site.register(StudentStatus) +admin.site.register(StudentStatus, LeafletGeoAdmin)