jrrribeiro commited on
Commit
3607d7a
·
verified ·
1 Parent(s): e79be8f

Sync app from GitHub Actions (feature/project-state-security-privacy-review)

Browse files
src/ui/app_factory.py CHANGED
@@ -1822,6 +1822,36 @@ def _species_status_dropdown_script() -> str:
1822
  """
1823
 
1824
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1825
  def _validate_hf_admin_storage_source_dataset(
1826
  *,
1827
  project: Project,
@@ -2975,6 +3005,19 @@ def _is_reject_correction_required_status(status_value: object) -> bool:
2975
  return str(status_value or "").strip().startswith(VALIDATION_REJECT_CORRECTION_REQUIRED_STATUS)
2976
 
2977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2978
  def _save_selected_validation(
2979
  validation_service: _ValidationServiceProtocol,
2980
  audio_service: _AudioServiceProtocol,
@@ -3632,7 +3675,7 @@ def create_app() -> gr.Blocks:
3632
  with gr.Blocks(
3633
  title="BirdNET-Validator-App",
3634
  css=APP_CSS,
3635
- head=CRITICAL_HEAD_HTML + _species_status_dropdown_script(),
3636
  fill_width=False,
3637
  elem_classes=["bn-shell"],
3638
  ) as wrapper:
@@ -5432,6 +5475,11 @@ def create_app() -> gr.Blocks:
5432
  allow_custom_value=True,
5433
  filterable=True,
5434
  value=None,
 
 
 
 
 
5435
  )
5436
 
5437
  status = gr.Markdown(value="", elem_classes=["bn-status-strip"])
@@ -5687,12 +5735,12 @@ def create_app() -> gr.Blocks:
5687
  only_conflicts: bool,
5688
  ):
5689
  if not project_slug:
5690
- return "Select a project before validating", cache_key, None, rows, page, idx, "", ""
5691
  validator_name_value = _validator_name_from_session(session)
5692
  if not validator_name_value:
5693
- return "Login before validating", cache_key, None, rows, page, idx, "", ""
5694
  if not _can_access_project(session, project_slug):
5695
- return "Access denied for this project", cache_key, None, rows, page, idx, "", ""
5696
  result = _save_selected_validation_with_refresh(
5697
  validation_service=validation_service,
5698
  audio_service=audio_service,
@@ -5714,8 +5762,10 @@ def create_app() -> gr.Blocks:
5714
  updated_after=updated_after_value,
5715
  show_conflicts_only=bool(only_conflicts),
5716
  )
5717
- _invalidate_report_cache(project_slug)
5718
- return result
 
 
5719
 
5720
  def reapply_for_project(
5721
  project_slug: str,
@@ -5820,7 +5870,7 @@ def create_app() -> gr.Blocks:
5820
  value = (corrected_value or "").strip()
5821
 
5822
  if not project_slug:
5823
- return gr.update(choices=base_choices, value=value or None), custom_by_project
5824
 
5825
  updated = {k: list(v) for k, v in (custom_by_project or {}).items()}
5826
  custom_values = updated.get(project_slug, [])
@@ -5829,7 +5879,7 @@ def create_app() -> gr.Blocks:
5829
  updated[project_slug] = custom_values
5830
 
5831
  final_choices = list(dict.fromkeys([*base_choices, *updated.get(project_slug, [])]))
5832
- return gr.update(choices=final_choices, value=value or None), updated
5833
 
5834
  def toggle_favorite_detection(
5835
  project_slug: str,
@@ -6147,7 +6197,7 @@ def create_app() -> gr.Blocks:
6147
  corrected_species_input.change(
6148
  fn=save_corrected_species_option,
6149
  inputs=[selected_project_state, corrected_species_input, project_species_state, custom_corrected_species_state],
6150
- outputs=[corrected_species_input, custom_corrected_species_state],
6151
  )
6152
 
6153
  approve_event = approve_btn.click(
@@ -6184,7 +6234,18 @@ def create_app() -> gr.Blocks:
6184
  updated_after_filter,
6185
  show_conflicts_only,
6186
  ],
6187
- outputs=[status, cache_key_state, audio_player, table, page_state, selected_index, pending_status_state, conflict_detection_key_state],
 
 
 
 
 
 
 
 
 
 
 
6188
  )
6189
  reject_event = reject_btn.click(
6190
  fn=lambda project_slug, rows, idx, session, notes, corrected_species_value, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value, only_conflicts: save_for_project(
@@ -6220,7 +6281,18 @@ def create_app() -> gr.Blocks:
6220
  updated_after_filter,
6221
  show_conflicts_only,
6222
  ],
6223
- outputs=[status, cache_key_state, audio_player, table, page_state, selected_index, pending_status_state, conflict_detection_key_state],
 
 
 
 
 
 
 
 
 
 
 
6224
  )
6225
  uncertain_event = uncertain_btn.click(
6226
  fn=lambda project_slug, rows, idx, session, notes, corrected_species_value, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value, only_conflicts: save_for_project(
@@ -6256,7 +6328,18 @@ def create_app() -> gr.Blocks:
6256
  updated_after_filter,
6257
  show_conflicts_only,
6258
  ],
6259
- outputs=[status, cache_key_state, audio_player, table, page_state, selected_index, pending_status_state, conflict_detection_key_state],
 
 
 
 
 
 
 
 
 
 
 
6260
  )
6261
  skip_event = skip_btn.click(
6262
  fn=lambda project_slug, rows, idx, session, notes, corrected_species_value, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value, only_conflicts: save_for_project(
@@ -6292,7 +6375,18 @@ def create_app() -> gr.Blocks:
6292
  updated_after_filter,
6293
  show_conflicts_only,
6294
  ],
6295
- outputs=[status, cache_key_state, audio_player, table, page_state, selected_index, pending_status_state, conflict_detection_key_state],
 
 
 
 
 
 
 
 
 
 
 
6296
  )
6297
 
6298
  approve_event.then(
 
1822
  """
1823
 
1824
 
1825
+ def _corrected_species_required_script() -> str:
1826
+ """Toggle the corrected-species error style from a lightweight HTML marker."""
1827
+ return """
1828
+ <script>
1829
+ (() => {
1830
+ const INPUT_ID = "bn-corrected-species-input";
1831
+ const MARKER_ID = "bn-corrected-species-error-marker";
1832
+
1833
+ function markerIsActive() {
1834
+ const marker = document.getElementById(MARKER_ID);
1835
+ const state = marker ? marker.querySelector("[data-error]") : null;
1836
+ return !!state && state.dataset.error === "true";
1837
+ }
1838
+
1839
+ function applyCorrectedSpeciesErrorState() {
1840
+ const root = document.getElementById(INPUT_ID);
1841
+ if (!root) return;
1842
+ root.classList.toggle("bn-corrected-species-error", markerIsActive());
1843
+ }
1844
+
1845
+ const observer = new MutationObserver(applyCorrectedSpeciesErrorState);
1846
+ observer.observe(document.body, { childList: true, subtree: true, characterData: true });
1847
+ window.addEventListener("focus", applyCorrectedSpeciesErrorState);
1848
+ document.addEventListener("click", () => window.setTimeout(applyCorrectedSpeciesErrorState, 0), true);
1849
+ applyCorrectedSpeciesErrorState();
1850
+ })();
1851
+ </script>
1852
+ """
1853
+
1854
+
1855
  def _validate_hf_admin_storage_source_dataset(
1856
  *,
1857
  project: Project,
 
3005
  return str(status_value or "").strip().startswith(VALIDATION_REJECT_CORRECTION_REQUIRED_STATUS)
3006
 
3007
 
3008
+ def _corrected_species_error_payload(active: bool) -> str:
3009
+ value = "true" if active else "false"
3010
+ return f'<span class="bn-corrected-species-error-state" data-error="{value}"></span>'
3011
+
3012
+
3013
+ def _corrected_species_ui_after_validation(save_status: object) -> tuple[object, str]:
3014
+ if _is_reject_correction_required_status(save_status):
3015
+ return gr.update(), _corrected_species_error_payload(True)
3016
+ if str(save_status or "").startswith("Validation saved"):
3017
+ return gr.update(value=None), _corrected_species_error_payload(False)
3018
+ return gr.update(), _corrected_species_error_payload(False)
3019
+
3020
+
3021
  def _save_selected_validation(
3022
  validation_service: _ValidationServiceProtocol,
3023
  audio_service: _AudioServiceProtocol,
 
3675
  with gr.Blocks(
3676
  title="BirdNET-Validator-App",
3677
  css=APP_CSS,
3678
+ head=CRITICAL_HEAD_HTML + _species_status_dropdown_script() + _corrected_species_required_script(),
3679
  fill_width=False,
3680
  elem_classes=["bn-shell"],
3681
  ) as wrapper:
 
5475
  allow_custom_value=True,
5476
  filterable=True,
5477
  value=None,
5478
+ elem_id="bn-corrected-species-input",
5479
+ )
5480
+ corrected_species_error_marker = gr.HTML(
5481
+ value=_corrected_species_error_payload(False),
5482
+ elem_id="bn-corrected-species-error-marker",
5483
  )
5484
 
5485
  status = gr.Markdown(value="", elem_classes=["bn-status-strip"])
 
5735
  only_conflicts: bool,
5736
  ):
5737
  if not project_slug:
5738
+ return "Select a project before validating", cache_key, None, rows, page, idx, "", "", gr.update(), _corrected_species_error_payload(False)
5739
  validator_name_value = _validator_name_from_session(session)
5740
  if not validator_name_value:
5741
+ return "Login before validating", cache_key, None, rows, page, idx, "", "", gr.update(), _corrected_species_error_payload(False)
5742
  if not _can_access_project(session, project_slug):
5743
+ return "Access denied for this project", cache_key, None, rows, page, idx, "", "", gr.update(), _corrected_species_error_payload(False)
5744
  result = _save_selected_validation_with_refresh(
5745
  validation_service=validation_service,
5746
  audio_service=audio_service,
 
5762
  updated_after=updated_after_value,
5763
  show_conflicts_only=bool(only_conflicts),
5764
  )
5765
+ if str(result[0]).startswith("Validation saved"):
5766
+ _invalidate_report_cache(project_slug)
5767
+ corrected_update, corrected_error_payload = _corrected_species_ui_after_validation(result[0])
5768
+ return (*result, corrected_update, corrected_error_payload)
5769
 
5770
  def reapply_for_project(
5771
  project_slug: str,
 
5870
  value = (corrected_value or "").strip()
5871
 
5872
  if not project_slug:
5873
+ return gr.update(choices=base_choices, value=value or None), custom_by_project, _corrected_species_error_payload(False)
5874
 
5875
  updated = {k: list(v) for k, v in (custom_by_project or {}).items()}
5876
  custom_values = updated.get(project_slug, [])
 
5879
  updated[project_slug] = custom_values
5880
 
5881
  final_choices = list(dict.fromkeys([*base_choices, *updated.get(project_slug, [])]))
5882
+ return gr.update(choices=final_choices, value=value or None), updated, _corrected_species_error_payload(False)
5883
 
5884
  def toggle_favorite_detection(
5885
  project_slug: str,
 
6197
  corrected_species_input.change(
6198
  fn=save_corrected_species_option,
6199
  inputs=[selected_project_state, corrected_species_input, project_species_state, custom_corrected_species_state],
6200
+ outputs=[corrected_species_input, custom_corrected_species_state, corrected_species_error_marker],
6201
  )
6202
 
6203
  approve_event = approve_btn.click(
 
6234
  updated_after_filter,
6235
  show_conflicts_only,
6236
  ],
6237
+ outputs=[
6238
+ status,
6239
+ cache_key_state,
6240
+ audio_player,
6241
+ table,
6242
+ page_state,
6243
+ selected_index,
6244
+ pending_status_state,
6245
+ conflict_detection_key_state,
6246
+ corrected_species_input,
6247
+ corrected_species_error_marker,
6248
+ ],
6249
  )
6250
  reject_event = reject_btn.click(
6251
  fn=lambda project_slug, rows, idx, session, notes, corrected_species_value, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value, only_conflicts: save_for_project(
 
6281
  updated_after_filter,
6282
  show_conflicts_only,
6283
  ],
6284
+ outputs=[
6285
+ status,
6286
+ cache_key_state,
6287
+ audio_player,
6288
+ table,
6289
+ page_state,
6290
+ selected_index,
6291
+ pending_status_state,
6292
+ conflict_detection_key_state,
6293
+ corrected_species_input,
6294
+ corrected_species_error_marker,
6295
+ ],
6296
  )
6297
  uncertain_event = uncertain_btn.click(
6298
  fn=lambda project_slug, rows, idx, session, notes, corrected_species_value, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value, only_conflicts: save_for_project(
 
6328
  updated_after_filter,
6329
  show_conflicts_only,
6330
  ],
6331
+ outputs=[
6332
+ status,
6333
+ cache_key_state,
6334
+ audio_player,
6335
+ table,
6336
+ page_state,
6337
+ selected_index,
6338
+ pending_status_state,
6339
+ conflict_detection_key_state,
6340
+ corrected_species_input,
6341
+ corrected_species_error_marker,
6342
+ ],
6343
  )
6344
  skip_event = skip_btn.click(
6345
  fn=lambda project_slug, rows, idx, session, notes, corrected_species_value, cache_key, page, species, confidence, validator_filter_value, status_filter_value, updated_after_value, only_conflicts: save_for_project(
 
6375
  updated_after_filter,
6376
  show_conflicts_only,
6377
  ],
6378
+ outputs=[
6379
+ status,
6380
+ cache_key_state,
6381
+ audio_player,
6382
+ table,
6383
+ page_state,
6384
+ selected_index,
6385
+ pending_status_state,
6386
+ conflict_detection_key_state,
6387
+ corrected_species_input,
6388
+ corrected_species_error_marker,
6389
+ ],
6390
  )
6391
 
6392
  approve_event.then(
src/ui/theme.py CHANGED
@@ -1687,6 +1687,30 @@ button.bn-report-download-action-blue:hover {
1687
  box-shadow: 0 0 0 1px rgba(22, 163, 74, 0.35) !important;
1688
  }
1689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1690
  textarea,
1691
  input,
1692
  select {
 
1687
  box-shadow: 0 0 0 1px rgba(22, 163, 74, 0.35) !important;
1688
  }
1689
 
1690
+ #bn-corrected-species-error-marker {
1691
+ display: none !important;
1692
+ }
1693
+
1694
+ #bn-corrected-species-input.bn-corrected-species-error,
1695
+ #bn-corrected-species-input.bn-corrected-species-error > div,
1696
+ #bn-corrected-species-input.bn-corrected-species-error .wrap,
1697
+ #bn-corrected-species-input.bn-corrected-species-error .wrap-inner {
1698
+ border-color: #ef4444 !important;
1699
+ background: #fff5f5 !important;
1700
+ box-shadow: 0 0 0 1px rgba(239, 68, 68, 0.32) !important;
1701
+ }
1702
+
1703
+ #bn-corrected-species-input.bn-corrected-species-error label,
1704
+ #bn-corrected-species-input.bn-corrected-species-error .label-wrap {
1705
+ color: #b42318 !important;
1706
+ }
1707
+
1708
+ #bn-corrected-species-input.bn-corrected-species-error input,
1709
+ #bn-corrected-species-input.bn-corrected-species-error select,
1710
+ #bn-corrected-species-input.bn-corrected-species-error textarea {
1711
+ background: #fff5f5 !important;
1712
+ }
1713
+
1714
  textarea,
1715
  input,
1716
  select {
tests/unit/test_app_factory_audio_helpers.py CHANGED
@@ -48,6 +48,9 @@ from src.ui.app_factory import (
48
  _species_status_dropdown_script,
49
  _species_status_payload,
50
  _species_dropdown_choices,
 
 
 
51
  VALIDATION_REJECT_CORRECTION_REQUIRED_STATUS,
52
  )
53
  from src.auth.auth_service import AuthService
@@ -1167,6 +1170,29 @@ def test_reject_validation_saves_corrected_species_when_provided() -> None:
1167
  assert validation_service.calls[0]["corrected_species"] == "Correct Species"
1168
 
1169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1170
  def test_save_selected_validation_advances_to_highest_confidence_pending_row() -> None:
1171
  class QueueAfterSave:
1172
  def list_all_detections(self, **kwargs: object) -> list[object]:
 
48
  _species_status_dropdown_script,
49
  _species_status_payload,
50
  _species_dropdown_choices,
51
+ _corrected_species_error_payload,
52
+ _corrected_species_required_script,
53
+ _corrected_species_ui_after_validation,
54
  VALIDATION_REJECT_CORRECTION_REQUIRED_STATUS,
55
  )
56
  from src.auth.auth_service import AuthService
 
1170
  assert validation_service.calls[0]["corrected_species"] == "Correct Species"
1171
 
1172
 
1173
+ def test_corrected_species_ui_state_marks_error_and_resets_after_save() -> None:
1174
+ blocked_update, blocked_marker = _corrected_species_ui_after_validation(VALIDATION_REJECT_CORRECTION_REQUIRED_STATUS)
1175
+ saved_update, saved_marker = _corrected_species_ui_after_validation("Validation saved: dkey -> negative")
1176
+ conflict_update, conflict_marker = _corrected_species_ui_after_validation("Concurrency conflict: stale")
1177
+
1178
+ assert isinstance(blocked_update, dict)
1179
+ assert 'data-error="true"' in blocked_marker
1180
+ assert saved_update["value"] is None
1181
+ assert 'data-error="false"' in saved_marker
1182
+ assert "value" not in conflict_update
1183
+ assert 'data-error="false"' in conflict_marker
1184
+
1185
+
1186
+ def test_corrected_species_error_script_targets_marker_and_input() -> None:
1187
+ script = _corrected_species_required_script()
1188
+ payload = _corrected_species_error_payload(True)
1189
+
1190
+ assert "bn-corrected-species-input" in script
1191
+ assert "bn-corrected-species-error-marker" in script
1192
+ assert "bn-corrected-species-error" in script
1193
+ assert 'data-error="true"' in payload
1194
+
1195
+
1196
  def test_save_selected_validation_advances_to_highest_confidence_pending_row() -> None:
1197
  class QueueAfterSave:
1198
  def list_all_detections(self, **kwargs: object) -> list[object]: