Sign in to SystemGroup per user on the orders page
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
The cutoff in .env is a Gregorian date. Delivery-note dates on the report
|
||||
are Jalali, so the cutoff is converted before the comparison.
|
||||
|
||||
The host stays in .env. Username and password are supplied per request.
|
||||
"""
|
||||
|
||||
import os
|
||||
@@ -18,6 +20,31 @@ class CrmOrdersError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class CrmAuthError(CrmOrdersError):
|
||||
"""SystemGroup rejected the username or password."""
|
||||
|
||||
|
||||
# Thirty days. The API refreshes this on each successful fetch.
|
||||
SESSION_TTL_SECONDS = 30 * 24 * 60 * 60
|
||||
|
||||
_AUTH_MARKERS = ("Login appears to have failed", "redirected to login")
|
||||
|
||||
|
||||
def login_rejected(message: str) -> bool:
|
||||
text = message or ""
|
||||
return any(marker in text for marker in _AUTH_MARKERS)
|
||||
|
||||
|
||||
def report_base_url() -> str:
|
||||
"""Shared SystemGroup host. Credentials are not read from the environment."""
|
||||
base = (_setting("REPORT_BASE_URL") or _setting("CRM_BASE_URL") or "").strip()
|
||||
if not base:
|
||||
raise CrmOrdersError("Set REPORT_BASE_URL.")
|
||||
if not base.endswith("/"):
|
||||
base += "/"
|
||||
return base
|
||||
|
||||
|
||||
def parse_jalali(raw: str) -> tuple[int, int, int] | None:
|
||||
text = (raw or "").translate(_DIGIT_MAP).strip().replace("-", "/")
|
||||
bits = text.split("/")
|
||||
@@ -164,7 +191,7 @@ def orders_after(lines, start: tuple[int, int, int]):
|
||||
return kept
|
||||
|
||||
|
||||
def recent_customer_orders(client=None) -> dict:
|
||||
def recent_customer_orders(username: str, password: str, client=None) -> dict:
|
||||
"""Fetch the store delivery-note report and keep rows after the cutoff."""
|
||||
_day, start = orders_cutoff()
|
||||
prefixes = code_prefixes()
|
||||
@@ -180,13 +207,20 @@ def recent_customer_orders(client=None) -> dict:
|
||||
|
||||
try:
|
||||
if client is None:
|
||||
client = SystemGroupClient(prefer_report_env=True, timeout=30)
|
||||
client = SystemGroupClient(
|
||||
base_url=report_base_url(),
|
||||
username=username,
|
||||
password=password,
|
||||
timeout=30,
|
||||
)
|
||||
body, content_type = client.fetch_customer_orders()
|
||||
lines = parse_customer_orders_document(body, content_type=content_type)
|
||||
except (SystemGroupClientError, SystemGroupParseError) as exc:
|
||||
text = str(exc)
|
||||
if login_rejected(text):
|
||||
raise CrmAuthError("SystemGroup rejected this username or password.") from exc
|
||||
if text.startswith("Missing required env vars"):
|
||||
text = "Set REPORT_BASE_URL, REPORT_USERNAME, and REPORT_PASSWORD."
|
||||
text = "Set REPORT_BASE_URL."
|
||||
raise CrmOrdersError(text) from exc
|
||||
|
||||
kept = [
|
||||
|
||||
Reference in New Issue
Block a user