Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Ebooko
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TheJoeCoder
Ebooko
Commits
c6ed24a3
Verified
Commit
c6ed24a3
authored
Aug 25, 2024
by
TheJoeCoder
Browse files
Options
Downloads
Patches
Plain Diff
Prevent KoReader recursive writeback
parent
62225bac
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
api_koreader/models.py
+8
-0
8 additions, 0 deletions
api_koreader/models.py
api_koreader/views.py
+4
-1
4 additions, 1 deletion
api_koreader/views.py
with
12 additions
and
1 deletion
api_koreader/models.py
+
8
−
0
View file @
c6ed24a3
import
hashlib
import
io
import
io
from
django.db
import
models
from
django.db
import
models
...
@@ -24,6 +25,9 @@ class SyncDocumentEntry(models.Model):
...
@@ -24,6 +25,9 @@ class SyncDocumentEntry(models.Model):
@receiver
(
post_save
,
sender
=
SyncDocumentEntry
)
@receiver
(
post_save
,
sender
=
SyncDocumentEntry
)
def
post_save_document
(
sender
,
instance
:
SyncDocumentEntry
,
**
kwargs
):
def
post_save_document
(
sender
,
instance
:
SyncDocumentEntry
,
**
kwargs
):
# Stop recursion
if
not
instance
.
device
.
startswith
(
"
KoReader
"
):
return
book_matches
:
QuerySet
[
Book
]
=
Book
.
objects
.
filter
(
partial_md5__iexact
=
instance
.
key
)
book_matches
:
QuerySet
[
Book
]
=
Book
.
objects
.
filter
(
partial_md5__iexact
=
instance
.
key
)
if
book_matches
:
if
book_matches
:
book
=
book_matches
.
first
()
book
=
book_matches
.
first
()
...
@@ -43,6 +47,9 @@ def post_save_document(sender, instance: SyncDocumentEntry, **kwargs):
...
@@ -43,6 +47,9 @@ def post_save_document(sender, instance: SyncDocumentEntry, **kwargs):
@receiver
(
post_save
,
sender
=
UserBook
)
@receiver
(
post_save
,
sender
=
UserBook
)
def
post_save_userbook
(
sender
,
instance
:
UserBook
,
**
kwargs
):
def
post_save_userbook
(
sender
,
instance
:
UserBook
,
**
kwargs
):
# Stop recursion
if
instance
.
last_progress_device
.
startswith
(
"
KoReader
"
):
return
# Find the corresponding SyncDocumentEntry
# Find the corresponding SyncDocumentEntry
sync_doc_q
:
QuerySet
[
SyncDocumentEntry
]
=
SyncDocumentEntry
.
objects
.
filter
(
user
=
instance
.
user
,
key__iexact
=
instance
.
book
.
partial_md5
)
sync_doc_q
:
QuerySet
[
SyncDocumentEntry
]
=
SyncDocumentEntry
.
objects
.
filter
(
user
=
instance
.
user
,
key__iexact
=
instance
.
book
.
partial_md5
)
if
sync_doc_q
.
first
():
if
sync_doc_q
.
first
():
...
@@ -54,4 +61,5 @@ def post_save_userbook(sender, instance: UserBook, **kwargs):
...
@@ -54,4 +61,5 @@ def post_save_userbook(sender, instance: UserBook, **kwargs):
sync_doc
.
percentage
=
instance
.
percentage_read
sync_doc
.
percentage
=
instance
.
percentage_read
sync_doc
.
device
=
instance
.
last_progress_device
sync_doc
.
device
=
instance
.
last_progress_device
sync_doc
.
updated_at
=
instance
.
last_read
sync_doc
.
updated_at
=
instance
.
last_read
sync_doc
.
device_id
=
hashlib
.
md5
(
sync_doc
.
device
.
encode
()).
hexdigest
()
# Generate a device ID
sync_doc
.
save
()
sync_doc
.
save
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
api_koreader/views.py
+
4
−
1
View file @
c6ed24a3
import
json
import
json
from
json
import
JSONDecodeError
from
json
import
JSONDecodeError
from
django.db.transaction
import
commit
from
django.http
import
JsonResponse
,
HttpResponse
from
django.http
import
JsonResponse
,
HttpResponse
from
django.utils
import
timezone
from
django.utils
import
timezone
from
django.views.decorators.csrf
import
csrf_exempt
from
django.views.decorators.csrf
import
csrf_exempt
...
@@ -99,7 +100,9 @@ def update_progress(request):
...
@@ -99,7 +100,9 @@ def update_progress(request):
form
=
SyncDocumentEntryForm
(
body_object
,
instance
=
existing_object
)
form
=
SyncDocumentEntryForm
(
body_object
,
instance
=
existing_object
)
if
form
.
is_valid
():
if
form
.
is_valid
():
obj
=
form
.
save
()
obj
:
SyncDocumentEntry
=
form
.
save
(
commit
=
False
)
obj
.
device
=
"
KoReader -
"
+
obj
.
device
obj
.
save
()
return
JsonResponse
({
"
document
"
:
obj
.
key
,
"
timestamp
"
:
int
(
obj
.
updated_at
.
timestamp
())},
status
=
200
)
return
JsonResponse
({
"
document
"
:
obj
.
key
,
"
timestamp
"
:
int
(
obj
.
updated_at
.
timestamp
())},
status
=
200
)
print
(
form
.
errors
)
print
(
form
.
errors
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment