Skip to content

T002 - Refactored export functionality to improve maintainability and separation of concerns - #809

Open
LizzzzHFFF wants to merge 16 commits into
dbca-wa:developfrom
LizzzzHFFF:fix/T002
Open

T002 - Refactored export functionality to improve maintainability and separation of concerns#809
LizzzzHFFF wants to merge 16 commits into
dbca-wa:developfrom
LizzzzHFFF:fix/T002

Conversation

@LizzzzHFFF

@LizzzzHFFF LizzzzHFFF commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Added TrvObservationSummary model for TRV_OBSERVATION_SUMMARY
  • Enhanced export to include complete observation summaries
  • Restore legacy field export columns
  • Add turtle status and measurements export
  • Restore flipper and PIT tag export fields
  • Align CSV and XLSX field export headers
  • Extract export helper functions from views

Changes:

  • Moved export configuration (headers, field mappings, constants) out of views.py into export_config.py.
  • Moved export processing and helper functions out of views.py into export_helpers.py.
  • Kept views.py focused on request handling and export orchestration.
  • No intended functional changes to export outputs.
  • Verified export results against existing example files to ensure output consistency.
  • Align field and processed observation export with agreed format

Added export fields

  • left_flipper_tags,
  • right_flipper_tags,
  • unknown_flipper_tags,
  • all_flipper_tags,
  • left_pit_tags,
  • right_pit_tags,
  • unknown_pit_tags,
  • all_pit_tags,
  • all_damage
  • all_measurements
  • all_samples
  • observation_status

Added Alive Status filter to Export Data page

  • Added Alive Status lookup endpoint
  • Added Alive Status filtering for Field and Processed exports

Compatibility

  • Retained existing export fields:
    • measurement_1_*
    • measurement_2_*
    • damage_1_*
    • damage_2_*
  • Available statuses:1. Yes 2. No 3. Possible 4. Uncertain 5. Didn't Check 6. NA

Testing

  • Verified options populate correctly
  • Verified export filtering works for all status values
  • Verified existing export filters continue to function
  • Verified CSV export output
  • Verified XLSX export output

@LizzzzHFFF
LizzzzHFFF requested a review from RickWangPerth July 16, 2026 07:52

@LizzzzHFFF LizzzzHFFF left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All done, and check the orders in the export files.

@LizzzzHFFF
LizzzzHFFF marked this pull request as ready for review July 27, 2026 06:37
@LizzzzHFFF
LizzzzHFFF marked this pull request as draft July 29, 2026 07:22
@LizzzzHFFF LizzzzHFFF changed the title T002 - Fix inconsistent observation summaries in export T002 - Refactored export functionality to improve maintainability and separation of concerns Aug 5, 2026
@LizzzzHFFF
LizzzzHFFF marked this pull request as ready for review August 31, 2026 05:36

@RickWangPerth RickWangPerth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add focused unit tests for CSV/XLSX header order and populated values, ENTRY_ID, empty and multi-related records, organisation scoping, and exports above SQL Server's parameter limit.

Code quality: git diff --check currently reports trailing whitespace and a final blank line. Please also remove duplicate imports and commented-out legacy code, and keep the export row-building path single-sourced and tested.

"OBSERVATION_DATE_OLD": _attr(observation, "observation_date_old"),
"ALIVE": _raw_fk(observation, "alive"),

"ENTRY_ID": "",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: ENTRY_ID is exported as an empty value for every processed row.

The associated TrtDataEntry is already available as de, and DATA_ENTRY_ID is populated immediately below. Please populate ENTRY_ID from the agreed legacy source (likely de.data_entry_id) or remove the column if it is intentionally unavailable. A silently blank required column makes the export incomplete.


def get_processed_export_row(entry, context):

observation = entry

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: The detailed observations fetched into context["observations"] are never used.

get_processed_export_row() assigns observation = entry, so later related-object access falls back to per-row lazy queries despite the prefetch work above. Please resolve the observation from context["observations"] with a safe fallback, or put the complete select_related(...) on the export queryset.

observation.observation_id: observation
for observation in _safe_queryset(
TrtObservations.objects.filter(
observation_id__in=observation_ids

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Large exports can silently lose their related data on SQL Server.

The complete observation-id set is passed through several __in queries in this helper. Once it exceeds SQL Server's parameter limit, _safe_queryset() catches the DatabaseError and returns [], so the download can succeed with related columns blank. Please query IDs in safe chunks and surface database failures rather than converting them to empty export data.

Comment thread wamtram2/views.py
summary_dict = {
s.observation_id: s
for s in TrvObservationSummary.objects.filter(
observation_id__in=obs_ids

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: The summary-view lookup also exceeds SQL Server's parameter limit for large exports.

This query sends the entire obs_ids set in one IN (...) clause and is not protected by the helper's error handling, so a large export fails with a 500 response. Please fetch summary rows in the same safe-sized chunks as the other related data.

if value is None:
return ""

return str(value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Escape formula-leading user text before writing CSV/XLSX cells.

Free-text fields such as comments are passed through unchanged into both formats. A value starting with =, +, -, or @ can be interpreted as a spreadsheet formula when the recipient opens the export. Please sanitize formula-leading text or explicitly write it as text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants