|
1 | | -from datetime import datetime |
| 1 | +from datetime import date, datetime, timedelta |
2 | 2 | import json |
3 | 3 | import logging |
4 | 4 | from typing import Any, Literal |
|
8 | 8 | from django.contrib.gis.geos import Point, Polygon |
9 | 9 | from django.db.models import Count |
10 | 10 | from django.http import HttpRequest, JsonResponse |
| 11 | +from django.utils.timezone import now |
11 | 12 | from ninja import Query, Router, Schema |
12 | 13 | from ninja.pagination import paginate |
13 | 14 |
|
14 | | -from bats_ai.core.models import ProcessingTask, ProcessingTaskType |
| 15 | +from bats_ai.core.models import ExportedAnnotationFile, ProcessingTask, ProcessingTaskType |
15 | 16 | from bats_ai.core.models.nabat import NABatRecording, NABatRecordingAnnotation |
| 17 | +from bats_ai.tasks.nabat.nabat_export_task import export_nabat_annotations_task |
16 | 18 | from bats_ai.tasks.nabat.nabat_update_species import update_nabat_species |
17 | 19 |
|
18 | 20 | logger = logging.getLogger(__name__) |
@@ -194,3 +196,25 @@ def get_stats(request: HttpRequest): |
194 | 196 | 'total_recordings': total_recordings, |
195 | 197 | 'total_annotations': total_annotations, |
196 | 198 | } |
| 199 | + |
| 200 | + |
| 201 | +class AnnotationExportRequest(Schema): |
| 202 | + start_date: date | None = None |
| 203 | + end_date: date | None = None |
| 204 | + recording_ids: list[int] | None = None |
| 205 | + usernames: list[str] | None = None |
| 206 | + min_confidence: float | None = None |
| 207 | + max_confidence: float | None = None |
| 208 | + |
| 209 | + |
| 210 | +@router.post( |
| 211 | + '/export', |
| 212 | +) |
| 213 | +def export_annotations(request: HttpRequest, filters: AnnotationExportRequest): |
| 214 | + export = ExportedAnnotationFile.objects.create( |
| 215 | + filters_applied=filters.dict(), |
| 216 | + status='pending', |
| 217 | + expires_at=now() + timedelta(hours=24), |
| 218 | + ) |
| 219 | + export_nabat_annotations_task.delay(filters.dict(), export.id) |
| 220 | + return export.id |
0 commit comments