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
ede76c21
Verified
Commit
ede76c21
authored
11 months ago
by
TheJoeCoder
Browse files
Options
Downloads
Patches
Plain Diff
Various improvements to view book page
parent
1ced057b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
reader/models.py
+3
-0
3 additions, 0 deletions
reader/models.py
reader/templates/reader/view_book.html
+28
-0
28 additions, 0 deletions
reader/templates/reader/view_book.html
reader/urls.py
+1
-0
1 addition, 0 deletions
reader/urls.py
reader/views.py
+24
-12
24 additions, 12 deletions
reader/views.py
with
56 additions
and
12 deletions
reader/models.py
+
3
−
0
View file @
ede76c21
...
...
@@ -160,6 +160,9 @@ class UserBook(models.Model):
# Last read
last_read
=
models
.
DateTimeField
(
auto_now_add
=
True
)
def
percentage_read_whole
(
self
):
return
round
(
self
.
percentage_read
*
100
)
class
Meta
:
constraints
=
[
UniqueConstraint
(
fields
=
[
'
user
'
,
'
book
'
],
name
=
'
unique_user_book
'
),
...
...
This diff is collapsed.
Click to expand it.
reader/templates/reader/view_book.html
+
28
−
0
View file @
ede76c21
...
...
@@ -20,7 +20,35 @@
<i
class=
"bi bi-lock"
></i>
{% endif %}
</h1>
<h2
class=
"subtitle"
>
{{ book.author }}
</h2>
<br>
{% if book.description %}
<p>
{{ book.description|escape|truncatewords:20 }}
</p>
{% endif %}
{% if user_in_library %}
<br>
<br>
<div
class=
"columns"
>
<div
class=
"column"
>
<progress
class=
"progress is-success"
value=
"{{ user_book.percentage_read_whole }}"
max=
"100"
>
{{ user_book.percentage_read_whole }}%
</progress>
</div>
<div
class=
"column is-narrow"
>
<span>
{{ user_book.percentage_read_whole }}%
</span>
</div>
<div
class=
"column is-narrow"
>
<a
href=
"#"
class=
"button is-success"
>
Read
</a>
</div>
</div>
{% elif user.is_authenticated %}
<form
action=
"{% url 'add_to_library' book_id=book.id %}"
method=
"post"
>
{% csrf_token %}
<button
type=
"submit"
class=
"button is-success"
>
Add to library
</button>
</form>
{% else %}
<a
href=
"{% url 'login' %}"
class=
"button is-primary"
>
Login to read
</a>
{% endif %}
</div>
</div>
...
...
This diff is collapsed.
Click to expand it.
reader/urls.py
+
1
−
0
View file @
ede76c21
...
...
@@ -7,4 +7,5 @@ urlpatterns = [
path
(
"
library
"
,
views
.
library
,
name
=
'
library
'
),
path
(
"
books/create
"
,
views
.
create_book
,
name
=
'
create_book
'
),
path
(
"
books/<int:book_id>
"
,
views
.
view_book
,
name
=
'
view_book
'
),
path
(
"
books/<int:book_id>/to_library
"
,
views
.
add_to_library
,
name
=
'
edit_book
'
),
]
\ No newline at end of file
This diff is collapsed.
Click to expand it.
reader/views.py
+
24
−
12
View file @
ede76c21
from
django.contrib.auth.decorators
import
login_required
from
django.core.exceptions
import
PermissionDenied
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.views.decorators.http
import
require_POST
from
reader.forms
import
BookForm
from
reader.models
import
Book
,
UserBook
def
user_can_access_book
(
request
,
book
)
->
bool
:
has_permission
=
False
if
book
.
public
or
book
.
owner
==
request
.
user
:
has_permission
=
True
elif
request
.
user
.
is_authenticated
:
has_permission
=
(
request
.
user
.
has_perm
(
"
reader.book_view_others
"
,
book
)
or
request
.
user
.
has_perm
(
"
reader.book_view_others
"
))
return
has_permission
def
index
(
request
):
return
render
(
request
,
"
reader/index.html
"
)
...
...
@@ -70,16 +81,7 @@ def view_book(request, book_id):
book
=
get_object_or_404
(
Book
,
id
=
book_id
)
# Check user has permission to access this book
has_permission
=
False
if
book
.
public
:
has_permission
=
True
elif
book
.
owner
==
request
.
user
:
has_permission
=
True
elif
request
.
user
.
is_authenticated
:
has_permission
=
(
request
.
user
.
has_perm
(
"
reader.book_view_others
"
,
book
)
or
request
.
user
.
has_perm
(
"
reader.book_view_others
"
))
if
not
has_permission
:
if
not
user_can_access_book
(
request
,
book
):
raise
PermissionDenied
# Get the user's book stats for this book
...
...
@@ -91,7 +93,7 @@ def view_book(request, book_id):
# Render
return
render
(
request
,
"
reader/view_book.html
"
,
{
"
book
"
:
book
,
"
user_book
_q
"
:
user_book_q
,
"
user_book
"
:
user_book_q
.
first
()
,
"
user_in_library
"
:
user_in_library
})
...
...
@@ -120,3 +122,13 @@ def library(request):
}
books
.
append
(
ub
)
return
render
(
request
,
"
reader/library.html
"
,
{
"
books
"
:
books
,
"
page
"
:
"
library
"
})
@require_POST
@login_required
(
login_url
=
'
login
'
)
def
add_to_library
(
request
,
book_id
):
book
=
get_object_or_404
(
Book
,
id
=
book_id
)
if
not
user_can_access_book
(
request
,
book
):
raise
PermissionDenied
userbook
=
UserBook
(
book
=
book
,
user
=
request
.
user
)
userbook
.
save
()
return
redirect
(
"
library
"
)
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