-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:laws-africa/peachjam
- Loading branch information
Showing
13 changed files
with
247 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Peach Jam API | ||
|
||
This Django application is the Peach Jam API. It has two components: | ||
|
||
1. an internal API for use by frontend components | ||
2. a public-facing API for external consumption. | ||
|
||
## Public API | ||
|
||
The public API is read-only. Users must authenticate and require a specific permission to access a particular | ||
document type through the API. | ||
|
||
Full documentation of the API is available in OpenAPI format from these endpoints: | ||
|
||
* `/api/v1/schema`: in [OpenAPI](https://swagger.io/specification/) format | ||
* `/api/v1/schema/redoc`: in [Redoc](https://github.com/Redocly/redoc) format | ||
* `/api/v1/schema/swagger-ui`: in [Swagger UI](https://swagger.io/tools/swagger-ui/) format | ||
|
||
### Authentication | ||
|
||
Authenticate with an `Authorization: Token <token>` header that includes your authentication token. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from django.http import Http404, HttpResponse | ||
from drf_spectacular.utils import OpenApiTypes, extend_schema | ||
from rest_framework import viewsets | ||
from rest_framework.decorators import action | ||
from rest_framework.permissions import BasePermission, DjangoModelPermissions | ||
|
||
from peachjam.models import Gazette, Judgment | ||
from peachjam_api.serializers import GazetteSerializer, JudgmentSerializer | ||
|
||
|
||
class JudgmentAPIPermission(BasePermission): | ||
permission_name = "peachjam.api_judgment" | ||
|
||
def has_permission(self, request, view): | ||
# user must have perms to access judgments through the api | ||
return ( | ||
request.user | ||
and request.user.is_authenticated | ||
and request.user.has_perm(self.permission_name) | ||
) | ||
|
||
|
||
class GazetteAPIPermission(JudgmentAPIPermission): | ||
permission_name = "peachjam.api_gazette" | ||
|
||
|
||
class BaseDocumentViewSet(viewsets.ReadOnlyModelViewSet): | ||
permission_classes = [DjangoModelPermissions] | ||
filterset_fields = { | ||
"jurisdiction": ["exact"], | ||
"work_frbr_uri": ["exact"], | ||
"title": ["exact", "icontains"], | ||
"date": ["exact", "gte", "lte"], | ||
"updated_at": ["exact", "gte", "lte"], | ||
"created_at": ["exact", "gte", "lte"], | ||
} | ||
ordering_fields = ["title", "date", "updated_at", "created_at"] | ||
ordering = ["updated_at"] | ||
|
||
@extend_schema(responses={(200, "text/plain"): OpenApiTypes.STR}) | ||
@action(detail=True, url_path="source.txt") | ||
def source_txt(self, request, pk=None): | ||
"""Source document in text form (if available).""" | ||
obj = self.get_object() | ||
# we only allow certain formats | ||
try: | ||
content = getattr(obj, "document_content") | ||
except AttributeError: | ||
raise Http404() | ||
|
||
# return content.content_text as a normal drf response object | ||
return HttpResponse(content.content_text, content_type="text/plain") | ||
|
||
@extend_schema(responses={(200, "application/pdf"): OpenApiTypes.BINARY}) | ||
@action(detail=True, url_path="source.pdf") | ||
def source_pdf(self, request, pk=None): | ||
"""Source document in PDF form (if available).""" | ||
obj = self.get_object() | ||
# we only allow certain formats | ||
try: | ||
source_file = getattr(obj, "source_file") | ||
except AttributeError: | ||
raise Http404() | ||
|
||
if not source_file.file or source_file.mimetype != "application/pdf": | ||
raise Http404() | ||
|
||
return self.make_response( | ||
source_file.file, source_file.mimetype, source_file.filename_for_download() | ||
) | ||
|
||
def make_response(self, f, content_type, fname): | ||
file_bytes = f.read() | ||
response = HttpResponse(file_bytes, content_type=content_type) | ||
response["Content-Disposition"] = f"inline; filename={fname}" | ||
response["Content-Length"] = str(len(file_bytes)) | ||
return response | ||
|
||
|
||
class GazettesViewSet(BaseDocumentViewSet): | ||
permission_classes = [ | ||
GazetteAPIPermission, | ||
DjangoModelPermissions, | ||
] | ||
queryset = Gazette.objects.all() | ||
serializer_class = GazetteSerializer | ||
|
||
|
||
class JudgmentsViewSet(BaseDocumentViewSet): | ||
permission_classes = [ | ||
JudgmentAPIPermission, | ||
DjangoModelPermissions, | ||
] | ||
queryset = Judgment.objects.select_related("court").all() | ||
serializer_class = JudgmentSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.urls import include, path | ||
from rest_framework import routers | ||
|
||
from . import public_views | ||
|
||
router = routers.DefaultRouter(trailing_slash=False) | ||
router.register( | ||
r"judgments", | ||
public_views.JudgmentsViewSet, | ||
basename="judgments", | ||
) | ||
router.register( | ||
r"gazettes", | ||
public_views.GazettesViewSet, | ||
basename="gazettes", | ||
) | ||
|
||
urlpatterns = [ | ||
# public-facing API | ||
path("v1/", include(router.urls)), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters