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
777c2726
Commit
777c2726
authored
Dec 07, 2020
by
Ozzieisaacs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changed session_handing
parent
c25afdc2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
254 additions
and
243 deletions
+254
-243
__init__.py
cps/__init__.py
+3
-2
admin.py
cps/admin.py
+56
-56
config_sql.py
cps/config_sql.py
+6
-2
db.py
cps/db.py
+3
-2
editbooks.py
cps/editbooks.py
+4
-4
helper.py
cps/helper.py
+6
-9
kobo.py
cps/kobo.py
+26
-25
kobo_auth.py
cps/kobo_auth.py
+6
-6
oauth_bb.py
cps/oauth_bb.py
+24
-24
opds.py
cps/opds.py
+6
-6
shelf.py
cps/shelf.py
+46
-46
ub.py
cps/ub.py
+19
-12
web.py
cps/web.py
+49
-49
No files found.
cps/__init__.py
View file @
777c2726
...
...
@@ -71,7 +71,7 @@ lm.session_protection = 'strong'
ub
.
init_db
(
cli
.
settingspath
)
# pylint: disable=no-member
config
=
config_sql
.
load_configuration
(
ub
.
s
ession
)
config
=
config_sql
.
load_configuration
(
ub
.
Scoped_S
ession
)
web_server
=
WebServer
()
...
...
@@ -94,12 +94,13 @@ def create_app():
app
.
root_path
=
app
.
root_path
.
decode
(
'utf-8'
)
app
.
instance_path
=
app
.
instance_path
.
decode
(
'utf-8'
)
#if os.environ.get('FLASK_DEBUG'):
cache_buster
.
init_cache_busting
(
app
)
log
.
info
(
'Starting Calibre Web...'
)
Principal
(
app
)
lm
.
init_app
(
app
)
app
.
secret_key
=
os
.
getenv
(
'SECRET_KEY'
,
config_sql
.
get_flask_session_key
(
ub
.
s
ession
))
app
.
secret_key
=
os
.
getenv
(
'SECRET_KEY'
,
config_sql
.
get_flask_session_key
(
ub
.
Scoped_S
ession
))
web_server
.
init_app
(
app
,
config
)
...
...
cps/admin.py
View file @
777c2726
This diff is collapsed.
Click to expand it.
cps/config_sql.py
View file @
777c2726
...
...
@@ -395,7 +395,8 @@ def _migrate_database(session):
_migrate_table
(
session
,
_Flask_Settings
)
def
load_configuration
(
session
):
def
load_configuration
(
Session
):
session
=
Session
()
_migrate_database
(
session
)
if
not
session
.
query
(
_Settings
)
.
count
():
...
...
@@ -409,12 +410,15 @@ def load_configuration(session):
session
.
query
(
ub
.
User
)
.
filter
(
ub
.
User
.
mature_content
!=
True
)
.
\
update
({
"denied_tags"
:
conf
.
config_mature_content_tags
},
synchronize_session
=
False
)
session
.
commit
()
session
.
close
()
return
conf
def
get_flask_session_key
(
session
):
def
get_flask_session_key
(
Session
):
session
=
Session
()
flask_settings
=
session
.
query
(
_Flask_Settings
)
.
one_or_none
()
if
flask_settings
==
None
:
flask_settings
=
_Flask_Settings
(
os
.
urandom
(
32
))
session
.
add
(
flask_settings
)
session
.
commit
()
session
.
close
()
return
flask_settings
.
flask_session_key
cps/db.py
View file @
777c2726
...
...
@@ -32,9 +32,10 @@ from sqlalchemy.orm import relationship, sessionmaker, scoped_session
from
sqlalchemy.orm.collections
import
InstrumentedList
from
sqlalchemy.ext.declarative
import
declarative_base
,
DeclarativeMeta
from
sqlalchemy.pool
import
StaticPool
from
flask_login
import
current_user
from
sqlalchemy.sql.expression
import
and_
,
true
,
false
,
text
,
func
,
or_
from
sqlalchemy.ext.associationproxy
import
association_proxy
from
flask_login
import
current_user
from
flask
import
g
from
babel
import
Locale
as
LC
from
babel.core
import
UnknownLocaleError
from
flask_babel
import
gettext
as
_
...
...
@@ -564,7 +565,7 @@ class CalibreDB():
def
common_filters
(
self
,
allow_show_archived
=
False
):
if
not
allow_show_archived
:
archived_books
=
(
ub
.
session
.
query
(
ub
.
ArchivedBook
)
g
.
ub
session
.
query
(
ub
.
ArchivedBook
)
.
filter
(
ub
.
ArchivedBook
.
user_id
==
int
(
current_user
.
id
))
.
filter
(
ub
.
ArchivedBook
.
is_archived
==
True
)
.
all
()
...
...
cps/editbooks.py
View file @
777c2726
...
...
@@ -27,7 +27,7 @@ import json
from
shutil
import
copyfile
from
uuid
import
uuid4
from
flask
import
Blueprint
,
request
,
flash
,
redirect
,
url_for
,
abort
,
Markup
,
Response
from
flask
import
Blueprint
,
request
,
flash
,
redirect
,
url_for
,
abort
,
Markup
,
Response
,
g
from
flask_babel
import
gettext
as
_
from
flask_login
import
current_user
,
login_required
from
sqlalchemy.exc
import
OperationalError
...
...
@@ -212,10 +212,10 @@ def delete_book(book_id, book_format, jsonResponse):
flash
(
error
,
category
=
"warning"
)
if
not
book_format
:
# delete book from Shelfs, Downloads, Read list
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
()
g
.
ub
session
.
query
(
ub
.
BookShelf
)
.
filter
(
ub
.
BookShelf
.
book_id
==
book_id
)
.
delete
()
g
.
ub
session
.
query
(
ub
.
ReadBook
)
.
filter
(
ub
.
ReadBook
.
book_id
==
book_id
)
.
delete
()
ub
.
delete_download
(
book_id
)
ub
.
session
.
commit
()
g
.
ub
session
.
commit
()
# check if only this book links to:
# author, language, series, tags, custom columns
...
...
cps/helper.py
View file @
777c2726
...
...
@@ -24,10 +24,7 @@ import io
import
mimetypes
import
re
import
shutil
import
glob
import
time
import
zipfile
import
json
import
unicodedata
from
datetime
import
datetime
,
timedelta
from
tempfile
import
gettempdir
...
...
@@ -35,7 +32,7 @@ from tempfile import gettempdir
import
requests
from
babel.dates
import
format_datetime
from
babel.units
import
format_unit
from
flask
import
send_from_directory
,
make_response
,
redirect
,
abort
,
url_for
,
send_file
from
flask
import
send_from_directory
,
make_response
,
redirect
,
abort
,
url_for
,
g
from
flask_babel
import
gettext
as
_
from
flask_login
import
current_user
from
sqlalchemy.sql.expression
import
true
,
false
,
and_
,
text
...
...
@@ -485,7 +482,7 @@ def delete_book_gdrive(book, book_format):
def
reset_password
(
user_id
):
existing_user
=
ub
.
session
.
query
(
ub
.
User
)
.
filter
(
ub
.
User
.
id
==
user_id
)
.
first
()
existing_user
=
g
.
ub
session
.
query
(
ub
.
User
)
.
filter
(
ub
.
User
.
id
==
user_id
)
.
first
()
if
not
existing_user
:
return
0
,
None
if
not
config
.
get_mail_server_configured
():
...
...
@@ -493,11 +490,11 @@ def reset_password(user_id):
try
:
password
=
generate_random_password
()
existing_user
.
password
=
generate_password_hash
(
password
)
ub
.
session
.
commit
()
g
.
ub
session
.
commit
()
send_registration_mail
(
existing_user
.
email
,
existing_user
.
nickname
,
password
,
True
)
return
1
,
existing_user
.
nickname
except
Exception
:
ub
.
session
.
rollback
()
g
.
ub
session
.
rollback
()
return
0
,
None
...
...
@@ -779,11 +776,11 @@ def tags_filters():
def
check_valid_domain
(
domain_text
):
# domain_text = domain_text.split('@', 1)[-1].lower()
sql
=
"SELECT * FROM registration WHERE (:domain LIKE domain and allow = 1);"
result
=
ub
.
session
.
query
(
ub
.
Registration
)
.
from_statement
(
text
(
sql
))
.
params
(
domain
=
domain_text
)
.
all
()
result
=
g
.
ub
session
.
query
(
ub
.
Registration
)
.
from_statement
(
text
(
sql
))
.
params
(
domain
=
domain_text
)
.
all
()
if
not
len
(
result
):
return
False
sql
=
"SELECT * FROM registration WHERE (:domain LIKE domain and allow = 0);"
result
=
ub
.
session
.
query
(
ub
.
Registration
)
.
from_statement
(
text
(
sql
))
.
params
(
domain
=
domain_text
)
.
all
()
result
=
g
.
ub
session
.
query
(
ub
.
Registration
)
.
from_statement
(
text
(
sql
))
.
params
(
domain
=
domain_text
)
.
all
()
return
not
len
(
result
)
...
...
cps/kobo.py
View file @
777c2726
...
...
@@ -37,7 +37,8 @@ from flask import (
current_app
,
url_for
,
redirect
,
abort
abort
,
g
)
from
flask_login
import
current_user
from
werkzeug.datastructures
import
Headers
...
...
@@ -210,7 +211,7 @@ def HandleSyncRequest():
# generate reading state data
changed_reading_states
=
(
ub
.
session
.
query
(
ub
.
KoboReadingState
)
g
.
ub
session
.
query
(
ub
.
KoboReadingState
)
.
filter
(
and_
(
func
.
datetime
(
ub
.
KoboReadingState
.
last_modified
)
>
sync_token
.
reading_state_last_modified
,
ub
.
KoboReadingState
.
user_id
==
current_user
.
id
,
ub
.
KoboReadingState
.
book_id
.
notin_
(
reading_states_in_new_entitlements
))))
...
...
@@ -439,19 +440,19 @@ def HandleTagCreate():
log
.
debug
(
"Received malformed v1/library/tags request."
)
abort
(
400
,
description
=
"Malformed tags POST request. Data has empty 'Name', missing 'Name' or 'Items' field"
)
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
name
==
name
,
ub
.
Shelf
.
user_id
==
shelf
=
g
.
ub
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
name
==
name
,
ub
.
Shelf
.
user_id
==
current_user
.
id
)
.
one_or_none
()
if
shelf
and
not
shelf_lib
.
check_shelf_edit_permissions
(
shelf
):
abort
(
401
,
description
=
"User is unauthaurized to create shelf."
)
if
not
shelf
:
shelf
=
ub
.
Shelf
(
user_id
=
current_user
.
id
,
name
=
name
,
uuid
=
str
(
uuid
.
uuid4
()))
ub
.
session
.
add
(
shelf
)
g
.
ub
session
.
add
(
shelf
)
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."
)
ub
.
session
.
commit
()
g
.
ub
session
.
commit
()
return
make_response
(
jsonify
(
str
(
shelf
.
uuid
)),
201
)
...
...
@@ -459,7 +460,7 @@ def HandleTagCreate():
@
kobo
.
route
(
"/v1/library/tags/<tag_id>"
,
methods
=
[
"DELETE"
,
"PUT"
])
@
requires_kobo_auth
def
HandleTagUpdate
(
tag_id
):
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
uuid
==
tag_id
,
shelf
=
g
.
ub
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
uuid
==
tag_id
,
ub
.
Shelf
.
user_id
==
current_user
.
id
)
.
one_or_none
()
if
not
shelf
:
log
.
debug
(
"Received Kobo tag update request on a collection unknown to CalibreWeb"
)
...
...
@@ -483,8 +484,8 @@ def HandleTagUpdate(tag_id):
abort
(
400
,
description
=
"Malformed tags POST request. Data is missing 'Name' field"
)
shelf
.
name
=
name
ub
.
session
.
merge
(
shelf
)
ub
.
session
.
commit
()
g
.
ub
session
.
merge
(
shelf
)
g
.
ub
session
.
commit
()
return
make_response
(
' '
,
200
)
...
...
@@ -522,7 +523,7 @@ def HandleTagAddItem(tag_id):
log
.
debug
(
"Received malformed v1/library/tags/<tag_id>/items/delete request."
)
abort
(
400
,
description
=
"Malformed tags POST request. Data is missing 'Items' field"
)
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
uuid
==
tag_id
,
shelf
=
g
.
ub
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
uuid
==
tag_id
,
ub
.
Shelf
.
user_id
==
current_user
.
id
)
.
one_or_none
()
if
not
shelf
:
log
.
debug
(
"Received Kobo request on a collection unknown to CalibreWeb"
)
...
...
@@ -535,8 +536,8 @@ def HandleTagAddItem(tag_id):
if
items_unknown_to_calibre
:
log
.
debug
(
"Received request to add an unknown book to a collection. Silently ignoring item."
)
ub
.
session
.
merge
(
shelf
)
ub
.
session
.
commit
()
g
.
ub
session
.
merge
(
shelf
)
g
.
ub
session
.
commit
()
return
make_response
(
''
,
201
)
...
...
@@ -552,7 +553,7 @@ def HandleTagRemoveItem(tag_id):
log
.
debug
(
"Received malformed v1/library/tags/<tag_id>/items/delete request."
)
abort
(
400
,
description
=
"Malformed tags POST request. Data is missing 'Items' field"
)
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
uuid
==
tag_id
,
shelf
=
g
.
ub
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
uuid
==
tag_id
,
ub
.
Shelf
.
user_id
==
current_user
.
id
)
.
one_or_none
()
if
not
shelf
:
log
.
debug
(
...
...
@@ -577,7 +578,7 @@ def HandleTagRemoveItem(tag_id):
shelf
.
books
.
filter
(
ub
.
BookShelf
.
book_id
==
book
.
id
)
.
delete
()
except
KeyError
:
items_unknown_to_calibre
.
append
(
item
)
ub
.
session
.
commit
()
g
.
ub
session
.
commit
()
if
items_unknown_to_calibre
:
log
.
debug
(
"Received request to remove an unknown book to a collecition. Silently ignoring item."
)
...
...
@@ -590,7 +591,7 @@ def HandleTagRemoveItem(tag_id):
def
sync_shelves
(
sync_token
,
sync_results
):
new_tags_last_modified
=
sync_token
.
tags_last_modified
for
shelf
in
ub
.
session
.
query
(
ub
.
ShelfArchive
)
.
filter
(
func
.
datetime
(
ub
.
ShelfArchive
.
last_modified
)
>
sync_token
.
tags_last_modified
,
for
shelf
in
g
.
ub
session
.
query
(
ub
.
ShelfArchive
)
.
filter
(
func
.
datetime
(
ub
.
ShelfArchive
.
last_modified
)
>
sync_token
.
tags_last_modified
,
ub
.
ShelfArchive
.
user_id
==
current_user
.
id
):
new_tags_last_modified
=
max
(
shelf
.
last_modified
,
new_tags_last_modified
)
...
...
@@ -603,7 +604,7 @@ def sync_shelves(sync_token, sync_results):
}
})
for
shelf
in
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
func
.
datetime
(
ub
.
Shelf
.
last_modified
)
>
sync_token
.
tags_last_modified
,
for
shelf
in
g
.
ub
session
.
query
(
ub
.
Shelf
)
.
filter
(
func
.
datetime
(
ub
.
Shelf
.
last_modified
)
>
sync_token
.
tags_last_modified
,
ub
.
Shelf
.
user_id
==
current_user
.
id
):
if
not
shelf_lib
.
check_shelf_view_permissions
(
shelf
):
continue
...
...
@@ -623,7 +624,7 @@ def sync_shelves(sync_token, sync_results):
"ChangedTag"
:
tag
})
sync_token
.
tags_last_modified
=
new_tags_last_modified
ub
.
session
.
commit
()
g
.
ub
session
.
commit
()
# Creates a Kobo "Tag" object from a ub.Shelf object
...
...
@@ -700,11 +701,11 @@ def HandleStateRequest(book_uuid):
update_results_response
[
"StatusInfoResult"
]
=
{
"Result"
:
"Success"
}
except
(
KeyError
,
TypeError
,
ValueError
,
StatementError
):
log
.
debug
(
"Received malformed v1/library/<book_uuid>/state request."
)
ub
.
session
.
rollback
()
g
.
ub
session
.
rollback
()
abort
(
400
,
description
=
"Malformed request data is missing 'ReadingStates' key"
)
ub
.
session
.
merge
(
kobo_reading_state
)
ub
.
session
.
commit
()
g
.
ub
session
.
merge
(
kobo_reading_state
)
g
.
ub
session
.
commit
()
return
jsonify
({
"RequestResult"
:
"Success"
,
"UpdateResults"
:
[
update_results_response
],
...
...
@@ -732,7 +733,7 @@ def get_ub_read_status(kobo_read_status):
def
get_or_create_reading_state
(
book_id
):
book_read
=
ub
.
session
.
query
(
ub
.
ReadBook
)
.
filter
(
ub
.
ReadBook
.
book_id
==
book_id
,
book_read
=
g
.
ub
session
.
query
(
ub
.
ReadBook
)
.
filter
(
ub
.
ReadBook
.
book_id
==
book_id
,
ub
.
ReadBook
.
user_id
==
current_user
.
id
)
.
one_or_none
()
if
not
book_read
:
book_read
=
ub
.
ReadBook
(
user_id
=
current_user
.
id
,
book_id
=
book_id
)
...
...
@@ -741,8 +742,8 @@ def get_or_create_reading_state(book_id):
kobo_reading_state
.
current_bookmark
=
ub
.
KoboBookmark
()
kobo_reading_state
.
statistics
=
ub
.
KoboStatistics
()
book_read
.
kobo_reading_state
=
kobo_reading_state
ub
.
session
.
add
(
book_read
)
ub
.
session
.
commit
()
g
.
ub
session
.
add
(
book_read
)
g
.
ub
session
.
commit
()
return
book_read
.
kobo_reading_state
...
...
@@ -835,7 +836,7 @@ def HandleBookDeletionRequest(book_uuid):
book_id
=
book
.
id
archived_book
=
(
ub
.
session
.
query
(
ub
.
ArchivedBook
)
g
.
ub
session
.
query
(
ub
.
ArchivedBook
)
.
filter
(
ub
.
ArchivedBook
.
book_id
==
book_id
)
.
first
()
)
...
...
@@ -844,8 +845,8 @@ def HandleBookDeletionRequest(book_uuid):
archived_book
.
is_archived
=
True
archived_book
.
last_modified
=
datetime
.
datetime
.
utcnow
()
ub
.
session
.
merge
(
archived_book
)
ub
.
session
.
commit
()
g
.
ub
session
.
merge
(
archived_book
)
g
.
ub
session
.
commit
()
return
(
""
,
204
)
...
...
cps/kobo_auth.py
View file @
777c2726
...
...
@@ -102,7 +102,7 @@ def requires_kobo_auth(f):
auth_token
=
get_auth_token
()
if
auth_token
is
not
None
:
user
=
(
ub
.
session
.
query
(
ub
.
User
)
g
.
ub
session
.
query
(
ub
.
User
)
.
join
(
ub
.
RemoteAuthToken
)
.
filter
(
ub
.
RemoteAuthToken
.
auth_token
==
auth_token
)
.
filter
(
ub
.
RemoteAuthToken
.
token_type
==
1
)
.
first
()
...
...
@@ -135,7 +135,7 @@ def generate_auth_token(user_id):
)
else
:
# Invalidate any prevously generated Kobo Auth token for this user.
auth_token
=
ub
.
session
.
query
(
ub
.
RemoteAuthToken
)
.
filter
(
auth_token
=
g
.
ub
session
.
query
(
ub
.
RemoteAuthToken
)
.
filter
(
ub
.
RemoteAuthToken
.
user_id
==
user_id
)
.
filter
(
ub
.
RemoteAuthToken
.
token_type
==
1
)
.
first
()
...
...
@@ -146,8 +146,8 @@ def generate_auth_token(user_id):
auth_token
.
auth_token
=
(
hexlify
(
urandom
(
16
)))
.
decode
(
"utf-8"
)
auth_token
.
token_type
=
1
ub
.
session
.
add
(
auth_token
)
ub
.
session
.
commit
()
g
.
ub
session
.
add
(
auth_token
)
g
.
ub
session
.
commit
()
return
render_title_template
(
"generate_kobo_auth_url.html"
,
title
=
_
(
u"Kobo Setup"
),
...
...
@@ -162,7 +162,7 @@ def generate_auth_token(user_id):
@
login_required
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
)
\
g
.
ub
session
.
query
(
ub
.
RemoteAuthToken
)
.
filter
(
ub
.
RemoteAuthToken
.
user_id
==
user_id
)
\
.
filter
(
ub
.
RemoteAuthToken
.
token_type
==
1
)
.
delete
()
ub
.
session
.
commit
()
g
.
ub
session
.
commit
()
return
""
cps/oauth_bb.py
View file @
777c2726
...
...
@@ -24,7 +24,7 @@ from __future__ import division, print_function, unicode_literals
import
json
from
functools
import
wraps
from
flask
import
session
,
request
,
make_response
,
abort
from
flask
import
session
,
request
,
make_response
,
abort
,
g
from
flask
import
Blueprint
,
flash
,
redirect
,
url_for
from
flask_babel
import
gettext
as
_
from
flask_dance.consumer
import
oauth_authorized
,
oauth_error
...
...
@@ -74,7 +74,7 @@ def register_user_with_oauth(user=None):
else
:
for
oauth_key
in
all_oauth
.
keys
():
# Find this OAuth token in the database, or create it
query
=
ub
.
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
query
=
g
.
ub
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
provider
=
oauth_key
,
provider_user_id
=
session
[
str
(
oauth_key
)
+
"_oauth_user_id"
],
)
...
...
@@ -85,10 +85,10 @@ def register_user_with_oauth(user=None):
# no found, return error
return
try
:
ub
.
session
.
commit
()
g
.
ub
session
.
commit
()
except
Exception
as
e
:
log
.
exception
(
e
)
ub
.
session
.
rollback
()
g
.
ub
session
.
rollback
()
def
logout_oauth_user
():
...
...
@@ -99,19 +99,19 @@ def logout_oauth_user():
if
ub
.
oauth_support
:
oauthblueprints
=
[]
if
not
ub
.
session
.
query
(
ub
.
OAuthProvider
)
.
count
():
if
not
g
.
ub
session
.
query
(
ub
.
OAuthProvider
)
.
count
():
oauthProvider
=
ub
.
OAuthProvider
()
oauthProvider
.
provider_name
=
"github"
oauthProvider
.
active
=
False
ub
.
session
.
add
(
oauthProvider
)
ub
.
session
.
commit
()
g
.
ub
session
.
add
(
oauthProvider
)
g
.
ub
session
.
commit
()
oauthProvider
=
ub
.
OAuthProvider
()
oauthProvider
.
provider_name
=
"google"
oauthProvider
.
active
=
False
ub
.
session
.
add
(
oauthProvider
)
ub
.
session
.
commit
()
g
.
ub
session
.
add
(
oauthProvider
)
g
.
ub
session
.
commit
()
oauth_ids
=
ub
.
session
.
query
(
ub
.
OAuthProvider
)
.
all
()
oauth_ids
=
g
.
ub
session
.
query
(
ub
.
OAuthProvider
)
.
all
()
ele1
=
dict
(
provider_name
=
'github'
,
id
=
oauth_ids
[
0
]
.
id
,
active
=
oauth_ids
[
0
]
.
active
,
...
...
@@ -141,7 +141,7 @@ if ub.oauth_support:
scope
=
element
[
'scope'
]
)
element
[
'blueprint'
]
=
blueprint
element
[
'blueprint'
]
.
backend
=
OAuthBackend
(
ub
.
OAuth
,
ub
.
session
,
str
(
element
[
'id'
]),
element
[
'blueprint'
]
.
backend
=
OAuthBackend
(
ub
.
OAuth
,
g
.
ub
session
,
str
(
element
[
'id'
]),
user
=
current_user
,
user_required
=
True
)
app
.
register_blueprint
(
blueprint
,
url_prefix
=
"/login"
)
if
element
[
'active'
]:
...
...
@@ -185,7 +185,7 @@ if ub.oauth_support:
session
[
provider_id
+
"_oauth_token"
]
=
token
# Find this OAuth token in the database, or create it
query
=
ub
.
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
query
=
g
.
ub
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
provider
=
provider_id
,
provider_user_id
=
provider_user_id
,
)
...
...
@@ -200,11 +200,11 @@ if ub.oauth_support:
token
=
token
,
)
try
:
ub
.
session
.
add
(
oauth_entry
)
ub
.
session
.
commit
()
g
.
ub
session
.
add
(
oauth_entry
)
g
.
ub
session
.
commit
()
except
Exception
as
e
:
log
.
exception
(
e
)
ub
.
session
.
rollback
()
g
.
ub
session
.
rollback
()
# Disable Flask-Dance's default behavior for saving the OAuth token
# Value differrs depending on flask-dance version
...
...
@@ -212,7 +212,7 @@ if ub.oauth_support:
def
bind_oauth_or_register
(
provider_id
,
provider_user_id
,
redirect_url
,
provider_name
):
query
=
ub
.
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
query
=
g
.
ub
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
provider
=
provider_id
,
provider_user_id
=
provider_user_id
,
)
...
...
@@ -230,13 +230,13 @@ if ub.oauth_support:
if
current_user
and
current_user
.
is_authenticated
:
oauth_entry
.
user
=
current_user
try
:
ub
.
session
.
add
(
oauth_entry
)
ub
.
session
.
commit
()
g
.
ub
session
.
add
(
oauth_entry
)
g
.
ub
session
.
commit
()
flash
(
_
(
u"Link to
%(oauth)
s Succeeded"
,
oauth
=
provider_name
),
category
=
"success"
)
return
redirect
(
url_for
(
'web.profile'
))
except
Exception
as
e
:
log
.
exception
(
e
)
ub
.
session
.
rollback
()
g
.
ub
session
.
rollback
()
else
:
flash
(
_
(
u"Login failed, No User Linked With OAuth Account"
),
category
=
"error"
)
log
.
info
(
'Login failed, No User Linked With OAuth Account'
)
...
...
@@ -253,7 +253,7 @@ if ub.oauth_support:
def
get_oauth_status
():
status
=
[]
query
=
ub
.
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
query
=
g
.
ub
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
user_id
=
current_user
.
id
,
)
try
:
...
...
@@ -268,7 +268,7 @@ if ub.oauth_support:
def
unlink_oauth
(
provider
):
if
request
.
host_url
+
'me'
!=
request
.
referrer
:
pass
query
=
ub
.
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
query
=
g
.
ub
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
provider
=
provider
,
user_id
=
current_user
.
id
,
)
...
...
@@ -277,13 +277,13 @@ if ub.oauth_support:
if
current_user
and
current_user
.
is_authenticated
:
oauth_entry
.
user
=
current_user
try
:
ub
.
session
.
delete
(
oauth_entry
)
ub
.
session
.
commit
()
g
.
ub
session
.
delete
(
oauth_entry
)
g
.
ub
session
.
commit
()
logout_oauth_user
()
flash
(
_
(
u"Unlink to
%(oauth)
s Succeeded"
,
oauth
=
oauth_check
[
provider
]),
category
=
"success"
)
except
Exception
as
e
:
log
.
exception
(
e
)
ub
.
session
.
rollback
()
g
.
ub
session
.
rollback
()
flash
(
_
(
u"Unlink to
%(oauth)
s Failed"
,
oauth
=
oauth_check
[
provider
]),
category
=
"error"
)
except
NoResultFound
:
log
.
warning
(
"oauth
%
s for user
%
d not found"
,
provider
,
current_user
.
id
)
...
...
cps/opds.py
View file @
777c2726
...
...
@@ -33,7 +33,7 @@ from werkzeug.security import check_password_hash
from
.
import
constants
,
logger
,
config
,
db
,
calibre_db
,
ub
,
services
,
get_locale
,
isoLanguages
from
.helper
import
get_download_link
,
get_book_cover
from
.pagination
import
Pagination
from
.web
import
render_read_books
,
download_required
,
load_user_from_request
from
.web
import
render_read_books
,
load_user_from_request
from
flask_babel
import
gettext
as
_
from
babel
import
Locale
as
LC
from
babel.core
import
UnknownLocaleError
...
...
@@ -128,7 +128,7 @@ def feed_best_rated():
@
requires_basic_auth_if_no_ano
def
feed_hot
():
off
=
request
.
args
.
get
(
"offset"
)
or
0
all_books
=
ub
.
session
.
query
(
ub
.
Downloads
,
func
.
count
(
ub
.
Downloads
.
book_id
))
.
order_by
(
all_books
=
g
.
ub
session
.
query
(
ub
.
Downloads
,
func
.
count
(
ub
.
Downloads
.
book_id
))
.
order_by
(
func
.
count
(
ub
.
Downloads
.
book_id
)
.
desc
())
.
group_by
(
ub
.
Downloads
.
book_id
)
hot_books
=
all_books
.
offset
(
off
)
.
limit
(
config
.
config_books_per_page
)
entries
=
list
()
...
...
@@ -361,17 +361,17 @@ def feed_shelfindex():
def
feed_shelf
(
book_id
):
off
=
request
.
args
.
get
(
"offset"
)
or
0
if
current_user
.
is_anonymous
:
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
is_public
==
1
,
shelf
=
g
.
ub
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
is_public
==
1
,
ub
.
Shelf
.
id
==
book_id
)
.
first
()
else
:
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
or_
(
and_
(
ub
.
Shelf
.
user_id
==
int
(
current_user
.
id
),
shelf
=
g
.
ub
session
.
query
(
ub
.
Shelf
)
.
filter
(
or_
(
and_
(
ub
.
Shelf
.
user_id
==
int
(
current_user
.
id
),
ub
.
Shelf
.
id
==
book_id
),
and_
(
ub
.
Shelf
.
is_public
==
1
,
ub
.
Shelf
.
id
==
book_id
)))
.
first
()
result
=
list
()
# user is allowed to access shelf
if
shelf
:
books_in_shelf
=
ub
.
session
.
query
(
ub
.
BookShelf
)
.
filter
(
ub
.
BookShelf
.
shelf
==
book_id
)
.
order_by
(
books_in_shelf
=
g
.
ub
session
.
query
(
ub
.
BookShelf
)
.
filter
(
ub
.
BookShelf
.
shelf
==
book_id
)
.
order_by
(
ub
.
BookShelf
.
order
.
asc
())
.
all
()
for
book
in
books_in_shelf
:
cur_book
=
calibre_db
.
get_book
(
book
.
book_id
)
...
...
@@ -427,7 +427,7 @@ def check_auth(username, password):
username
=
username
.
encode
(
'windows-1252'
)
except
UnicodeEncodeError
:
username
=
username
.
encode
(
'utf-8'
)
user
=
ub
.
session
.
query
(
ub
.
User
)
.
filter
(
func
.
lower
(
ub
.
User
.
nickname
)
==
user
=
g
.
ub
session
.
query
(
ub
.
User
)
.
filter
(
func
.
lower
(
ub
.
User
.
nickname
)
==
username
.
decode
(
'utf-8'
)
.
lower
())
.
first
()
return
bool
(
user
and
check_password_hash
(
str
(
user
.
password
),
password
))
...
...
cps/shelf.py
View file @
777c2726
This diff is collapsed.
Click to expand it.
cps/ub.py
View file @
777c2726
...
...
@@ -46,12 +46,13 @@ from sqlalchemy import String, Integer, SmallInteger, Boolean, DateTime, Float,
from
sqlalchemy.ext.declarative
import
declarative_base
from
sqlalchemy.orm.attributes
import
flag_modified
from
sqlalchemy.orm
import
backref
,
relationship
,
sessionmaker
,
Session
from
sqlalchemy.orm
import
relationship
,
scoped_session
from
werkzeug.security
import
generate_password_hash
from
.
import
constants
s
ession
=
None
Scoped_S
ession
=
None
app_DB_path
=
None
Base
=
declarative_base
()
searched_ids
=
{}
...
...
@@ -219,9 +220,9 @@ class UserBase:
except
AttributeError
:
pass
try
:
session
.
commit
()
g
.
ub
session
.
commit
()
except
(
exc
.
OperationalError
,
exc
.
InvalidRequestError
):
session
.
rollback
()
g
.
ub
session
.
rollback
()
# ToDo: Error message
def
__repr__
(
self
):
...
...
@@ -279,6 +280,7 @@ class Anonymous(AnonymousUserMixin, UserBase):
self
.
loadSettings
()
def
loadSettings
(
self
):
session
=
Scoped_Session
()
data
=
session
.
query
(
User
)
.
filter
(
User
.
role
.
op
(
'&'
)(
constants
.
ROLE_ANONYMOUS
)
==
constants
.
ROLE_ANONYMOUS
)
\
.
first
()
# type: User
self
.
nickname
=
data
.
nickname
...
...
@@ -297,6 +299,7 @@ class Anonymous(AnonymousUserMixin, UserBase):
# Initialize flask_session once
if
'view'
not
in
flask_session
:
flask_session
[
'view'
]
=
{}
session
.
close
()
def
role_admin
(
self
):
...
...
@@ -673,18 +676,18 @@ def clean_database(session):
# Save downloaded books per user in calibre-web's own database
def
update_download
(
book_id
,
user_id
):
check
=
session
.
query
(
Downloads
)
.
filter
(
Downloads
.
user_id
==
user_id
)
.
filter
(
Downloads
.
book_id
==
book_id
)
.
first
()
check
=
g
.
ub
session
.
query
(
Downloads
)
.
filter
(
Downloads
.
user_id
==
user_id
)
.
filter
(
Downloads
.
book_id
==
book_id
)
.
first
()
if
not
check
:
new_download
=
Downloads
(
user_id
=
user_id
,
book_id
=
book_id
)
session
.
add
(
new_download
)
session
.
commit
()
g
.
ub
session
.
add
(
new_download
)
g
.
ub
session
.
commit
()
# Delete non exisiting downloaded books in calibre-web's own database
def
delete_download
(
book_id
):
session
.
query
(
Downloads
)
.
filter
(
book_id
==
Downloads
.
book_id
)
.
delete
()
session
.
commit
()
g
.
ub
session
.
query
(
Downloads
)
.
filter
(
book_id
==
Downloads
.
book_id
)
.
delete
()
g
.
ub
session
.
commit
()
# Generate user Guest (translated text), as anonymous user, no rights
def
create_anonymous_user
(
session
):
...
...
@@ -716,18 +719,21 @@ def create_admin_user(session):
except
Exception
:
session
.
rollback
()
def
create_session
():
pass
def
init_db
(
app_db_path
):
# Open session for database connection
global
s
ession
global
Scoped_S
ession
global
app_DB_path
global
engine
app_DB_path
=
app_db_path
engine
=
create_engine
(
u'sqlite:///{0}'
.
format
(
app_db_path
),
echo
=
False
)
S
ession
=
sessionmaker
()
Session
.
configure
(
bind
=
engine
)
session
=
Session
()
S
coped_Session
=
scoped_session
(
sessionmaker
())
#
sessionmaker()
S
coped_S
ession
.
configure
(
bind
=
engine
)
session
=
S
coped_S
ession
()
if
os
.
path
.
exists
(
app_db_path
):
Base
.
metadata
.
create_all
(
engine
)
...
...
@@ -737,6 +743,7 @@ def init_db(app_db_path):
Base
.
metadata
.
create_all
(
engine
)
create_admin_user
(
session
)
create_anonymous_user
(
session
)
session
.
close
()
def
dispose
():
...
...
cps/web.py
View file @
777c2726
This diff is collapsed.
Click to expand it.
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