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
955a850c
Verified
Commit
955a850c
authored
11 months ago
by
TheJoeCoder
Browse files
Options
Downloads
Patches
Plain Diff
push progress endpoint
parent
e90d77cb
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
reader/forms.py
+7
-1
7 additions, 1 deletion
reader/forms.py
reader/urls.py
+1
-0
1 addition, 0 deletions
reader/urls.py
reader/views.py
+57
-3
57 additions, 3 deletions
reader/views.py
with
65 additions
and
4 deletions
reader/forms.py
+
7
−
1
View file @
955a850c
...
@@ -10,7 +10,7 @@ from django.core.exceptions import ValidationError
...
@@ -10,7 +10,7 @@ from django.core.exceptions import ValidationError
from
django.db.models.fields.files
import
FieldFile
from
django.db.models.fields.files
import
FieldFile
from
api_koreader.xpointer_cfi_utils
import
validate_epub
from
api_koreader.xpointer_cfi_utils
import
validate_epub
from
reader.models
import
Book
from
reader.models
import
Book
,
UserBook
class
FileTypeValidator
:
class
FileTypeValidator
:
...
@@ -68,3 +68,9 @@ class BookForm(forms.ModelForm):
...
@@ -68,3 +68,9 @@ class BookForm(forms.ModelForm):
if
not
self
.
request
.
user
.
has_perm
(
"
reader.book_create_loanable
"
):
if
not
self
.
request
.
user
.
has_perm
(
"
reader.book_create_loanable
"
):
self
.
fields
[
"
loanable
"
].
disabled
=
True
self
.
fields
[
"
loanable
"
].
disabled
=
True
self
.
fields
[
"
loanable_copies
"
].
disabled
=
True
self
.
fields
[
"
loanable_copies
"
].
disabled
=
True
class
UserBookProgressForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
UserBook
fields
=
[
"
percentage_read
"
,
"
last_progress_device
"
,
"
last_progress_cfi
"
]
This diff is collapsed.
Click to expand it.
reader/urls.py
+
1
−
0
View file @
955a850c
...
@@ -9,4 +9,5 @@ urlpatterns = [
...
@@ -9,4 +9,5 @@ urlpatterns = [
path
(
"
books/<int:book_id>
"
,
views
.
view_book
,
name
=
'
view_book
'
),
path
(
"
books/<int:book_id>
"
,
views
.
view_book
,
name
=
'
view_book
'
),
path
(
"
books/<int:book_id>/to_library
"
,
views
.
add_to_library
,
name
=
'
add_to_library
'
),
path
(
"
books/<int:book_id>/to_library
"
,
views
.
add_to_library
,
name
=
'
add_to_library
'
),
path
(
"
books/<int:book_id>/read
"
,
views
.
read_book
,
name
=
'
read_book
'
),
path
(
"
books/<int:book_id>/read
"
,
views
.
read_book
,
name
=
'
read_book
'
),
path
(
"
books/<int:book_id>/progress
"
,
views
.
update_progress
,
name
=
'
update_progress
'
),
]
]
\ No newline at end of file
This diff is collapsed.
Click to expand it.
reader/views.py
+
57
−
3
View file @
955a850c
import
re
from
django.contrib.auth.decorators
import
login_required
from
django.contrib.auth.decorators
import
login_required
from
django.core.exceptions
import
PermissionDenied
from
django.core.exceptions
import
PermissionDenied
from
django.http
import
JsonResponse
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.views.decorators.http
import
require_POST
from
django.views.decorators.http
import
require_POST
from
reader.forms
import
BookForm
from
reader.forms
import
BookForm
,
UserBookProgressForm
from
reader.models
import
Book
,
UserBook
from
reader.models
import
Book
,
UserBook
...
@@ -139,15 +142,66 @@ def read_book(request, book_id):
...
@@ -139,15 +142,66 @@ def read_book(request, book_id):
if
not
user_can_access_book
(
request
,
book
):
if
not
user_can_access_book
(
request
,
book
):
raise
PermissionDenied
raise
PermissionDenied
userbook
=
get_object_or_404
(
UserBook
,
book
=
book
,
user
=
request
.
user
)
userbook
=
get_object_or_404
(
UserBook
,
book
=
book
,
user
=
request
.
user
)
pagenumber
=
None
cfi_ref
=
None
# Check file type
# Check file type
match
book
.
file_type
:
match
book
.
file_type
:
case
"
pdf
"
:
case
"
pdf
"
:
reader_template
=
"
reader/book_read/pdf_viewer.html
"
reader_template
=
"
reader/book_read/pdf_viewer.html
"
try
:
# Try to parse page number
pagenumber
=
int
(
userbook
.
last_progress_cfi
)
except
ValueError
:
pass
except
TypeError
:
pass
case
"
epub
"
:
case
"
epub
"
:
reader_template
=
"
reader/book_read/epub_viewer.html
"
reader_template
=
"
reader/book_read/epub_viewer.html
"
# Try to parse CFI
rgx
=
re
.
compile
(
r
"
epubcfi\((.*)\)
"
)
m
=
rgx
.
match
(
userbook
.
last_progress_cfi
)
if
m
:
cfi_ref
=
userbook
.
last_progress_cfi
case
_
:
case
_
:
reader_template
=
"
reader/book_read/invalid_filetype.html
"
reader_template
=
"
reader/book_read/invalid_filetype.html
"
return
render
(
request
,
reader_template
,
{
return
render
(
request
,
reader_template
,
{
"
book
"
:
book
,
"
book
"
:
book
,
"
userbook
"
:
userbook
"
userbook
"
:
userbook
,
"
pagenumber
"
:
pagenumber
,
"
cfi_ref
"
:
cfi_ref
})
})
@require_POST
@login_required
(
login_url
=
'
login
'
)
def
update_progress
(
request
,
book_id
):
book
=
get_object_or_404
(
Book
,
id
=
book_id
)
if
not
user_can_access_book
(
request
,
book
):
raise
PermissionDenied
userbook
=
get_object_or_404
(
UserBook
,
book
=
book
,
user
=
request
.
user
)
form
=
UserBookProgressForm
(
data
=
request
.
POST
,
instance
=
userbook
)
if
form
.
is_valid
():
valid_for_filetype
=
False
if
book
.
file_type
==
"
pdf
"
:
# Check page number is valid
try
:
int
(
form
.
cleaned_data
[
"
last_progress_cfi
"
])
valid_for_filetype
=
True
# If no exception, it's valid
except
ValueError
:
pass
except
TypeError
:
pass
elif
book
.
file_type
==
"
epub
"
:
# Check CFI is valid
rgx
=
re
.
compile
(
r
"
epubcfi\((.*)\)
"
)
m
=
rgx
.
match
(
form
.
cleaned_data
[
"
last_progress_cfi
"
])
if
m
:
valid_for_filetype
=
True
if
valid_for_filetype
:
form
.
save
()
return
JsonResponse
({
"
success
"
:
True
})
else
:
return
JsonResponse
({
"
error
"
:
"
Invalid progress for file type
"
},
status
=
400
)
else
:
return
JsonResponse
({
"
error
"
:
form
.
errors
},
status
=
400
)
\ No newline at end of file
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
register
or
sign in
to comment