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
ab97ead2
Commit
ab97ead2
authored
Jul 16, 2016
by
wuqi
Browse files
Options
Browse Files
Download
Plain Diff
merge test
parents
350f4e21
d051a1db
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
18 deletions
+57
-18
cps.py
cps.py
+2
-1
layout.html
cps/templates/layout.html
+3
-3
web.py
cps/web.py
+52
-14
No files found.
cps.py
View file @
ab97ead2
#!/usr/bin/env python
import
os
import
os
import
sys
import
sys
...
@@ -21,4 +22,4 @@ if config.DEVELOPMENT:
...
@@ -21,4 +22,4 @@ if config.DEVELOPMENT:
else
:
else
:
http_server
=
HTTPServer
(
WSGIContainer
(
web
.
app
))
http_server
=
HTTPServer
(
WSGIContainer
(
web
.
app
))
http_server
.
listen
(
config
.
PORT
)
http_server
.
listen
(
config
.
PORT
)
IOLoop
.
instance
()
.
start
()
IOLoop
.
instance
()
.
start
()
\ No newline at end of file
cps/templates/layout.html
View file @
ab97ead2
...
@@ -79,9 +79,9 @@
...
@@ -79,9 +79,9 @@
<li><a
href=
"{{url_for('user_list')}}"
><span
class=
"glyphicon glyphicon-dashboard"
></span>
Admin
</a></li>
<li><a
href=
"{{url_for('user_list')}}"
><span
class=
"glyphicon glyphicon-dashboard"
></span>
Admin
</a></li>
{% endif %}
{% endif %}
<li><a
href=
"{{url_for('profile')}}"
><span
class=
"glyphicon glyphicon-user"
></span>
{{g.user.nickname}}
</a></li>
<li><a
href=
"{{url_for('profile')}}"
><span
class=
"glyphicon glyphicon-user"
></span>
{{g.user.nickname}}
</a></li>
<li><a
href=
"{{url_for('logout', next=
request.path
)}}"
><span
class=
"glyphicon glyphicon-log-out"
></span>
Logout
</a></li>
<li><a
href=
"{{url_for('logout', next=
'%s%s' % (request.script_root, request.path)
)}}"
><span
class=
"glyphicon glyphicon-log-out"
></span>
Logout
</a></li>
{% else %}
{% else %}
<li><a
href=
"{{url_for('login', next=
request.path
)}}"
><span
class=
"glyphicon glyphicon-log-in"
></span>
Login
</a></li>
<li><a
href=
"{{url_for('login', next=
'%s%s' % (request.script_root, request.path)
)}}"
><span
class=
"glyphicon glyphicon-log-in"
></span>
Login
</a></li>
{% if g.allow_registration %}
{% if g.allow_registration %}
<li><a
href=
"{{url_for('register')}}"
><span
class=
"glyphicon glyphicon-user"
></span>
Register
</a></li>
<li><a
href=
"{{url_for('register')}}"
><span
class=
"glyphicon glyphicon-user"
></span>
Register
</a></li>
{% endif %}
{% endif %}
...
@@ -159,4 +159,4 @@
...
@@ -159,4 +159,4 @@
</div>
</div>
{% block js %}{% endblock %}
{% block js %}{% endblock %}
</body>
</body>
</html>
</html>
\ No newline at end of file
cps/web.py
View file @
ab97ead2
...
@@ -35,8 +35,46 @@ try:
...
@@ -35,8 +35,46 @@ try:
except
ImportError
,
e
:
except
ImportError
,
e
:
use_generic_pdf_cover
=
True
use_generic_pdf_cover
=
True
from
shutil
import
copyfile
from
shutil
import
copyfile
from
cgi
import
escape
from
cgi
import
escape
class
ReverseProxied
(
object
):
'''Wrap the application in this middleware and configure the
front-end server to add these headers, to let you quietly bind
this to a URL other than / and to an HTTP scheme that is
different than what is used locally.
Code courtesy of: http://flask.pocoo.org/snippets/35/
In nginx:
location /myprefix {
proxy_pass http://127.0.0.1:8083;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Script-Name /myprefix;
}
'''
def
__init__
(
self
,
app
):
self
.
app
=
app
def
__call__
(
self
,
environ
,
start_response
):
script_name
=
environ
.
get
(
'HTTP_X_SCRIPT_NAME'
,
''
)
if
script_name
:
environ
[
'SCRIPT_NAME'
]
=
script_name
path_info
=
environ
.
get
(
'PATH_INFO'
,
''
)
if
path_info
and
path_info
.
startswith
(
script_name
):
environ
[
'PATH_INFO'
]
=
path_info
[
len
(
script_name
):]
scheme
=
environ
.
get
(
'HTTP_X_SCHEME'
,
''
)
if
scheme
:
environ
[
'wsgi.url_scheme'
]
=
scheme
server
=
environ
.
get
(
'HTTP_X_FORWARDED_SERVER'
,
''
)
if
server
:
environ
[
'HTTP_HOST'
]
=
server
return
self
.
app
(
environ
,
start_response
)
app
=
(
Flask
(
__name__
))
app
=
(
Flask
(
__name__
))
app
.
wsgi_app
=
ReverseProxied
(
app
.
wsgi_app
)
formatter
=
logging
.
Formatter
(
formatter
=
logging
.
Formatter
(
"[
%(asctime)
s] {
%(pathname)
s:
%(lineno)
d}
%(levelname)
s -
%(message)
s"
)
"[
%(asctime)
s] {
%(pathname)
s:
%(lineno)
d}
%(levelname)
s -
%(message)
s"
)
...
@@ -584,7 +622,7 @@ def register():
...
@@ -584,7 +622,7 @@ def register():
if
not
config
.
PUBLIC_REG
:
if
not
config
.
PUBLIC_REG
:
abort
(
404
)
abort
(
404
)
if
current_user
is
not
None
and
current_user
.
is_authenticated
():
if
current_user
is
not
None
and
current_user
.
is_authenticated
():
return
redirect
(
url_for
(
'index'
))
return
redirect
(
url_for
(
'index'
,
_external
=
True
))
if
request
.
method
==
"POST"
:
if
request
.
method
==
"POST"
:
to_save
=
request
.
form
.
to_dict
()
to_save
=
request
.
form
.
to_dict
()
...
@@ -608,7 +646,7 @@ def register():
...
@@ -608,7 +646,7 @@ def register():
flash
(
"An unknown error occured. Please try again later."
,
category
=
"error"
)
flash
(
"An unknown error occured. Please try again later."
,
category
=
"error"
)
return
render_template
(
'register.html'
,
title
=
"register"
)
return
render_template
(
'register.html'
,
title
=
"register"
)
flash
(
"Your account has been created. Please login."
,
category
=
"success"
)
flash
(
"Your account has been created. Please login."
,
category
=
"success"
)
return
redirect
(
url_for
(
'login'
))
return
redirect
(
url_for
(
'login'
,
_external
=
True
))
else
:
else
:
flash
(
"This username or email address is already in use."
,
category
=
"error"
)
flash
(
"This username or email address is already in use."
,
category
=
"error"
)
return
render_template
(
'register.html'
,
title
=
"register"
)
return
render_template
(
'register.html'
,
title
=
"register"
)
...
@@ -620,7 +658,7 @@ def login():
...
@@ -620,7 +658,7 @@ def login():
error
=
None
error
=
None
if
current_user
is
not
None
and
current_user
.
is_authenticated
():
if
current_user
is
not
None
and
current_user
.
is_authenticated
():
return
redirect
(
url_for
(
'index'
))
return
redirect
(
url_for
(
'index'
,
_external
=
True
))
if
request
.
method
==
"POST"
:
if
request
.
method
==
"POST"
:
form
=
request
.
form
.
to_dict
()
form
=
request
.
form
.
to_dict
()
...
@@ -629,7 +667,7 @@ def login():
...
@@ -629,7 +667,7 @@ def login():
if
user
and
check_password_hash
(
user
.
password
,
form
[
'password'
]):
if
user
and
check_password_hash
(
user
.
password
,
form
[
'password'
]):
login_user
(
user
,
remember
=
True
)
login_user
(
user
,
remember
=
True
)
flash
(
"you are now logged in as: '
%
s'"
%
user
.
nickname
,
category
=
"success"
)
flash
(
"you are now logged in as: '
%
s'"
%
user
.
nickname
,
category
=
"success"
)
return
redirect
(
request
.
args
.
get
(
"next"
)
or
url_for
(
"index"
))
return
redirect
(
request
.
args
.
get
(
"next"
)
or
url_for
(
"index"
,
_external
=
True
))
else
:
else
:
flash
(
"Wrong Username or Password"
,
category
=
"error"
)
flash
(
"Wrong Username or Password"
,
category
=
"error"
)
...
@@ -640,7 +678,7 @@ def login():
...
@@ -640,7 +678,7 @@ def login():
def
logout
():
def
logout
():
if
current_user
is
not
None
and
current_user
.
is_authenticated
():
if
current_user
is
not
None
and
current_user
.
is_authenticated
():
logout_user
()
logout_user
()
return
redirect
(
request
.
args
.
get
(
"next"
)
or
url_for
(
"index"
))
return
redirect
(
request
.
args
.
get
(
"next"
)
or
url_for
(
"index"
,
_external
=
True
))
@
app
.
route
(
'/send/<int:book_id>'
)
@
app
.
route
(
'/send/<int:book_id>'
)
...
@@ -667,7 +705,7 @@ def add_to_shelf(shelf_id, book_id):
...
@@ -667,7 +705,7 @@ def add_to_shelf(shelf_id, book_id):
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
id
==
shelf_id
)
.
first
()
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
id
==
shelf_id
)
.
first
()
if
not
shelf
.
is_public
and
not
shelf
.
user_id
==
int
(
current_user
.
id
):
if
not
shelf
.
is_public
and
not
shelf
.
user_id
==
int
(
current_user
.
id
):
flash
(
"Sorry you are not allowed to add a book to the the shelf:
%
s"
%
shelf
.
name
)
flash
(
"Sorry you are not allowed to add a book to the the shelf:
%
s"
%
shelf
.
name
)
return
redirect
(
url_for
(
'index'
))
return
redirect
(
url_for
(
'index'
,
_external
=
True
))
ins
=
ub
.
BookShelf
(
shelf
=
shelf
.
id
,
book_id
=
book_id
)
ins
=
ub
.
BookShelf
(
shelf
=
shelf
.
id
,
book_id
=
book_id
)
ub
.
session
.
add
(
ins
)
ub
.
session
.
add
(
ins
)
...
@@ -684,7 +722,7 @@ def remove_from_shelf(shelf_id, book_id):
...
@@ -684,7 +722,7 @@ def remove_from_shelf(shelf_id, book_id):
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
id
==
shelf_id
)
.
first
()
shelf
=
ub
.
session
.
query
(
ub
.
Shelf
)
.
filter
(
ub
.
Shelf
.
id
==
shelf_id
)
.
first
()
if
not
shelf
.
is_public
and
not
shelf
.
user_id
==
int
(
current_user
.
id
):
if
not
shelf
.
is_public
and
not
shelf
.
user_id
==
int
(
current_user
.
id
):
flash
(
"Sorry you are not allowed to remove a book from this shelf:
%
s"
%
shelf
.
name
)
flash
(
"Sorry you are not allowed to remove a book from this shelf:
%
s"
%
shelf
.
name
)
return
redirect
(
url_for
(
'index'
))
return
redirect
(
url_for
(
'index'
,
_external
=
True
))
book_shelf
=
ub
.
session
.
query
(
ub
.
BookShelf
)
.
filter
(
ub
.
BookShelf
.
shelf
==
shelf_id
,
ub
.
BookShelf
.
book_id
==
book_id
)
.
first
()
book_shelf
=
ub
.
session
.
query
(
ub
.
BookShelf
)
.
filter
(
ub
.
BookShelf
.
shelf
==
shelf_id
,
ub
.
BookShelf
.
book_id
==
book_id
)
.
first
()
...
@@ -795,7 +833,7 @@ def new_user():
...
@@ -795,7 +833,7 @@ def new_user():
ub
.
session
.
add
(
content
)
ub
.
session
.
add
(
content
)
ub
.
session
.
commit
()
ub
.
session
.
commit
()
flash
(
"User '
%
s' created"
%
content
.
nickname
,
category
=
"success"
)
flash
(
"User '
%
s' created"
%
content
.
nickname
,
category
=
"success"
)
return
redirect
(
url_for
(
'user_list'
))
return
redirect
(
url_for
(
'user_list'
,
_external
=
True
))
except
IntegrityError
:
except
IntegrityError
:
ub
.
session
.
rollback
()
ub
.
session
.
rollback
()
flash
(
"Found an existing account for this email address or nickname."
,
category
=
"error"
)
flash
(
"Found an existing account for this email address or nickname."
,
category
=
"error"
)
...
@@ -837,7 +875,7 @@ def edit_user(user_id):
...
@@ -837,7 +875,7 @@ def edit_user(user_id):
if
"delete"
in
to_save
:
if
"delete"
in
to_save
:
ub
.
session
.
delete
(
content
)
ub
.
session
.
delete
(
content
)
flash
(
"User '
%
s' deleted"
%
content
.
nickname
,
category
=
"success"
)
flash
(
"User '
%
s' deleted"
%
content
.
nickname
,
category
=
"success"
)
return
redirect
(
url_for
(
'user_list'
))
return
redirect
(
url_for
(
'user_list'
,
_external
=
True
))
else
:
else
:
if
to_save
[
"password"
]:
if
to_save
[
"password"
]:
content
.
password
=
generate_password_hash
(
to_save
[
"password"
])
content
.
password
=
generate_password_hash
(
to_save
[
"password"
])
...
@@ -1112,7 +1150,7 @@ def edit_book(book_id):
...
@@ -1112,7 +1150,7 @@ def edit_book(book_id):
for
b
in
edited_books_id
:
for
b
in
edited_books_id
:
helper
.
update_dir_stucture
(
b
)
helper
.
update_dir_stucture
(
b
)
if
"detail_view"
in
to_save
:
if
"detail_view"
in
to_save
:
return
redirect
(
url_for
(
'show_book'
,
id
=
book
.
id
))
return
redirect
(
url_for
(
'show_book'
,
id
=
book
.
id
,
_external
=
True
))
else
:
else
:
return
render_template
(
'edit_book.html'
,
book
=
book
,
authors
=
author_names
,
cc
=
cc
)
return
render_template
(
'edit_book.html'
,
book
=
book
,
authors
=
author_names
,
cc
=
cc
)
else
:
else
:
...
@@ -1136,7 +1174,7 @@ def upload():
...
@@ -1136,7 +1174,7 @@ def upload():
author
=
"Unknown"
author
=
"Unknown"
else
:
else
:
flash
(
"Upload is only available for PDF files"
,
category
=
"error"
)
flash
(
"Upload is only available for PDF files"
,
category
=
"error"
)
return
redirect
(
url_for
(
'index'
))
return
redirect
(
url_for
(
'index'
,
_external
=
True
))
title_dir
=
helper
.
get_valid_filename
(
title
,
False
)
title_dir
=
helper
.
get_valid_filename
(
title
,
False
)
author_dir
=
helper
.
get_valid_filename
(
author
.
decode
(
'utf-8'
),
False
)
author_dir
=
helper
.
get_valid_filename
(
author
.
decode
(
'utf-8'
),
False
)
...
@@ -1148,12 +1186,12 @@ def upload():
...
@@ -1148,12 +1186,12 @@ def upload():
os
.
makedirs
(
filepath
)
os
.
makedirs
(
filepath
)
except
OSError
:
except
OSError
:
flash
(
"Failed to create path
%
s (Permission denied)."
%
filepath
,
category
=
"error"
)
flash
(
"Failed to create path
%
s (Permission denied)."
%
filepath
,
category
=
"error"
)
return
redirect
(
url_for
(
'index'
))
return
redirect
(
url_for
(
'index'
,
_external
=
True
))
try
:
try
:
file
.
save
(
saved_filename
)
file
.
save
(
saved_filename
)
except
OSError
:
except
OSError
:
flash
(
"Failed to store file
%
s (Permission denied)."
%
saved_filename
,
category
=
"error"
)
flash
(
"Failed to store file
%
s (Permission denied)."
%
saved_filename
,
category
=
"error"
)
return
redirect
(
url_for
(
'index'
))
return
redirect
(
url_for
(
'index'
,
_external
=
True
))
file_size
=
os
.
path
.
getsize
(
saved_filename
)
file_size
=
os
.
path
.
getsize
(
saved_filename
)
has_cover
=
0
has_cover
=
0
if
fileextension
.
upper
()
==
".PDF"
:
if
fileextension
.
upper
()
==
".PDF"
:
...
@@ -1186,4 +1224,4 @@ def upload():
...
@@ -1186,4 +1224,4 @@ def upload():
if
current_user
.
role_edit
()
or
current_user
.
role_admin
():
if
current_user
.
role_edit
()
or
current_user
.
role_admin
():
return
render_template
(
'edit_book.html'
,
book
=
db_book
,
authors
=
author_names
,
cc
=
cc
)
return
render_template
(
'edit_book.html'
,
book
=
db_book
,
authors
=
author_names
,
cc
=
cc
)
book_in_shelfs
=
[]
book_in_shelfs
=
[]
return
render_template
(
'detail.html'
,
entry
=
db_book
,
cc
=
cc
,
title
=
db_book
.
title
,
books_shelfs
=
book_in_shelfs
)
return
render_template
(
'detail.html'
,
entry
=
db_book
,
cc
=
cc
,
title
=
db_book
.
title
,
books_shelfs
=
book_in_shelfs
)
\ No newline at end of file
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