From c2124afba3d33af3e29ba77bc6712e0f3f9fc0c6 Mon Sep 17 00:00:00 2001 From: Mike VanDenburgh Date: Tue, 25 Oct 2022 12:13:23 -0400 Subject: [PATCH 1/2] Install `django_filter` --- django/poetry.lock | 17 ++++++++++++++++- django/pyproject.toml | 6 +++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/django/poetry.lock b/django/poetry.lock index 63ceb4f0..f1efdd6b 100644 --- a/django/poetry.lock +++ b/django/poetry.lock @@ -242,6 +242,17 @@ tzdata = {version = "*", markers = "sys_platform == \"win32\""} bcrypt = ["bcrypt"] argon2 = ["argon2-cffi (>=19.1.0)"] +[[package]] +name = "django-filter" +version = "22.1" +description = "Django-filter is a reusable Django application for allowing users to filter querysets dynamically." +category = "main" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +Django = ">=3.2" + [[package]] name = "django-stubs" version = "1.12.0" @@ -991,7 +1002,7 @@ python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7" [metadata] lock-version = "1.1" python-versions = "~3.10.0" -content-hash = "5c2c6b4a5f4d7b50f423aed7df0d02c04fa3d5e4aff5bfac0593b691c59069da" +content-hash = "0c5f7e7895e90e85458bc83b12cf890bb4b2bdd73313d28838ffc2bb5997c257" [metadata.files] affine = [ @@ -1091,6 +1102,10 @@ django = [ {file = "Django-4.1-py3-none-any.whl", hash = "sha256:031ccb717782f6af83a0063a1957686e87cb4581ea61b47b3e9addf60687989a"}, {file = "Django-4.1.tar.gz", hash = "sha256:032f8a6fc7cf05ccd1214e4a2e21dfcd6a23b9d575c6573cacc8c67828dbe642"}, ] +django-filter = [ + {file = "django-filter-22.1.tar.gz", hash = "sha256:ed473b76e84f7e83b2511bb2050c3efb36d135207d0128dfe3ae4b36e3594ba5"}, + {file = "django_filter-22.1-py3-none-any.whl", hash = "sha256:ed429e34760127e3520a67f415bec4c905d4649fbe45d0d6da37e6ff5e0287eb"}, +] django-stubs = [ {file = "django-stubs-1.12.0.tar.gz", hash = "sha256:ea8b35d0da49f7b2ee99a79125f1943e033431dd114726d6643cc35de619230e"}, {file = "django_stubs-1.12.0-py3-none-any.whl", hash = "sha256:0dff8ec0ba3abe046450b3d8a29ce9e72629893d2c1ef679189cc2bfdb6d2f64"}, diff --git a/django/pyproject.toml b/django/pyproject.toml index 059595ac..91819b2d 100644 --- a/django/pyproject.toml +++ b/django/pyproject.toml @@ -31,6 +31,7 @@ uritemplate = "^4.1.1" iso3166 = "^2.1.1" rio-tiler = "^3.1.6" mercantile = "^1.2.1" +django-filter = "^22.1" [tool.poetry.dev-dependencies] black = "~22.6.0" @@ -67,7 +68,10 @@ plugins = ["mypy_django_plugin.main", "mypy_drf_plugin.main"] [[tool.mypy.overrides]] -module = "mercantile" +module = [ + "django_filters", + "mercantile" +] ignore_missing_imports = true From 16ac2a50d5bca5d44bdca7777705ea1ab29a6e38 Mon Sep 17 00:00:00 2001 From: Mike VanDenburgh Date: Tue, 25 Oct 2022 12:14:23 -0400 Subject: [PATCH 2/2] Add `SiteEvaluationsFilter` for filtering by timestamp --- django/src/rdwatch/views/site_evaluation.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/django/src/rdwatch/views/site_evaluation.py b/django/src/rdwatch/views/site_evaluation.py index 0ac7bbd3..7e33a110 100644 --- a/django/src/rdwatch/views/site_evaluation.py +++ b/django/src/rdwatch/views/site_evaluation.py @@ -1,3 +1,5 @@ +import django_filters + from django.contrib.gis.db.models.aggregates import Collect from django.contrib.postgres.aggregates import JSONBAgg from django.db.models import Count, Max, Min @@ -12,6 +14,14 @@ from rdwatch.serializers import SiteEvaluationListSerializer +class SiteEvaluationsFilter(django_filters.FilterSet): + timestamp = django_filters.DateTimeFromToRangeFilter() + + class Meta: + model = SiteEvaluation + fields = ["timestamp"] + + class SiteEvaluationsSchema(AutoSchema): def get_operation_id(self, *args): return "getSiteEvaluations" @@ -28,8 +38,10 @@ def get_responses(self, *args, **kwargs): @schema(SiteEvaluationsSchema()) def site_evaluations(request: HttpRequest): queryset = ( - SiteEvaluation.objects.order_by("-timestamp") - .annotate( + SiteEvaluationsFilter( + request.GET, queryset=SiteEvaluation.objects.order_by("-timestamp") + ) + .qs.annotate( timemin=Min("observations__timestamp"), timemax=Max("observations__timestamp"), )