Skip to content

Commit 49800e5

Browse files
committed
Fix KOSync readout
1 parent 6d8e340 commit 49800e5

2 files changed

Lines changed: 25 additions & 17 deletions

File tree

cps/progress_syncing/protocols/kosync.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ def get_progress(document: str):
475475
{
476476
"document": "abc123...",
477477
"progress": "location string",
478-
"percentage": 45.67,
478+
"percentage": 0.4567, # Decimal fraction (0.4567 = 45.67%)
479479
"device": "KOReader",
480480
"device_id": "device123",
481481
"timestamp": 1699564800,
@@ -484,6 +484,10 @@ def get_progress(document: str):
484484
"calibre_book_format": "EPUB", # Optional
485485
"calibre_checksum_version": "koreader" # Optional
486486
}
487+
488+
Note:
489+
Percentage is returned as decimal (0.4567 = 45.67%) as expected by KOReader.
490+
Internally stored as percentage (0-100) in database.
487491
"""
488492
try:
489493
user = authenticate_user()
@@ -503,10 +507,14 @@ def get_progress(document: str):
503507
log.debug(f"No progress found for user {user.id}, document {document}")
504508
return create_sync_response({})
505509

510+
# KOReader expects percentage as a decimal fraction (0.9411 = 94.11%)
511+
# We store it as percentage (0-100), so convert back to decimal (0-1)
512+
percentage_decimal = progress_record.percentage / 100.0
513+
506514
response_data = {
507515
"document": document,
508516
"progress": progress_record.progress,
509-
"percentage": progress_record.percentage,
517+
"percentage": percentage_decimal,
510518
"device": progress_record.device,
511519
"device_id": progress_record.device_id,
512520
"timestamp": int(progress_record.timestamp.timestamp())

tests/integration/test_kosync_update_read_status.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_sets_finished_at_99_percent(self, cwa_api_client):
3535
payload = {
3636
'document': 'test-doc-99pct',
3737
'progress': '0.99',
38-
'percentage': 99.0,
38+
'percentage': 0.99, # KOReader sends as decimal (0.99 = 99%)
3939
'device': 'pytest',
4040
'device_id': 'test-device'
4141
}
@@ -56,7 +56,7 @@ def test_sets_finished_at_100_percent(self, cwa_api_client):
5656
payload = {
5757
'document': 'test-doc-100pct',
5858
'progress': '1.0',
59-
'percentage': 100.0,
59+
'percentage': 1.0, # KOReader sends as decimal (1.0 = 100%)
6060
'device': 'pytest',
6161
'device_id': 'test-device'
6262
}
@@ -75,7 +75,7 @@ def test_sets_in_progress_at_50_percent(self, cwa_api_client):
7575
payload = {
7676
'document': 'test-doc-50pct',
7777
'progress': '0.5',
78-
'percentage': 50.0,
78+
'percentage': 0.50, # KOReader sends as decimal (0.50 = 50%)
7979
'device': 'pytest',
8080
'device_id': 'test-device'
8181
}
@@ -94,7 +94,7 @@ def test_sets_in_progress_at_one_percent(self, cwa_api_client):
9494
payload = {
9595
'document': 'test-doc-1pct',
9696
'progress': '0.01',
97-
'percentage': 1.0,
97+
'percentage': 0.01, # KOReader sends as decimal (0.01 = 1%)
9898
'device': 'pytest',
9999
'device_id': 'test-device'
100100
}
@@ -113,7 +113,7 @@ def test_sets_in_progress_at_98_percent(self, cwa_api_client):
113113
payload = {
114114
'document': 'test-doc-98pct',
115115
'progress': '0.989',
116-
'percentage': 98.9,
116+
'percentage': 0.989, # KOReader sends as decimal (0.989 = 98.9%)
117117
'device': 'pytest',
118118
'device_id': 'test-device'
119119
}
@@ -158,7 +158,7 @@ def test_creates_new_record_for_first_sync(self, cwa_api_client):
158158
payload = {
159159
'document': unique_doc,
160160
'progress': '0.25',
161-
'percentage': 25.0,
161+
'percentage': 0.25, # KOReader sends as decimal (0.25 = 25%)
162162
'device': 'pytest',
163163
'device_id': 'test-device'
164164
}
@@ -169,12 +169,12 @@ def test_creates_new_record_for_first_sync(self, cwa_api_client):
169169

170170
assert response.status_code == 200
171171

172-
# Verify we can retrieve it
172+
# Verify we can retrieve it (should return as decimal)
173173
response = cwa_api_client.get(f'/kosync/syncs/progress/{unique_doc}',
174174
headers=headers)
175175
assert response.status_code == 200
176176
data = response.json()
177-
assert float(data['percentage']) == 25.0
177+
assert float(data['percentage']) == 0.25 # Returns as decimal
178178

179179
def test_updates_existing_record(self, cwa_api_client):
180180
"""Subsequent syncs update the existing record"""
@@ -187,7 +187,7 @@ def test_updates_existing_record(self, cwa_api_client):
187187
payload = {
188188
'document': doc_id,
189189
'progress': '0.3',
190-
'percentage': 30.0,
190+
'percentage': 0.30, # KOReader sends as decimal (0.30 = 30%)
191191
'device': 'pytest',
192192
'device_id': 'test-device'
193193
}
@@ -198,18 +198,18 @@ def test_updates_existing_record(self, cwa_api_client):
198198

199199
# Second sync with updated progress
200200
payload['progress'] = '0.6'
201-
payload['percentage'] = 60.0
201+
payload['percentage'] = 0.60 # KOReader sends as decimal (0.60 = 60%)
202202
response = cwa_api_client.put('/kosync/syncs/progress',
203203
json=payload,
204204
headers=headers)
205205
assert response.status_code == 200
206206

207-
# Verify updated value
207+
# Verify updated value (should return as decimal)
208208
response = cwa_api_client.get(f'/kosync/syncs/progress/{doc_id}',
209209
headers=headers)
210210
assert response.status_code == 200
211211
data = response.json()
212-
assert float(data['percentage']) == 60.0
212+
assert float(data['percentage']) == 0.60 # Returns as decimal
213213

214214

215215
@pytest.mark.docker_integration
@@ -225,7 +225,7 @@ def test_handles_decimal_percentage(self, cwa_api_client):
225225
payload = {
226226
'document': 'test-doc-decimal',
227227
'progress': '0.4567',
228-
'percentage': 45.67,
228+
'percentage': 0.4567, # KOReader sends as decimal (0.4567 = 45.67%)
229229
'device': 'pytest',
230230
'device_id': 'test-device'
231231
}
@@ -244,7 +244,7 @@ def test_handles_threshold_boundary_99_point_0(self, cwa_api_client):
244244
payload = {
245245
'document': 'test-doc-boundary-99',
246246
'progress': '0.99',
247-
'percentage': 99.0,
247+
'percentage': 0.99, # KOReader sends as decimal (0.99 = 99%)
248248
'device': 'pytest',
249249
'device_id': 'test-device'
250250
}
@@ -263,7 +263,7 @@ def test_handles_threshold_boundary_98_point_9(self, cwa_api_client):
263263
payload = {
264264
'document': 'test-doc-boundary-989',
265265
'progress': '0.989',
266-
'percentage': 98.9,
266+
'percentage': 0.989, # KOReader sends as decimal (0.989 = 98.9%)
267267
'device': 'pytest',
268268
'device_id': 'test-device'
269269
}

0 commit comments

Comments
 (0)