Download the full order CSV instead of re-running the report #50

Merged
rhino merged 1 commits from cursor/direct-order-csv-export into main 2026-11-22 05:08:43 +00:00
2 changed files with 15 additions and 34 deletions
Showing only changes of commit 0fa05ab48d - Show all commits

View File

@@ -253,19 +253,14 @@ def export_filtered_customer_orders(client, day: date, prefixes: list[str]) -> t
def _load_order_export(client, day: date, prefixes: list[str]) -> tuple[str, str]:
"""Prefer the filtered export. A failed post still returns the full CSV."""
if getattr(client, "session", None) is None:
return client.fetch_customer_orders()
# The report GET redirects to login until the client has signed in.
if hasattr(client, "login") and not getattr(client, "_logged_in", False):
client.login()
try:
return export_filtered_customer_orders(client, day, prefixes)
except Exception as exc:
if login_rejected(str(exc)):
raise
# A broken filter post must not blank the page. The full CSV still filters locally.
return client.fetch_customer_orders()
"""Download the full CSV and let the local date and prefix checks drop rows.
A server-side filter re-runs the report after the page has already loaded
(~1.8s) to avoid downloading about 20KB. Direct export is faster.
ponytail: pass filter_state=report_filter_state(day, prefixes) if this
export starts taking more than a couple of seconds.
"""
return client.fetch_customer_orders()
def recent_customer_orders(username: str, password: str, client=None) -> dict:

View File

@@ -156,31 +156,17 @@ class SystemGroupLoginTests(unittest.TestCase):
self.assertEqual(posted["filterItemName"], "pp.code")
self.assertEqual(posted["operands"][0]["value"], "KR")
def test_filtered_export_logs_in_first(self):
calls = []
def test_load_uses_direct_export(self):
seen = {}
class Client:
def __init__(self):
self.session = object()
self._logged_in = False
def login(self):
calls.append("login")
self._logged_in = True
def fetch_customer_orders(self):
calls.append("full")
def fetch_customer_orders(self, **kwargs):
seen.update(kwargs)
return "full", "text/csv"
client = Client()
with patch(
"slips.crm_orders.export_filtered_customer_orders",
return_value=("filtered", "text/csv"),
) as filtered:
body, ctype = _load_order_export(client, date(2026, 3, 21), ["KR"])
self.assertEqual(calls, ["login"])
self.assertEqual((body, ctype), ("filtered", "text/csv"))
filtered.assert_called_once()
body, ctype = _load_order_export(Client(), date(2026, 3, 21), ["KR"])
self.assertEqual((body, ctype), ("full", "text/csv"))
self.assertNotIn("filter_state", seen)
class CrmSessionTests(TestCase):