Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
douban-api-proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
douban-api-proxy
Commits
cd60db41
Commit
cd60db41
authored
Jan 03, 2021
by
Ozzieisaacs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code refactor shelf handling
parent
5cce0121
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
72 deletions
+54
-72
shelf.py
cps/shelf.py
+54
-72
No files found.
cps/shelf.py
View file @
cd60db41
...
...
@@ -27,7 +27,7 @@ import sys
from
flask
import
Blueprint
,
request
,
flash
,
redirect
,
url_for
from
flask_babel
import
gettext
as
_
from
flask_login
import
login_required
,
current_user
from
sqlalchemy.sql.expression
import
func
from
sqlalchemy.sql.expression
import
func
,
true
from
sqlalchemy.exc
import
OperationalError
,
InvalidRequestError
from
.
import
logger
,
ub
,
calibre_db
,
db
...
...
@@ -221,96 +221,78 @@ def remove_from_shelf(shelf_id, book_id):
@
login_required
def
create_shelf
():
shelf
=
ub
.
Shelf
()
if
request
.
method
==
"POST"
:
to_save
=
request
.
form
.
to_dict
()
if
"is_public"
in
to_save
:
shelf
.
is_public
=
1
shelf
.
name
=
to_save
[
"title"
]
shelf
.
user_id
=
int
(
current_user
.
id
)
is_shelf_name_unique
=
False
if
shelf
.
is_public
==
1
:
is_shelf_name_unique
=
ub
.
session
.
query
(
ub
.
Shelf
)
\
.
filter
((
ub
.
Shelf
.
name
==
to_save
[
"title"
])
&
(
ub
.
Shelf
.
is_public
==
1
))
\
.
first
()
is
None
if
not
is_shelf_name_unique
:
flash
(
_
(
u"A public shelf with the name '
%(title)
s' already exists."
,
title
=
to_save
[
"title"
]),
category
=
"error"
)
else
:
is_shelf_name_unique
=
ub
.
session
.
query
(
ub
.
Shelf
)
\
.
filter
((
ub
.
Shelf
.
name
==
to_save
[
"title"
])
&
(
ub
.
Shelf
.
is_public
==
0
)
&
(
ub
.
Shelf
.
user_id
==
int
(
current_user
.
id
)))
\
.
first
()
is
None
if
not
is_shelf_name_unique
:
flash
(
_
(
u"A private shelf with the name '
%(title)
s' already exists."
,
title
=
to_save
[
"title"
]),
category
=
"error"
)
if
is_shelf_name_unique
:
try
:
ub
.
session
.
add
(
shelf
)
ub
.
session
.
commit
()
flash
(
_
(
u"Shelf
%(title)
s created"
,
title
=
to_save
[
"title"
]),
category
=
"success"
)
return
redirect
(
url_for
(
'shelf.show_shelf'
,
shelf_id
=
shelf
.
id
))
except
(
OperationalError
,
InvalidRequestError
):
ub
.
session
.
rollback
()
flash
(
_
(
u"Settings DB is not Writeable"
),
category
=
"error"
)
except
Exception
:
ub
.
session
.
rollback
()
flash
(
_
(
u"There was an error"
),
category
=
"error"
)
return
render_title_template
(
'shelf_edit.html'
,
shelf
=
shelf
,
title
=
_
(
u"Create a Shelf"
),
page
=
"shelfcreate"
)
else
:
return
render_title_template
(
'shelf_edit.html'
,
shelf
=
shelf
,
title
=
_
(
u"Create a Shelf"
),
page
=
"shelfcreate"
)
create_edit_shelf
(
shelf
)
return
render_title_template
(
'shelf_edit.html'
,
shelf
=
shelf
,
title
=
_
(
u"Create a Shelf"
),
page
=
"shelfcreate"
)
@
shelf
.
route
(
"/shelf/edit/<int:shelf_id>"
,
methods
=
[
"GET"
,
"POST"
])
@
login_required
def
edit_shelf
(
shelf_id
):
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
id
==
shelf_id
)
.
first
()
if
request
.
method
==
"POST"
:
to_save
=
request
.
form
.
to_dict
(
)
create_edit_shelf
(
shelf
,
shelf_id
)
return
render_title_template
(
'shelf_edit.html'
,
shelf
=
shelf
,
title
=
_
(
u"Edit a shelf"
),
page
=
"shelfedit"
)
is_shelf_name_unique
=
False
if
shelf
.
is_public
==
1
:
is_shelf_name_unique
=
ub
.
session
.
query
(
ub
.
Shelf
)
\
.
filter
((
ub
.
Shelf
.
name
==
to_save
[
"title"
])
&
(
ub
.
Shelf
.
is_public
==
1
))
\
.
filter
(
ub
.
Shelf
.
id
!=
shelf_id
)
\
.
first
()
is
None
if
not
is_shelf_name_unique
:
flash
(
_
(
u"A public shelf with the name '
%(title)
s' already exists."
,
title
=
to_save
[
"title"
]),
category
=
"error"
)
else
:
is_shelf_name_unique
=
ub
.
session
.
query
(
ub
.
Shelf
)
\
.
filter
((
ub
.
Shelf
.
name
==
to_save
[
"title"
])
&
(
ub
.
Shelf
.
is_public
==
0
)
&
(
ub
.
Shelf
.
user_id
==
int
(
current_user
.
id
)))
\
.
filter
(
ub
.
Shelf
.
id
!=
shelf_id
)
\
.
first
()
is
None
if
not
is_shelf_name_unique
:
flash
(
_
(
u"A private shelf with the name '
%(title)
s' already exists."
,
title
=
to_save
[
"title"
]),
category
=
"error"
)
if
is_shelf_name_unique
:
shelf
.
name
=
to_save
[
"title"
]
shelf
.
last_modified
=
datetime
.
utcnow
()
# if shelf ID is set, we are editing a shelf
def
create_edit_shelf
(
shelf
,
shelf_id
=
False
):
if
request
.
method
==
"POST"
:
to_save
=
request
.
form
.
to_dict
()
if
check_shelf_is_unique
(
shelf
,
to_save
,
shelf_id
):
if
"is_public"
in
to_save
:
shelf
.
is_public
=
1
else
:
shelf
.
is_public
=
0
shelf
.
name
=
to_save
[
"title"
]
shelf
.
last_modified
=
datetime
.
utcnow
()
if
not
shelf_id
:
shelf
.
user_id
=
int
(
current_user
.
id
)
try
:
if
shelf_id
:
ub
.
session
.
add
(
shelf
)
shelf_action
=
"changed"
flash_text
=
_
(
u"Shelf
%(title)
s changed"
,
title
=
to_save
[
"title"
])
else
:
shelf_action
=
"created"
flash_text
=
_
(
u"Shelf
%(title)
s created"
,
title
=
to_save
[
"title"
])
ub
.
session
.
commit
()
flash
(
_
(
u"Shelf
%(title)
s changed"
,
title
=
to_save
[
"title"
]),
category
=
"success"
)
except
(
OperationalError
,
InvalidRequestError
):
log
.
info
(
u"Shelf {} {}"
.
format
(
to_save
[
"title"
],
shelf_action
))
flash
(
flash_text
,
category
=
"success"
)
return
redirect
(
url_for
(
'shelf.show_shelf'
,
shelf_id
=
shelf
.
id
))
except
(
OperationalError
,
InvalidRequestError
)
as
e
:
ub
.
session
.
rollback
()
log
.
debug_or_exception
(
e
)
flash
(
_
(
u"Settings DB is not Writeable"
),
category
=
"error"
)
except
Exception
:
except
Exception
as
e
:
ub
.
session
.
rollback
()
log
.
debug_or_exception
(
e
)
flash
(
_
(
u"There was an error"
),
category
=
"error"
)
return
render_title_template
(
'shelf_edit.html'
,
shelf
=
shelf
,
title
=
_
(
u"Edit a shelf"
),
page
=
"shelfedit"
)
def
check_shelf_is_unique
(
shelf
,
to_save
,
shelf_id
=
False
):
if
shelf_id
:
ident
=
ub
.
Shelf
.
id
!=
shelf_id
else
:
ident
=
true
()
if
shelf
.
is_public
==
1
:
is_shelf_name_unique
=
ub
.
session
.
query
(
ub
.
Shelf
)
\
.
filter
((
ub
.
Shelf
.
name
==
to_save
[
"title"
])
&
(
ub
.
Shelf
.
is_public
==
1
))
\
.
filter
(
ident
)
\
.
first
()
is
None
if
not
is_shelf_name_unique
:
flash
(
_
(
u"A public shelf with the name '
%(title)
s' already exists."
,
title
=
to_save
[
"title"
]),
category
=
"error"
)
else
:
return
render_title_template
(
'shelf_edit.html'
,
shelf
=
shelf
,
title
=
_
(
u"Edit a shelf"
),
page
=
"shelfedit"
)
is_shelf_name_unique
=
ub
.
session
.
query
(
ub
.
Shelf
)
\
.
filter
((
ub
.
Shelf
.
name
==
to_save
[
"title"
])
&
(
ub
.
Shelf
.
is_public
==
0
)
&
(
ub
.
Shelf
.
user_id
==
int
(
current_user
.
id
)))
\
.
filter
(
ident
)
\
.
first
()
is
None
if
not
is_shelf_name_unique
:
flash
(
_
(
u"A private shelf with the name '
%(title)
s' already exists."
,
title
=
to_save
[
"title"
]),
category
=
"error"
)
return
is_shelf_name_unique
def
delete_shelf_helper
(
cur_shelf
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment