physionet-build
physionet-build copied to clipboard
V0: Annotation API - Annotation App Token Support
As per conversation https://github.com/MIT-LCP/physionet-build/issues/1047#issuecomment-3285413494, we want to add fine-grained token authorization. This PR introduces granular OAuth token scopes for the Annotations API, replacing the broad annotations:view and annotations:edit scopes with resource-specific permissions.
Changes
1. Fine-Grained OAuth Scopes (physionet/settings/base.py)
Replaced broad scopes with granular permissions:
-
Collections:
annotations:collections:readandannotations:collections:write -
Types:
annotations:types:readandannotations:types:write -
Annotations:
annotations:annotations:readandannotations:annotations:write
2. New Permission Classes (annotation/views.py)
- Added
AnnotationsCollectionsScopefor collection endpoints - Added
AnnotationsTypesScopefor type endpoints - Updated views to use resource-specific permission classes instead of generic
AnnotationsScope
3. New Collection Read Endpoint (annotation/views.py, annotation/urls.py)
Added AnnotationCollectionReadAPIView with:
- GET endpoint at
/annotations/collection/<slug>/ - Optimized queryset with
prefetch_related()to reduce database queries - Proper scope validation (
annotations:collections:read)
4. Serializer Improvements (annotation/serializers.py)
-
Moved
AnnotationCollectionSerializerto end of file (afterAnnotationSerializerdefinition) -
Added
annotationsfield toAnnotationCollectionSerializerto include related annotations in collection responses -
Fixed
AnnotationSerializer.to_representation()to properly access child location models:-
instance.location.textspanlocationinstead ofinstance.location -
instance.location.timeseriesintervallocationinstead ofinstance.location -
instance.location.imagebboxlocationinstead ofinstance.location
-
5. Updated Tests (annotation/tests.py)
- Added
_read_annotation_collection()helper method - Updated all tests to use new granular scopes
- Added
test_read_annotation_collection_correct_scope()to verify collection read endpoint with proper scope handling
All existing tests updated and passing with new scope requirements. New test added for collection read endpoint.