From 7c02748572fbfc899aa6b2109948656647aef342 Mon Sep 17 00:00:00 2001 From: "Aapo (f021b) Rista" Date: Tue, 10 Oct 2023 16:16:32 +0300 Subject: [PATCH] Add GIS / GeoDjango support --- Dockerfile | 11 +++++++++++ deviceregistry/settings.py | 35 +++++++++++++++++------------------ pyproject.toml | 2 +- requirements.txt | 2 +- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7e0cbc5..b853cc7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,17 +2,28 @@ FROM python:3.11-alpine +# DOCKER_IMAGE is checked in settings.py to determine which *_LIBRARY_PATHs to use +ENV DOCKER_IMAGE alpine + ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 +# Override these in the docker-compose.yml file or elsewhere ENV DATABASE_NAME=postgres ENV DATABASE_USER=postgres ENV DATABASE_PASSWORD=postgres ENV DATABASE_HOST=db ENV DATABASE_PORT=5432 + ENV MEDIA_HOME=/media ENV DJANGO_SETTINGS_MODULE=deviceregistry.settings +# Install GeoDjango dependencies +RUN apk add --no-cache \ + geos-dev \ + proj-dev \ + gdal-dev + RUN addgroup -S app && adduser -S app -G app WORKDIR /home/app diff --git a/deviceregistry/settings.py b/deviceregistry/settings.py index 56114b4..adc495b 100644 --- a/deviceregistry/settings.py +++ b/deviceregistry/settings.py @@ -10,9 +10,11 @@ https://docs.djangoproject.com/en/4.2/ref/settings/ """ -import environ +import os from pathlib import Path +import environ + env = environ.Env( # set casting, default value DEBUG=(bool, False) @@ -43,6 +45,7 @@ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + "django.contrib.gis", "rest_framework", "rest_framework.authtoken", "auditlog", @@ -78,13 +81,11 @@ WSGI_APPLICATION = "deviceregistry.wsgi.application" -# Database -# https://docs.djangoproject.com/en/4.2/ref/settings/#databases - +# Database https://docs.djangoproject.com/en/4.2/ref/settings/#databases DATABASES = { "default": { - "ENGINE": "django.db.backends.postgresql", - # "ENGINE": "django.contrib.gis.db.backends.postgis", + # "ENGINE": "django.db.backends.postgresql", + "ENGINE": "django.contrib.gis.db.backends.postgis", "NAME": env("DATABASE_NAME", default="postgres"), "USER": env("DATABASE_USER", default="postgres"), "PASSWORD": env("DATABASE_PASSWORD", default="postgres"), @@ -92,18 +93,7 @@ "PORT": env("DATABASE_PORT", default=5432), } } -# if env("DATABASE_LOCAL", default=False): -# # Temp database for development -# DATABASES = { -# "default": { -# "ENGINE": "django.contrib.gis.db.backends.postgis", -# "NAME": "deviceregistry", -# # "USER": env("DATABASE_USER", default="postgres"), -# # "PASSWORD": env("DATABASE_PASSWORD", default="postgres"), -# # "HOST": env("DATABASE_HOST", default="db"), -# # "PORT": env("DATABASE_PORT", default=5432), -# } -# } + # Password validation # https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators @@ -151,3 +141,12 @@ "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination", "PAGE_SIZE": 50, } + + +# Set library paths depending on which environment we are running in +# These are needed for GeoDjango running in Docker python:3.11-alpine +if os.environ.get("DOCKER_IMAGE", "") == "alpine": + GDAL_LIBRARY_PATH = "/usr/lib/libgdal.so" + GEOS_LIBRARY_PATH = "/usr/lib/libgeos_c.so" +else: + pass diff --git a/pyproject.toml b/pyproject.toml index bebf027..57e6dbc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ requires-python = ">=3.10" dynamic = ["version"] dependencies = [ - "Django >= 4.2.5, < 5.0", + "Django >= 4.2.6, < 5.0", "django-auditlog", "django-environ", "django-rest-framework", diff --git a/requirements.txt b/requirements.txt index 0ac0a9f..95c286f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,7 @@ # asgiref==3.7.2 # via django -django==4.2.5 +django==4.2.6 # via # django-auditlog # djangorestframework