Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add next_election_to_organisation to org API endpoint #1421

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1

orbs:
node: circleci/node@4.1.0
node: circleci/node@5.0.2

jobs:
build:
Expand Down
13 changes: 13 additions & 0 deletions every_election/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 19 additions & 2 deletions every_election/apps/api/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,6 +23,7 @@
OrganisationDivisionSerializer,
OrganisationGeoSerializer,
ElectionGeoSerializer,
OrganisationStandaloneSerializer,
)
from api import filters

Expand Down Expand Up @@ -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):
Expand Down