Spaces:
Sleeping
feat: Production hardening - CI/CD, integration tests, auth externalization
Browse files- Add GitHub Actions CI pipeline for automated testing
- Add 3 integration tests for validation write, conflict detection, and filtering
- Extend AuthService with 3 public APIs for user/project management
- list_usernames(include_inactive=False)
- upsert_user_project_role(username, project_slug, role)
- remove_user_project_role(username, project_slug)
- Refactor AdminPanel to use only public AuthService APIs
- Externalize bootstrap configuration via JSON files
- Add RuntimeConfig fields for projects_file_path, user_access_file_path, enable_demo_bootstrap
- Add app_factory bootstrap loaders for projects and user access JSON
- Move hardcoded demo data to configurable fallback
- Update documentation with new environment variables and JSON examples
- Fix: Remove TODO from admin_panel, remove dead _user_project_assignments field
- Test coverage: +7 new tests (3 integration + 4 bootstrap/auth unit tests)
All 95 tests passing, no regressions.
- .github/workflows/ci.yml +27 -0
- README.md +70 -1
- docs/END_TO_END_QA_CHECKLIST_SPRINT4.md +3 -3
- src/auth/auth_service.py +56 -6
- src/config/__init__.py +1 -0
- src/config/runtime_config.py +48 -0
- src/ui/admin_panel.py +6 -15
- src/ui/app_factory.py +1078 -171
- tests/integration/test_validation_flow_integration.py +195 -0
- tests/unit/test_app_factory_audio_helpers.py +224 -22
- tests/unit/test_auth_service.py +36 -7
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: CI
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches: [main]
|
| 6 |
+
pull_request:
|
| 7 |
+
branches: [main]
|
| 8 |
+
|
| 9 |
+
jobs:
|
| 10 |
+
test:
|
| 11 |
+
runs-on: ubuntu-latest
|
| 12 |
+
steps:
|
| 13 |
+
- name: Checkout
|
| 14 |
+
uses: actions/checkout@v4
|
| 15 |
+
|
| 16 |
+
- name: Setup Python
|
| 17 |
+
uses: actions/setup-python@v5
|
| 18 |
+
with:
|
| 19 |
+
python-version: '3.11'
|
| 20 |
+
|
| 21 |
+
- name: Install dependencies
|
| 22 |
+
run: |
|
| 23 |
+
python -m pip install --upgrade pip
|
| 24 |
+
pip install -r requirements.txt
|
| 25 |
+
|
| 26 |
+
- name: Run tests
|
| 27 |
+
run: pytest -q
|
|
@@ -40,6 +40,58 @@ python app.py
|
|
| 40 |
|
| 41 |
Default port is 7860.
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
## Typical Validation Flow
|
| 44 |
|
| 45 |
1. Login with a valid user.
|
|
@@ -95,7 +147,16 @@ Stop the existing process using that port, or launch after setting a different `
|
|
| 95 |
|
| 96 |
### Login works but no project appears
|
| 97 |
|
| 98 |
-
Your user likely has no project assignment.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
### Audio does not load for a detection
|
| 101 |
|
|
@@ -108,3 +169,11 @@ Check that:
|
|
| 108 |
### I get conflict messages while validating
|
| 109 |
|
| 110 |
This means another validator updated the same detection first. Refresh and reapply your decision on the newest version.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
Default port is 7860.
|
| 42 |
|
| 43 |
+
Optional runtime configuration:
|
| 44 |
+
|
| 45 |
+
- `BIRDNET_DETECTIONS_FILE`: path to JSON file with detections grouped by project slug.
|
| 46 |
+
- `BIRDNET_VALIDATIONS_DIR`: custom directory for append-only validation events and snapshot files.
|
| 47 |
+
- `BIRDNET_PAGE_SIZE`: queue page size (default: `25`).
|
| 48 |
+
- `BIRDNET_PROJECTS_FILE`: JSON file with project catalog used at app startup.
|
| 49 |
+
- `BIRDNET_USER_ACCESS_FILE`: JSON file mapping users to project roles (`admin`/`validator`).
|
| 50 |
+
- `BIRDNET_ENABLE_DEMO_BOOTSTRAP`: set to `true` only for local/demo mode to load built-in sample users/projects.
|
| 51 |
+
|
| 52 |
+
JSON seed format examples:
|
| 53 |
+
|
| 54 |
+
```json
|
| 55 |
+
{
|
| 56 |
+
"kenya-2024": [
|
| 57 |
+
{
|
| 58 |
+
"detection_key": "0000000000001001",
|
| 59 |
+
"audio_id": "audio_1001",
|
| 60 |
+
"scientific_name": "Cyanocorax cyanopogon",
|
| 61 |
+
"confidence": 0.91,
|
| 62 |
+
"start_time": 0.0,
|
| 63 |
+
"end_time": 1.0
|
| 64 |
+
}
|
| 65 |
+
]
|
| 66 |
+
}
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
Projects bootstrap file example (`BIRDNET_PROJECTS_FILE`):
|
| 70 |
+
|
| 71 |
+
```json
|
| 72 |
+
[
|
| 73 |
+
{
|
| 74 |
+
"project_slug": "kenya-2024",
|
| 75 |
+
"name": "Kenya Survey 2024",
|
| 76 |
+
"dataset_repo_id": "org/kenya-2024-dataset",
|
| 77 |
+
"active": true
|
| 78 |
+
}
|
| 79 |
+
]
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
User access bootstrap file example (`BIRDNET_USER_ACCESS_FILE`):
|
| 83 |
+
|
| 84 |
+
```json
|
| 85 |
+
{
|
| 86 |
+
"admin_user": {
|
| 87 |
+
"kenya-2024": "admin"
|
| 88 |
+
},
|
| 89 |
+
"validator_a": {
|
| 90 |
+
"kenya-2024": "validator"
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
## Typical Validation Flow
|
| 96 |
|
| 97 |
1. Login with a valid user.
|
|
|
|
| 147 |
|
| 148 |
### Login works but no project appears
|
| 149 |
|
| 150 |
+
Your user likely has no project assignment. Verify `BIRDNET_PROJECTS_FILE` and `BIRDNET_USER_ACCESS_FILE`, or add project access through the admin flow.
|
| 151 |
+
|
| 152 |
+
### Seed warning appears in Validation tab
|
| 153 |
+
|
| 154 |
+
If you see a seed warning banner:
|
| 155 |
+
|
| 156 |
+
1. Verify `BIRDNET_DETECTIONS_FILE` points to an existing file.
|
| 157 |
+
2. Confirm the file is valid UTF-8 JSON.
|
| 158 |
+
3. Confirm each project maps to a list of detections, or each list item includes `project_slug`.
|
| 159 |
+
4. If needed, unset `BIRDNET_DETECTIONS_FILE` to fall back to default demo detections.
|
| 160 |
|
| 161 |
### Audio does not load for a detection
|
| 162 |
|
|
|
|
| 169 |
### I get conflict messages while validating
|
| 170 |
|
| 171 |
This means another validator updated the same detection first. Refresh and reapply your decision on the newest version.
|
| 172 |
+
|
| 173 |
+
## Recent Updates
|
| 174 |
+
|
| 175 |
+
- Integrated Validation tab flow in the multi-project app (no placeholder stage).
|
| 176 |
+
- Added project-scoped queue badge (`Queue: N`) and improved queue context feedback.
|
| 177 |
+
- Added seed-file warning banner with actionable remediation guidance in Validation.
|
| 178 |
+
- Standardized all user-facing UI text and feedback messages in English.
|
| 179 |
+
- Updated authentication/session timestamps to timezone-aware UTC handling.
|
|
@@ -45,7 +45,7 @@ Pass criteria:
|
|
| 45 |
|
| 46 |
Pass criteria:
|
| 47 |
- Status changes to ready for selected project.
|
| 48 |
-
- Message "
|
| 49 |
|
| 50 |
### Smoke Evidence (copy/paste)
|
| 51 |
1. Date/time:
|
|
@@ -88,9 +88,9 @@ Expected access matrix:
|
|
| 88 |
| E2E-10 | ACL isolation | Test suite available | Run ACL tests | ACL tests pass (project isolation enforced) | PASS |
|
| 89 |
|
| 90 |
## Execution Notes
|
| 91 |
-
- This checklist is aligned with fixes applied in src/ui/app_factory.py.
|
| 92 |
- The app uses Gradio and callback-driven state transitions.
|
| 93 |
-
- ACL behavior is also covered by unit tests in tests/unit/test_acl_enforcement.py.
|
| 94 |
|
| 95 |
## Evidence Commands
|
| 96 |
1. python -m pytest tests/unit/test_acl_enforcement.py -q
|
|
|
|
| 45 |
|
| 46 |
Pass criteria:
|
| 47 |
- Status changes to ready for selected project.
|
| 48 |
+
- Message "Project not selected" no longer appears after valid selection.
|
| 49 |
|
| 50 |
### Smoke Evidence (copy/paste)
|
| 51 |
1. Date/time:
|
|
|
|
| 88 |
| E2E-10 | ACL isolation | Test suite available | Run ACL tests | ACL tests pass (project isolation enforced) | PASS |
|
| 89 |
|
| 90 |
## Execution Notes
|
| 91 |
+
- This checklist is aligned with fixes applied in `src/ui/app_factory.py`.
|
| 92 |
- The app uses Gradio and callback-driven state transitions.
|
| 93 |
+
- ACL behavior is also covered by unit tests in `tests/unit/test_acl_enforcement.py`.
|
| 94 |
|
| 95 |
## Evidence Commands
|
| 96 |
1. python -m pytest tests/unit/test_acl_enforcement.py -q
|
|
@@ -1,7 +1,7 @@
|
|
| 1 |
"""AuthService: User authentication, session management, and project ACL."""
|
| 2 |
|
| 3 |
from dataclasses import dataclass, field
|
| 4 |
-
from datetime import datetime, timedelta
|
| 5 |
from typing import Dict, List, Optional
|
| 6 |
import uuid
|
| 7 |
|
|
@@ -31,12 +31,12 @@ class Session:
|
|
| 31 |
|
| 32 |
def is_expired(self) -> bool:
|
| 33 |
"""Check if session has expired."""
|
| 34 |
-
return datetime.
|
| 35 |
|
| 36 |
def update_activity(self, ttl_minutes: int = 120) -> None:
|
| 37 |
"""Update last activity timestamp and extend expiration."""
|
| 38 |
-
self.last_activity = datetime.
|
| 39 |
-
self.expires_at = datetime.
|
| 40 |
|
| 41 |
|
| 42 |
class AuthService:
|
|
@@ -67,6 +67,56 @@ class AuthService:
|
|
| 67 |
is_active=True,
|
| 68 |
)
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def login(self, username: str) -> Optional[Session]:
|
| 71 |
"""Authenticate a user and create a new session.
|
| 72 |
|
|
@@ -91,7 +141,7 @@ class AuthService:
|
|
| 91 |
break
|
| 92 |
|
| 93 |
session_id = str(uuid.uuid4())
|
| 94 |
-
now = datetime.
|
| 95 |
session = Session(
|
| 96 |
session_id=session_id,
|
| 97 |
username=username,
|
|
@@ -182,7 +232,7 @@ class AuthService:
|
|
| 182 |
|
| 183 |
def cleanup_expired_sessions(self) -> None:
|
| 184 |
"""Remove all expired sessions (maintenance cleanup)."""
|
| 185 |
-
now = datetime.
|
| 186 |
expired = [
|
| 187 |
sid for sid, session in self._sessions.items() if session.is_expired()
|
| 188 |
]
|
|
|
|
| 1 |
"""AuthService: User authentication, session management, and project ACL."""
|
| 2 |
|
| 3 |
from dataclasses import dataclass, field
|
| 4 |
+
from datetime import UTC, datetime, timedelta
|
| 5 |
from typing import Dict, List, Optional
|
| 6 |
import uuid
|
| 7 |
|
|
|
|
| 31 |
|
| 32 |
def is_expired(self) -> bool:
|
| 33 |
"""Check if session has expired."""
|
| 34 |
+
return datetime.now(UTC) > self.expires_at
|
| 35 |
|
| 36 |
def update_activity(self, ttl_minutes: int = 120) -> None:
|
| 37 |
"""Update last activity timestamp and extend expiration."""
|
| 38 |
+
self.last_activity = datetime.now(UTC)
|
| 39 |
+
self.expires_at = datetime.now(UTC) + timedelta(minutes=ttl_minutes)
|
| 40 |
|
| 41 |
|
| 42 |
class AuthService:
|
|
|
|
| 67 |
is_active=True,
|
| 68 |
)
|
| 69 |
|
| 70 |
+
def list_usernames(self, include_inactive: bool = False) -> List[str]:
|
| 71 |
+
"""List registered usernames.
|
| 72 |
+
|
| 73 |
+
Args:
|
| 74 |
+
include_inactive: Include disabled users when True
|
| 75 |
+
|
| 76 |
+
Returns:
|
| 77 |
+
Sorted list of usernames
|
| 78 |
+
"""
|
| 79 |
+
usernames = []
|
| 80 |
+
for username, access in self._user_access.items():
|
| 81 |
+
if include_inactive or access.is_active:
|
| 82 |
+
usernames.append(username)
|
| 83 |
+
return sorted(usernames)
|
| 84 |
+
|
| 85 |
+
def upsert_user_project_role(self, username: str, project_slug: str, role: Role) -> None:
|
| 86 |
+
"""Create/update user assignment for a project.
|
| 87 |
+
|
| 88 |
+
Args:
|
| 89 |
+
username: User name
|
| 90 |
+
project_slug: Project slug
|
| 91 |
+
role: Role to apply for this project
|
| 92 |
+
"""
|
| 93 |
+
access = self._user_access.get(username)
|
| 94 |
+
if access is None:
|
| 95 |
+
access = UserProjectAccess(username=username, project_slugs={}, is_active=True)
|
| 96 |
+
self._user_access[username] = access
|
| 97 |
+
|
| 98 |
+
access.project_slugs[project_slug] = role
|
| 99 |
+
|
| 100 |
+
def remove_user_project_role(self, username: str, project_slug: str) -> bool:
|
| 101 |
+
"""Remove a user assignment from a project.
|
| 102 |
+
|
| 103 |
+
Args:
|
| 104 |
+
username: User name
|
| 105 |
+
project_slug: Project slug
|
| 106 |
+
|
| 107 |
+
Returns:
|
| 108 |
+
True when assignment existed and was removed
|
| 109 |
+
"""
|
| 110 |
+
access = self._user_access.get(username)
|
| 111 |
+
if access is None:
|
| 112 |
+
return False
|
| 113 |
+
|
| 114 |
+
if project_slug not in access.project_slugs:
|
| 115 |
+
return False
|
| 116 |
+
|
| 117 |
+
del access.project_slugs[project_slug]
|
| 118 |
+
return True
|
| 119 |
+
|
| 120 |
def login(self, username: str) -> Optional[Session]:
|
| 121 |
"""Authenticate a user and create a new session.
|
| 122 |
|
|
|
|
| 141 |
break
|
| 142 |
|
| 143 |
session_id = str(uuid.uuid4())
|
| 144 |
+
now = datetime.now(UTC)
|
| 145 |
session = Session(
|
| 146 |
session_id=session_id,
|
| 147 |
username=username,
|
|
|
|
| 232 |
|
| 233 |
def cleanup_expired_sessions(self) -> None:
|
| 234 |
"""Remove all expired sessions (maintenance cleanup)."""
|
| 235 |
+
now = datetime.now(UTC)
|
| 236 |
expired = [
|
| 237 |
sid for sid, session in self._sessions.items() if session.is_expired()
|
| 238 |
]
|
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Runtime configuration helpers."""
|
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@dataclass(frozen=True)
|
| 8 |
+
class RuntimeConfig:
|
| 9 |
+
detection_seed_path: str | None
|
| 10 |
+
validation_base_dir: str
|
| 11 |
+
page_size: int
|
| 12 |
+
projects_file_path: str | None
|
| 13 |
+
user_access_file_path: str | None
|
| 14 |
+
enable_demo_bootstrap: bool
|
| 15 |
+
|
| 16 |
+
@classmethod
|
| 17 |
+
def from_env(cls) -> "RuntimeConfig":
|
| 18 |
+
detection_seed_path = (os.getenv("BIRDNET_DETECTIONS_FILE") or "").strip() or None
|
| 19 |
+
|
| 20 |
+
validation_base_dir = (
|
| 21 |
+
os.getenv("BIRDNET_VALIDATIONS_DIR")
|
| 22 |
+
or str(Path(tempfile.gettempdir()) / "birdnet-validator-validations")
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
raw_page_size = (os.getenv("BIRDNET_PAGE_SIZE") or "").strip()
|
| 26 |
+
page_size = 25
|
| 27 |
+
if raw_page_size:
|
| 28 |
+
try:
|
| 29 |
+
parsed = int(raw_page_size)
|
| 30 |
+
if parsed > 0:
|
| 31 |
+
page_size = parsed
|
| 32 |
+
except ValueError:
|
| 33 |
+
page_size = 25
|
| 34 |
+
|
| 35 |
+
projects_file_path = (os.getenv("BIRDNET_PROJECTS_FILE") or "").strip() or None
|
| 36 |
+
user_access_file_path = (os.getenv("BIRDNET_USER_ACCESS_FILE") or "").strip() or None
|
| 37 |
+
|
| 38 |
+
raw_enable_demo_bootstrap = (os.getenv("BIRDNET_ENABLE_DEMO_BOOTSTRAP") or "").strip().lower()
|
| 39 |
+
enable_demo_bootstrap = raw_enable_demo_bootstrap in {"1", "true", "yes", "on"}
|
| 40 |
+
|
| 41 |
+
return cls(
|
| 42 |
+
detection_seed_path=detection_seed_path,
|
| 43 |
+
validation_base_dir=validation_base_dir,
|
| 44 |
+
page_size=page_size,
|
| 45 |
+
projects_file_path=projects_file_path,
|
| 46 |
+
user_access_file_path=user_access_file_path,
|
| 47 |
+
enable_demo_bootstrap=enable_demo_bootstrap,
|
| 48 |
+
)
|
|
@@ -19,7 +19,6 @@ class AdminPanelManager:
|
|
| 19 |
"""
|
| 20 |
self.auth_service = auth_service
|
| 21 |
self._projects: dict[str, Project] = {} # project_slug -> Project
|
| 22 |
-
self._user_project_assignments: dict[str, dict[str, str]] = {} # username -> {project_slug -> role}
|
| 23 |
|
| 24 |
def register_project(self, project: Project) -> bool:
|
| 25 |
"""Register a new project (idempotent).
|
|
@@ -73,7 +72,7 @@ class AdminPanelManager:
|
|
| 73 |
List of dicts with username and role
|
| 74 |
"""
|
| 75 |
result = []
|
| 76 |
-
for username in self.auth_service.
|
| 77 |
role = self.auth_service.get_user_role_for_project(username, project_slug)
|
| 78 |
if role is not None:
|
| 79 |
result.append({"username": username, "role": role.value})
|
|
@@ -99,12 +98,7 @@ class AdminPanelManager:
|
|
| 99 |
if role not in ["admin", "validator"]:
|
| 100 |
return False, f"Invalid role: {role}"
|
| 101 |
|
| 102 |
-
|
| 103 |
-
if username not in self.auth_service._user_access:
|
| 104 |
-
self.auth_service.register_user_project_access(username, {})
|
| 105 |
-
|
| 106 |
-
access = self.auth_service._user_access[username]
|
| 107 |
-
access.project_slugs[project_slug] = Role(role)
|
| 108 |
|
| 109 |
return True, f"✅ Assigned {username} to {project_slug} as {role}"
|
| 110 |
|
|
@@ -118,14 +112,14 @@ class AdminPanelManager:
|
|
| 118 |
Returns:
|
| 119 |
Tuple of (success, message)
|
| 120 |
"""
|
| 121 |
-
if username not in self.auth_service.
|
| 122 |
return False, f"User '{username}' not found"
|
| 123 |
|
| 124 |
-
|
| 125 |
-
if
|
| 126 |
return False, f"User '{username}' is not assigned to project '{project_slug}'"
|
| 127 |
|
| 128 |
-
|
| 129 |
return True, f"✅ Removed {username} from {project_slug}"
|
| 130 |
|
| 131 |
def toggleproject_active(self, project_slug: str, active: bool) -> Tuple[bool, str]:
|
|
@@ -192,9 +186,6 @@ def create_admin_panel(admin_manager: AdminPanelManager, current_session: Sessio
|
|
| 192 |
|
| 193 |
project_message = gr.Markdown()
|
| 194 |
|
| 195 |
-
# TODO: Implement create project button
|
| 196 |
-
# create_project_button = gr.Button("Create Project", variant="primary")
|
| 197 |
-
|
| 198 |
# Projects list
|
| 199 |
with gr.Row():
|
| 200 |
refresh_projects_button = gr.Button("Refresh Projects List")
|
|
|
|
| 19 |
"""
|
| 20 |
self.auth_service = auth_service
|
| 21 |
self._projects: dict[str, Project] = {} # project_slug -> Project
|
|
|
|
| 22 |
|
| 23 |
def register_project(self, project: Project) -> bool:
|
| 24 |
"""Register a new project (idempotent).
|
|
|
|
| 72 |
List of dicts with username and role
|
| 73 |
"""
|
| 74 |
result = []
|
| 75 |
+
for username in self.auth_service.list_usernames(include_inactive=True):
|
| 76 |
role = self.auth_service.get_user_role_for_project(username, project_slug)
|
| 77 |
if role is not None:
|
| 78 |
result.append({"username": username, "role": role.value})
|
|
|
|
| 98 |
if role not in ["admin", "validator"]:
|
| 99 |
return False, f"Invalid role: {role}"
|
| 100 |
|
| 101 |
+
self.auth_service.upsert_user_project_role(username, project_slug, Role(role))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
return True, f"✅ Assigned {username} to {project_slug} as {role}"
|
| 104 |
|
|
|
|
| 112 |
Returns:
|
| 113 |
Tuple of (success, message)
|
| 114 |
"""
|
| 115 |
+
if username not in self.auth_service.list_usernames(include_inactive=True):
|
| 116 |
return False, f"User '{username}' not found"
|
| 117 |
|
| 118 |
+
role = self.auth_service.get_user_role_for_project(username, project_slug)
|
| 119 |
+
if role is None:
|
| 120 |
return False, f"User '{username}' is not assigned to project '{project_slug}'"
|
| 121 |
|
| 122 |
+
_ = self.auth_service.remove_user_project_role(username, project_slug)
|
| 123 |
return True, f"✅ Removed {username} from {project_slug}"
|
| 124 |
|
| 125 |
def toggleproject_active(self, project_slug: str, active: bool) -> Tuple[bool, str]:
|
|
|
|
| 186 |
|
| 187 |
project_message = gr.Markdown()
|
| 188 |
|
|
|
|
|
|
|
|
|
|
| 189 |
# Projects list
|
| 190 |
with gr.Row():
|
| 191 |
refresh_projects_button = gr.Button("Refresh Projects List")
|
|
@@ -1,9 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
from datetime import date, datetime
|
| 4 |
from pathlib import Path
|
| 5 |
from typing import Protocol
|
| 6 |
|
|
|
|
| 7 |
from src.cache.ephemeral_cache_manager import EphemeralCacheManager
|
| 8 |
from src.domain.models import Detection, Project, Role
|
| 9 |
from src.repositories.append_only_validation_repository import AppendOnlyValidationRepository, OptimisticLockError
|
|
@@ -13,8 +15,7 @@ from src.services.detection_queue_service import DetectionQueueService
|
|
| 13 |
from src.services.validation_service import ValidationService
|
| 14 |
from src.auth.auth_service import AuthService
|
| 15 |
from src.ui.login_page import create_login_page
|
| 16 |
-
from src.ui.
|
| 17 |
-
from src.ui.admin_panel import AdminPanelManager, create_admin_panel
|
| 18 |
|
| 19 |
|
| 20 |
class _AudioFetchResultProtocol(Protocol):
|
|
@@ -61,43 +62,314 @@ class _QueueServiceProtocol(Protocol):
|
|
| 61 |
|
| 62 |
|
| 63 |
def _seed_service() -> DetectionQueueService:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
repo = InMemoryDetectionRepository()
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
Detection(
|
| 67 |
-
detection_key="
|
| 68 |
-
audio_id="
|
| 69 |
scientific_name="Cyanocorax cyanopogon",
|
| 70 |
confidence=0.93,
|
| 71 |
start_time=1.2,
|
| 72 |
end_time=2.5,
|
| 73 |
),
|
| 74 |
Detection(
|
| 75 |
-
detection_key="
|
| 76 |
-
audio_id="
|
| 77 |
scientific_name="Ramphastos toco",
|
| 78 |
confidence=0.88,
|
| 79 |
start_time=0.8,
|
| 80 |
end_time=2.1,
|
| 81 |
),
|
| 82 |
Detection(
|
| 83 |
-
detection_key="
|
| 84 |
-
audio_id="
|
| 85 |
scientific_name="Cyanocorax cyanopogon",
|
| 86 |
confidence=0.72,
|
| 87 |
start_time=3.1,
|
| 88 |
end_time=4.0,
|
| 89 |
),
|
| 90 |
Detection(
|
| 91 |
-
detection_key="
|
| 92 |
-
audio_id="
|
| 93 |
scientific_name="Psarocolius decumanus",
|
| 94 |
confidence=0.67,
|
| 95 |
start_time=5.0,
|
| 96 |
end_time=6.3,
|
| 97 |
),
|
| 98 |
]
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
|
| 103 |
def _page_to_table(
|
|
@@ -107,6 +379,7 @@ def _page_to_table(
|
|
| 107 |
page: int,
|
| 108 |
scientific_name: str,
|
| 109 |
min_confidence: float,
|
|
|
|
| 110 |
validator_filter: str = "",
|
| 111 |
status_filter: str = "all",
|
| 112 |
updated_after: object = None,
|
|
@@ -115,9 +388,9 @@ def _page_to_table(
|
|
| 115 |
):
|
| 116 |
filter_name = scientific_name.strip() if scientific_name.strip() else None
|
| 117 |
page_obj = service.get_page(
|
| 118 |
-
project_slug=
|
| 119 |
page=page,
|
| 120 |
-
page_size=
|
| 121 |
scientific_name=filter_name,
|
| 122 |
min_confidence=min_confidence,
|
| 123 |
)
|
|
@@ -189,12 +462,40 @@ def _page_to_table(
|
|
| 189 |
if show_conflicts_only:
|
| 190 |
rows = [row for row in rows if str(row[8]) == "CONFLICT"]
|
| 191 |
|
| 192 |
-
status = f"
|
| 193 |
if show_conflicts_only:
|
| 194 |
-
status = f"{status} |
|
| 195 |
return rows, status, page_obj.page
|
| 196 |
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
def _build_validation_report(snapshot_reader: _ValidationReadRepositoryProtocol, project_slug: str) -> str:
|
| 199 |
snapshot = snapshot_reader.load_current_snapshot(project_slug=project_slug)
|
| 200 |
events = snapshot_reader.list_events(project_slug=project_slug)
|
|
@@ -205,15 +506,15 @@ def _build_validation_report(snapshot_reader: _ValidationReadRepositoryProtocol,
|
|
| 205 |
counts[status_value] = counts.get(status_value, 0) + 1
|
| 206 |
|
| 207 |
parts = [
|
| 208 |
-
f"
|
| 209 |
-
f"
|
| 210 |
-
f"
|
| 211 |
]
|
| 212 |
if counts:
|
| 213 |
summary = ", ".join(f"{key}={value}" for key, value in sorted(counts.items()))
|
| 214 |
-
parts.append(f"
|
| 215 |
else:
|
| 216 |
-
parts.append("
|
| 217 |
return " | ".join(parts)
|
| 218 |
|
| 219 |
|
|
@@ -226,14 +527,14 @@ def _extract_audio_id(rows: object, selected_index: int) -> str:
|
|
| 226 |
normalized_rows = [list(item) for item in rows] if rows else []
|
| 227 |
|
| 228 |
if not normalized_rows:
|
| 229 |
-
raise ValueError("
|
| 230 |
if selected_index < 0 or selected_index >= len(normalized_rows):
|
| 231 |
-
raise ValueError("
|
| 232 |
|
| 233 |
value = normalized_rows[selected_index][1]
|
| 234 |
audio_id = str(value).strip()
|
| 235 |
if not audio_id:
|
| 236 |
-
raise ValueError("audio_id
|
| 237 |
return audio_id
|
| 238 |
|
| 239 |
|
|
@@ -246,14 +547,14 @@ def _extract_detection_key(rows: object, selected_index: int) -> str:
|
|
| 246 |
normalized_rows = [list(item) for item in rows] if rows else []
|
| 247 |
|
| 248 |
if not normalized_rows:
|
| 249 |
-
raise ValueError("
|
| 250 |
if selected_index < 0 or selected_index >= len(normalized_rows):
|
| 251 |
-
raise ValueError("
|
| 252 |
|
| 253 |
value = normalized_rows[selected_index][0]
|
| 254 |
detection_key = str(value).strip()
|
| 255 |
if not detection_key:
|
| 256 |
-
raise ValueError("detection_key
|
| 257 |
return detection_key
|
| 258 |
|
| 259 |
|
|
@@ -280,9 +581,9 @@ def _extract_expected_version(rows: object, selected_index: int) -> int:
|
|
| 280 |
normalized_rows = [list(item) for item in rows] if rows else []
|
| 281 |
|
| 282 |
if not normalized_rows:
|
| 283 |
-
raise ValueError("
|
| 284 |
if selected_index < 0 or selected_index >= len(normalized_rows):
|
| 285 |
-
raise ValueError("
|
| 286 |
|
| 287 |
value = normalized_rows[selected_index][7]
|
| 288 |
return int(value)
|
|
@@ -297,25 +598,25 @@ def _fetch_selected_audio(
|
|
| 297 |
) -> tuple[str | None, str, str]:
|
| 298 |
repo = dataset_repo.strip()
|
| 299 |
if not repo:
|
| 300 |
-
return None, "", "
|
| 301 |
|
| 302 |
try:
|
| 303 |
audio_id = _extract_audio_id(rows=rows, selected_index=selected_index)
|
| 304 |
result = audio_service.fetch(dataset_repo=repo, audio_id=audio_id)
|
| 305 |
-
status = f"Audio
|
| 306 |
return result.local_path, result.cache_key, status
|
| 307 |
except Exception as exc:
|
| 308 |
if previous_cache_key:
|
| 309 |
-
return None, previous_cache_key, f"
|
| 310 |
-
return None, "", f"
|
| 311 |
|
| 312 |
|
| 313 |
def _cleanup_selected_audio(audio_service: _AudioServiceProtocol, cache_key: str) -> tuple[str, str | None]:
|
| 314 |
if not cache_key:
|
| 315 |
-
return "
|
| 316 |
|
| 317 |
audio_service.cleanup_after_validation(cache_key=cache_key)
|
| 318 |
-
return "
|
| 319 |
|
| 320 |
|
| 321 |
def _save_selected_validation(
|
|
@@ -331,7 +632,7 @@ def _save_selected_validation(
|
|
| 331 |
) -> tuple[str, str, str | None]:
|
| 332 |
validator_name = validator.strip()
|
| 333 |
if not validator_name:
|
| 334 |
-
return "
|
| 335 |
|
| 336 |
try:
|
| 337 |
detection_key = _extract_detection_key(rows=rows, selected_index=selected_index)
|
|
@@ -346,17 +647,17 @@ def _save_selected_validation(
|
|
| 346 |
)
|
| 347 |
if cache_key:
|
| 348 |
audio_service.cleanup_after_validation(cache_key=cache_key)
|
| 349 |
-
return f"
|
| 350 |
except OptimisticLockError as exc:
|
| 351 |
return (
|
| 352 |
-
"
|
| 353 |
-
f"(detection_key={exc.detection_key},
|
| 354 |
-
"
|
| 355 |
cache_key,
|
| 356 |
None,
|
| 357 |
)
|
| 358 |
except Exception as exc:
|
| 359 |
-
return f"
|
| 360 |
|
| 361 |
|
| 362 |
def _save_selected_validation_with_refresh(
|
|
@@ -415,7 +716,7 @@ def _save_selected_validation_with_refresh(
|
|
| 415 |
else:
|
| 416 |
refreshed_index = 0
|
| 417 |
|
| 418 |
-
if "
|
| 419 |
conflict_key = selected_key
|
| 420 |
refreshed_rows, page_status, refreshed_page = _page_to_table(
|
| 421 |
service=queue_service,
|
|
@@ -432,7 +733,7 @@ def _save_selected_validation_with_refresh(
|
|
| 432 |
)
|
| 433 |
refreshed_index = _find_detection_row_index(refreshed_rows, selected_key) if selected_key else 0
|
| 434 |
pending_status_value = status_value
|
| 435 |
-
status = f"{save_status}
|
| 436 |
else:
|
| 437 |
conflict_key = ""
|
| 438 |
pending_status_value = ""
|
|
@@ -485,7 +786,7 @@ def _reapply_last_conflict_validation_with_refresh(
|
|
| 485 |
show_conflicts_only=show_conflicts_only,
|
| 486 |
)
|
| 487 |
return (
|
| 488 |
-
f"
|
| 489 |
cache_key,
|
| 490 |
None,
|
| 491 |
refreshed_rows,
|
|
@@ -539,7 +840,7 @@ def _batch_validate_conflicts(
|
|
| 539 |
"""Apply the same validation status to all visible conflicts in the table."""
|
| 540 |
validator_name = validator.strip()
|
| 541 |
if not validator_name:
|
| 542 |
-
return "
|
| 543 |
|
| 544 |
normalized_rows: list[list[object]]
|
| 545 |
if hasattr(rows, "values"):
|
|
@@ -548,11 +849,11 @@ def _batch_validate_conflicts(
|
|
| 548 |
normalized_rows = [list(item) for item in rows] if rows else []
|
| 549 |
|
| 550 |
if not normalized_rows:
|
| 551 |
-
return "
|
| 552 |
|
| 553 |
conflict_rows = [row for row in normalized_rows if str(row[8]) == "CONFLICT"]
|
| 554 |
if not conflict_rows:
|
| 555 |
-
return "
|
| 556 |
|
| 557 |
success_count = 0
|
| 558 |
failure_count = 0
|
|
@@ -592,7 +893,7 @@ def _batch_validate_conflicts(
|
|
| 592 |
show_conflicts_only=False,
|
| 593 |
)
|
| 594 |
|
| 595 |
-
summary = f"
|
| 596 |
status = f"{summary} | {page_status}"
|
| 597 |
|
| 598 |
return status, "", None, refreshed_rows, refreshed_page
|
|
@@ -618,11 +919,11 @@ def _batch_reapply_all_pending(
|
|
| 618 |
) -> tuple[str, str, str | None, list[list[object]], int]:
|
| 619 |
"""Reapply all pending validations (stored conflicts) with current version."""
|
| 620 |
if not pending_statuses:
|
| 621 |
-
return "
|
| 622 |
|
| 623 |
validator_name = validator.strip()
|
| 624 |
if not validator_name:
|
| 625 |
-
return "
|
| 626 |
|
| 627 |
success_count = 0
|
| 628 |
conflict_count = 0
|
|
@@ -663,7 +964,7 @@ def _batch_reapply_all_pending(
|
|
| 663 |
show_conflicts_only=False,
|
| 664 |
)
|
| 665 |
|
| 666 |
-
summary = f"
|
| 667 |
status = f"{summary} | {page_status}"
|
| 668 |
|
| 669 |
return status, "", None, refreshed_rows, refreshed_page
|
|
@@ -678,36 +979,40 @@ def build_demo_app(project_slug: str = "demo-project") -> gr.Blocks:
|
|
| 678 |
Returns:
|
| 679 |
Gradio Blocks with validation interface
|
| 680 |
"""
|
| 681 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 682 |
audio_service = AudioFetchService(EphemeralCacheManager(ttl_seconds=300, max_files=128))
|
| 683 |
-
validation_base_dir =
|
| 684 |
validation_repository = AppendOnlyValidationRepository(base_dir=validation_base_dir)
|
| 685 |
validation_service = ValidationService(validation_repository)
|
| 686 |
|
| 687 |
with gr.Blocks(title="BirdNET-Validator-App") as demo:
|
| 688 |
gr.Markdown("# BirdNET-Validator-App")
|
| 689 |
-
gr.Markdown("Sprint 2:
|
| 690 |
|
| 691 |
-
dataset_repo = gr.Textbox(label="Dataset repo", value="
|
| 692 |
|
| 693 |
with gr.Row():
|
| 694 |
-
species_filter = gr.Textbox(label="
|
| 695 |
-
min_confidence = gr.Slider(label="
|
| 696 |
-
show_conflicts_only = gr.Checkbox(label="
|
| 697 |
|
| 698 |
with gr.Row():
|
| 699 |
-
validator_filter = gr.Textbox(label="
|
| 700 |
validation_status_filter = gr.Dropdown(
|
| 701 |
-
label="
|
| 702 |
choices=["all", "pending", "positive", "negative", "uncertain", "skip"],
|
| 703 |
value="all",
|
| 704 |
)
|
| 705 |
-
updated_after_filter = gr.DateTime(label="
|
| 706 |
|
| 707 |
with gr.Row():
|
| 708 |
-
prev_btn = gr.Button("
|
| 709 |
-
next_btn = gr.Button("
|
| 710 |
-
refresh_btn = gr.Button("
|
| 711 |
|
| 712 |
page_state = gr.State(value=1)
|
| 713 |
table = gr.Dataframe(
|
|
@@ -723,54 +1028,54 @@ def build_demo_app(project_slug: str = "demo-project") -> gr.Blocks:
|
|
| 723 |
"conflict_flag",
|
| 724 |
"conflict_severity",
|
| 725 |
],
|
| 726 |
-
label="
|
| 727 |
interactive=False,
|
| 728 |
)
|
| 729 |
-
selected_index = gr.Number(label="
|
| 730 |
|
| 731 |
with gr.Row():
|
| 732 |
-
load_audio_btn = gr.Button("
|
| 733 |
-
clear_audio_btn = gr.Button("
|
| 734 |
|
| 735 |
with gr.Row():
|
| 736 |
-
validator_name = gr.Textbox(label="
|
| 737 |
-
validation_notes = gr.Textbox(label="
|
| 738 |
|
| 739 |
with gr.Row():
|
| 740 |
-
approve_btn = gr.Button("
|
| 741 |
-
reject_btn = gr.Button("
|
| 742 |
-
uncertain_btn = gr.Button("
|
| 743 |
-
skip_btn = gr.Button("
|
| 744 |
-
reapply_btn = gr.Button("
|
| 745 |
|
| 746 |
with gr.Row():
|
| 747 |
-
batch_approve_conflicts_btn = gr.Button("
|
| 748 |
-
batch_reject_conflicts_btn = gr.Button("
|
| 749 |
|
| 750 |
-
report_btn = gr.Button("
|
| 751 |
|
| 752 |
-
audio_player = gr.Audio(label="
|
| 753 |
cache_key_state = gr.State(value="")
|
| 754 |
pending_status_state = gr.State(value="")
|
| 755 |
conflict_detection_key_state = gr.State(value="")
|
| 756 |
status = gr.Textbox(label="Status", interactive=False)
|
| 757 |
-
report_box = gr.Textbox(label="
|
| 758 |
|
| 759 |
# Keyboard shortcuts: 1=positive, 2=negative, 3=uncertain, 4=skip, R=reapply
|
| 760 |
keyboard_shortcuts_info = gr.HTML(
|
| 761 |
value="<div style='font-size: 12px; color: #666; padding: 8px; background-color: #f5f5f5; border-radius: 4px; margin-bottom: 10px;'>"
|
| 762 |
-
"<strong>
|
| 763 |
"</div>"
|
| 764 |
"<script>"
|
| 765 |
"document.addEventListener('keydown', function(event) {"
|
| 766 |
" if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') return;"
|
| 767 |
" const key = event.key.toLowerCase();"
|
| 768 |
" let buttonText = null;"
|
| 769 |
-
" if (key === '1') buttonText = '
|
| 770 |
-
" else if (key === '2') buttonText = '
|
| 771 |
-
" else if (key === '3') buttonText = '
|
| 772 |
-
" else if (key === '4') buttonText = '
|
| 773 |
-
" else if (key === 'r') buttonText = '
|
| 774 |
" if (buttonText) {"
|
| 775 |
" event.preventDefault();"
|
| 776 |
" const buttons = document.querySelectorAll('button');"
|
|
@@ -801,6 +1106,7 @@ def build_demo_app(project_slug: str = "demo-project") -> gr.Blocks:
|
|
| 801 |
page=page,
|
| 802 |
scientific_name=species,
|
| 803 |
min_confidence=confidence,
|
|
|
|
| 804 |
validator_filter=validator_filter_value,
|
| 805 |
status_filter=status_filter_value,
|
| 806 |
updated_after=updated_after_value,
|
|
@@ -1206,56 +1512,31 @@ def create_app() -> gr.Blocks:
|
|
| 1206 |
# Initialize auth service
|
| 1207 |
auth_service = AuthService(session_ttl_minutes=120)
|
| 1208 |
|
| 1209 |
-
# Setup demo users (in production: database/LDAP/OAuth)
|
| 1210 |
-
auth_service.register_user_project_access(
|
| 1211 |
-
"admin_user",
|
| 1212 |
-
{"kenya-2024": Role.admin, "nairobi-2023": Role.admin},
|
| 1213 |
-
)
|
| 1214 |
-
auth_service.register_user_project_access(
|
| 1215 |
-
"validator_demo",
|
| 1216 |
-
{"demo-project": Role.validator, "kenya-2024": Role.validator},
|
| 1217 |
-
)
|
| 1218 |
-
auth_service.register_user_project_access(
|
| 1219 |
-
"validator_other",
|
| 1220 |
-
{"nairobi-2023": Role.validator},
|
| 1221 |
-
)
|
| 1222 |
-
|
| 1223 |
# Initialize admin panel manager
|
| 1224 |
admin_manager = AdminPanelManager(auth_service)
|
| 1225 |
|
| 1226 |
-
|
| 1227 |
-
|
| 1228 |
-
|
| 1229 |
-
|
| 1230 |
-
|
| 1231 |
-
dataset_repo_id="birdnet/kenya-2024-dataset",
|
| 1232 |
-
active=True,
|
| 1233 |
-
)
|
| 1234 |
-
)
|
| 1235 |
-
admin_manager.register_project(
|
| 1236 |
-
Project(
|
| 1237 |
-
project_slug="nairobi-2023",
|
| 1238 |
-
name="Nairobi Survey 2023",
|
| 1239 |
-
dataset_repo_id="birdnet/nairobi-2023-dataset",
|
| 1240 |
-
active=True,
|
| 1241 |
-
)
|
| 1242 |
-
)
|
| 1243 |
-
admin_manager.register_project(
|
| 1244 |
-
Project(
|
| 1245 |
-
project_slug="demo-project",
|
| 1246 |
-
name="Demo Project",
|
| 1247 |
-
dataset_repo_id="birdnet/demo-dataset",
|
| 1248 |
-
active=True,
|
| 1249 |
-
)
|
| 1250 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1251 |
|
| 1252 |
with gr.Blocks(title="BirdNET-Validator-App - Multi-Project") as wrapper:
|
| 1253 |
-
gr.Markdown("# BirdNET-Validator-App - Sprint 4:
|
| 1254 |
-
gr.Markdown("**
|
|
|
|
|
|
|
| 1255 |
|
| 1256 |
# Session state
|
| 1257 |
session_state = gr.State(value=None)
|
| 1258 |
selected_project_state = gr.State(value=None)
|
|
|
|
|
|
|
| 1259 |
|
| 1260 |
def _project_rows() -> list[list[object]]:
|
| 1261 |
projects = admin_manager.list_projects()
|
|
@@ -1294,35 +1575,35 @@ def create_app() -> gr.Blocks:
|
|
| 1294 |
|
| 1295 |
# ===== TAB 2: Admin Panel =====
|
| 1296 |
with gr.Tab("⚙️ Admin", id="admin_tab"):
|
| 1297 |
-
admin_info = gr.Markdown(value="⚠️
|
| 1298 |
|
| 1299 |
def create_admin_display(session):
|
| 1300 |
"""Show admin panel or access denied message."""
|
| 1301 |
if session is None:
|
| 1302 |
return (
|
| 1303 |
-
"❌ **Not
|
| 1304 |
gr.update(visible=False),
|
| 1305 |
)
|
| 1306 |
if session.role.value != "admin":
|
| 1307 |
return (
|
| 1308 |
-
"❌ **Access
|
| 1309 |
gr.update(visible=False),
|
| 1310 |
)
|
| 1311 |
return (
|
| 1312 |
-
f"✅ **Admin Panel** — Welcome {session.username}
|
| 1313 |
gr.update(visible=True),
|
| 1314 |
)
|
| 1315 |
|
| 1316 |
with gr.Group(visible=False) as admin_controls:
|
| 1317 |
-
gr.Markdown("####
|
| 1318 |
|
| 1319 |
with gr.Row():
|
| 1320 |
create_project_slug = gr.Textbox(
|
| 1321 |
-
label="
|
| 1322 |
placeholder="ex: amazonas-2026",
|
| 1323 |
)
|
| 1324 |
create_project_name = gr.Textbox(
|
| 1325 |
-
label="
|
| 1326 |
placeholder="ex: Amazonas Survey 2026",
|
| 1327 |
)
|
| 1328 |
create_project_repo = gr.Textbox(
|
|
@@ -1334,13 +1615,13 @@ def create_app() -> gr.Blocks:
|
|
| 1334 |
|
| 1335 |
def create_project(session, slug: str, name: str, repo_id: str):
|
| 1336 |
if session is None or session.role.value != "admin":
|
| 1337 |
-
return "❌ Access denied.
|
| 1338 |
|
| 1339 |
slug = (slug or "").strip()
|
| 1340 |
name = (name or "").strip()
|
| 1341 |
repo_id = (repo_id or "").strip()
|
| 1342 |
if not slug or not name or not repo_id:
|
| 1343 |
-
return "⚠️
|
| 1344 |
|
| 1345 |
created = admin_manager.register_project(
|
| 1346 |
Project(
|
|
@@ -1352,30 +1633,38 @@ def create_app() -> gr.Blocks:
|
|
| 1352 |
)
|
| 1353 |
if not created:
|
| 1354 |
return (
|
| 1355 |
-
f"⚠️
|
| 1356 |
_project_rows(),
|
| 1357 |
gr.update(choices=_project_slugs()),
|
| 1358 |
gr.update(),
|
| 1359 |
gr.update(),
|
| 1360 |
gr.update(),
|
|
|
|
| 1361 |
)
|
| 1362 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1363 |
return (
|
| 1364 |
-
f"✅
|
| 1365 |
_project_rows(),
|
| 1366 |
gr.update(choices=_project_slugs(), value=slug),
|
| 1367 |
gr.update(value=""),
|
| 1368 |
gr.update(value=""),
|
| 1369 |
gr.update(value=""),
|
|
|
|
| 1370 |
)
|
| 1371 |
|
| 1372 |
-
create_project_btn = gr.Button("➕
|
| 1373 |
projects_table = gr.Dataframe(
|
| 1374 |
value=_project_rows(),
|
| 1375 |
headers=["project_slug", "name", "dataset_repo_id", "active"],
|
| 1376 |
interactive=False,
|
| 1377 |
)
|
| 1378 |
-
refresh_projects_btn = gr.Button("🔄
|
| 1379 |
|
| 1380 |
def refresh_projects(session):
|
| 1381 |
if session is None or session.role.value != "admin":
|
|
@@ -1389,14 +1678,14 @@ def create_app() -> gr.Blocks:
|
|
| 1389 |
)
|
| 1390 |
|
| 1391 |
with gr.Group(visible=False) as admin_users_controls:
|
| 1392 |
-
gr.Markdown("####
|
| 1393 |
with gr.Row():
|
| 1394 |
admin_username = gr.Textbox(
|
| 1395 |
-
label="
|
| 1396 |
)
|
| 1397 |
admin_project = gr.Dropdown(
|
| 1398 |
choices=_project_slugs(),
|
| 1399 |
-
label="
|
| 1400 |
)
|
| 1401 |
admin_role = gr.Dropdown(
|
| 1402 |
choices=["admin", "validator"],
|
|
@@ -1408,7 +1697,7 @@ def create_app() -> gr.Blocks:
|
|
| 1408 |
|
| 1409 |
def assign_user(session, username: str, project: str, role: str):
|
| 1410 |
if session is None or session.role.value != "admin":
|
| 1411 |
-
return "❌ Access denied.
|
| 1412 |
success, msg = admin_manager.assign_user_to_project(
|
| 1413 |
username, project, role
|
| 1414 |
)
|
|
@@ -1416,7 +1705,7 @@ def create_app() -> gr.Blocks:
|
|
| 1416 |
return msg, gr.update(value=""), gr.update(value=None), gr.update(value="validator")
|
| 1417 |
return msg, gr.update(), gr.update(), gr.update()
|
| 1418 |
|
| 1419 |
-
assign_btn = gr.Button("✅
|
| 1420 |
assign_btn.click(
|
| 1421 |
fn=assign_user,
|
| 1422 |
inputs=[session_state, admin_username, admin_project, admin_role],
|
|
@@ -1433,6 +1722,7 @@ def create_app() -> gr.Blocks:
|
|
| 1433 |
create_project_slug,
|
| 1434 |
create_project_name,
|
| 1435 |
create_project_repo,
|
|
|
|
| 1436 |
],
|
| 1437 |
)
|
| 1438 |
|
|
@@ -1449,13 +1739,13 @@ def create_app() -> gr.Blocks:
|
|
| 1449 |
)
|
| 1450 |
|
| 1451 |
# ===== TAB 3: Project Selection =====
|
| 1452 |
-
with gr.Tab("📁
|
| 1453 |
project_info_display = gr.Markdown(
|
| 1454 |
-
value="⚠️
|
| 1455 |
)
|
| 1456 |
project_selector = gr.Dropdown(
|
| 1457 |
choices=[],
|
| 1458 |
-
label="
|
| 1459 |
interactive=False,
|
| 1460 |
)
|
| 1461 |
|
|
@@ -1464,79 +1754,696 @@ def create_app() -> gr.Blocks:
|
|
| 1464 |
if session is None:
|
| 1465 |
return (
|
| 1466 |
gr.Dropdown(choices=[], value=None, interactive=False),
|
| 1467 |
-
"❌ Not
|
| 1468 |
None,
|
|
|
|
| 1469 |
)
|
| 1470 |
|
| 1471 |
projects = session.authorized_projects
|
| 1472 |
if not projects:
|
| 1473 |
return (
|
| 1474 |
gr.Dropdown(choices=[], value=None, interactive=False),
|
| 1475 |
-
"⚠️ **No
|
| 1476 |
None,
|
|
|
|
| 1477 |
)
|
| 1478 |
|
| 1479 |
selected = projects[0]
|
| 1480 |
role = auth_service.get_user_role_for_project(session.username, selected)
|
| 1481 |
role_label = role.value.upper() if role else "UNKNOWN"
|
|
|
|
|
|
|
| 1482 |
return (
|
| 1483 |
gr.Dropdown(choices=projects, value=selected, interactive=True),
|
| 1484 |
f"📁 **Project:** {selected} | **Your Role:** {role_label}",
|
| 1485 |
selected,
|
|
|
|
| 1486 |
)
|
| 1487 |
|
| 1488 |
session_state.change(
|
| 1489 |
fn=update_project_selector,
|
| 1490 |
inputs=[session_state],
|
| 1491 |
-
outputs=[project_selector, project_info_display, selected_project_state],
|
| 1492 |
)
|
| 1493 |
|
| 1494 |
def update_selected_project(selected: str, session):
|
| 1495 |
"""Update state when project is selected."""
|
| 1496 |
if session and selected:
|
| 1497 |
-
|
| 1498 |
-
|
|
|
|
|
|
|
| 1499 |
|
| 1500 |
project_selector.change(
|
| 1501 |
fn=update_selected_project,
|
| 1502 |
inputs=[project_selector, session_state],
|
| 1503 |
-
outputs=[selected_project_state],
|
| 1504 |
)
|
| 1505 |
|
| 1506 |
# ===== TAB 4: Validation =====
|
| 1507 |
-
with gr.Tab("✓
|
| 1508 |
validation_status = gr.Markdown(
|
| 1509 |
-
value="ℹ️
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1510 |
)
|
| 1511 |
|
| 1512 |
-
def get_validation_status(session, selected_project):
|
| 1513 |
"""Show status message based on login/project state."""
|
| 1514 |
if session is None:
|
| 1515 |
-
return "❌ **
|
| 1516 |
if selected_project is None:
|
| 1517 |
-
return f"⚠️ **
|
| 1518 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1519 |
|
| 1520 |
session_state.change(
|
| 1521 |
-
fn=lambda s, p: get_validation_status(s, p),
|
| 1522 |
-
inputs=[session_state, selected_project_state],
|
| 1523 |
outputs=[validation_status],
|
| 1524 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1525 |
|
| 1526 |
selected_project_state.change(
|
| 1527 |
-
fn=lambda s, p: get_validation_status(s, p),
|
| 1528 |
-
inputs=[session_state, selected_project_state],
|
| 1529 |
outputs=[validation_status],
|
| 1530 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1531 |
|
| 1532 |
-
# Note: In a real implementation, we would load build_demo_app(selected_project) here
|
| 1533 |
-
# For now, we show a placeholder since Gradio tabs don't support dynamic nested Blocks
|
| 1534 |
gr.Markdown("---")
|
| 1535 |
-
|
| 1536 |
-
|
| 1537 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1538 |
interactive=False,
|
| 1539 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1540 |
)
|
| 1541 |
|
| 1542 |
return wrapper
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import hashlib
|
| 3 |
+
import json
|
| 4 |
from datetime import date, datetime
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import Protocol
|
| 7 |
|
| 8 |
+
from src.config.runtime_config import RuntimeConfig
|
| 9 |
from src.cache.ephemeral_cache_manager import EphemeralCacheManager
|
| 10 |
from src.domain.models import Detection, Project, Role
|
| 11 |
from src.repositories.append_only_validation_repository import AppendOnlyValidationRepository, OptimisticLockError
|
|
|
|
| 15 |
from src.services.validation_service import ValidationService
|
| 16 |
from src.auth.auth_service import AuthService
|
| 17 |
from src.ui.login_page import create_login_page
|
| 18 |
+
from src.ui.admin_panel import AdminPanelManager
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
class _AudioFetchResultProtocol(Protocol):
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
def _seed_service() -> DetectionQueueService:
|
| 65 |
+
return _seed_service_for_projects(["demo-project"])
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def _seed_service_for_projects(
|
| 69 |
+
project_slugs: list[str],
|
| 70 |
+
seed_file_path: str | None = None,
|
| 71 |
+
) -> DetectionQueueService:
|
| 72 |
repo = InMemoryDetectionRepository()
|
| 73 |
+
detected_by_project = _load_seed_detections(seed_file_path)
|
| 74 |
+
|
| 75 |
+
for project_slug in project_slugs:
|
| 76 |
+
seeded_items = detected_by_project.get(project_slug, [])
|
| 77 |
+
items = seeded_items or _default_demo_detections(project_slug)
|
| 78 |
+
items = sorted(items, key=lambda item: item.detection_key)
|
| 79 |
+
repo.seed(project_slug, items)
|
| 80 |
+
|
| 81 |
+
return DetectionQueueService(repo)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def _build_detection_repository(
|
| 85 |
+
project_slugs: list[str],
|
| 86 |
+
seed_file_path: str | None,
|
| 87 |
+
) -> tuple[DetectionQueueService, str]:
|
| 88 |
+
warning = _validate_seed_file(seed_file_path)
|
| 89 |
+
return _seed_service_for_projects(project_slugs, seed_file_path=seed_file_path), warning
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def _validate_seed_file(seed_file_path: str | None) -> str:
|
| 93 |
+
if not seed_file_path:
|
| 94 |
+
return ""
|
| 95 |
+
|
| 96 |
+
normalized_path = Path(seed_file_path)
|
| 97 |
+
if not normalized_path.exists():
|
| 98 |
+
return (
|
| 99 |
+
f"⚠️ BIRDNET_DETECTIONS_FILE not found: {normalized_path}. "
|
| 100 |
+
"Set BIRDNET_DETECTIONS_FILE to a valid JSON file path or unset it to use default demo detections."
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
try:
|
| 104 |
+
payload = json.loads(normalized_path.read_text(encoding="utf-8"))
|
| 105 |
+
except Exception as exc:
|
| 106 |
+
return (
|
| 107 |
+
f"⚠️ BIRDNET_DETECTIONS_FILE invalid: {exc}. "
|
| 108 |
+
"Fix JSON syntax and ensure UTF-8 encoding."
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
if isinstance(payload, dict):
|
| 112 |
+
non_list_projects = [slug for slug, rows in payload.items() if not isinstance(rows, list)]
|
| 113 |
+
if non_list_projects:
|
| 114 |
+
sample = ", ".join(non_list_projects[:3])
|
| 115 |
+
return (
|
| 116 |
+
f"⚠️ Invalid seed JSON: projects without a detection list ({sample}). "
|
| 117 |
+
"Each project key must map to a list of detection objects."
|
| 118 |
+
)
|
| 119 |
+
return ""
|
| 120 |
+
|
| 121 |
+
if isinstance(payload, list):
|
| 122 |
+
missing_project = 0
|
| 123 |
+
for row in payload:
|
| 124 |
+
if not isinstance(row, dict):
|
| 125 |
+
continue
|
| 126 |
+
if not str(row.get("project_slug", "")).strip():
|
| 127 |
+
missing_project += 1
|
| 128 |
+
if missing_project:
|
| 129 |
+
return (
|
| 130 |
+
"⚠️ Invalid seed JSON: entries without project_slug in list. "
|
| 131 |
+
"Add project_slug to each detection object when using list format."
|
| 132 |
+
)
|
| 133 |
+
return ""
|
| 134 |
+
|
| 135 |
+
return (
|
| 136 |
+
"⚠️ Invalid seed JSON: format must be object-by-project or detection list. "
|
| 137 |
+
"See README for supported examples."
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def _default_demo_detections(project_slug: str) -> list[Detection]:
|
| 142 |
+
stable_prefix = hashlib.sha1(project_slug.encode("utf-8")).hexdigest()[:8]
|
| 143 |
+
slug_prefix = project_slug.replace("-", "_")
|
| 144 |
+
return [
|
| 145 |
Detection(
|
| 146 |
+
detection_key=f"{stable_prefix}00001001",
|
| 147 |
+
audio_id=f"{slug_prefix}_audio_1001",
|
| 148 |
scientific_name="Cyanocorax cyanopogon",
|
| 149 |
confidence=0.93,
|
| 150 |
start_time=1.2,
|
| 151 |
end_time=2.5,
|
| 152 |
),
|
| 153 |
Detection(
|
| 154 |
+
detection_key=f"{stable_prefix}00001002",
|
| 155 |
+
audio_id=f"{slug_prefix}_audio_1002",
|
| 156 |
scientific_name="Ramphastos toco",
|
| 157 |
confidence=0.88,
|
| 158 |
start_time=0.8,
|
| 159 |
end_time=2.1,
|
| 160 |
),
|
| 161 |
Detection(
|
| 162 |
+
detection_key=f"{stable_prefix}00001003",
|
| 163 |
+
audio_id=f"{slug_prefix}_audio_1003",
|
| 164 |
scientific_name="Cyanocorax cyanopogon",
|
| 165 |
confidence=0.72,
|
| 166 |
start_time=3.1,
|
| 167 |
end_time=4.0,
|
| 168 |
),
|
| 169 |
Detection(
|
| 170 |
+
detection_key=f"{stable_prefix}00001004",
|
| 171 |
+
audio_id=f"{slug_prefix}_audio_1004",
|
| 172 |
scientific_name="Psarocolius decumanus",
|
| 173 |
confidence=0.67,
|
| 174 |
start_time=5.0,
|
| 175 |
end_time=6.3,
|
| 176 |
),
|
| 177 |
]
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def _load_seed_detections(seed_file_path: str | None) -> dict[str, list[Detection]]:
|
| 181 |
+
if not seed_file_path:
|
| 182 |
+
return {}
|
| 183 |
+
|
| 184 |
+
normalized_path = Path(seed_file_path)
|
| 185 |
+
if not normalized_path.exists():
|
| 186 |
+
return {}
|
| 187 |
+
|
| 188 |
+
try:
|
| 189 |
+
payload = json.loads(normalized_path.read_text(encoding="utf-8"))
|
| 190 |
+
except Exception:
|
| 191 |
+
return {}
|
| 192 |
+
|
| 193 |
+
result: dict[str, list[Detection]] = {}
|
| 194 |
+
|
| 195 |
+
if isinstance(payload, dict):
|
| 196 |
+
for project_slug, rows in payload.items():
|
| 197 |
+
parsed_rows = _parse_detection_rows(rows)
|
| 198 |
+
if parsed_rows:
|
| 199 |
+
result[str(project_slug)] = parsed_rows
|
| 200 |
+
return result
|
| 201 |
+
|
| 202 |
+
if isinstance(payload, list):
|
| 203 |
+
grouped: dict[str, list[dict[str, object]]] = {}
|
| 204 |
+
for row in payload:
|
| 205 |
+
if not isinstance(row, dict):
|
| 206 |
+
continue
|
| 207 |
+
project_slug = str(row.get("project_slug", "")).strip()
|
| 208 |
+
if not project_slug:
|
| 209 |
+
continue
|
| 210 |
+
grouped.setdefault(project_slug, []).append(row)
|
| 211 |
+
|
| 212 |
+
for project_slug, rows in grouped.items():
|
| 213 |
+
parsed_rows = _parse_detection_rows(rows)
|
| 214 |
+
if parsed_rows:
|
| 215 |
+
result[project_slug] = parsed_rows
|
| 216 |
+
|
| 217 |
+
return result
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def _parse_detection_rows(rows: object) -> list[Detection]:
|
| 221 |
+
parsed: list[Detection] = []
|
| 222 |
+
if not isinstance(rows, list):
|
| 223 |
+
return parsed
|
| 224 |
+
|
| 225 |
+
for raw in rows:
|
| 226 |
+
if not isinstance(raw, dict):
|
| 227 |
+
continue
|
| 228 |
+
try:
|
| 229 |
+
parsed.append(
|
| 230 |
+
Detection(
|
| 231 |
+
detection_key=str(raw.get("detection_key", "")).strip(),
|
| 232 |
+
audio_id=str(raw.get("audio_id", "")).strip(),
|
| 233 |
+
scientific_name=str(raw.get("scientific_name", "")).strip(),
|
| 234 |
+
confidence=float(raw.get("confidence", 0.0)),
|
| 235 |
+
start_time=float(raw.get("start_time", 0.0)),
|
| 236 |
+
end_time=float(raw.get("end_time", 0.0)),
|
| 237 |
+
)
|
| 238 |
+
)
|
| 239 |
+
except Exception:
|
| 240 |
+
continue
|
| 241 |
+
|
| 242 |
+
return parsed
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def _default_projects() -> list[Project]:
|
| 246 |
+
return [
|
| 247 |
+
Project(
|
| 248 |
+
project_slug="kenya-2024",
|
| 249 |
+
name="Kenya Survey 2024",
|
| 250 |
+
dataset_repo_id="birdnet/kenya-2024-dataset",
|
| 251 |
+
active=True,
|
| 252 |
+
),
|
| 253 |
+
Project(
|
| 254 |
+
project_slug="nairobi-2023",
|
| 255 |
+
name="Nairobi Survey 2023",
|
| 256 |
+
dataset_repo_id="birdnet/nairobi-2023-dataset",
|
| 257 |
+
active=True,
|
| 258 |
+
),
|
| 259 |
+
Project(
|
| 260 |
+
project_slug="demo-project",
|
| 261 |
+
name="Demo Project",
|
| 262 |
+
dataset_repo_id="birdnet/demo-dataset",
|
| 263 |
+
active=True,
|
| 264 |
+
),
|
| 265 |
+
]
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def _default_user_access() -> dict[str, dict[str, Role]]:
|
| 269 |
+
return {
|
| 270 |
+
"admin_user": {"kenya-2024": Role.admin, "nairobi-2023": Role.admin},
|
| 271 |
+
"validator_demo": {"demo-project": Role.validator, "kenya-2024": Role.validator},
|
| 272 |
+
"validator_other": {"nairobi-2023": Role.validator},
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
def _load_projects_from_file(projects_file_path: str | None) -> list[Project]:
|
| 277 |
+
if not projects_file_path:
|
| 278 |
+
return []
|
| 279 |
+
|
| 280 |
+
path = Path(projects_file_path)
|
| 281 |
+
if not path.exists():
|
| 282 |
+
return []
|
| 283 |
+
|
| 284 |
+
try:
|
| 285 |
+
payload = json.loads(path.read_text(encoding="utf-8"))
|
| 286 |
+
except Exception:
|
| 287 |
+
return []
|
| 288 |
+
|
| 289 |
+
if not isinstance(payload, list):
|
| 290 |
+
return []
|
| 291 |
+
|
| 292 |
+
projects: list[Project] = []
|
| 293 |
+
for row in payload:
|
| 294 |
+
if not isinstance(row, dict):
|
| 295 |
+
continue
|
| 296 |
+
try:
|
| 297 |
+
projects.append(
|
| 298 |
+
Project(
|
| 299 |
+
project_slug=str(row.get("project_slug", "")).strip(),
|
| 300 |
+
name=str(row.get("name", "")).strip(),
|
| 301 |
+
dataset_repo_id=str(row.get("dataset_repo_id", "")).strip(),
|
| 302 |
+
active=bool(row.get("active", True)),
|
| 303 |
+
)
|
| 304 |
+
)
|
| 305 |
+
except Exception:
|
| 306 |
+
continue
|
| 307 |
+
return projects
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
def _load_user_access_from_file(user_access_file_path: str | None) -> dict[str, dict[str, Role]]:
|
| 311 |
+
if not user_access_file_path:
|
| 312 |
+
return {}
|
| 313 |
+
|
| 314 |
+
path = Path(user_access_file_path)
|
| 315 |
+
if not path.exists():
|
| 316 |
+
return {}
|
| 317 |
+
|
| 318 |
+
try:
|
| 319 |
+
payload = json.loads(path.read_text(encoding="utf-8"))
|
| 320 |
+
except Exception:
|
| 321 |
+
return {}
|
| 322 |
+
|
| 323 |
+
if not isinstance(payload, dict):
|
| 324 |
+
return {}
|
| 325 |
+
|
| 326 |
+
result: dict[str, dict[str, Role]] = {}
|
| 327 |
+
for username, roles_payload in payload.items():
|
| 328 |
+
if not isinstance(roles_payload, dict):
|
| 329 |
+
continue
|
| 330 |
+
normalized_roles: dict[str, Role] = {}
|
| 331 |
+
for project_slug, role_value in roles_payload.items():
|
| 332 |
+
role_text = str(role_value).strip().lower()
|
| 333 |
+
if role_text not in {"admin", "validator"}:
|
| 334 |
+
continue
|
| 335 |
+
normalized_roles[str(project_slug)] = Role(role_text)
|
| 336 |
+
if normalized_roles:
|
| 337 |
+
result[str(username)] = normalized_roles
|
| 338 |
+
return result
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
def _bootstrap_auth_and_projects(auth_service: AuthService, admin_manager: AdminPanelManager, runtime_config: RuntimeConfig) -> str:
|
| 342 |
+
projects = _load_projects_from_file(runtime_config.projects_file_path)
|
| 343 |
+
user_access = _load_user_access_from_file(runtime_config.user_access_file_path)
|
| 344 |
+
|
| 345 |
+
used_demo_fallback = False
|
| 346 |
+
if not projects and runtime_config.enable_demo_bootstrap:
|
| 347 |
+
projects = _default_projects()
|
| 348 |
+
used_demo_fallback = True
|
| 349 |
+
|
| 350 |
+
if not user_access and runtime_config.enable_demo_bootstrap:
|
| 351 |
+
user_access = _default_user_access()
|
| 352 |
+
used_demo_fallback = True
|
| 353 |
+
|
| 354 |
+
for project in projects:
|
| 355 |
+
_ = admin_manager.register_project(project)
|
| 356 |
+
|
| 357 |
+
for username, access in user_access.items():
|
| 358 |
+
auth_service.register_user_project_access(username, access)
|
| 359 |
+
|
| 360 |
+
if used_demo_fallback:
|
| 361 |
+
return (
|
| 362 |
+
"⚠️ Demo bootstrap enabled via BIRDNET_ENABLE_DEMO_BOOTSTRAP. "
|
| 363 |
+
"Use BIRDNET_PROJECTS_FILE and BIRDNET_USER_ACCESS_FILE for production auth/project setup."
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
+
if not projects or not user_access:
|
| 367 |
+
return (
|
| 368 |
+
"⚠️ Production bootstrap incomplete. Configure BIRDNET_PROJECTS_FILE and "
|
| 369 |
+
"BIRDNET_USER_ACCESS_FILE (or enable demo bootstrap for local testing)."
|
| 370 |
+
)
|
| 371 |
+
|
| 372 |
+
return ""
|
| 373 |
|
| 374 |
|
| 375 |
def _page_to_table(
|
|
|
|
| 379 |
page: int,
|
| 380 |
scientific_name: str,
|
| 381 |
min_confidence: float,
|
| 382 |
+
page_size: int = 25,
|
| 383 |
validator_filter: str = "",
|
| 384 |
status_filter: str = "all",
|
| 385 |
updated_after: object = None,
|
|
|
|
| 388 |
):
|
| 389 |
filter_name = scientific_name.strip() if scientific_name.strip() else None
|
| 390 |
page_obj = service.get_page(
|
| 391 |
+
project_slug=project_slug,
|
| 392 |
page=page,
|
| 393 |
+
page_size=page_size,
|
| 394 |
scientific_name=filter_name,
|
| 395 |
min_confidence=min_confidence,
|
| 396 |
)
|
|
|
|
| 462 |
if show_conflicts_only:
|
| 463 |
rows = [row for row in rows if str(row[8]) == "CONFLICT"]
|
| 464 |
|
| 465 |
+
status = f"Page {page_obj.page}/{page_obj.total_pages} | Base total: {page_obj.total_items} | Shown: {len(rows)}"
|
| 466 |
if show_conflicts_only:
|
| 467 |
+
status = f"{status} | Conflicts only: {len(rows)} item(ns)"
|
| 468 |
return rows, status, page_obj.page
|
| 469 |
|
| 470 |
|
| 471 |
+
def _get_project_detection_count(service: _QueueServiceProtocol, project_slug: str) -> int:
|
| 472 |
+
if not project_slug:
|
| 473 |
+
return 0
|
| 474 |
+
|
| 475 |
+
try:
|
| 476 |
+
page_obj = service.get_page(
|
| 477 |
+
project_slug=project_slug,
|
| 478 |
+
page=1,
|
| 479 |
+
page_size=1,
|
| 480 |
+
)
|
| 481 |
+
return int(getattr(page_obj, "total_items", 0))
|
| 482 |
+
except Exception:
|
| 483 |
+
return 0
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
def _build_queue_badge(service: _QueueServiceProtocol, project_slug: str | None) -> str:
|
| 487 |
+
if not project_slug:
|
| 488 |
+
return "<div style='display:inline-block;padding:6px 10px;border-radius:999px;background:#f3f4f6;color:#374151;font-weight:600;'>Queue: --</div>"
|
| 489 |
+
|
| 490 |
+
total = _get_project_detection_count(service, project_slug)
|
| 491 |
+
return (
|
| 492 |
+
"<div style='display:inline-block;padding:6px 10px;border-radius:999px;"
|
| 493 |
+
"background:#e0f2fe;color:#0c4a6e;font-weight:700;'>"
|
| 494 |
+
f"Queue: {total}"
|
| 495 |
+
"</div>"
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
+
|
| 499 |
def _build_validation_report(snapshot_reader: _ValidationReadRepositoryProtocol, project_slug: str) -> str:
|
| 500 |
snapshot = snapshot_reader.load_current_snapshot(project_slug=project_slug)
|
| 501 |
events = snapshot_reader.list_events(project_slug=project_slug)
|
|
|
|
| 506 |
counts[status_value] = counts.get(status_value, 0) + 1
|
| 507 |
|
| 508 |
parts = [
|
| 509 |
+
f"Project: {project_slug}",
|
| 510 |
+
f"Append-only events: {len(events)}",
|
| 511 |
+
f"Detections with current state: {len(snapshot)}",
|
| 512 |
]
|
| 513 |
if counts:
|
| 514 |
summary = ", ".join(f"{key}={value}" for key, value in sorted(counts.items()))
|
| 515 |
+
parts.append(f"Current status: {summary}")
|
| 516 |
else:
|
| 517 |
+
parts.append("Current status: no validations")
|
| 518 |
return " | ".join(parts)
|
| 519 |
|
| 520 |
|
|
|
|
| 527 |
normalized_rows = [list(item) for item in rows] if rows else []
|
| 528 |
|
| 529 |
if not normalized_rows:
|
| 530 |
+
raise ValueError("No detections loaded in table")
|
| 531 |
if selected_index < 0 or selected_index >= len(normalized_rows):
|
| 532 |
+
raise ValueError("Select a valid detection row in table")
|
| 533 |
|
| 534 |
value = normalized_rows[selected_index][1]
|
| 535 |
audio_id = str(value).strip()
|
| 536 |
if not audio_id:
|
| 537 |
+
raise ValueError("Invalid audio_id in selected detection")
|
| 538 |
return audio_id
|
| 539 |
|
| 540 |
|
|
|
|
| 547 |
normalized_rows = [list(item) for item in rows] if rows else []
|
| 548 |
|
| 549 |
if not normalized_rows:
|
| 550 |
+
raise ValueError("No detections loaded in table")
|
| 551 |
if selected_index < 0 or selected_index >= len(normalized_rows):
|
| 552 |
+
raise ValueError("Select a valid detection row in table")
|
| 553 |
|
| 554 |
value = normalized_rows[selected_index][0]
|
| 555 |
detection_key = str(value).strip()
|
| 556 |
if not detection_key:
|
| 557 |
+
raise ValueError("Invalid detection_key in selected detection")
|
| 558 |
return detection_key
|
| 559 |
|
| 560 |
|
|
|
|
| 581 |
normalized_rows = [list(item) for item in rows] if rows else []
|
| 582 |
|
| 583 |
if not normalized_rows:
|
| 584 |
+
raise ValueError("No detections loaded in table")
|
| 585 |
if selected_index < 0 or selected_index >= len(normalized_rows):
|
| 586 |
+
raise ValueError("Select a valid detection row in table")
|
| 587 |
|
| 588 |
value = normalized_rows[selected_index][7]
|
| 589 |
return int(value)
|
|
|
|
| 598 |
) -> tuple[str | None, str, str]:
|
| 599 |
repo = dataset_repo.strip()
|
| 600 |
if not repo:
|
| 601 |
+
return None, "", "Provide dataset repo in owner/repo format. Example: org/dataset-name"
|
| 602 |
|
| 603 |
try:
|
| 604 |
audio_id = _extract_audio_id(rows=rows, selected_index=selected_index)
|
| 605 |
result = audio_service.fetch(dataset_repo=repo, audio_id=audio_id)
|
| 606 |
+
status = f"Audio loaded ({result.source}) for audio_id={audio_id}"
|
| 607 |
return result.local_path, result.cache_key, status
|
| 608 |
except Exception as exc:
|
| 609 |
if previous_cache_key:
|
| 610 |
+
return None, previous_cache_key, f"Failed to load audio: {exc}"
|
| 611 |
+
return None, "", f"Failed to load audio: {exc}"
|
| 612 |
|
| 613 |
|
| 614 |
def _cleanup_selected_audio(audio_service: _AudioServiceProtocol, cache_key: str) -> tuple[str, str | None]:
|
| 615 |
if not cache_key:
|
| 616 |
+
return "No cached audio to clean", None
|
| 617 |
|
| 618 |
audio_service.cleanup_after_validation(cache_key=cache_key)
|
| 619 |
+
return "Audio cache cleaned after validation", None
|
| 620 |
|
| 621 |
|
| 622 |
def _save_selected_validation(
|
|
|
|
| 632 |
) -> tuple[str, str, str | None]:
|
| 633 |
validator_name = validator.strip()
|
| 634 |
if not validator_name:
|
| 635 |
+
return "Provide validator name before saving", cache_key, None
|
| 636 |
|
| 637 |
try:
|
| 638 |
detection_key = _extract_detection_key(rows=rows, selected_index=selected_index)
|
|
|
|
| 647 |
)
|
| 648 |
if cache_key:
|
| 649 |
audio_service.cleanup_after_validation(cache_key=cache_key)
|
| 650 |
+
return f"Validation saved: {detection_key} -> {status_value}", "", None
|
| 651 |
except OptimisticLockError as exc:
|
| 652 |
return (
|
| 653 |
+
"Concurrency conflict: this detection was updated by another validator "
|
| 654 |
+
f"(detection_key={exc.detection_key}, current version={exc.current_version}, expected={exc.expected_version}). "
|
| 655 |
+
"Refresh the table.",
|
| 656 |
cache_key,
|
| 657 |
None,
|
| 658 |
)
|
| 659 |
except Exception as exc:
|
| 660 |
+
return f"Failed to save validation: {exc}", cache_key, None
|
| 661 |
|
| 662 |
|
| 663 |
def _save_selected_validation_with_refresh(
|
|
|
|
| 716 |
else:
|
| 717 |
refreshed_index = 0
|
| 718 |
|
| 719 |
+
if "Concurrency conflict" in save_status:
|
| 720 |
conflict_key = selected_key
|
| 721 |
refreshed_rows, page_status, refreshed_page = _page_to_table(
|
| 722 |
service=queue_service,
|
|
|
|
| 733 |
)
|
| 734 |
refreshed_index = _find_detection_row_index(refreshed_rows, selected_key) if selected_key else 0
|
| 735 |
pending_status_value = status_value
|
| 736 |
+
status = f"{save_status} Table reloaded to resolve conflict."
|
| 737 |
else:
|
| 738 |
conflict_key = ""
|
| 739 |
pending_status_value = ""
|
|
|
|
| 786 |
show_conflicts_only=show_conflicts_only,
|
| 787 |
)
|
| 788 |
return (
|
| 789 |
+
f"No pending validation to reapply | {page_status}",
|
| 790 |
cache_key,
|
| 791 |
None,
|
| 792 |
refreshed_rows,
|
|
|
|
| 840 |
"""Apply the same validation status to all visible conflicts in the table."""
|
| 841 |
validator_name = validator.strip()
|
| 842 |
if not validator_name:
|
| 843 |
+
return "Provide validator name", "", None, [], page
|
| 844 |
|
| 845 |
normalized_rows: list[list[object]]
|
| 846 |
if hasattr(rows, "values"):
|
|
|
|
| 849 |
normalized_rows = [list(item) for item in rows] if rows else []
|
| 850 |
|
| 851 |
if not normalized_rows:
|
| 852 |
+
return "No conflict detection to validate", "", None, [], page
|
| 853 |
|
| 854 |
conflict_rows = [row for row in normalized_rows if str(row[8]) == "CONFLICT"]
|
| 855 |
if not conflict_rows:
|
| 856 |
+
return "No conflict detection identified in table", "", None, normalized_rows, page
|
| 857 |
|
| 858 |
success_count = 0
|
| 859 |
failure_count = 0
|
|
|
|
| 893 |
show_conflicts_only=False,
|
| 894 |
)
|
| 895 |
|
| 896 |
+
summary = f"Processed {len(conflict_rows)} conflicts: {success_count} success, {conflict_count} new conflicts, {failure_count} failures"
|
| 897 |
status = f"{summary} | {page_status}"
|
| 898 |
|
| 899 |
return status, "", None, refreshed_rows, refreshed_page
|
|
|
|
| 919 |
) -> tuple[str, str, str | None, list[list[object]], int]:
|
| 920 |
"""Reapply all pending validations (stored conflicts) with current version."""
|
| 921 |
if not pending_statuses:
|
| 922 |
+
return "No pending validation to reapply", "", None, [], page
|
| 923 |
|
| 924 |
validator_name = validator.strip()
|
| 925 |
if not validator_name:
|
| 926 |
+
return "Provide validator name", "", None, [], page
|
| 927 |
|
| 928 |
success_count = 0
|
| 929 |
conflict_count = 0
|
|
|
|
| 964 |
show_conflicts_only=False,
|
| 965 |
)
|
| 966 |
|
| 967 |
+
summary = f"Reapplied {len(pending_statuses)} validations: {success_count} success, {conflict_count} new conflicts, {failure_count} failures"
|
| 968 |
status = f"{summary} | {page_status}"
|
| 969 |
|
| 970 |
return status, "", None, refreshed_rows, refreshed_page
|
|
|
|
| 979 |
Returns:
|
| 980 |
Gradio Blocks with validation interface
|
| 981 |
"""
|
| 982 |
+
runtime_config = RuntimeConfig.from_env()
|
| 983 |
+
service = _seed_service_for_projects(
|
| 984 |
+
[project_slug],
|
| 985 |
+
seed_file_path=runtime_config.detection_seed_path,
|
| 986 |
+
)
|
| 987 |
audio_service = AudioFetchService(EphemeralCacheManager(ttl_seconds=300, max_files=128))
|
| 988 |
+
validation_base_dir = runtime_config.validation_base_dir
|
| 989 |
validation_repository = AppendOnlyValidationRepository(base_dir=validation_base_dir)
|
| 990 |
validation_service = ValidationService(validation_repository)
|
| 991 |
|
| 992 |
with gr.Blocks(title="BirdNET-Validator-App") as demo:
|
| 993 |
gr.Markdown("# BirdNET-Validator-App")
|
| 994 |
+
gr.Markdown("Sprint 2: paged queue + on-demand audio with ephemeral cache.")
|
| 995 |
|
| 996 |
+
dataset_repo = gr.Textbox(label="Dataset repo", value="YOUR_USER/birdnet-project-dataset")
|
| 997 |
|
| 998 |
with gr.Row():
|
| 999 |
+
species_filter = gr.Textbox(label="Species filter", placeholder="Ex: Cyanocorax cyanopogon")
|
| 1000 |
+
min_confidence = gr.Slider(label="Minimum confidence", minimum=0.0, maximum=1.0, step=0.01, value=0.0)
|
| 1001 |
+
show_conflicts_only = gr.Checkbox(label="Show only conflicts", value=False)
|
| 1002 |
|
| 1003 |
with gr.Row():
|
| 1004 |
+
validator_filter = gr.Textbox(label="Validator filter", placeholder="Ex: validator-demo")
|
| 1005 |
validation_status_filter = gr.Dropdown(
|
| 1006 |
+
label="Status filter",
|
| 1007 |
choices=["all", "pending", "positive", "negative", "uncertain", "skip"],
|
| 1008 |
value="all",
|
| 1009 |
)
|
| 1010 |
+
updated_after_filter = gr.DateTime(label="Updated since", include_time=False, type="string")
|
| 1011 |
|
| 1012 |
with gr.Row():
|
| 1013 |
+
prev_btn = gr.Button("Previous page")
|
| 1014 |
+
next_btn = gr.Button("Next page")
|
| 1015 |
+
refresh_btn = gr.Button("Apply filters")
|
| 1016 |
|
| 1017 |
page_state = gr.State(value=1)
|
| 1018 |
table = gr.Dataframe(
|
|
|
|
| 1028 |
"conflict_flag",
|
| 1029 |
"conflict_severity",
|
| 1030 |
],
|
| 1031 |
+
label="Detections",
|
| 1032 |
interactive=False,
|
| 1033 |
)
|
| 1034 |
+
selected_index = gr.Number(label="Selected row", value=0, precision=0)
|
| 1035 |
|
| 1036 |
with gr.Row():
|
| 1037 |
+
load_audio_btn = gr.Button("Load selected audio")
|
| 1038 |
+
clear_audio_btn = gr.Button("Clear cache after validation")
|
| 1039 |
|
| 1040 |
with gr.Row():
|
| 1041 |
+
validator_name = gr.Textbox(label="Validator", value="validator-demo")
|
| 1042 |
+
validation_notes = gr.Textbox(label="Notes", placeholder="Optional")
|
| 1043 |
|
| 1044 |
with gr.Row():
|
| 1045 |
+
approve_btn = gr.Button("Mark positive")
|
| 1046 |
+
reject_btn = gr.Button("Mark negative")
|
| 1047 |
+
uncertain_btn = gr.Button("Uncertain")
|
| 1048 |
+
skip_btn = gr.Button("Skip")
|
| 1049 |
+
reapply_btn = gr.Button("Reapply validation after conflict")
|
| 1050 |
|
| 1051 |
with gr.Row():
|
| 1052 |
+
batch_approve_conflicts_btn = gr.Button("Approve all conflicts")
|
| 1053 |
+
batch_reject_conflicts_btn = gr.Button("Reject all conflicts")
|
| 1054 |
|
| 1055 |
+
report_btn = gr.Button("Generate validation report")
|
| 1056 |
|
| 1057 |
+
audio_player = gr.Audio(label="On-demand audio", type="filepath")
|
| 1058 |
cache_key_state = gr.State(value="")
|
| 1059 |
pending_status_state = gr.State(value="")
|
| 1060 |
conflict_detection_key_state = gr.State(value="")
|
| 1061 |
status = gr.Textbox(label="Status", interactive=False)
|
| 1062 |
+
report_box = gr.Textbox(label="Report", interactive=False)
|
| 1063 |
|
| 1064 |
# Keyboard shortcuts: 1=positive, 2=negative, 3=uncertain, 4=skip, R=reapply
|
| 1065 |
keyboard_shortcuts_info = gr.HTML(
|
| 1066 |
value="<div style='font-size: 12px; color: #666; padding: 8px; background-color: #f5f5f5; border-radius: 4px; margin-bottom: 10px;'>"
|
| 1067 |
+
"<strong>Keyboard shortcuts:</strong> 1=Positive | 2=Negative | 3=Uncertain | 4=Skip | R=Reapply"
|
| 1068 |
"</div>"
|
| 1069 |
"<script>"
|
| 1070 |
"document.addEventListener('keydown', function(event) {"
|
| 1071 |
" if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') return;"
|
| 1072 |
" const key = event.key.toLowerCase();"
|
| 1073 |
" let buttonText = null;"
|
| 1074 |
+
" if (key === '1') buttonText = 'Mark positive';"
|
| 1075 |
+
" else if (key === '2') buttonText = 'Mark negative';"
|
| 1076 |
+
" else if (key === '3') buttonText = 'Uncertain';"
|
| 1077 |
+
" else if (key === '4') buttonText = 'Skip';"
|
| 1078 |
+
" else if (key === 'r') buttonText = 'Reapply validation after conflict';"
|
| 1079 |
" if (buttonText) {"
|
| 1080 |
" event.preventDefault();"
|
| 1081 |
" const buttons = document.querySelectorAll('button');"
|
|
|
|
| 1106 |
page=page,
|
| 1107 |
scientific_name=species,
|
| 1108 |
min_confidence=confidence,
|
| 1109 |
+
page_size=runtime_config.page_size,
|
| 1110 |
validator_filter=validator_filter_value,
|
| 1111 |
status_filter=status_filter_value,
|
| 1112 |
updated_after=updated_after_value,
|
|
|
|
| 1512 |
# Initialize auth service
|
| 1513 |
auth_service = AuthService(session_ttl_minutes=120)
|
| 1514 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1515 |
# Initialize admin panel manager
|
| 1516 |
admin_manager = AdminPanelManager(auth_service)
|
| 1517 |
|
| 1518 |
+
runtime_config = RuntimeConfig.from_env()
|
| 1519 |
+
bootstrap_warning = _bootstrap_auth_and_projects(auth_service, admin_manager, runtime_config)
|
| 1520 |
+
queue_service, seed_warning = _build_detection_repository(
|
| 1521 |
+
[project["project_slug"] for project in admin_manager.list_projects()],
|
| 1522 |
+
seed_file_path=runtime_config.detection_seed_path,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1523 |
)
|
| 1524 |
+
service_ref: dict[str, DetectionQueueService] = {"queue": queue_service}
|
| 1525 |
+
audio_service = AudioFetchService(EphemeralCacheManager(ttl_seconds=300, max_files=128))
|
| 1526 |
+
validation_repository = AppendOnlyValidationRepository(base_dir=runtime_config.validation_base_dir)
|
| 1527 |
+
validation_service = ValidationService(validation_repository)
|
| 1528 |
|
| 1529 |
with gr.Blocks(title="BirdNET-Validator-App - Multi-Project") as wrapper:
|
| 1530 |
+
gr.Markdown("# BirdNET-Validator-App - Sprint 4: Multi-Project Security")
|
| 1531 |
+
gr.Markdown("**Version with authentication, project-level authorization, and admin panel**")
|
| 1532 |
+
if bootstrap_warning:
|
| 1533 |
+
gr.Markdown(bootstrap_warning)
|
| 1534 |
|
| 1535 |
# Session state
|
| 1536 |
session_state = gr.State(value=None)
|
| 1537 |
selected_project_state = gr.State(value=None)
|
| 1538 |
+
selected_dataset_repo_state = gr.State(value="")
|
| 1539 |
+
seed_warning_state = gr.State(value=seed_warning)
|
| 1540 |
|
| 1541 |
def _project_rows() -> list[list[object]]:
|
| 1542 |
projects = admin_manager.list_projects()
|
|
|
|
| 1575 |
|
| 1576 |
# ===== TAB 2: Admin Panel =====
|
| 1577 |
with gr.Tab("⚙️ Admin", id="admin_tab"):
|
| 1578 |
+
admin_info = gr.Markdown(value="⚠️ Login first")
|
| 1579 |
|
| 1580 |
def create_admin_display(session):
|
| 1581 |
"""Show admin panel or access denied message."""
|
| 1582 |
if session is None:
|
| 1583 |
return (
|
| 1584 |
+
"❌ **Not authenticated** — Login first in the **Login** tab.",
|
| 1585 |
gr.update(visible=False),
|
| 1586 |
)
|
| 1587 |
if session.role.value != "admin":
|
| 1588 |
return (
|
| 1589 |
+
"❌ **Access denied** — Only administrators can access this panel.",
|
| 1590 |
gr.update(visible=False),
|
| 1591 |
)
|
| 1592 |
return (
|
| 1593 |
+
f"✅ **Admin Panel** — Welcome, {session.username}. Use the controls below to manage projects and users.",
|
| 1594 |
gr.update(visible=True),
|
| 1595 |
)
|
| 1596 |
|
| 1597 |
with gr.Group(visible=False) as admin_controls:
|
| 1598 |
+
gr.Markdown("#### Registered Projects")
|
| 1599 |
|
| 1600 |
with gr.Row():
|
| 1601 |
create_project_slug = gr.Textbox(
|
| 1602 |
+
label="New Project Slug",
|
| 1603 |
placeholder="ex: amazonas-2026",
|
| 1604 |
)
|
| 1605 |
create_project_name = gr.Textbox(
|
| 1606 |
+
label="Project Name",
|
| 1607 |
placeholder="ex: Amazonas Survey 2026",
|
| 1608 |
)
|
| 1609 |
create_project_repo = gr.Textbox(
|
|
|
|
| 1615 |
|
| 1616 |
def create_project(session, slug: str, name: str, repo_id: str):
|
| 1617 |
if session is None or session.role.value != "admin":
|
| 1618 |
+
return "❌ Access denied. Only admin can create projects.", gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
|
| 1619 |
|
| 1620 |
slug = (slug or "").strip()
|
| 1621 |
name = (name or "").strip()
|
| 1622 |
repo_id = (repo_id or "").strip()
|
| 1623 |
if not slug or not name or not repo_id:
|
| 1624 |
+
return "⚠️ Fill slug, name, and repo id.", gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
|
| 1625 |
|
| 1626 |
created = admin_manager.register_project(
|
| 1627 |
Project(
|
|
|
|
| 1633 |
)
|
| 1634 |
if not created:
|
| 1635 |
return (
|
| 1636 |
+
f"⚠️ Project '{slug}' already exists.",
|
| 1637 |
_project_rows(),
|
| 1638 |
gr.update(choices=_project_slugs()),
|
| 1639 |
gr.update(),
|
| 1640 |
gr.update(),
|
| 1641 |
gr.update(),
|
| 1642 |
+
gr.update(),
|
| 1643 |
)
|
| 1644 |
|
| 1645 |
+
refreshed_service, refreshed_warning = _build_detection_repository(
|
| 1646 |
+
_project_slugs(),
|
| 1647 |
+
seed_file_path=runtime_config.detection_seed_path,
|
| 1648 |
+
)
|
| 1649 |
+
service_ref["queue"] = refreshed_service
|
| 1650 |
+
|
| 1651 |
return (
|
| 1652 |
+
f"✅ Project '{slug}' created successfully.",
|
| 1653 |
_project_rows(),
|
| 1654 |
gr.update(choices=_project_slugs(), value=slug),
|
| 1655 |
gr.update(value=""),
|
| 1656 |
gr.update(value=""),
|
| 1657 |
gr.update(value=""),
|
| 1658 |
+
refreshed_warning,
|
| 1659 |
)
|
| 1660 |
|
| 1661 |
+
create_project_btn = gr.Button("➕ Create Project", variant="primary")
|
| 1662 |
projects_table = gr.Dataframe(
|
| 1663 |
value=_project_rows(),
|
| 1664 |
headers=["project_slug", "name", "dataset_repo_id", "active"],
|
| 1665 |
interactive=False,
|
| 1666 |
)
|
| 1667 |
+
refresh_projects_btn = gr.Button("🔄 Refresh List")
|
| 1668 |
|
| 1669 |
def refresh_projects(session):
|
| 1670 |
if session is None or session.role.value != "admin":
|
|
|
|
| 1678 |
)
|
| 1679 |
|
| 1680 |
with gr.Group(visible=False) as admin_users_controls:
|
| 1681 |
+
gr.Markdown("#### Assign User to Project")
|
| 1682 |
with gr.Row():
|
| 1683 |
admin_username = gr.Textbox(
|
| 1684 |
+
label="Username", placeholder="validator_001"
|
| 1685 |
)
|
| 1686 |
admin_project = gr.Dropdown(
|
| 1687 |
choices=_project_slugs(),
|
| 1688 |
+
label="Project",
|
| 1689 |
)
|
| 1690 |
admin_role = gr.Dropdown(
|
| 1691 |
choices=["admin", "validator"],
|
|
|
|
| 1697 |
|
| 1698 |
def assign_user(session, username: str, project: str, role: str):
|
| 1699 |
if session is None or session.role.value != "admin":
|
| 1700 |
+
return "❌ Access denied. Only admin can assign users.", gr.update(), gr.update(), gr.update()
|
| 1701 |
success, msg = admin_manager.assign_user_to_project(
|
| 1702 |
username, project, role
|
| 1703 |
)
|
|
|
|
| 1705 |
return msg, gr.update(value=""), gr.update(value=None), gr.update(value="validator")
|
| 1706 |
return msg, gr.update(), gr.update(), gr.update()
|
| 1707 |
|
| 1708 |
+
assign_btn = gr.Button("✅ Assign", variant="primary")
|
| 1709 |
assign_btn.click(
|
| 1710 |
fn=assign_user,
|
| 1711 |
inputs=[session_state, admin_username, admin_project, admin_role],
|
|
|
|
| 1722 |
create_project_slug,
|
| 1723 |
create_project_name,
|
| 1724 |
create_project_repo,
|
| 1725 |
+
seed_warning_state,
|
| 1726 |
],
|
| 1727 |
)
|
| 1728 |
|
|
|
|
| 1739 |
)
|
| 1740 |
|
| 1741 |
# ===== TAB 3: Project Selection =====
|
| 1742 |
+
with gr.Tab("📁 Select Project", id="project_tab"):
|
| 1743 |
project_info_display = gr.Markdown(
|
| 1744 |
+
value="⚠️ Login first in the **Login** tab"
|
| 1745 |
)
|
| 1746 |
project_selector = gr.Dropdown(
|
| 1747 |
choices=[],
|
| 1748 |
+
label="Authorized Project",
|
| 1749 |
interactive=False,
|
| 1750 |
)
|
| 1751 |
|
|
|
|
| 1754 |
if session is None:
|
| 1755 |
return (
|
| 1756 |
gr.Dropdown(choices=[], value=None, interactive=False),
|
| 1757 |
+
"❌ Not authenticated. Login first.",
|
| 1758 |
None,
|
| 1759 |
+
"",
|
| 1760 |
)
|
| 1761 |
|
| 1762 |
projects = session.authorized_projects
|
| 1763 |
if not projects:
|
| 1764 |
return (
|
| 1765 |
gr.Dropdown(choices=[], value=None, interactive=False),
|
| 1766 |
+
"⚠️ **No projects assigned**\n\nYou do not have access to projects yet. Contact an administrator.",
|
| 1767 |
None,
|
| 1768 |
+
"",
|
| 1769 |
)
|
| 1770 |
|
| 1771 |
selected = projects[0]
|
| 1772 |
role = auth_service.get_user_role_for_project(session.username, selected)
|
| 1773 |
role_label = role.value.upper() if role else "UNKNOWN"
|
| 1774 |
+
selected_project = admin_manager.get_project(selected)
|
| 1775 |
+
dataset_repo_id = selected_project.dataset_repo_id if selected_project else ""
|
| 1776 |
return (
|
| 1777 |
gr.Dropdown(choices=projects, value=selected, interactive=True),
|
| 1778 |
f"📁 **Project:** {selected} | **Your Role:** {role_label}",
|
| 1779 |
selected,
|
| 1780 |
+
dataset_repo_id,
|
| 1781 |
)
|
| 1782 |
|
| 1783 |
session_state.change(
|
| 1784 |
fn=update_project_selector,
|
| 1785 |
inputs=[session_state],
|
| 1786 |
+
outputs=[project_selector, project_info_display, selected_project_state, selected_dataset_repo_state],
|
| 1787 |
)
|
| 1788 |
|
| 1789 |
def update_selected_project(selected: str, session):
|
| 1790 |
"""Update state when project is selected."""
|
| 1791 |
if session and selected:
|
| 1792 |
+
selected_project = admin_manager.get_project(selected)
|
| 1793 |
+
dataset_repo_id = selected_project.dataset_repo_id if selected_project else ""
|
| 1794 |
+
return selected, dataset_repo_id
|
| 1795 |
+
return None, ""
|
| 1796 |
|
| 1797 |
project_selector.change(
|
| 1798 |
fn=update_selected_project,
|
| 1799 |
inputs=[project_selector, session_state],
|
| 1800 |
+
outputs=[selected_project_state, selected_dataset_repo_state],
|
| 1801 |
)
|
| 1802 |
|
| 1803 |
# ===== TAB 4: Validation =====
|
| 1804 |
+
with gr.Tab("✓ Validation", id="validation_tab"):
|
| 1805 |
validation_status = gr.Markdown(
|
| 1806 |
+
value="ℹ️ Login and select a project to start"
|
| 1807 |
+
)
|
| 1808 |
+
queue_badge = gr.HTML(value=_build_queue_badge(service_ref["queue"], None))
|
| 1809 |
+
seed_warning_banner = gr.Markdown(value="", visible=False)
|
| 1810 |
+
|
| 1811 |
+
def render_seed_warning(warning_text: str):
|
| 1812 |
+
text = (warning_text or "").strip()
|
| 1813 |
+
if not text:
|
| 1814 |
+
return gr.update(value="", visible=False)
|
| 1815 |
+
return gr.update(value=text, visible=True)
|
| 1816 |
+
|
| 1817 |
+
seed_warning_state.change(
|
| 1818 |
+
fn=render_seed_warning,
|
| 1819 |
+
inputs=[seed_warning_state],
|
| 1820 |
+
outputs=[seed_warning_banner],
|
| 1821 |
+
)
|
| 1822 |
+
wrapper.load(
|
| 1823 |
+
fn=render_seed_warning,
|
| 1824 |
+
inputs=[seed_warning_state],
|
| 1825 |
+
outputs=[seed_warning_banner],
|
| 1826 |
)
|
| 1827 |
|
| 1828 |
+
def get_validation_status(session, selected_project, dataset_repo_id):
|
| 1829 |
"""Show status message based on login/project state."""
|
| 1830 |
if session is None:
|
| 1831 |
+
return "❌ **Not authenticated** — Login first in the **Login** tab"
|
| 1832 |
if selected_project is None:
|
| 1833 |
+
return f"⚠️ **Project not selected** — Select a project in the **Select Project** tab"
|
| 1834 |
+
total_detections = _get_project_detection_count(service_ref["queue"], selected_project)
|
| 1835 |
+
return (
|
| 1836 |
+
f"✅ **Ready to validate** — Project: **{selected_project}** | "
|
| 1837 |
+
f"User: **{session.username}** | Dataset: **{dataset_repo_id or 'not set'}** | "
|
| 1838 |
+
f"Loaded detections: **{total_detections}**"
|
| 1839 |
+
)
|
| 1840 |
|
| 1841 |
session_state.change(
|
| 1842 |
+
fn=lambda s, p, r: get_validation_status(s, p, r),
|
| 1843 |
+
inputs=[session_state, selected_project_state, selected_dataset_repo_state],
|
| 1844 |
outputs=[validation_status],
|
| 1845 |
)
|
| 1846 |
+
session_state.change(
|
| 1847 |
+
fn=lambda p: _build_queue_badge(service_ref["queue"], p),
|
| 1848 |
+
inputs=[selected_project_state],
|
| 1849 |
+
outputs=[queue_badge],
|
| 1850 |
+
)
|
| 1851 |
|
| 1852 |
selected_project_state.change(
|
| 1853 |
+
fn=lambda s, p, r: get_validation_status(s, p, r),
|
| 1854 |
+
inputs=[session_state, selected_project_state, selected_dataset_repo_state],
|
| 1855 |
outputs=[validation_status],
|
| 1856 |
)
|
| 1857 |
+
selected_project_state.change(
|
| 1858 |
+
fn=lambda p: _build_queue_badge(service_ref["queue"], p),
|
| 1859 |
+
inputs=[selected_project_state],
|
| 1860 |
+
outputs=[queue_badge],
|
| 1861 |
+
)
|
| 1862 |
+
|
| 1863 |
+
selected_dataset_repo_state.change(
|
| 1864 |
+
fn=lambda s, p, r: get_validation_status(s, p, r),
|
| 1865 |
+
inputs=[session_state, selected_project_state, selected_dataset_repo_state],
|
| 1866 |
+
outputs=[validation_status],
|
| 1867 |
+
)
|
| 1868 |
+
|
| 1869 |
+
wrapper.load(
|
| 1870 |
+
fn=lambda p: _build_queue_badge(service_ref["queue"], p),
|
| 1871 |
+
inputs=[selected_project_state],
|
| 1872 |
+
outputs=[queue_badge],
|
| 1873 |
+
)
|
| 1874 |
|
|
|
|
|
|
|
| 1875 |
gr.Markdown("---")
|
| 1876 |
+
dataset_repo = gr.Textbox(label="Dataset repo", interactive=False)
|
| 1877 |
+
|
| 1878 |
+
with gr.Row():
|
| 1879 |
+
species_filter = gr.Textbox(label="Species filter", placeholder="Ex: Cyanocorax cyanopogon")
|
| 1880 |
+
min_confidence = gr.Slider(label="Minimum confidence", minimum=0.0, maximum=1.0, step=0.01, value=0.0)
|
| 1881 |
+
show_conflicts_only = gr.Checkbox(label="Show only conflicts", value=False)
|
| 1882 |
+
|
| 1883 |
+
with gr.Row():
|
| 1884 |
+
validator_filter = gr.Textbox(label="Validator filter", placeholder="Ex: validator-demo")
|
| 1885 |
+
validation_status_filter = gr.Dropdown(
|
| 1886 |
+
label="Status filter",
|
| 1887 |
+
choices=["all", "pending", "positive", "negative", "uncertain", "skip"],
|
| 1888 |
+
value="all",
|
| 1889 |
+
)
|
| 1890 |
+
updated_after_filter = gr.DateTime(label="Updated since", include_time=False, type="string")
|
| 1891 |
+
|
| 1892 |
+
with gr.Row():
|
| 1893 |
+
prev_btn = gr.Button("Previous page")
|
| 1894 |
+
next_btn = gr.Button("Next page")
|
| 1895 |
+
refresh_btn = gr.Button("Apply filters")
|
| 1896 |
+
|
| 1897 |
+
page_state = gr.State(value=1)
|
| 1898 |
+
table = gr.Dataframe(
|
| 1899 |
+
headers=[
|
| 1900 |
+
"detection_key",
|
| 1901 |
+
"audio_id",
|
| 1902 |
+
"scientific_name",
|
| 1903 |
+
"confidence",
|
| 1904 |
+
"start_time",
|
| 1905 |
+
"end_time",
|
| 1906 |
+
"validation_status",
|
| 1907 |
+
"version",
|
| 1908 |
+
"conflict_flag",
|
| 1909 |
+
"conflict_severity",
|
| 1910 |
+
],
|
| 1911 |
+
label="Detections",
|
| 1912 |
interactive=False,
|
| 1913 |
+
)
|
| 1914 |
+
selected_index = gr.Number(label="Selected row", value=0, precision=0)
|
| 1915 |
+
|
| 1916 |
+
with gr.Row():
|
| 1917 |
+
load_audio_btn = gr.Button("Load selected audio")
|
| 1918 |
+
clear_audio_btn = gr.Button("Clear cache after validation")
|
| 1919 |
+
|
| 1920 |
+
with gr.Row():
|
| 1921 |
+
validator_name = gr.Textbox(label="Validator", value="validator-demo")
|
| 1922 |
+
validation_notes = gr.Textbox(label="Notes", placeholder="Optional")
|
| 1923 |
+
|
| 1924 |
+
with gr.Row():
|
| 1925 |
+
approve_btn = gr.Button("Mark positive")
|
| 1926 |
+
reject_btn = gr.Button("Mark negative")
|
| 1927 |
+
uncertain_btn = gr.Button("Uncertain")
|
| 1928 |
+
skip_btn = gr.Button("Skip")
|
| 1929 |
+
reapply_btn = gr.Button("Reapply validation after conflict")
|
| 1930 |
+
|
| 1931 |
+
with gr.Row():
|
| 1932 |
+
batch_approve_conflicts_btn = gr.Button("Approve all conflicts")
|
| 1933 |
+
batch_reject_conflicts_btn = gr.Button("Reject all conflicts")
|
| 1934 |
+
|
| 1935 |
+
report_btn = gr.Button("Generate validation report")
|
| 1936 |
+
audio_player = gr.Audio(label="On-demand audio", type="filepath")
|
| 1937 |
+
cache_key_state = gr.State(value="")
|
| 1938 |
+
pending_status_state = gr.State(value="")
|
| 1939 |
+
conflict_detection_key_state = gr.State(value="")
|
| 1940 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 1941 |
+
report_box = gr.Textbox(label="Report", interactive=False)
|
| 1942 |
+
|
| 1943 |
+
def refresh(
|
| 1944 |
+
project_slug: str,
|
| 1945 |
+
page: int,
|
| 1946 |
+
species: str,
|
| 1947 |
+
confidence: float,
|
| 1948 |
+
validator_filter_value: str,
|
| 1949 |
+
status_filter_value: str,
|
| 1950 |
+
updated_after_value: object,
|
| 1951 |
+
only_conflicts: bool,
|
| 1952 |
+
):
|
| 1953 |
+
if not project_slug:
|
| 1954 |
+
return [], "Select a project to load the queue", 1
|
| 1955 |
+
return _page_to_table(
|
| 1956 |
+
service=service_ref["queue"],
|
| 1957 |
+
snapshot_reader=validation_repository,
|
| 1958 |
+
project_slug=project_slug,
|
| 1959 |
+
page=page,
|
| 1960 |
+
scientific_name=species,
|
| 1961 |
+
min_confidence=confidence,
|
| 1962 |
+
page_size=runtime_config.page_size,
|
| 1963 |
+
validator_filter=validator_filter_value,
|
| 1964 |
+
status_filter=status_filter_value,
|
| 1965 |
+
updated_after=updated_after_value,
|
| 1966 |
+
show_conflicts_only=only_conflicts,
|
| 1967 |
+
)
|
| 1968 |
+
|
| 1969 |
+
def go_next(
|
| 1970 |
+
project_slug: str,
|
| 1971 |
+
page: int,
|
| 1972 |
+
species: str,
|
| 1973 |
+
confidence: float,
|
| 1974 |
+
validator_filter_value: str,
|
| 1975 |
+
status_filter_value: str,
|
| 1976 |
+
updated_after_value: object,
|
| 1977 |
+
only_conflicts: bool,
|
| 1978 |
+
):
|
| 1979 |
+
return refresh(
|
| 1980 |
+
project_slug,
|
| 1981 |
+
page + 1,
|
| 1982 |
+
species,
|
| 1983 |
+
confidence,
|
| 1984 |
+
validator_filter_value,
|
| 1985 |
+
status_filter_value,
|
| 1986 |
+
updated_after_value,
|
| 1987 |
+
only_conflicts,
|
| 1988 |
+
)
|
| 1989 |
+
|
| 1990 |
+
def go_prev(
|
| 1991 |
+
project_slug: str,
|
| 1992 |
+
page: int,
|
| 1993 |
+
species: str,
|
| 1994 |
+
confidence: float,
|
| 1995 |
+
validator_filter_value: str,
|
| 1996 |
+
status_filter_value: str,
|
| 1997 |
+
updated_after_value: object,
|
| 1998 |
+
only_conflicts: bool,
|
| 1999 |
+
):
|
| 2000 |
+
return refresh(
|
| 2001 |
+
project_slug,
|
| 2002 |
+
max(1, page - 1),
|
| 2003 |
+
species,
|
| 2004 |
+
confidence,
|
| 2005 |
+
validator_filter_value,
|
| 2006 |
+
status_filter_value,
|
| 2007 |
+
updated_after_value,
|
| 2008 |
+
only_conflicts,
|
| 2009 |
+
)
|
| 2010 |
+
|
| 2011 |
+
def refresh_for_selected_project(project_slug: str):
|
| 2012 |
+
return refresh(project_slug, 1, "", 0.0, "", "all", "", False)
|
| 2013 |
+
|
| 2014 |
+
def save_for_project(
|
| 2015 |
+
project_slug: str,
|
| 2016 |
+
status_value: str,
|
| 2017 |
+
rows: object,
|
| 2018 |
+
idx: int,
|
| 2019 |
+
name: str,
|
| 2020 |
+
notes: str,
|
| 2021 |
+
cache_key: str,
|
| 2022 |
+
page: int,
|
| 2023 |
+
species: str,
|
| 2024 |
+
confidence: float,
|
| 2025 |
+
validator_filter_value: str,
|
| 2026 |
+
status_filter_value: str,
|
| 2027 |
+
updated_after_value: object,
|
| 2028 |
+
only_conflicts: bool,
|
| 2029 |
+
):
|
| 2030 |
+
if not project_slug:
|
| 2031 |
+
return "Select a project before validating", cache_key, None, rows, page, idx, "", ""
|
| 2032 |
+
return _save_selected_validation_with_refresh(
|
| 2033 |
+
validation_service=validation_service,
|
| 2034 |
+
audio_service=audio_service,
|
| 2035 |
+
queue_service=service_ref["queue"],
|
| 2036 |
+
snapshot_reader=validation_repository,
|
| 2037 |
+
project_slug=project_slug,
|
| 2038 |
+
rows=rows,
|
| 2039 |
+
selected_index=int(idx),
|
| 2040 |
+
status_value=status_value,
|
| 2041 |
+
validator=name,
|
| 2042 |
+
notes=notes,
|
| 2043 |
+
cache_key=cache_key,
|
| 2044 |
+
page=int(page),
|
| 2045 |
+
scientific_name=species,
|
| 2046 |
+
min_confidence=float(confidence),
|
| 2047 |
+
validator_filter=validator_filter_value,
|
| 2048 |
+
status_filter=status_filter_value,
|
| 2049 |
+
updated_after=updated_after_value,
|
| 2050 |
+
show_conflicts_only=bool(only_conflicts),
|
| 2051 |
+
)
|
| 2052 |
+
|
| 2053 |
+
def reapply_for_project(
|
| 2054 |
+
project_slug: str,
|
| 2055 |
+
rows: object,
|
| 2056 |
+
idx: int,
|
| 2057 |
+
pending_status: str,
|
| 2058 |
+
conflict_key: str,
|
| 2059 |
+
name: str,
|
| 2060 |
+
notes: str,
|
| 2061 |
+
cache_key: str,
|
| 2062 |
+
page: int,
|
| 2063 |
+
species: str,
|
| 2064 |
+
confidence: float,
|
| 2065 |
+
validator_filter_value: str,
|
| 2066 |
+
status_filter_value: str,
|
| 2067 |
+
updated_after_value: object,
|
| 2068 |
+
only_conflicts: bool,
|
| 2069 |
+
):
|
| 2070 |
+
if not project_slug:
|
| 2071 |
+
return "Select a project before reapplying", cache_key, None, rows, page, idx, pending_status, conflict_key
|
| 2072 |
+
return _reapply_last_conflict_validation_with_refresh(
|
| 2073 |
+
validation_service=validation_service,
|
| 2074 |
+
audio_service=audio_service,
|
| 2075 |
+
queue_service=service_ref["queue"],
|
| 2076 |
+
snapshot_reader=validation_repository,
|
| 2077 |
+
project_slug=project_slug,
|
| 2078 |
+
rows=rows,
|
| 2079 |
+
selected_index=int(idx),
|
| 2080 |
+
pending_status_value=pending_status,
|
| 2081 |
+
conflict_detection_key=conflict_key,
|
| 2082 |
+
validator=name,
|
| 2083 |
+
notes=notes,
|
| 2084 |
+
cache_key=cache_key,
|
| 2085 |
+
page=int(page),
|
| 2086 |
+
scientific_name=species,
|
| 2087 |
+
min_confidence=float(confidence),
|
| 2088 |
+
validator_filter=validator_filter_value,
|
| 2089 |
+
status_filter=status_filter_value,
|
| 2090 |
+
updated_after=updated_after_value,
|
| 2091 |
+
show_conflicts_only=bool(only_conflicts),
|
| 2092 |
+
)
|
| 2093 |
+
|
| 2094 |
+
def batch_for_project(
|
| 2095 |
+
project_slug: str,
|
| 2096 |
+
rows: object,
|
| 2097 |
+
status_value: str,
|
| 2098 |
+
name: str,
|
| 2099 |
+
notes: str,
|
| 2100 |
+
cache_key: str,
|
| 2101 |
+
page: int,
|
| 2102 |
+
species: str,
|
| 2103 |
+
confidence: float,
|
| 2104 |
+
validator_filter_value: str,
|
| 2105 |
+
status_filter_value: str,
|
| 2106 |
+
updated_after_value: object,
|
| 2107 |
+
):
|
| 2108 |
+
if not project_slug:
|
| 2109 |
+
return "Select a project before validating", cache_key, None, rows, page
|
| 2110 |
+
return _batch_validate_conflicts(
|
| 2111 |
+
validation_service=validation_service,
|
| 2112 |
+
audio_service=audio_service,
|
| 2113 |
+
queue_service=service_ref["queue"],
|
| 2114 |
+
snapshot_reader=validation_repository,
|
| 2115 |
+
project_slug=project_slug,
|
| 2116 |
+
rows=rows,
|
| 2117 |
+
status_value=status_value,
|
| 2118 |
+
validator=name,
|
| 2119 |
+
notes=notes,
|
| 2120 |
+
cache_key=cache_key,
|
| 2121 |
+
page=int(page),
|
| 2122 |
+
scientific_name=species,
|
| 2123 |
+
min_confidence=float(confidence),
|
| 2124 |
+
validator_filter=validator_filter_value,
|
| 2125 |
+
status_filter=status_filter_value,
|
| 2126 |
+
updated_after=updated_after_value,
|
| 2127 |
+
)
|
| 2128 |
+
|
| 2129 |
+
def build_report_for_project(project_slug: str) -> str:
|
| 2130 |
+
if not project_slug:
|
| 2131 |
+
return "Select a project to generate report"
|
| 2132 |
+
return _build_validation_report(validation_repository, project_slug)
|
| 2133 |
+
|
| 2134 |
+
def on_select(evt: gr.SelectData):
|
| 2135 |
+
return evt.index[0]
|
| 2136 |
+
|
| 2137 |
+
table.select(fn=on_select, inputs=None, outputs=[selected_index])
|
| 2138 |
+
refresh_btn.click(
|
| 2139 |
+
fn=refresh,
|
| 2140 |
+
inputs=[
|
| 2141 |
+
selected_project_state,
|
| 2142 |
+
page_state,
|
| 2143 |
+
species_filter,
|
| 2144 |
+
min_confidence,
|
| 2145 |
+
validator_filter,
|
| 2146 |
+
validation_status_filter,
|
| 2147 |
+
updated_after_filter,
|
| 2148 |
+
show_conflicts_only,
|
| 2149 |
+
],
|
| 2150 |
+
outputs=[table, status, page_state],
|
| 2151 |
+
)
|
| 2152 |
+
next_btn.click(
|
| 2153 |
+
fn=go_next,
|
| 2154 |
+
inputs=[
|
| 2155 |
+
selected_project_state,
|
| 2156 |
+
page_state,
|
| 2157 |
+
species_filter,
|
| 2158 |
+
min_confidence,
|
| 2159 |
+
validator_filter,
|
| 2160 |
+
validation_status_filter,
|
| 2161 |
+
updated_after_filter,
|
| 2162 |
+
show_conflicts_only,
|
| 2163 |
+
],
|
| 2164 |
+
outputs=[table, status, page_state],
|
| 2165 |
+
)
|
| 2166 |
+
prev_btn.click(
|
| 2167 |
+
fn=go_prev,
|
| 2168 |
+
inputs=[
|
| 2169 |
+
selected_project_state,
|
| 2170 |
+
page_state,
|
| 2171 |
+
species_filter,
|
| 2172 |
+
min_confidence,
|
| 2173 |
+
validator_filter,
|
| 2174 |
+
validation_status_filter,
|
| 2175 |
+
updated_after_filter,
|
| 2176 |
+
show_conflicts_only,
|
| 2177 |
+
],
|
| 2178 |
+
outputs=[table, status, page_state],
|
| 2179 |
+
)
|
| 2180 |
+
|
| 2181 |
+
selected_dataset_repo_state.change(
|
| 2182 |
+
fn=lambda repo_id: gr.update(value=repo_id),
|
| 2183 |
+
inputs=[selected_dataset_repo_state],
|
| 2184 |
+
outputs=[dataset_repo],
|
| 2185 |
+
)
|
| 2186 |
+
selected_project_state.change(
|
| 2187 |
+
fn=refresh_for_selected_project,
|
| 2188 |
+
inputs=[selected_project_state],
|
| 2189 |
+
outputs=[table, status, page_state],
|
| 2190 |
+
)
|
| 2191 |
+
|
| 2192 |
+
load_audio_btn.click(
|
| 2193 |
+
fn=lambda repo, rows, idx, cache_key: _fetch_selected_audio(
|
| 2194 |
+
audio_service=audio_service,
|
| 2195 |
+
dataset_repo=repo,
|
| 2196 |
+
rows=rows,
|
| 2197 |
+
selected_index=int(idx),
|
| 2198 |
+
previous_cache_key=cache_key,
|
| 2199 |
+
),
|
| 2200 |
+
inputs=[dataset_repo, table, selected_index, cache_key_state],
|
| 2201 |
+
outputs=[audio_player, cache_key_state, status],
|
| 2202 |
+
)
|
| 2203 |
+
clear_audio_btn.click(
|
| 2204 |
+
fn=lambda cache_key: _cleanup_selected_audio(audio_service=audio_service, cache_key=cache_key),
|
| 2205 |
+
inputs=[cache_key_state],
|
| 2206 |
+
outputs=[status, audio_player],
|
| 2207 |
+
)
|
| 2208 |
+
|
| 2209 |
+
approve_btn.click(
|
| 2210 |
+
fn=lambda project_slug, rows, idx, name, notes, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value, only_conflicts: save_for_project(
|
| 2211 |
+
project_slug,
|
| 2212 |
+
"positive",
|
| 2213 |
+
rows,
|
| 2214 |
+
idx,
|
| 2215 |
+
name,
|
| 2216 |
+
notes,
|
| 2217 |
+
cache_key,
|
| 2218 |
+
page,
|
| 2219 |
+
species,
|
| 2220 |
+
confidence,
|
| 2221 |
+
validator_filter_value,
|
| 2222 |
+
status_filter_value,
|
| 2223 |
+
updated_after_value,
|
| 2224 |
+
only_conflicts,
|
| 2225 |
+
),
|
| 2226 |
+
inputs=[
|
| 2227 |
+
selected_project_state,
|
| 2228 |
+
table,
|
| 2229 |
+
selected_index,
|
| 2230 |
+
validator_name,
|
| 2231 |
+
validation_notes,
|
| 2232 |
+
cache_key_state,
|
| 2233 |
+
page_state,
|
| 2234 |
+
species_filter,
|
| 2235 |
+
min_confidence,
|
| 2236 |
+
validator_filter,
|
| 2237 |
+
validation_status_filter,
|
| 2238 |
+
updated_after_filter,
|
| 2239 |
+
show_conflicts_only,
|
| 2240 |
+
],
|
| 2241 |
+
outputs=[status, cache_key_state, audio_player, table, page_state, selected_index, pending_status_state, conflict_detection_key_state],
|
| 2242 |
+
)
|
| 2243 |
+
reject_btn.click(
|
| 2244 |
+
fn=lambda project_slug, rows, idx, name, notes, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value, only_conflicts: save_for_project(
|
| 2245 |
+
project_slug,
|
| 2246 |
+
"negative",
|
| 2247 |
+
rows,
|
| 2248 |
+
idx,
|
| 2249 |
+
name,
|
| 2250 |
+
notes,
|
| 2251 |
+
cache_key,
|
| 2252 |
+
page,
|
| 2253 |
+
species,
|
| 2254 |
+
confidence,
|
| 2255 |
+
validator_filter_value,
|
| 2256 |
+
status_filter_value,
|
| 2257 |
+
updated_after_value,
|
| 2258 |
+
only_conflicts,
|
| 2259 |
+
),
|
| 2260 |
+
inputs=[
|
| 2261 |
+
selected_project_state,
|
| 2262 |
+
table,
|
| 2263 |
+
selected_index,
|
| 2264 |
+
validator_name,
|
| 2265 |
+
validation_notes,
|
| 2266 |
+
cache_key_state,
|
| 2267 |
+
page_state,
|
| 2268 |
+
species_filter,
|
| 2269 |
+
min_confidence,
|
| 2270 |
+
validator_filter,
|
| 2271 |
+
validation_status_filter,
|
| 2272 |
+
updated_after_filter,
|
| 2273 |
+
show_conflicts_only,
|
| 2274 |
+
],
|
| 2275 |
+
outputs=[status, cache_key_state, audio_player, table, page_state, selected_index, pending_status_state, conflict_detection_key_state],
|
| 2276 |
+
)
|
| 2277 |
+
uncertain_btn.click(
|
| 2278 |
+
fn=lambda project_slug, rows, idx, name, notes, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value, only_conflicts: save_for_project(
|
| 2279 |
+
project_slug,
|
| 2280 |
+
"uncertain",
|
| 2281 |
+
rows,
|
| 2282 |
+
idx,
|
| 2283 |
+
name,
|
| 2284 |
+
notes,
|
| 2285 |
+
cache_key,
|
| 2286 |
+
page,
|
| 2287 |
+
species,
|
| 2288 |
+
confidence,
|
| 2289 |
+
validator_filter_value,
|
| 2290 |
+
status_filter_value,
|
| 2291 |
+
updated_after_value,
|
| 2292 |
+
only_conflicts,
|
| 2293 |
+
),
|
| 2294 |
+
inputs=[
|
| 2295 |
+
selected_project_state,
|
| 2296 |
+
table,
|
| 2297 |
+
selected_index,
|
| 2298 |
+
validator_name,
|
| 2299 |
+
validation_notes,
|
| 2300 |
+
cache_key_state,
|
| 2301 |
+
page_state,
|
| 2302 |
+
species_filter,
|
| 2303 |
+
min_confidence,
|
| 2304 |
+
validator_filter,
|
| 2305 |
+
validation_status_filter,
|
| 2306 |
+
updated_after_filter,
|
| 2307 |
+
show_conflicts_only,
|
| 2308 |
+
],
|
| 2309 |
+
outputs=[status, cache_key_state, audio_player, table, page_state, selected_index, pending_status_state, conflict_detection_key_state],
|
| 2310 |
+
)
|
| 2311 |
+
skip_btn.click(
|
| 2312 |
+
fn=lambda project_slug, rows, idx, name, notes, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value, only_conflicts: save_for_project(
|
| 2313 |
+
project_slug,
|
| 2314 |
+
"skip",
|
| 2315 |
+
rows,
|
| 2316 |
+
idx,
|
| 2317 |
+
name,
|
| 2318 |
+
notes,
|
| 2319 |
+
cache_key,
|
| 2320 |
+
page,
|
| 2321 |
+
species,
|
| 2322 |
+
confidence,
|
| 2323 |
+
validator_filter_value,
|
| 2324 |
+
status_filter_value,
|
| 2325 |
+
updated_after_value,
|
| 2326 |
+
only_conflicts,
|
| 2327 |
+
),
|
| 2328 |
+
inputs=[
|
| 2329 |
+
selected_project_state,
|
| 2330 |
+
table,
|
| 2331 |
+
selected_index,
|
| 2332 |
+
validator_name,
|
| 2333 |
+
validation_notes,
|
| 2334 |
+
cache_key_state,
|
| 2335 |
+
page_state,
|
| 2336 |
+
species_filter,
|
| 2337 |
+
min_confidence,
|
| 2338 |
+
validator_filter,
|
| 2339 |
+
validation_status_filter,
|
| 2340 |
+
updated_after_filter,
|
| 2341 |
+
show_conflicts_only,
|
| 2342 |
+
],
|
| 2343 |
+
outputs=[status, cache_key_state, audio_player, table, page_state, selected_index, pending_status_state, conflict_detection_key_state],
|
| 2344 |
+
)
|
| 2345 |
+
reapply_btn.click(
|
| 2346 |
+
fn=lambda project_slug, rows, idx, pending_status, conflict_key, name, notes, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value, only_conflicts: reapply_for_project(
|
| 2347 |
+
project_slug,
|
| 2348 |
+
rows,
|
| 2349 |
+
idx,
|
| 2350 |
+
pending_status,
|
| 2351 |
+
conflict_key,
|
| 2352 |
+
name,
|
| 2353 |
+
notes,
|
| 2354 |
+
cache_key,
|
| 2355 |
+
page,
|
| 2356 |
+
species,
|
| 2357 |
+
confidence,
|
| 2358 |
+
validator_filter_value,
|
| 2359 |
+
status_filter_value,
|
| 2360 |
+
updated_after_value,
|
| 2361 |
+
only_conflicts,
|
| 2362 |
+
),
|
| 2363 |
+
inputs=[
|
| 2364 |
+
selected_project_state,
|
| 2365 |
+
table,
|
| 2366 |
+
selected_index,
|
| 2367 |
+
pending_status_state,
|
| 2368 |
+
conflict_detection_key_state,
|
| 2369 |
+
validator_name,
|
| 2370 |
+
validation_notes,
|
| 2371 |
+
cache_key_state,
|
| 2372 |
+
page_state,
|
| 2373 |
+
species_filter,
|
| 2374 |
+
min_confidence,
|
| 2375 |
+
validator_filter,
|
| 2376 |
+
validation_status_filter,
|
| 2377 |
+
updated_after_filter,
|
| 2378 |
+
show_conflicts_only,
|
| 2379 |
+
],
|
| 2380 |
+
outputs=[status, cache_key_state, audio_player, table, page_state, selected_index, pending_status_state, conflict_detection_key_state],
|
| 2381 |
+
)
|
| 2382 |
+
|
| 2383 |
+
batch_approve_conflicts_btn.click(
|
| 2384 |
+
fn=lambda project_slug, rows, name, notes, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value: batch_for_project(
|
| 2385 |
+
project_slug,
|
| 2386 |
+
rows,
|
| 2387 |
+
"positive",
|
| 2388 |
+
name,
|
| 2389 |
+
notes,
|
| 2390 |
+
cache_key,
|
| 2391 |
+
page,
|
| 2392 |
+
species,
|
| 2393 |
+
confidence,
|
| 2394 |
+
validator_filter_value,
|
| 2395 |
+
status_filter_value,
|
| 2396 |
+
updated_after_value,
|
| 2397 |
+
),
|
| 2398 |
+
inputs=[
|
| 2399 |
+
selected_project_state,
|
| 2400 |
+
table,
|
| 2401 |
+
validator_name,
|
| 2402 |
+
validation_notes,
|
| 2403 |
+
cache_key_state,
|
| 2404 |
+
page_state,
|
| 2405 |
+
species_filter,
|
| 2406 |
+
min_confidence,
|
| 2407 |
+
validator_filter,
|
| 2408 |
+
validation_status_filter,
|
| 2409 |
+
updated_after_filter,
|
| 2410 |
+
],
|
| 2411 |
+
outputs=[status, cache_key_state, audio_player, table, page_state],
|
| 2412 |
+
)
|
| 2413 |
+
batch_reject_conflicts_btn.click(
|
| 2414 |
+
fn=lambda project_slug, rows, name, notes, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value: batch_for_project(
|
| 2415 |
+
project_slug,
|
| 2416 |
+
rows,
|
| 2417 |
+
"negative",
|
| 2418 |
+
name,
|
| 2419 |
+
notes,
|
| 2420 |
+
cache_key,
|
| 2421 |
+
page,
|
| 2422 |
+
species,
|
| 2423 |
+
confidence,
|
| 2424 |
+
validator_filter_value,
|
| 2425 |
+
status_filter_value,
|
| 2426 |
+
updated_after_value,
|
| 2427 |
+
),
|
| 2428 |
+
inputs=[
|
| 2429 |
+
selected_project_state,
|
| 2430 |
+
table,
|
| 2431 |
+
validator_name,
|
| 2432 |
+
validation_notes,
|
| 2433 |
+
cache_key_state,
|
| 2434 |
+
page_state,
|
| 2435 |
+
species_filter,
|
| 2436 |
+
min_confidence,
|
| 2437 |
+
validator_filter,
|
| 2438 |
+
validation_status_filter,
|
| 2439 |
+
updated_after_filter,
|
| 2440 |
+
],
|
| 2441 |
+
outputs=[status, cache_key_state, audio_player, table, page_state],
|
| 2442 |
+
)
|
| 2443 |
+
report_btn.click(
|
| 2444 |
+
fn=build_report_for_project,
|
| 2445 |
+
inputs=[selected_project_state],
|
| 2446 |
+
outputs=[report_box],
|
| 2447 |
)
|
| 2448 |
|
| 2449 |
return wrapper
|
|
@@ -0,0 +1,195 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
from src.domain.models import Detection
|
| 4 |
+
from src.repositories.append_only_validation_repository import AppendOnlyValidationRepository
|
| 5 |
+
from src.repositories.in_memory_detection_repository import InMemoryDetectionRepository
|
| 6 |
+
from src.services.detection_queue_service import DetectionQueueService
|
| 7 |
+
from src.services.validation_service import ValidationService
|
| 8 |
+
from src.ui.app_factory import _page_to_table, _save_selected_validation_with_refresh
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class FakeAudioService:
|
| 12 |
+
def __init__(self) -> None:
|
| 13 |
+
self.cleaned: list[str] = []
|
| 14 |
+
|
| 15 |
+
def cleanup_after_validation(self, cache_key: str) -> None:
|
| 16 |
+
self.cleaned.append(cache_key)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _seed_detections(repo: InMemoryDetectionRepository, project_slug: str) -> None:
|
| 20 |
+
repo.seed(
|
| 21 |
+
project_slug,
|
| 22 |
+
[
|
| 23 |
+
Detection(
|
| 24 |
+
detection_key="0000000000000001",
|
| 25 |
+
audio_id="audio_001",
|
| 26 |
+
scientific_name="Species A",
|
| 27 |
+
confidence=0.92,
|
| 28 |
+
start_time=0.0,
|
| 29 |
+
end_time=1.0,
|
| 30 |
+
),
|
| 31 |
+
Detection(
|
| 32 |
+
detection_key="0000000000000002",
|
| 33 |
+
audio_id="audio_002",
|
| 34 |
+
scientific_name="Species B",
|
| 35 |
+
confidence=0.81,
|
| 36 |
+
start_time=0.5,
|
| 37 |
+
end_time=1.5,
|
| 38 |
+
),
|
| 39 |
+
],
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _build_stack(tmp_path: Path, project_slug: str = "demo-project"):
|
| 44 |
+
detection_repo = InMemoryDetectionRepository()
|
| 45 |
+
_seed_detections(detection_repo, project_slug)
|
| 46 |
+
|
| 47 |
+
queue_service = DetectionQueueService(detection_repo)
|
| 48 |
+
validation_repo = AppendOnlyValidationRepository(base_dir=str(tmp_path))
|
| 49 |
+
validation_service = ValidationService(validation_repo)
|
| 50 |
+
return queue_service, validation_repo, validation_service
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def test_validation_write_refreshes_table_and_snapshot(tmp_path: Path) -> None:
|
| 54 |
+
project_slug = "demo-project"
|
| 55 |
+
queue_service, validation_repo, validation_service = _build_stack(tmp_path, project_slug)
|
| 56 |
+
audio_service = FakeAudioService()
|
| 57 |
+
|
| 58 |
+
rows, _, page = _page_to_table(
|
| 59 |
+
service=queue_service,
|
| 60 |
+
snapshot_reader=validation_repo,
|
| 61 |
+
project_slug=project_slug,
|
| 62 |
+
page=1,
|
| 63 |
+
scientific_name="",
|
| 64 |
+
min_confidence=0.0,
|
| 65 |
+
page_size=25,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
assert rows[0][6] == "pending"
|
| 69 |
+
assert rows[0][7] == 0
|
| 70 |
+
|
| 71 |
+
status, cache_key, _, refreshed_rows, refreshed_page, _, pending_status, conflict_key = (
|
| 72 |
+
_save_selected_validation_with_refresh(
|
| 73 |
+
validation_service=validation_service,
|
| 74 |
+
audio_service=audio_service,
|
| 75 |
+
queue_service=queue_service,
|
| 76 |
+
snapshot_reader=validation_repo,
|
| 77 |
+
project_slug=project_slug,
|
| 78 |
+
rows=rows,
|
| 79 |
+
selected_index=0,
|
| 80 |
+
status_value="positive",
|
| 81 |
+
validator="validator-a",
|
| 82 |
+
notes="confirmed",
|
| 83 |
+
cache_key="cache:audio_001",
|
| 84 |
+
page=page,
|
| 85 |
+
scientific_name="",
|
| 86 |
+
min_confidence=0.0,
|
| 87 |
+
validator_filter="",
|
| 88 |
+
status_filter="all",
|
| 89 |
+
updated_after=None,
|
| 90 |
+
show_conflicts_only=False,
|
| 91 |
+
)
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
assert "Validation saved" in status
|
| 95 |
+
assert cache_key == ""
|
| 96 |
+
assert refreshed_page == 1
|
| 97 |
+
assert pending_status == ""
|
| 98 |
+
assert conflict_key == ""
|
| 99 |
+
assert refreshed_rows[0][6] == "positive"
|
| 100 |
+
assert refreshed_rows[0][7] == 1
|
| 101 |
+
assert audio_service.cleaned == ["cache:audio_001"]
|
| 102 |
+
|
| 103 |
+
events = validation_repo.list_events(project_slug)
|
| 104 |
+
snapshot = validation_repo.load_current_snapshot(project_slug)
|
| 105 |
+
assert len(events) == 1
|
| 106 |
+
assert snapshot["0000000000000001"]["status"] == "positive"
|
| 107 |
+
assert snapshot["0000000000000001"]["version"] == 1
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def test_stale_version_returns_conflict_and_marks_conflict_row(tmp_path: Path) -> None:
|
| 111 |
+
project_slug = "demo-project"
|
| 112 |
+
queue_service, validation_repo, validation_service = _build_stack(tmp_path, project_slug)
|
| 113 |
+
audio_service = FakeAudioService()
|
| 114 |
+
|
| 115 |
+
stale_rows, _, page = _page_to_table(
|
| 116 |
+
service=queue_service,
|
| 117 |
+
snapshot_reader=validation_repo,
|
| 118 |
+
project_slug=project_slug,
|
| 119 |
+
page=1,
|
| 120 |
+
scientific_name="",
|
| 121 |
+
min_confidence=0.0,
|
| 122 |
+
page_size=25,
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
_ = validation_service.validate_detection(
|
| 126 |
+
project_slug=project_slug,
|
| 127 |
+
detection_key="0000000000000001",
|
| 128 |
+
status="positive",
|
| 129 |
+
validator="validator-a",
|
| 130 |
+
expected_version=0,
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
status, _, _, refreshed_rows, _, _, pending_status, conflict_key = _save_selected_validation_with_refresh(
|
| 134 |
+
validation_service=validation_service,
|
| 135 |
+
audio_service=audio_service,
|
| 136 |
+
queue_service=queue_service,
|
| 137 |
+
snapshot_reader=validation_repo,
|
| 138 |
+
project_slug=project_slug,
|
| 139 |
+
rows=stale_rows,
|
| 140 |
+
selected_index=0,
|
| 141 |
+
status_value="negative",
|
| 142 |
+
validator="validator-b",
|
| 143 |
+
notes="should conflict",
|
| 144 |
+
cache_key="",
|
| 145 |
+
page=page,
|
| 146 |
+
scientific_name="",
|
| 147 |
+
min_confidence=0.0,
|
| 148 |
+
validator_filter="",
|
| 149 |
+
status_filter="all",
|
| 150 |
+
updated_after=None,
|
| 151 |
+
show_conflicts_only=False,
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
assert "Concurrency conflict" in status
|
| 155 |
+
assert pending_status == "negative"
|
| 156 |
+
assert conflict_key == "0000000000000001"
|
| 157 |
+
|
| 158 |
+
conflict_row = next(row for row in refreshed_rows if row[0] == "0000000000000001")
|
| 159 |
+
assert conflict_row[8] == "CONFLICT"
|
| 160 |
+
assert conflict_row[9] == "HIGH"
|
| 161 |
+
|
| 162 |
+
snapshot = validation_repo.load_current_snapshot(project_slug)
|
| 163 |
+
assert snapshot["0000000000000001"]["status"] == "positive"
|
| 164 |
+
assert snapshot["0000000000000001"]["version"] == 1
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def test_snapshot_filters_work_with_real_validation_data(tmp_path: Path) -> None:
|
| 168 |
+
project_slug = "demo-project"
|
| 169 |
+
queue_service, validation_repo, validation_service = _build_stack(tmp_path, project_slug)
|
| 170 |
+
|
| 171 |
+
_ = validation_service.validate_detection(
|
| 172 |
+
project_slug=project_slug,
|
| 173 |
+
detection_key="0000000000000002",
|
| 174 |
+
status="uncertain",
|
| 175 |
+
validator="validator-z",
|
| 176 |
+
notes="needs review",
|
| 177 |
+
expected_version=0,
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
filtered_rows, status, _ = _page_to_table(
|
| 181 |
+
service=queue_service,
|
| 182 |
+
snapshot_reader=validation_repo,
|
| 183 |
+
project_slug=project_slug,
|
| 184 |
+
page=1,
|
| 185 |
+
scientific_name="",
|
| 186 |
+
min_confidence=0.0,
|
| 187 |
+
page_size=25,
|
| 188 |
+
validator_filter="validator-z",
|
| 189 |
+
status_filter="uncertain",
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
assert len(filtered_rows) == 1
|
| 193 |
+
assert filtered_rows[0][0] == "0000000000000002"
|
| 194 |
+
assert filtered_rows[0][6] == "uncertain"
|
| 195 |
+
assert "Shown: 1" in status
|
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from dataclasses import dataclass
|
| 2 |
from datetime import date
|
| 3 |
from pathlib import Path
|
|
|
|
| 4 |
|
| 5 |
import pandas as pd
|
| 6 |
|
|
@@ -17,7 +18,18 @@ from src.ui.app_factory import (
|
|
| 17 |
_reapply_last_conflict_validation_with_refresh,
|
| 18 |
_batch_validate_conflicts,
|
| 19 |
create_app,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
@dataclass
|
|
@@ -120,6 +132,9 @@ class FakeSnapshotReader:
|
|
| 120 |
|
| 121 |
|
| 122 |
class FakeQueueService:
|
|
|
|
|
|
|
|
|
|
| 123 |
class _Page:
|
| 124 |
def __init__(self) -> None:
|
| 125 |
self.page = 1
|
|
@@ -153,7 +168,7 @@ class FakeQueueService:
|
|
| 153 |
]
|
| 154 |
|
| 155 |
def get_page(self, **kwargs: object) -> "FakeQueueService._Page":
|
| 156 |
-
|
| 157 |
return FakeQueueService._Page()
|
| 158 |
|
| 159 |
|
|
@@ -181,7 +196,7 @@ def test_fetch_selected_audio_success() -> None:
|
|
| 181 |
|
| 182 |
assert path == "/tmp/audio_03.wav"
|
| 183 |
assert cache_key == "key:audio_03"
|
| 184 |
-
assert "Audio
|
| 185 |
|
| 186 |
|
| 187 |
def test_extract_detection_key_from_rows() -> None:
|
|
@@ -203,7 +218,7 @@ def test_fetch_selected_audio_validates_repo() -> None:
|
|
| 203 |
|
| 204 |
assert path is None
|
| 205 |
assert cache_key == ""
|
| 206 |
-
assert "
|
| 207 |
|
| 208 |
|
| 209 |
def test_cleanup_selected_audio() -> None:
|
|
@@ -211,7 +226,7 @@ def test_cleanup_selected_audio() -> None:
|
|
| 211 |
|
| 212 |
status, player_value = _cleanup_selected_audio(service, "key:audio_03")
|
| 213 |
|
| 214 |
-
assert "
|
| 215 |
assert player_value is None
|
| 216 |
assert service.cleaned == ["key:audio_03"]
|
| 217 |
|
|
@@ -233,7 +248,7 @@ def test_save_selected_validation_saves_and_cleans_audio_cache() -> None:
|
|
| 233 |
cache_key="cache:audio_11",
|
| 234 |
)
|
| 235 |
|
| 236 |
-
assert "
|
| 237 |
assert cache_key == ""
|
| 238 |
assert audio_path is None
|
| 239 |
assert len(validation_service.calls) == 1
|
|
@@ -259,7 +274,7 @@ def test_save_selected_validation_returns_conflict_message() -> None:
|
|
| 259 |
cache_key="cache:audio_11",
|
| 260 |
)
|
| 261 |
|
| 262 |
-
assert "
|
| 263 |
assert cache_key == "cache:audio_11"
|
| 264 |
assert audio_path is None
|
| 265 |
|
|
@@ -267,30 +282,32 @@ def test_save_selected_validation_returns_conflict_message() -> None:
|
|
| 267 |
def test_build_validation_report() -> None:
|
| 268 |
report = _build_validation_report(FakeSnapshotReader(), "demo-project")
|
| 269 |
|
| 270 |
-
assert "
|
| 271 |
-
assert "
|
| 272 |
-
assert "
|
| 273 |
assert "positive=1" in report
|
| 274 |
assert "negative=1" in report
|
| 275 |
|
| 276 |
|
| 277 |
def test_page_to_table_includes_validation_status() -> None:
|
|
|
|
| 278 |
rows, status, page = _page_to_table(
|
| 279 |
-
service=
|
| 280 |
snapshot_reader=FakeSnapshotReader(),
|
| 281 |
-
project_slug="
|
| 282 |
page=1,
|
| 283 |
scientific_name="",
|
| 284 |
min_confidence=0.0,
|
| 285 |
)
|
| 286 |
|
| 287 |
assert page == 1
|
| 288 |
-
assert "
|
| 289 |
assert rows[0][0] == "dkey_01"
|
| 290 |
assert rows[0][6] == "positive"
|
| 291 |
assert rows[0][7] == 2
|
| 292 |
assert rows[0][8] == ""
|
| 293 |
assert rows[0][9] == ""
|
|
|
|
| 294 |
|
| 295 |
|
| 296 |
def test_page_to_table_marks_conflict_row() -> None:
|
|
@@ -320,7 +337,7 @@ def test_page_to_table_conflicts_only_filter_hides_non_conflicts() -> None:
|
|
| 320 |
)
|
| 321 |
|
| 322 |
assert rows == []
|
| 323 |
-
assert "
|
| 324 |
|
| 325 |
|
| 326 |
def test_page_to_table_conflicts_only_filter_keeps_conflict_rows() -> None:
|
|
@@ -337,7 +354,7 @@ def test_page_to_table_conflicts_only_filter_keeps_conflict_rows() -> None:
|
|
| 337 |
|
| 338 |
assert len(rows) == 1
|
| 339 |
assert rows[0][8] == "CONFLICT"
|
| 340 |
-
assert "
|
| 341 |
|
| 342 |
|
| 343 |
def test_page_to_table_filters_by_validator() -> None:
|
|
@@ -433,7 +450,7 @@ def test_save_selected_validation_with_refresh_success() -> None:
|
|
| 433 |
show_conflicts_only=False,
|
| 434 |
)
|
| 435 |
|
| 436 |
-
assert "
|
| 437 |
assert cache_key == ""
|
| 438 |
assert audio_path is None
|
| 439 |
assert refreshed_page == 1
|
|
@@ -469,8 +486,8 @@ def test_save_selected_validation_with_refresh_conflict() -> None:
|
|
| 469 |
show_conflicts_only=False,
|
| 470 |
)
|
| 471 |
|
| 472 |
-
assert "
|
| 473 |
-
assert "
|
| 474 |
assert cache_key == "cache:audio_11"
|
| 475 |
assert audio_path is None
|
| 476 |
assert refreshed_page == 1
|
|
@@ -509,7 +526,7 @@ def test_reapply_last_conflict_validation_with_refresh() -> None:
|
|
| 509 |
show_conflicts_only=False,
|
| 510 |
)
|
| 511 |
|
| 512 |
-
assert "
|
| 513 |
assert cache_key == ""
|
| 514 |
assert audio_path is None
|
| 515 |
assert refreshed_page == 1
|
|
@@ -546,7 +563,7 @@ def test_reapply_last_conflict_without_pending_status() -> None:
|
|
| 546 |
show_conflicts_only=False,
|
| 547 |
)
|
| 548 |
|
| 549 |
-
assert "
|
| 550 |
assert pending_status == ""
|
| 551 |
assert conflict_key == ""
|
| 552 |
|
|
@@ -588,8 +605,8 @@ def test_batch_validate_conflicts_all_success() -> None:
|
|
| 588 |
updated_after="",
|
| 589 |
)
|
| 590 |
|
| 591 |
-
assert "
|
| 592 |
-
assert "2
|
| 593 |
assert cache_key == ""
|
| 594 |
assert refreshed_page == 1
|
| 595 |
assert len(validation_service.calls) == 2
|
|
@@ -622,5 +639,190 @@ def test_batch_validate_conflicts_no_conflicts() -> None:
|
|
| 622 |
updated_after="",
|
| 623 |
)
|
| 624 |
|
| 625 |
-
assert "
|
| 626 |
assert len(validation_service.calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from dataclasses import dataclass
|
| 2 |
from datetime import date
|
| 3 |
from pathlib import Path
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
import pandas as pd
|
| 7 |
|
|
|
|
| 18 |
_reapply_last_conflict_validation_with_refresh,
|
| 19 |
_batch_validate_conflicts,
|
| 20 |
create_app,
|
| 21 |
+
_load_seed_detections,
|
| 22 |
+
_validate_seed_file,
|
| 23 |
+
_build_detection_repository,
|
| 24 |
+
_get_project_detection_count,
|
| 25 |
+
_build_queue_badge,
|
| 26 |
+
_load_projects_from_file,
|
| 27 |
+
_load_user_access_from_file,
|
| 28 |
+
_bootstrap_auth_and_projects,
|
| 29 |
)
|
| 30 |
+
from src.auth.auth_service import AuthService
|
| 31 |
+
from src.config.runtime_config import RuntimeConfig
|
| 32 |
+
from src.ui.admin_panel import AdminPanelManager
|
| 33 |
|
| 34 |
|
| 35 |
@dataclass
|
|
|
|
| 132 |
|
| 133 |
|
| 134 |
class FakeQueueService:
|
| 135 |
+
def __init__(self) -> None:
|
| 136 |
+
self.last_kwargs: dict[str, object] = {}
|
| 137 |
+
|
| 138 |
class _Page:
|
| 139 |
def __init__(self) -> None:
|
| 140 |
self.page = 1
|
|
|
|
| 168 |
]
|
| 169 |
|
| 170 |
def get_page(self, **kwargs: object) -> "FakeQueueService._Page":
|
| 171 |
+
self.last_kwargs = kwargs
|
| 172 |
return FakeQueueService._Page()
|
| 173 |
|
| 174 |
|
|
|
|
| 196 |
|
| 197 |
assert path == "/tmp/audio_03.wav"
|
| 198 |
assert cache_key == "key:audio_03"
|
| 199 |
+
assert "Audio loaded" in status
|
| 200 |
|
| 201 |
|
| 202 |
def test_extract_detection_key_from_rows() -> None:
|
|
|
|
| 218 |
|
| 219 |
assert path is None
|
| 220 |
assert cache_key == ""
|
| 221 |
+
assert "Provide dataset repo" in status
|
| 222 |
|
| 223 |
|
| 224 |
def test_cleanup_selected_audio() -> None:
|
|
|
|
| 226 |
|
| 227 |
status, player_value = _cleanup_selected_audio(service, "key:audio_03")
|
| 228 |
|
| 229 |
+
assert "Audio cache cleaned" in status
|
| 230 |
assert player_value is None
|
| 231 |
assert service.cleaned == ["key:audio_03"]
|
| 232 |
|
|
|
|
| 248 |
cache_key="cache:audio_11",
|
| 249 |
)
|
| 250 |
|
| 251 |
+
assert "Validation saved" in status
|
| 252 |
assert cache_key == ""
|
| 253 |
assert audio_path is None
|
| 254 |
assert len(validation_service.calls) == 1
|
|
|
|
| 274 |
cache_key="cache:audio_11",
|
| 275 |
)
|
| 276 |
|
| 277 |
+
assert "Concurrency conflict" in status
|
| 278 |
assert cache_key == "cache:audio_11"
|
| 279 |
assert audio_path is None
|
| 280 |
|
|
|
|
| 282 |
def test_build_validation_report() -> None:
|
| 283 |
report = _build_validation_report(FakeSnapshotReader(), "demo-project")
|
| 284 |
|
| 285 |
+
assert "Project: demo-project" in report
|
| 286 |
+
assert "Append-only events: 2" in report
|
| 287 |
+
assert "Detections with current state: 2" in report
|
| 288 |
assert "positive=1" in report
|
| 289 |
assert "negative=1" in report
|
| 290 |
|
| 291 |
|
| 292 |
def test_page_to_table_includes_validation_status() -> None:
|
| 293 |
+
queue = FakeQueueService()
|
| 294 |
rows, status, page = _page_to_table(
|
| 295 |
+
service=queue,
|
| 296 |
snapshot_reader=FakeSnapshotReader(),
|
| 297 |
+
project_slug="kenya-2024",
|
| 298 |
page=1,
|
| 299 |
scientific_name="",
|
| 300 |
min_confidence=0.0,
|
| 301 |
)
|
| 302 |
|
| 303 |
assert page == 1
|
| 304 |
+
assert "Page 1/1" in status
|
| 305 |
assert rows[0][0] == "dkey_01"
|
| 306 |
assert rows[0][6] == "positive"
|
| 307 |
assert rows[0][7] == 2
|
| 308 |
assert rows[0][8] == ""
|
| 309 |
assert rows[0][9] == ""
|
| 310 |
+
assert queue.last_kwargs["project_slug"] == "kenya-2024"
|
| 311 |
|
| 312 |
|
| 313 |
def test_page_to_table_marks_conflict_row() -> None:
|
|
|
|
| 337 |
)
|
| 338 |
|
| 339 |
assert rows == []
|
| 340 |
+
assert "Conflicts only: 0 item(ns)" in status
|
| 341 |
|
| 342 |
|
| 343 |
def test_page_to_table_conflicts_only_filter_keeps_conflict_rows() -> None:
|
|
|
|
| 354 |
|
| 355 |
assert len(rows) == 1
|
| 356 |
assert rows[0][8] == "CONFLICT"
|
| 357 |
+
assert "Conflicts only: 1 item(ns)" in status
|
| 358 |
|
| 359 |
|
| 360 |
def test_page_to_table_filters_by_validator() -> None:
|
|
|
|
| 450 |
show_conflicts_only=False,
|
| 451 |
)
|
| 452 |
|
| 453 |
+
assert "Validation saved" in status
|
| 454 |
assert cache_key == ""
|
| 455 |
assert audio_path is None
|
| 456 |
assert refreshed_page == 1
|
|
|
|
| 486 |
show_conflicts_only=False,
|
| 487 |
)
|
| 488 |
|
| 489 |
+
assert "Concurrency conflict" in status
|
| 490 |
+
assert "Table reloaded" in status
|
| 491 |
assert cache_key == "cache:audio_11"
|
| 492 |
assert audio_path is None
|
| 493 |
assert refreshed_page == 1
|
|
|
|
| 526 |
show_conflicts_only=False,
|
| 527 |
)
|
| 528 |
|
| 529 |
+
assert "Validation saved" in status
|
| 530 |
assert cache_key == ""
|
| 531 |
assert audio_path is None
|
| 532 |
assert refreshed_page == 1
|
|
|
|
| 563 |
show_conflicts_only=False,
|
| 564 |
)
|
| 565 |
|
| 566 |
+
assert "No pending validation" in status
|
| 567 |
assert pending_status == ""
|
| 568 |
assert conflict_key == ""
|
| 569 |
|
|
|
|
| 605 |
updated_after="",
|
| 606 |
)
|
| 607 |
|
| 608 |
+
assert "Processed 2 conflicts" in status
|
| 609 |
+
assert "2 success" in status
|
| 610 |
assert cache_key == ""
|
| 611 |
assert refreshed_page == 1
|
| 612 |
assert len(validation_service.calls) == 2
|
|
|
|
| 639 |
updated_after="",
|
| 640 |
)
|
| 641 |
|
| 642 |
+
assert "No conflict detection" in status
|
| 643 |
assert len(validation_service.calls) == 0
|
| 644 |
+
|
| 645 |
+
|
| 646 |
+
def test_load_seed_detections_from_json_dict(tmp_path: Path) -> None:
|
| 647 |
+
payload = {
|
| 648 |
+
"kenya-2024": [
|
| 649 |
+
{
|
| 650 |
+
"detection_key": "0000000000001001",
|
| 651 |
+
"audio_id": "audio_1001",
|
| 652 |
+
"scientific_name": "Cyanocorax cyanopogon",
|
| 653 |
+
"confidence": 0.91,
|
| 654 |
+
"start_time": 0.0,
|
| 655 |
+
"end_time": 1.0,
|
| 656 |
+
}
|
| 657 |
+
]
|
| 658 |
+
}
|
| 659 |
+
seed_file = tmp_path / "detections.json"
|
| 660 |
+
seed_file.write_text(json.dumps(payload), encoding="utf-8")
|
| 661 |
+
|
| 662 |
+
result = _load_seed_detections(str(seed_file))
|
| 663 |
+
|
| 664 |
+
assert "kenya-2024" in result
|
| 665 |
+
assert len(result["kenya-2024"]) == 1
|
| 666 |
+
assert result["kenya-2024"][0].audio_id == "audio_1001"
|
| 667 |
+
|
| 668 |
+
|
| 669 |
+
def test_validate_seed_file_warns_for_invalid_shape(tmp_path: Path) -> None:
|
| 670 |
+
seed_file = tmp_path / "detections-invalid.json"
|
| 671 |
+
seed_file.write_text(json.dumps({"kenya-2024": {"wrong": True}}), encoding="utf-8")
|
| 672 |
+
|
| 673 |
+
warning = _validate_seed_file(str(seed_file))
|
| 674 |
+
|
| 675 |
+
assert "Invalid" in warning
|
| 676 |
+
|
| 677 |
+
|
| 678 |
+
def test_validate_seed_file_warns_when_file_missing(tmp_path: Path) -> None:
|
| 679 |
+
missing_path = tmp_path / "missing-seed.json"
|
| 680 |
+
|
| 681 |
+
warning = _validate_seed_file(str(missing_path))
|
| 682 |
+
|
| 683 |
+
assert "not found" in warning
|
| 684 |
+
assert "BIRDNET_DETECTIONS_FILE" in warning
|
| 685 |
+
|
| 686 |
+
|
| 687 |
+
def test_build_detection_repository_includes_new_project_defaults() -> None:
|
| 688 |
+
queue, warning = _build_detection_repository(["brand-new-project"], seed_file_path=None)
|
| 689 |
+
page = queue.get_page(project_slug="brand-new-project", page=1, page_size=10)
|
| 690 |
+
|
| 691 |
+
assert warning == ""
|
| 692 |
+
assert page.project_slug == "brand-new-project"
|
| 693 |
+
assert len(page.items) == 4
|
| 694 |
+
|
| 695 |
+
|
| 696 |
+
def test_get_project_detection_count_reads_total_items() -> None:
|
| 697 |
+
queue = FakeQueueService()
|
| 698 |
+
|
| 699 |
+
total = _get_project_detection_count(queue, "kenya-2024")
|
| 700 |
+
|
| 701 |
+
assert total == 2
|
| 702 |
+
|
| 703 |
+
|
| 704 |
+
def test_get_project_detection_count_handles_service_error() -> None:
|
| 705 |
+
class BrokenQueueService:
|
| 706 |
+
def get_page(self, **kwargs: object):
|
| 707 |
+
_ = kwargs
|
| 708 |
+
raise RuntimeError("boom")
|
| 709 |
+
|
| 710 |
+
total = _get_project_detection_count(BrokenQueueService(), "kenya-2024")
|
| 711 |
+
|
| 712 |
+
assert total == 0
|
| 713 |
+
|
| 714 |
+
|
| 715 |
+
def test_build_queue_badge_without_project() -> None:
|
| 716 |
+
badge = _build_queue_badge(FakeQueueService(), None)
|
| 717 |
+
|
| 718 |
+
assert "Queue: --" in badge
|
| 719 |
+
|
| 720 |
+
|
| 721 |
+
def test_build_queue_badge_with_project() -> None:
|
| 722 |
+
badge = _build_queue_badge(FakeQueueService(), "kenya-2024")
|
| 723 |
+
|
| 724 |
+
assert "Queue: 2" in badge
|
| 725 |
+
|
| 726 |
+
|
| 727 |
+
def test_fetch_selected_audio_repo_hint() -> None:
|
| 728 |
+
service = FakeAudioService()
|
| 729 |
+
rows = [["k1", "audio_03", "sp", 0.9, 0.0, 1.0]]
|
| 730 |
+
|
| 731 |
+
_, _, status = _fetch_selected_audio(
|
| 732 |
+
audio_service=service,
|
| 733 |
+
dataset_repo="",
|
| 734 |
+
rows=rows,
|
| 735 |
+
selected_index=0,
|
| 736 |
+
previous_cache_key="",
|
| 737 |
+
)
|
| 738 |
+
|
| 739 |
+
assert "owner/repo" in status
|
| 740 |
+
assert "Example" in status
|
| 741 |
+
|
| 742 |
+
|
| 743 |
+
def test_load_projects_from_file_reads_valid_payload(tmp_path: Path) -> None:
|
| 744 |
+
payload = [
|
| 745 |
+
{
|
| 746 |
+
"project_slug": "project-a",
|
| 747 |
+
"name": "Project A",
|
| 748 |
+
"dataset_repo_id": "org/project-a",
|
| 749 |
+
"active": True,
|
| 750 |
+
}
|
| 751 |
+
]
|
| 752 |
+
path = tmp_path / "projects.json"
|
| 753 |
+
path.write_text(json.dumps(payload), encoding="utf-8")
|
| 754 |
+
|
| 755 |
+
projects = _load_projects_from_file(str(path))
|
| 756 |
+
|
| 757 |
+
assert len(projects) == 1
|
| 758 |
+
assert projects[0].project_slug == "project-a"
|
| 759 |
+
|
| 760 |
+
|
| 761 |
+
def test_load_user_access_from_file_reads_valid_payload(tmp_path: Path) -> None:
|
| 762 |
+
payload = {
|
| 763 |
+
"validator_a": {"project-a": "validator"},
|
| 764 |
+
"admin_a": {"project-a": "admin", "project-b": "admin"},
|
| 765 |
+
}
|
| 766 |
+
path = tmp_path / "access.json"
|
| 767 |
+
path.write_text(json.dumps(payload), encoding="utf-8")
|
| 768 |
+
|
| 769 |
+
access = _load_user_access_from_file(str(path))
|
| 770 |
+
|
| 771 |
+
assert access["validator_a"]["project-a"].value == "validator"
|
| 772 |
+
assert access["admin_a"]["project-b"].value == "admin"
|
| 773 |
+
|
| 774 |
+
|
| 775 |
+
def test_bootstrap_auth_and_projects_uses_config_files_without_demo_fallback(tmp_path: Path) -> None:
|
| 776 |
+
projects_file = tmp_path / "projects.json"
|
| 777 |
+
projects_file.write_text(
|
| 778 |
+
json.dumps(
|
| 779 |
+
[
|
| 780 |
+
{
|
| 781 |
+
"project_slug": "project-a",
|
| 782 |
+
"name": "Project A",
|
| 783 |
+
"dataset_repo_id": "org/project-a",
|
| 784 |
+
"active": True,
|
| 785 |
+
}
|
| 786 |
+
]
|
| 787 |
+
),
|
| 788 |
+
encoding="utf-8",
|
| 789 |
+
)
|
| 790 |
+
users_file = tmp_path / "users.json"
|
| 791 |
+
users_file.write_text(
|
| 792 |
+
json.dumps({"validator_a": {"project-a": "validator"}}),
|
| 793 |
+
encoding="utf-8",
|
| 794 |
+
)
|
| 795 |
+
|
| 796 |
+
runtime_config = RuntimeConfig(
|
| 797 |
+
detection_seed_path=None,
|
| 798 |
+
validation_base_dir=str(tmp_path / "validations"),
|
| 799 |
+
page_size=25,
|
| 800 |
+
projects_file_path=str(projects_file),
|
| 801 |
+
user_access_file_path=str(users_file),
|
| 802 |
+
enable_demo_bootstrap=False,
|
| 803 |
+
)
|
| 804 |
+
auth_service = AuthService()
|
| 805 |
+
admin_manager = AdminPanelManager(auth_service)
|
| 806 |
+
|
| 807 |
+
warning = _bootstrap_auth_and_projects(auth_service, admin_manager, runtime_config)
|
| 808 |
+
|
| 809 |
+
assert warning == ""
|
| 810 |
+
assert auth_service.login("validator_a") is not None
|
| 811 |
+
assert any(p["project_slug"] == "project-a" for p in admin_manager.list_projects())
|
| 812 |
+
|
| 813 |
+
|
| 814 |
+
def test_bootstrap_auth_and_projects_warns_when_not_configured(tmp_path: Path) -> None:
|
| 815 |
+
runtime_config = RuntimeConfig(
|
| 816 |
+
detection_seed_path=None,
|
| 817 |
+
validation_base_dir=str(tmp_path / "validations"),
|
| 818 |
+
page_size=25,
|
| 819 |
+
projects_file_path=None,
|
| 820 |
+
user_access_file_path=None,
|
| 821 |
+
enable_demo_bootstrap=False,
|
| 822 |
+
)
|
| 823 |
+
auth_service = AuthService()
|
| 824 |
+
admin_manager = AdminPanelManager(auth_service)
|
| 825 |
+
|
| 826 |
+
warning = _bootstrap_auth_and_projects(auth_service, admin_manager, runtime_config)
|
| 827 |
+
|
| 828 |
+
assert "Production bootstrap incomplete" in warning
|
|
@@ -1,7 +1,7 @@
|
|
| 1 |
"""Unit tests for AuthService and ACL enforcement."""
|
| 2 |
|
| 3 |
import pytest
|
| 4 |
-
from datetime import datetime, timedelta
|
| 5 |
|
| 6 |
from src.auth.auth_service import AuthService, Session, UserProjectAccess
|
| 7 |
from src.domain.models import Role
|
|
@@ -84,7 +84,7 @@ class TestAuthService:
|
|
| 84 |
assert session is not None
|
| 85 |
|
| 86 |
# Manually set expiration to past
|
| 87 |
-
session.expires_at = datetime.
|
| 88 |
|
| 89 |
# Try to retrieve expired session
|
| 90 |
retrieved = short_ttl_service.get_session(session.session_id)
|
|
@@ -165,7 +165,7 @@ class TestAuthService:
|
|
| 165 |
session2 = auth_service.login("validator1")
|
| 166 |
|
| 167 |
# Expire session1
|
| 168 |
-
session1.expires_at = datetime.
|
| 169 |
|
| 170 |
# Cleanup
|
| 171 |
auth_service.cleanup_expired_sessions()
|
|
@@ -207,6 +207,35 @@ class TestAuthService:
|
|
| 207 |
assert len(projects) == 2
|
| 208 |
assert auth_service.get_user_role_for_project("user1", "project-2") == Role.admin
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
def test_session_isolation_between_users(self, auth_service, setup_users):
|
| 211 |
"""Test that sessions are isolated between users."""
|
| 212 |
session_admin = auth_service.login("admin_user")
|
|
@@ -253,7 +282,7 @@ class TestSessionModel:
|
|
| 253 |
|
| 254 |
def test_session_creation(self):
|
| 255 |
"""Test creating a session."""
|
| 256 |
-
now = datetime.
|
| 257 |
session = Session(
|
| 258 |
session_id="test-session-123",
|
| 259 |
username="test_user",
|
|
@@ -271,7 +300,7 @@ class TestSessionModel:
|
|
| 271 |
|
| 272 |
def test_session_expiration_check(self):
|
| 273 |
"""Test session expiration detection."""
|
| 274 |
-
now = datetime.
|
| 275 |
|
| 276 |
# Not expired
|
| 277 |
session = Session(
|
|
@@ -292,7 +321,7 @@ class TestSessionModel:
|
|
| 292 |
|
| 293 |
def test_session_activity_update(self):
|
| 294 |
"""Test updating session activity."""
|
| 295 |
-
now = datetime.
|
| 296 |
session = Session(
|
| 297 |
session_id="test",
|
| 298 |
username="user",
|
|
@@ -312,6 +341,6 @@ class TestSessionModel:
|
|
| 312 |
|
| 313 |
assert session.last_activity >= original_activity
|
| 314 |
# The new expiration should be approximately 30 minutes from now
|
| 315 |
-
expected_expires = datetime.
|
| 316 |
# Allow 5 seconds of variance due to timing
|
| 317 |
assert abs((session.expires_at - expected_expires).total_seconds()) < 5
|
|
|
|
| 1 |
"""Unit tests for AuthService and ACL enforcement."""
|
| 2 |
|
| 3 |
import pytest
|
| 4 |
+
from datetime import UTC, datetime, timedelta
|
| 5 |
|
| 6 |
from src.auth.auth_service import AuthService, Session, UserProjectAccess
|
| 7 |
from src.domain.models import Role
|
|
|
|
| 84 |
assert session is not None
|
| 85 |
|
| 86 |
# Manually set expiration to past
|
| 87 |
+
session.expires_at = datetime.now(UTC) - timedelta(seconds=1)
|
| 88 |
|
| 89 |
# Try to retrieve expired session
|
| 90 |
retrieved = short_ttl_service.get_session(session.session_id)
|
|
|
|
| 165 |
session2 = auth_service.login("validator1")
|
| 166 |
|
| 167 |
# Expire session1
|
| 168 |
+
session1.expires_at = datetime.now(UTC) - timedelta(seconds=1)
|
| 169 |
|
| 170 |
# Cleanup
|
| 171 |
auth_service.cleanup_expired_sessions()
|
|
|
|
| 207 |
assert len(projects) == 2
|
| 208 |
assert auth_service.get_user_role_for_project("user1", "project-2") == Role.admin
|
| 209 |
|
| 210 |
+
def test_upsert_user_project_role_creates_and_updates_user(self, auth_service):
|
| 211 |
+
auth_service.upsert_user_project_role("new_user", "project-1", Role.validator)
|
| 212 |
+
assert auth_service.get_user_role_for_project("new_user", "project-1") == Role.validator
|
| 213 |
+
|
| 214 |
+
auth_service.upsert_user_project_role("new_user", "project-1", Role.admin)
|
| 215 |
+
assert auth_service.get_user_role_for_project("new_user", "project-1") == Role.admin
|
| 216 |
+
|
| 217 |
+
def test_remove_user_project_role(self, auth_service):
|
| 218 |
+
auth_service.register_user_project_access("temp_user", {"project-1": Role.validator})
|
| 219 |
+
|
| 220 |
+
removed = auth_service.remove_user_project_role("temp_user", "project-1")
|
| 221 |
+
assert removed is True
|
| 222 |
+
assert auth_service.get_user_role_for_project("temp_user", "project-1") is None
|
| 223 |
+
|
| 224 |
+
removed_again = auth_service.remove_user_project_role("temp_user", "project-1")
|
| 225 |
+
assert removed_again is False
|
| 226 |
+
|
| 227 |
+
def test_list_usernames_honors_active_filter(self, auth_service):
|
| 228 |
+
auth_service.register_user_project_access("active_user", {"project-1": Role.validator})
|
| 229 |
+
auth_service.register_user_project_access("inactive_user", {"project-1": Role.validator})
|
| 230 |
+
auth_service.set_user_active("inactive_user", False)
|
| 231 |
+
|
| 232 |
+
active_only = auth_service.list_usernames()
|
| 233 |
+
with_inactive = auth_service.list_usernames(include_inactive=True)
|
| 234 |
+
|
| 235 |
+
assert "active_user" in active_only
|
| 236 |
+
assert "inactive_user" not in active_only
|
| 237 |
+
assert "inactive_user" in with_inactive
|
| 238 |
+
|
| 239 |
def test_session_isolation_between_users(self, auth_service, setup_users):
|
| 240 |
"""Test that sessions are isolated between users."""
|
| 241 |
session_admin = auth_service.login("admin_user")
|
|
|
|
| 282 |
|
| 283 |
def test_session_creation(self):
|
| 284 |
"""Test creating a session."""
|
| 285 |
+
now = datetime.now(UTC)
|
| 286 |
session = Session(
|
| 287 |
session_id="test-session-123",
|
| 288 |
username="test_user",
|
|
|
|
| 300 |
|
| 301 |
def test_session_expiration_check(self):
|
| 302 |
"""Test session expiration detection."""
|
| 303 |
+
now = datetime.now(UTC)
|
| 304 |
|
| 305 |
# Not expired
|
| 306 |
session = Session(
|
|
|
|
| 321 |
|
| 322 |
def test_session_activity_update(self):
|
| 323 |
"""Test updating session activity."""
|
| 324 |
+
now = datetime.now(UTC)
|
| 325 |
session = Session(
|
| 326 |
session_id="test",
|
| 327 |
username="user",
|
|
|
|
| 341 |
|
| 342 |
assert session.last_activity >= original_activity
|
| 343 |
# The new expiration should be approximately 30 minutes from now
|
| 344 |
+
expected_expires = datetime.now(UTC) + timedelta(minutes=30)
|
| 345 |
# Allow 5 seconds of variance due to timing
|
| 346 |
assert abs((session.expires_at - expected_expires).total_seconds()) < 5
|