-
Notifications
You must be signed in to change notification settings - Fork 3
Convert report API to ADRF views (prep for async embedding trigger) #230
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
Open
samuelvkwong
wants to merge
29
commits into
main
Choose a base branch
from
feat/adrf-views
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
693e642
docs(reports): spec for ADRF report views refactor
samuelvkwong 5e4a1be
docs(reports): implementation plan for ADRF report views
samuelvkwong 31111bd
refactor(reports): extract bulk_upsert_reports into radis/reports/api…
samuelvkwong 59d0f28
test(reports): add end-to-end report API tests + async-shape guards
samuelvkwong 5b30886
feat(reports): add ADRF report views (not yet wired into urls)
samuelvkwong 00232c9
feat(reports): swap report API URLs to ADRF views; remove ReportViewSet
samuelvkwong d6d5e26
fix(reports): address Gemini async-safety findings on PR #230
samuelvkwong 86ac291
test(reports): use transaction=True on HTTP tests against ADRF views
samuelvkwong 07b8751
test(reports): migrate HTTP tests to AsyncClient
samuelvkwong 06cbfd3
test(reports): wrap sync ORM helpers with sync_to_async in async tests
samuelvkwong 8329d45
docs(reports): correct ADRF spec motivation — inline embedding, not e…
samuelvkwong 7215e2f
refactor(reports): collapse three ADRF views into one ReportViewSet
samuelvkwong 6961339
refactor(reports): keep viewsets.py naming, fold bulk helper back in
samuelvkwong 590cfab
fix(reports): use adrf.routers.DefaultRouter so dispatch reaches asyn…
samuelvkwong 103f36b
Merge branch 'main' into feat/adrf-views
samuelvkwong 0028280
refactor(reports): split async coordination from sync atomic helpers
samuelvkwong 7b4f549
fix(reports): drop redundant @transaction.atomic on acreate/aupdate h…
samuelvkwong d47a70a
refactor(reports): extract async write operations into operations.py
samuelvkwong ed9b0e0
refactor(reports): make ReportSerializer async-native (acreate/aupdate)
samuelvkwong a54f203
refactor(reports): move atomic transaction ownership into the serializer
samuelvkwong e4ef7a8
refactor(reports): wrap bulk_upsert_reports CPU phases in sync_to_async
samuelvkwong c01c550
docs(reports): correct viewsets.py async-roadmap comment + document e…
samuelvkwong 580c4e5
docs(reports): trim async-roadmap detail from viewsets.py docstring
samuelvkwong 6ec0df0
docs(reports): condense async-roadmap implication paragraph
samuelvkwong dcbe6af
refactor(reports): move BULK_DB_BATCH_SIZE to settings
samuelvkwong f54f0ef
docs(reports): redistribute comments to reflect current architecture
samuelvkwong e272a4a
refactor(reports): demote REPORTS_BULK_DB_BATCH_SIZE from env to code…
samuelvkwong 76dc20f
fix(reports): wrap transaction.on_commit in sync_to_async for acreate…
samuelvkwong 7ed69b5
docs(reports): trim migration-flavored comments
samuelvkwong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
223 changes: 223 additions & 0 deletions
223
docs/superpowers/specs/2026-06-08-adrf-report-views-design.md
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,94 @@ | ||
| """Async domain operations for the report API. | ||
|
|
||
| Each function is a pure async write operation using native async ORM | ||
| methods (`aget_or_create`, `acreate`, `aset`, `asave`, `adelete`, ...). | ||
| None of these functions open their own transactions — atomicity is the | ||
| caller's responsibility. The caller is a sync helper decorated with | ||
| `@sync_to_async(thread_sensitive=True)` + `@transaction.atomic` that | ||
| invokes these operations via `async_to_sync(...)`. | ||
|
|
||
| The `thread_sensitive=True` chain ensures the outer sync helper and any | ||
| nested `sync_to_async` adapters (which Django's `a*` ORM methods use | ||
| internally) all run on the same Django thread, so the transaction | ||
| context held by the outer helper applies to every write performed by | ||
| these operations. | ||
| """ | ||
| import logging | ||
| from typing import Any | ||
|
|
||
| from ..models import Language, Metadata, Modality, Report | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def create_report_from_validated( | ||
| validated_data: dict[str, Any], | ||
| ) -> Report: | ||
| """Create a Report and its nested associations from validated payload. | ||
|
|
||
| Pops `language`, `groups`, `metadata`, `modalities` out of | ||
| `validated_data` and uses the remaining keys as direct Report fields. | ||
| """ | ||
| language = validated_data.pop("language") | ||
| groups = validated_data.pop("groups") | ||
| metadata = validated_data.pop("metadata") | ||
| modalities = validated_data.pop("modalities") | ||
|
|
||
| language_instance, _ = await Language.objects.aget_or_create(**language) | ||
| report = await Report.objects.acreate( | ||
| **validated_data, language=language_instance | ||
| ) | ||
|
|
||
| await report.groups.aset(groups) | ||
|
|
||
| for item in metadata: | ||
| await Metadata.objects.acreate(report=report, **item) | ||
|
|
||
| modality_instances: list[Modality] = [] | ||
| for modality in modalities: | ||
| instance, _ = await Modality.objects.aget_or_create(**modality) | ||
| modality_instances.append(instance) | ||
| await report.modalities.aset(modality_instances) | ||
|
|
||
| return report | ||
|
|
||
|
|
||
| async def update_report_from_validated( | ||
| report: Report, validated_data: dict[str, Any] | ||
| ) -> Report: | ||
| """Replace all mutable fields and nested associations on an existing Report. | ||
|
|
||
| Matches the legacy `ReportSerializer.update` semantics: metadata is | ||
| fully replaced (delete + recreate), modalities and groups are reset | ||
| to the provided sets. | ||
| """ | ||
| language = validated_data.pop("language") | ||
| groups = validated_data.pop("groups") | ||
| metadata = validated_data.pop("metadata") | ||
| modalities = validated_data.pop("modalities") | ||
|
|
||
| language_instance = await Language.objects.aget(**language) | ||
| report.language = language_instance | ||
| for attr, value in validated_data.items(): | ||
| setattr(report, attr, value) | ||
| await report.asave() | ||
|
|
||
| await report.groups.aset(groups) | ||
|
|
||
| await report.metadata.all().adelete() | ||
| for item in metadata: | ||
| await Metadata.objects.acreate(report=report, **item) | ||
|
|
||
| await report.modalities.aclear() | ||
| modality_instances: list[Modality] = [] | ||
| for modality in modalities: | ||
| instance, _ = await Modality.objects.aget_or_create(**modality) | ||
| modality_instances.append(instance) | ||
| await report.modalities.aset(modality_instances) | ||
|
|
||
| return report | ||
|
|
||
|
|
||
| async def delete_report(report: Report) -> None: | ||
| """Delete a single Report row.""" | ||
| await report.adelete() |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.