Skip to content

Commit 4a8b530

Browse files
committed
Fix minor issues found in PR review
1 parent d568bb1 commit 4a8b530

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

backend/onyx/connectors/paperless_ngx/connector.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
DOCUMENT_TYPES_ENDPOINT = "/api/document_types/"
3939
PROFILE_ENDPOINT = "/api/profile/"
4040

41+
PAPERLESS_NGX_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S+00:00"
42+
4143
# TODO: handle custom fields?
4244
# CUSTOM_FIELDS_ENDPOINT = "/api/custom_fields/"
4345

@@ -124,24 +126,26 @@ def __init__(
124126
break
125127
except ValueError:
126128
continue
127-
if not hasattr(self, "ui_min_start_date"):
129+
if not self.master_start_date:
128130
raise ValueError("Could not parse date with any format")
129131
except ValueError as e:
130132
raise ConnectorValidationError(
131133
f"Could not parse ui_min_start_date: {ui_min_start_date}. Error: {e}"
132134
)
133135

134136
# match ui_name in Datefield from ui_date_field str from UI
137+
self.master_date_field: Optional[DateField] = DateField.MODIFIED_DATE
135138
if ui_date_field:
139+
found = False
136140
for date_field in DateField:
137141
if date_field.ui_name == ui_date_field:
138142
self.master_date_field = date_field
143+
found = True
139144
break
140-
raise ConnectorValidationError(
141-
f"Could not parse ui_date_field: {ui_date_field}."
142-
)
143-
else:
144-
self.master_date_field = DateField.MODIFIED_DATE
145+
if not found:
146+
raise ConnectorValidationError(
147+
f"Could not parse ui_date_field: {ui_date_field}."
148+
)
145149

146150
logger.info("Initialized PaperlessNgxConnector")
147151

@@ -499,7 +503,9 @@ def _parse_document(self, doc_data: Dict[str, Any]) -> Optional[Document]:
499503
logger.warning(
500504
f"Could not parse timestamp for document {doc_id}: {modified_timestamp_str}"
501505
)
502-
updated_at = datetime.now(timezone.utc)
506+
raise ValueError(
507+
f"Invalid timestamp found in document data {doc_id}: {modified_timestamp_str}"
508+
)
503509

504510
doc_owner = next(
505511
(user for user in self.all_users if user["id"] == doc_data.get("owner")),
@@ -625,8 +631,8 @@ def poll_source(
625631
end_dt = datetime.fromtimestamp(end, timezone.utc)
626632

627633
# Format dates for API query in ISO format
628-
start_iso = start_dt.strftime("%Y-%m-%dT%H:%M:%S+00:00")
629-
end_iso = end_dt.strftime("%Y-%m-%dT%H:%M:%S+00:00")
634+
start_iso = start_dt.strftime(PAPERLESS_NGX_DATE_FORMAT)
635+
end_iso = end_dt.strftime(PAPERLESS_NGX_DATE_FORMAT)
630636

631637
logger.info(
632638
f"Polling Paperless-ngx for documents modified between {start_iso} and {end_iso}."
@@ -680,12 +686,12 @@ def retrieve_all_slim_documents(
680686
# Apply date filtering if provided
681687
if start is not None:
682688
start_dt = datetime.fromtimestamp(start, timezone.utc)
683-
start_iso = start_dt.strftime("%Y-%m-%d")
689+
start_iso = start_dt.strftime(PAPERLESS_NGX_DATE_FORMAT)
684690
logger.info(f"Filtering slim documents from {start_iso}")
685691

686692
if end is not None:
687693
end_dt = datetime.fromtimestamp(end, timezone.utc)
688-
end_iso = end_dt.strftime("%Y-%m-%d")
694+
end_iso = end_dt.strftime(PAPERLESS_NGX_DATE_FORMAT)
689695
logger.info(f"Filtering slim documents until {end_iso}")
690696

691697
# Get only the IDs by making the request and extracting minimal information

backend/tests/daily/connectors/paperless_ngx/run_manual_test_paperless_server.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ USERNAME="admin"
1111
PASSWORD="admin"
1212

1313
# change to your local IP address if necessary
14-
HOST=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | head -n 1)
14+
HOST=$(ip addr | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | head -n 1)
1515
if [ -z "$HOST" ]; then
1616
HOST="localhost"
1717
fi
@@ -87,10 +87,10 @@ while ! curl -s "http://$HOST:$PORT" > /dev/null; do
8787
done
8888
echo -ne "\nPaperless-ngx is up and running!\n\n"
8989

90-
# get the CSRF token
90+
# get the CSRF token in a cookies file
9191
curl -s -u $USERNAME:$PASSWORD "http://$HOST:$PORT/api/schema/view/" -H "Content-Type: application/json" -c paperless-test-cookies.txt -o /dev/null
9292

93-
# get the auth token
93+
# get the auth token and use the CSRF token from the cookies file
9494
AUTH_TOKEN=$(curl -s -u $USERNAME:$PASSWORD -X POST "http://$HOST:$PORT/api/profile/generate_auth_token/" -H "Content-Type: application/json" -c paperless-test-cookies.txt | tr -d '"')
9595
rm paperless-test-cookies.txt
9696

backend/tests/daily/connectors/paperless_ngx/test_paperless_ngx_connector.py

+4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ def test_load_credentials(
124124

125125
assert connector.api_url == "http://test.com"
126126
assert connector.auth_token == "test_token"
127+
mock_get.assert_called_with(
128+
f"http://test.com{PROFILE_ENDPOINT}",
129+
headers={"Authorization": "Token test_token", "Accept": "application/json"},
130+
)
127131

128132

129133
def test_load_from_state(

0 commit comments

Comments
 (0)