Skip to content

Commit

Permalink
add a documents end point to the API
Browse files Browse the repository at this point in the history
Lets people get the list of council documents from the API
  • Loading branch information
struan committed Jul 29, 2024
1 parent c2f6f38 commit 5a5455d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
41 changes: 40 additions & 1 deletion caps/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import reverse, serializers

from caps.models import Council, Promise, SavedSearch
from caps.models import Council, PlanDocument, Promise, SavedSearch


class CouncilSerializer(serializers.HyperlinkedModelSerializer):
Expand All @@ -16,6 +16,9 @@ class CouncilSerializer(serializers.HyperlinkedModelSerializer):
carbon_reduction_statements = serializers.HyperlinkedIdentityField(
view_name="council-commitments", lookup_field="authority_code"
)
climate_documents = serializers.HyperlinkedIdentityField(
view_name="council-documents", lookup_field="authority_code"
)

class Meta:
model = Council
Expand All @@ -29,6 +32,7 @@ class Meta:
"authority_code",
"plan_count",
"document_count",
"climate_documents",
"plans_last_update",
"carbon_reduction_commitment",
"carbon_neutral_date",
Expand Down Expand Up @@ -74,6 +78,41 @@ def get_council(self, obj):
return result


class PlanDocumentSerializer(serializers.HyperlinkedModelSerializer):
council = serializers.SerializerMethodField()
document_type = serializers.SerializerMethodField()
cached_url = serializers.CharField(source="file")

class Meta:
model = PlanDocument
fields = [
"council",
"title",
"document_type",
"file_type",
"url",
"cached_url",
"updated_at",
]

def get_council(self, obj):
code = obj.council.authority_code
# do this is a string otherwise you get an array as the result
result = "{}".format(
reverse.reverse(
"council-detail", args=[code], request=self.context["request"]
),
)
return result

def get_document_type(self, obj):
type_code = obj.get_document_type_display()
if type_code is None:
type_code = "unknown"

return type_code


class SearchTermSerializer(serializers.HyperlinkedModelSerializer):
times_seen = serializers.IntegerField()

Expand Down
31 changes: 31 additions & 0 deletions caps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from caps.api.serializers import (
CouncilSerializer,
PlanDocumentSerializer,
PromiseSerializer,
SearchTermSerializer,
)
Expand Down Expand Up @@ -195,6 +196,36 @@ def get_queryset(self):
)


class CouncilDocumentsViewSet(viewsets.ReadOnlyModelViewSet):
"""
Information about a council's climate documents.
Lists all documents we have for a council.
* council - link to the council
* title - title of the document
* document_type - the type of document, if known, e.g Action Plan
* file_type - what type of file, e.g PDF
* url - link to document on the council's website
* cached_url - link to our cached copy of the file
* last_update_at - date the information was last update
"""

queryset = PlanDocument.objects.order_by("updated_at").all()
serializer_class = PlanDocumentSerializer
pagination_class = None

def get_queryset(self):
return (
PlanDocument.objects.filter(
council__authority_code=self.kwargs["authority_code"]
)
.select_related("council")
.order_by("updated_at")
.all()
)


class SearchTermViewSet(viewsets.ReadOnlyModelViewSet):
"""
List of search terms that returned results, ordered by most popular terms
Expand Down
15 changes: 10 additions & 5 deletions caps/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.urls import include, path
from django.contrib import admin
import haystack.generic_views
from django.conf import settings
from django.contrib import admin
from django.urls import include, path
from django.views.generic.base import RedirectView

import haystack.generic_views
from caps.forms import HighlightedSearchForm
import caps.views as views
import caps.api.views as api_views
import caps.views as views
from caps.api import routers
from caps.forms import HighlightedSearchForm

router = routers.Router()
router.register(r"councils", api_views.CouncilViewSet, basename="council")
Expand Down Expand Up @@ -55,6 +55,11 @@
api_views.CouncilCommitmentsViewSet.as_view({"get": "list"}),
name="council-commitments",
),
path(
"api/councils/<str:authority_code>/documents",
api_views.CouncilDocumentsViewSet.as_view({"get": "list"}),
name="council-documents",
),
path("content/<str:markdown_slug>/", views.MarkdownView.as_view(), name="content"),
# used for testing page in debug mode
path("404/", views.NotFoundPageView.as_view(), name="404"),
Expand Down

0 comments on commit 5a5455d

Please sign in to comment.