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
5cce0121
Commit
5cce0121
authored
Jan 03, 2021
by
Ozzieisaacs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code refactoring ub.session.commit()
Code cosmentics admin.py
parent
4578af7a
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
195 additions
and
282 deletions
+195
-282
admin.py
cps/admin.py
+147
-180
editbooks.py
cps/editbooks.py
+1
-1
kobo.py
cps/kobo.py
+11
-34
kobo_auth.py
cps/kobo_auth.py
+3
-9
oauth_bb.py
cps/oauth_bb.py
+9
-24
remotelogin.py
cps/remotelogin.py
+6
-7
shelf.py
cps/shelf.py
+2
-10
ub.py
cps/ub.py
+12
-1
web.py
cps/web.py
+4
-16
No files found.
cps/admin.py
View file @
5cce0121
This diff is collapsed.
Click to expand it.
cps/editbooks.py
View file @
5cce0121
...
...
@@ -240,7 +240,7 @@ def delete_book(book_id, book_format, jsonResponse):
ub
.
session
.
query
(
ub
.
BookShelf
)
.
filter
(
ub
.
BookShelf
.
book_id
==
book_id
)
.
delete
()
ub
.
session
.
query
(
ub
.
ReadBook
)
.
filter
(
ub
.
ReadBook
.
book_id
==
book_id
)
.
delete
()
ub
.
delete_download
(
book_id
)
ub
.
session
.
commit
()
ub
.
session
_
commit
()
# check if only this book links to:
# author, language, series, tags, custom columns
...
...
cps/kobo.py
View file @
5cce0121
...
...
@@ -462,10 +462,7 @@ def HandleTagCreate():
items_unknown_to_calibre
=
add_items_to_shelf
(
items
,
shelf
)
if
items_unknown_to_calibre
:
log
.
debug
(
"Received request to add unknown books to a collection. Silently ignoring items."
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
()
return
make_response
(
jsonify
(
str
(
shelf
.
uuid
)),
201
)
...
...
@@ -497,10 +494,7 @@ def HandleTagUpdate(tag_id):
shelf
.
name
=
name
ub
.
session
.
merge
(
shelf
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
()
return
make_response
(
' '
,
200
)
...
...
@@ -552,11 +546,7 @@ def HandleTagAddItem(tag_id):
log
.
debug
(
"Received request to add an unknown book to a collection. Silently ignoring item."
)
ub
.
session
.
merge
(
shelf
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
()
return
make_response
(
''
,
201
)
...
...
@@ -596,10 +586,7 @@ def HandleTagRemoveItem(tag_id):
shelf
.
books
.
filter
(
ub
.
BookShelf
.
book_id
==
book
.
id
)
.
delete
()
except
KeyError
:
items_unknown_to_calibre
.
append
(
item
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
()
if
items_unknown_to_calibre
:
log
.
debug
(
"Received request to remove an unknown book to a collecition. Silently ignoring item."
)
...
...
@@ -645,10 +632,7 @@ def sync_shelves(sync_token, sync_results):
"ChangedTag"
:
tag
})
sync_token
.
tags_last_modified
=
new_tags_last_modified
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
()
# Creates a Kobo "Tag" object from a ub.Shelf object
...
...
@@ -729,10 +713,7 @@ def HandleStateRequest(book_uuid):
abort
(
400
,
description
=
"Malformed request data is missing 'ReadingStates' key"
)
ub
.
session
.
merge
(
kobo_reading_state
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
()
return
jsonify
({
"RequestResult"
:
"Success"
,
"UpdateResults"
:
[
update_results_response
],
...
...
@@ -770,10 +751,10 @@ def get_or_create_reading_state(book_id):
kobo_reading_state
.
statistics
=
ub
.
KoboStatistics
()
book_read
.
kobo_reading_state
=
kobo_reading_state
ub
.
session
.
add
(
book_read
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
#
try:
#
ub.session.commit()
#
except OperationalError:
#
ub.session.rollback()
return
book_read
.
kobo_reading_state
...
...
@@ -876,11 +857,7 @@ def HandleBookDeletionRequest(book_uuid):
archived_book
.
last_modified
=
datetime
.
datetime
.
utcnow
()
ub
.
session
.
merge
(
archived_book
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
()
return
(
""
,
204
)
...
...
cps/kobo_auth.py
View file @
5cce0121
...
...
@@ -148,10 +148,7 @@ def generate_auth_token(user_id):
auth_token
.
token_type
=
1
ub
.
session
.
add
(
auth_token
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
()
return
render_title_template
(
"generate_kobo_auth_url.html"
,
title
=
_
(
u"Kobo Setup"
),
...
...
@@ -168,8 +165,5 @@ def delete_auth_token(user_id):
# Invalidate any prevously generated Kobo Auth token for this user.
ub
.
session
.
query
(
ub
.
RemoteAuthToken
)
.
filter
(
ub
.
RemoteAuthToken
.
user_id
==
user_id
)
\
.
filter
(
ub
.
RemoteAuthToken
.
token_type
==
1
)
.
delete
()
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
return
""
return
ub
.
session_commit
()
cps/oauth_bb.py
View file @
5cce0121
...
...
@@ -85,11 +85,7 @@ def register_user_with_oauth(user=None):
except
NoResultFound
:
# no found, return error
return
try
:
ub
.
session
.
commit
()
except
Exception
as
e
:
log
.
debug_or_exception
(
e
)
ub
.
session
.
rollback
()
ub
.
session_commit
(
"User {} with OAuth for provider {} registered"
.
format
(
user
.
nickname
,
oauth_key
))
def
logout_oauth_user
():
...
...
@@ -101,19 +97,12 @@ def logout_oauth_user():
if
ub
.
oauth_support
:
oauthblueprints
=
[]
if
not
ub
.
session
.
query
(
ub
.
OAuthProvider
)
.
count
():
oauthProvider
=
ub
.
OAuthProvider
()
oauthProvider
.
provider_name
=
"github"
oauthProvider
.
active
=
False
ub
.
session
.
add
(
oauthProvider
)
ub
.
session
.
commit
()
oauthProvider
=
ub
.
OAuthProvider
()
oauthProvider
.
provider_name
=
"google"
oauthProvider
.
active
=
False
ub
.
session
.
add
(
oauthProvider
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
for
provider
in
(
"github"
,
"google"
):
oauthProvider
=
ub
.
OAuthProvider
()
oauthProvider
.
provider_name
=
provider
oauthProvider
.
active
=
False
ub
.
session
.
add
(
oauthProvider
)
ub
.
session_commit
(
"{} Blueprint Created"
.
format
(
provider
))
oauth_ids
=
ub
.
session
.
query
(
ub
.
OAuthProvider
)
.
all
()
ele1
=
dict
(
provider_name
=
'github'
,
...
...
@@ -203,12 +192,8 @@ if ub.oauth_support:
provider_user_id
=
provider_user_id
,
token
=
token
,
)
try
:
ub
.
session
.
add
(
oauth_entry
)
ub
.
session
.
commit
()
except
Exception
as
e
:
log
.
debug_or_exception
(
e
)
ub
.
session
.
rollback
()
ub
.
session
.
add
(
oauth_entry
)
ub
.
session_commit
()
# Disable Flask-Dance's default behavior for saving the OAuth token
# Value differrs depending on flask-dance version
...
...
cps/remotelogin.py
View file @
5cce0121
...
...
@@ -59,8 +59,7 @@ def remote_login_required(f):
def
remote_login
():
auth_token
=
ub
.
RemoteAuthToken
()
ub
.
session
.
add
(
auth_token
)
ub
.
session
.
commit
()
ub
.
session_commit
()
verify_url
=
url_for
(
'remotelogin.verify_token'
,
token
=
auth_token
.
auth_token
,
_external
=
true
)
log
.
debug
(
u"Remot Login request with token:
%
s"
,
auth_token
.
auth_token
)
return
render_title_template
(
'remote_login.html'
,
title
=
_
(
u"login"
),
token
=
auth_token
.
auth_token
,
...
...
@@ -80,9 +79,9 @@ def verify_token(token):
return
redirect
(
url_for
(
'web.index'
))
# Token expired
if
datetime
.
now
()
>
auth_token
.
expiration
:
el
if
datetime
.
now
()
>
auth_token
.
expiration
:
ub
.
session
.
delete
(
auth_token
)
ub
.
session
.
commit
()
ub
.
session
_
commit
()
flash
(
_
(
u"Token has expired"
),
category
=
"error"
)
log
.
error
(
u"Remote Login token expired"
)
...
...
@@ -91,7 +90,7 @@ def verify_token(token):
# Update token with user information
auth_token
.
user_id
=
current_user
.
id
auth_token
.
verified
=
True
ub
.
session
.
commit
()
ub
.
session
_
commit
()
flash
(
_
(
u"Success! Please return to your device"
),
category
=
"success"
)
log
.
debug
(
u"Remote Login token for userid
%
s verified"
,
auth_token
.
user_id
)
...
...
@@ -114,7 +113,7 @@ def token_verified():
# Token expired
elif
datetime
.
now
()
>
auth_token
.
expiration
:
ub
.
session
.
delete
(
auth_token
)
ub
.
session
.
commit
()
ub
.
session
_
commit
()
data
[
'status'
]
=
'error'
data
[
'message'
]
=
_
(
u"Token has expired"
)
...
...
@@ -127,7 +126,7 @@ def token_verified():
login_user
(
user
)
ub
.
session
.
delete
(
auth_token
)
ub
.
session
.
commit
(
)
ub
.
session
_commit
(
"User {} logged in via remotelogin, token deleted"
.
format
(
user
.
nickname
)
)
data
[
'status'
]
=
'success'
log
.
debug
(
u"Remote Login for userid
%
s succeded"
,
user
.
id
)
...
...
cps/shelf.py
View file @
5cce0121
...
...
@@ -320,12 +320,7 @@ def delete_shelf_helper(cur_shelf):
ub
.
session
.
delete
(
cur_shelf
)
ub
.
session
.
query
(
ub
.
BookShelf
)
.
filter
(
ub
.
BookShelf
.
shelf
==
shelf_id
)
.
delete
()
ub
.
session
.
add
(
ub
.
ShelfArchive
(
uuid
=
cur_shelf
.
uuid
,
user_id
=
cur_shelf
.
user_id
))
try
:
ub
.
session
.
commit
()
log
.
info
(
"successfully deleted
%
s"
,
cur_shelf
)
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
(
"successfully deleted Shelf {}"
.
format
(
cur_shelf
.
name
))
@
shelf
.
route
(
"/shelf/delete/<int:shelf_id>"
)
...
...
@@ -388,10 +383,7 @@ def change_shelf_order(shelf_id, order):
book
=
ub
.
session
.
query
(
ub
.
BookShelf
)
.
filter
(
ub
.
BookShelf
.
shelf
==
shelf_id
)
\
.
filter
(
ub
.
BookShelf
.
book_id
==
entry
.
id
)
.
first
()
book
.
order
=
index
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
(
"Shelf-id:{} - Order changed"
.
format
(
shelf_id
))
def
render_show_shelf
(
shelf_type
,
shelf_id
,
page_no
,
sort_param
):
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
id
==
shelf_id
)
.
first
()
...
...
cps/ub.py
View file @
5cce0121
...
...
@@ -46,8 +46,9 @@ from sqlalchemy.orm.attributes import flag_modified
from
sqlalchemy.orm
import
backref
,
relationship
,
sessionmaker
,
Session
,
scoped_session
from
werkzeug.security
import
generate_password_hash
from
.
import
constants
from
.
import
constants
,
logger
log
=
logger
.
create
()
session
=
None
app_DB_path
=
None
...
...
@@ -695,3 +696,13 @@ def dispose():
old_session
.
bind
.
dispose
()
except
Exception
:
pass
def
session_commit
(
success
=
None
):
try
:
session
.
commit
()
if
success
:
log
.
info
(
success
)
except
(
exc
.
OperationalError
,
exc
.
InvalidRequestError
)
as
e
:
session
.
rollback
()
log
.
debug_or_exception
(
e
)
return
""
cps/web.py
View file @
5cce0121
...
...
@@ -135,10 +135,7 @@ def bookmark(book_id, book_format):
ub
.
Bookmark
.
book_id
==
book_id
,
ub
.
Bookmark
.
format
==
book_format
))
.
delete
()
if
not
bookmark_key
:
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
()
return
""
,
204
lbookmark
=
ub
.
Bookmark
(
user_id
=
current_user
.
id
,
...
...
@@ -146,10 +143,7 @@ def bookmark(book_id, book_format):
format
=
book_format
,
bookmark_key
=
bookmark_key
)
ub
.
session
.
merge
(
lbookmark
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
(
"Bookmark for user {} in book {} created"
.
format
(
current_user
.
id
,
book_id
))
return
""
,
201
...
...
@@ -174,10 +168,7 @@ def toggle_read(book_id):
kobo_reading_state
.
statistics
=
ub
.
KoboStatistics
()
book
.
kobo_reading_state
=
kobo_reading_state
ub
.
session
.
merge
(
book
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
(
"Book {} readbit toggled"
.
format
(
book_id
))
else
:
try
:
calibre_db
.
update_title_sort
(
config
)
...
...
@@ -211,10 +202,7 @@ def toggle_archived(book_id):
archived_book
=
ub
.
ArchivedBook
(
user_id
=
current_user
.
id
,
book_id
=
book_id
)
archived_book
.
is_archived
=
True
ub
.
session
.
merge
(
archived_book
)
try
:
ub
.
session
.
commit
()
except
OperationalError
:
ub
.
session
.
rollback
()
ub
.
session_commit
(
"Book {} archivebit toggled"
.
format
(
book_id
))
return
""
...
...
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