From d1f1fe6b04094b480eedffeb4ad4d4a33069f6a9 Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Fri, 22 Apr 2022 14:00:46 +0100 Subject: [PATCH 1/2] Add next_election_to_organisation to org API endpoint --- every_election/apps/api/serializers.py | 13 +++++++++++++ every_election/apps/api/views.py | 21 +++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/every_election/apps/api/serializers.py b/every_election/apps/api/serializers.py index 147705221..151012898 100644 --- a/every_election/apps/api/serializers.py +++ b/every_election/apps/api/serializers.py @@ -64,6 +64,19 @@ class Meta: fields = org_fields +class OrganisationStandaloneSerializer(OrganisationSerializer): + class Meta: + model = Organisation + fields = list(org_fields) + [ + "next_election_to_organisation", + ] + + next_election_to_organisation = serializers.SerializerMethodField() + + def get_next_election_to_organisation(self, object): + return object.next_election_to_organisation + + class OrganisationGeoSerializer(GeoFeatureModelSerializer): geography_model = GeometrySerializerMethodField() diff --git a/every_election/apps/api/views.py b/every_election/apps/api/views.py index 70161cc0a..47460d5c0 100644 --- a/every_election/apps/api/views.py +++ b/every_election/apps/api/views.py @@ -1,5 +1,10 @@ from collections import OrderedDict from datetime import datetime + +from django.db.models import ( + Subquery, + OuterRef, +) from django.http import Http404 from rest_framework import viewsets from rest_framework.decorators import action @@ -18,6 +23,7 @@ OrganisationDivisionSerializer, OrganisationGeoSerializer, ElectionGeoSerializer, + OrganisationStandaloneSerializer, ) from api import filters @@ -161,8 +167,19 @@ class ElectionSubTypeViewSet(viewsets.ReadOnlyModelViewSet): class OrganisationViewSet(viewsets.ReadOnlyModelViewSet): - queryset = Organisation.objects.all() - serializer_class = OrganisationSerializer + subquery = Subquery( + Election.public_objects.filter(group_type="organisation") + .future() + .filter(organisation=OuterRef("id")) + .order_by("poll_open_date") + .values("poll_open_date")[:1] + ) + + queryset = Organisation.objects.all().annotate( + next_election_to_organisation=subquery + ) + + serializer_class = OrganisationStandaloneSerializer filterset_fields = ["modified"] def get_object(self, **kwargs): From 8da43f7f210986621ae0a9d935cb758b82aa6363 Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Fri, 22 Apr 2022 15:12:26 +0100 Subject: [PATCH 2/2] Bump node version --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7aed643ec..1ed18f6c4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,7 +1,7 @@ version: 2.1 orbs: - node: circleci/node@4.1.0 + node: circleci/node@5.0.2 jobs: build: