Code cosmetics
This commit is contained in:
+116
-118
@@ -403,10 +403,10 @@ def timestamptodate(date, fmt=None):
|
||||
)
|
||||
native = date.replace(tzinfo=None)
|
||||
if fmt:
|
||||
format=fmt
|
||||
time_format=fmt
|
||||
else:
|
||||
format='%d %m %Y - %H:%S'
|
||||
return native.strftime(format)
|
||||
time_format='%d %m %Y - %H:%S'
|
||||
return native.strftime(time_format)
|
||||
|
||||
def admin_required(f):
|
||||
"""
|
||||
@@ -469,11 +469,11 @@ def edit_required(f):
|
||||
# Fill indexpage with all requested data from database
|
||||
def fill_indexpage(page, database, db_filter, order):
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
lang_filter = True
|
||||
if current_user.show_detail_random():
|
||||
random = db.session.query(db.Books).filter(filter).order_by(func.random()).limit(config.config_random_books)
|
||||
random = db.session.query(db.Books).filter(lang_filter).order_by(func.random()).limit(config.config_random_books)
|
||||
else:
|
||||
random = false
|
||||
off = int(int(config.config_books_per_page) * (page - 1))
|
||||
@@ -588,15 +588,15 @@ def feed_normal_search():
|
||||
|
||||
def feed_search(term):
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
lang_filter = True
|
||||
if term:
|
||||
entries = db.session.query(db.Books).filter(db.or_(db.Books.tags.any(db.Tags.name.like("%" + term + "%")),
|
||||
db.Books.series.any(db.Series.name.like("%" + term + "%")),
|
||||
db.Books.authors.any(db.Authors.name.like("%" + term + "%")),
|
||||
db.Books.publishers.any(db.Publishers.name.like("%" + term + "%")),
|
||||
db.Books.title.like("%" + term + "%"))).filter(filter).all()
|
||||
db.Books.title.like("%" + term + "%"))).filter(lang_filter).all()
|
||||
entriescount = len(entries) if len(entries) > 0 else 1
|
||||
pagination = Pagination(1, entriescount, entriescount)
|
||||
xml = render_title_template('feed.xml', searchterm=term, entries=entries, pagination=pagination)
|
||||
@@ -625,10 +625,10 @@ def feed_new():
|
||||
@requires_basic_auth_if_no_ano
|
||||
def feed_discover():
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
entries = db.session.query(db.Books).filter(filter).order_by(func.random()).limit(config.config_books_per_page)
|
||||
lang_filter = True
|
||||
entries = db.session.query(db.Books).filter(lang_filter).order_by(func.random()).limit(config.config_books_per_page)
|
||||
pagination = Pagination(1, config.config_books_per_page, int(config.config_books_per_page))
|
||||
xml = render_title_template('feed.xml', entries=entries, pagination=pagination)
|
||||
response = make_response(xml)
|
||||
@@ -656,9 +656,9 @@ def feed_hot():
|
||||
if not off:
|
||||
off = 0
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
lang_filter = True
|
||||
all_books = ub.session.query(ub.Downloads, ub.func.count(ub.Downloads.book_id)).order_by(
|
||||
ub.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)
|
||||
@@ -667,7 +667,7 @@ def feed_hot():
|
||||
downloadBook = db.session.query(db.Books).filter(db.Books.id == book.Downloads.book_id).first()
|
||||
if downloadBook:
|
||||
entries.append(
|
||||
db.session.query(db.Books).filter(filter).filter(db.Books.id == book.Downloads.book_id).first())
|
||||
db.session.query(db.Books).filter(lang_filter).filter(db.Books.id == book.Downloads.book_id).first())
|
||||
else:
|
||||
ub.session.query(ub.Downloads).filter(book.Downloads.book_id == ub.Downloads.book_id).delete()
|
||||
ub.session.commit()
|
||||
@@ -686,10 +686,10 @@ def feed_authorindex():
|
||||
if not off:
|
||||
off = 0
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
entries = db.session.query(db.Authors).join(db.books_authors_link).join(db.Books).filter(filter)\
|
||||
lang_filter = True
|
||||
entries = db.session.query(db.Authors).join(db.books_authors_link).join(db.Books).filter(lang_filter)\
|
||||
.group_by('books_authors_link.author').order_by(db.Authors.sort).limit(config.config_books_per_page).offset(off)
|
||||
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
||||
len(db.session.query(db.Authors).all()))
|
||||
@@ -699,14 +699,14 @@ def feed_authorindex():
|
||||
return response
|
||||
|
||||
|
||||
@app.route("/opds/author/<int:id>")
|
||||
@app.route("/opds/author/<int:book_id>")
|
||||
@requires_basic_auth_if_no_ano
|
||||
def feed_author(id):
|
||||
def feed_author(book_id):
|
||||
off = request.args.get("offset")
|
||||
if not off:
|
||||
off = 0
|
||||
entries, random, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1),
|
||||
db.Books, db.Books.authors.any(db.Authors.id == id), db.Books.timestamp.desc())
|
||||
db.Books, db.Books.authors.any(db.Authors.id == book_id), db.Books.timestamp.desc())
|
||||
xml = render_title_template('feed.xml', entries=entries, pagination=pagination)
|
||||
response = make_response(xml)
|
||||
response.headers["Content-Type"] = "application/xml"
|
||||
@@ -720,10 +720,10 @@ def feed_categoryindex():
|
||||
if not off:
|
||||
off = 0
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
entries = db.session.query(db.Tags).join(db.books_tags_link).join(db.Books).filter(filter).\
|
||||
lang_filter = True
|
||||
entries = db.session.query(db.Tags).join(db.books_tags_link).join(db.Books).filter(lang_filter).\
|
||||
group_by('books_tags_link.tag').order_by(db.Tags.name).offset(off).limit(config.config_books_per_page)
|
||||
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
||||
len(db.session.query(db.Tags).all()))
|
||||
@@ -733,14 +733,14 @@ def feed_categoryindex():
|
||||
return response
|
||||
|
||||
|
||||
@app.route("/opds/category/<int:id>")
|
||||
@app.route("/opds/category/<int:book_id>")
|
||||
@requires_basic_auth_if_no_ano
|
||||
def feed_category(id):
|
||||
def feed_category(book_id):
|
||||
off = request.args.get("offset")
|
||||
if not off:
|
||||
off = 0
|
||||
entries, random, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1),
|
||||
db.Books, db.Books.tags.any(db.Tags.id == id), db.Books.timestamp.desc())
|
||||
db.Books, db.Books.tags.any(db.Tags.id == book_id), db.Books.timestamp.desc())
|
||||
xml = render_title_template('feed.xml', entries=entries, pagination=pagination)
|
||||
response = make_response(xml)
|
||||
response.headers["Content-Type"] = "application/xml"
|
||||
@@ -754,10 +754,10 @@ def feed_seriesindex():
|
||||
if not off:
|
||||
off = 0
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
entries = db.session.query(db.Series).join(db.books_series_link).join(db.Books).filter(filter).\
|
||||
lang_filter = True
|
||||
entries = db.session.query(db.Series).join(db.books_series_link).join(db.Books).filter(lang_filter).\
|
||||
group_by('books_series_link.series').order_by(db.Series.sort).offset(off).all()
|
||||
pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
|
||||
len(db.session.query(db.Series).all()))
|
||||
@@ -767,14 +767,14 @@ def feed_seriesindex():
|
||||
return response
|
||||
|
||||
|
||||
@app.route("/opds/series/<int:id>")
|
||||
@app.route("/opds/series/<int:book_id>")
|
||||
@requires_basic_auth_if_no_ano
|
||||
def feed_series(id):
|
||||
def feed_series(book_id):
|
||||
off = request.args.get("offset")
|
||||
if not off:
|
||||
off = 0
|
||||
entries, random, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1),
|
||||
db.Books, db.Books.series.any(db.Series.id == id),db.Books.series_index)
|
||||
db.Books, db.Books.series.any(db.Series.id == book_id),db.Books.series_index)
|
||||
xml = render_title_template('feed.xml', entries=entries, pagination=pagination)
|
||||
response = make_response(xml)
|
||||
response.headers["Content-Type"] = "application/xml"
|
||||
@@ -793,8 +793,8 @@ def do_gdrive_download(df, headers):
|
||||
download_url = df.metadata.get('downloadUrl')
|
||||
s = partial(total_size, 1024 * 1024) # I'm downloading BIG files, so 100M chunk size is fine for me
|
||||
def stream():
|
||||
for bytes in s:
|
||||
headers = {"Range" : 'bytes=%s-%s' % (bytes[0], bytes[1])}
|
||||
for byte in s:
|
||||
headers = {"Range" : 'bytes=%s-%s' % (byte[0], byte[1])}
|
||||
resp, content = df.auth.Get_Http_Object().request(download_url, headers=headers)
|
||||
if resp.status == 206 :
|
||||
yield content
|
||||
@@ -803,14 +803,14 @@ def do_gdrive_download(df, headers):
|
||||
return
|
||||
return Response(stream_with_context(stream()), headers=headers)
|
||||
|
||||
@app.route("/opds/download/<book_id>/<format>/")
|
||||
@app.route("/opds/download/<book_id>/<book_format>/")
|
||||
@requires_basic_auth_if_no_ano
|
||||
@download_required
|
||||
def get_opds_download_link(book_id, format):
|
||||
def get_opds_download_link(book_id, book_format):
|
||||
startTime=time.time()
|
||||
format = format.split(".")[0]
|
||||
book_format = book_format.split(".")[0]
|
||||
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
||||
data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == format.upper()).first()
|
||||
data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == book_format.upper()).first()
|
||||
app.logger.info (data.name)
|
||||
if current_user.is_authenticated:
|
||||
helper.update_download(book_id, int(current_user.id))
|
||||
@@ -819,15 +819,15 @@ def get_opds_download_link(book_id, format):
|
||||
file_name = book.authors[0].name + '_' + file_name
|
||||
file_name = helper.get_valid_filename(file_name)
|
||||
headers = Headers ()
|
||||
headers["Content-Disposition"] = "attachment; filename*=UTF-8''%s.%s" % (quote(file_name.encode('utf8')), format)
|
||||
headers["Content-Disposition"] = "attachment; filename*=UTF-8''%s.%s" % (quote(file_name.encode('utf8')), book_format)
|
||||
app.logger.info (time.time()-startTime)
|
||||
startTime=time.time()
|
||||
if config.config_use_google_drive:
|
||||
app.logger.info(time.time() - startTime)
|
||||
df=gdriveutils.getFileFromEbooksFolder(Gdrive.Instance().drive, book.path, data.name + "." + format)
|
||||
df=gdriveutils.getFileFromEbooksFolder(Gdrive.Instance().drive, book.path, data.name + "." + book_format)
|
||||
return do_gdrive_download(df, headers)
|
||||
else:
|
||||
response = make_response(send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + format))
|
||||
response = make_response(send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + book_format))
|
||||
response.headers=headers
|
||||
return response
|
||||
|
||||
@@ -984,11 +984,11 @@ def index(page):
|
||||
@login_required_if_no_ano
|
||||
def hot_books(page):
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
lang_filter = True
|
||||
if current_user.show_detail_random():
|
||||
random = db.session.query(db.Books).filter(filter).order_by(func.random()).limit(config.config_random_books)
|
||||
random = db.session.query(db.Books).filter(lang_filter).order_by(func.random()).limit(config.config_random_books)
|
||||
else:
|
||||
random = false
|
||||
off = int(int(config.config_books_per_page) * (page - 1))
|
||||
@@ -1000,7 +1000,7 @@ def hot_books(page):
|
||||
downloadBook = db.session.query(db.Books).filter(db.Books.id == book.Downloads.book_id).first()
|
||||
if downloadBook:
|
||||
entries.append(
|
||||
db.session.query(db.Books).filter(filter).filter(db.Books.id == book.Downloads.book_id).first())
|
||||
db.session.query(db.Books).filter(lang_filter).filter(db.Books.id == book.Downloads.book_id).first())
|
||||
else:
|
||||
ub.session.query(ub.Downloads).filter(book.Downloads.book_id == ub.Downloads.book_id).delete()
|
||||
ub.session.commit()
|
||||
@@ -1033,22 +1033,22 @@ def discover(page):
|
||||
@login_required_if_no_ano
|
||||
def author_list():
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
lang_filter = True
|
||||
entries = db.session.query(db.Authors, func.count('books_authors_link.book').label('count')).join(
|
||||
db.books_authors_link).join(db.Books).filter(
|
||||
filter).group_by('books_authors_link.author').order_by(db.Authors.sort).all()
|
||||
lang_filter).group_by('books_authors_link.author').order_by(db.Authors.sort).all()
|
||||
return render_title_template('list.html', entries=entries, folder='author', title=_(u"Author list"))
|
||||
|
||||
|
||||
@app.route("/author/<int:id>", defaults={'page': 1})
|
||||
@app.route("/author/<int:id>/<int:page>'")
|
||||
@app.route("/author/<int:book_id>", defaults={'page': 1})
|
||||
@app.route("/author/<int:book_id>/<int:page>'")
|
||||
@login_required_if_no_ano
|
||||
def author(id,page):
|
||||
entries, random, pagination = fill_indexpage(page, db.Books, db.Books.authors.any(db.Authors.id == id),
|
||||
def author(book_id,page):
|
||||
entries, random, pagination = fill_indexpage(page, db.Books, db.Books.authors.any(db.Authors.id == book_id),
|
||||
db.Books.timestamp.desc())
|
||||
name = db.session.query(db.Authors).filter(db.Authors.id == id).first().name
|
||||
name = db.session.query(db.Authors).filter(db.Authors.id == book_id).first().name
|
||||
if entries:
|
||||
return render_title_template('index.html', random=random, entries=entries, title=_(u"Author: %(name)s", name=name))
|
||||
else:
|
||||
@@ -1060,22 +1060,22 @@ def author(id,page):
|
||||
@login_required_if_no_ano
|
||||
def series_list():
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
lang_filter = True
|
||||
entries = db.session.query(db.Series, func.count('books_series_link.book').label('count')).join(
|
||||
db.books_series_link).join(db.Books).filter(
|
||||
filter).group_by('books_series_link.series').order_by(db.Series.sort).all()
|
||||
lang_filter).group_by('books_series_link.series').order_by(db.Series.sort).all()
|
||||
return render_title_template('list.html', entries=entries, folder='series', title=_(u"Series list"))
|
||||
|
||||
|
||||
@app.route("/series/<int:id>/", defaults={'page': 1})
|
||||
@app.route("/series/<int:id>/<int:page>'")
|
||||
@app.route("/series/<int:book_id>/", defaults={'page': 1})
|
||||
@app.route("/series/<int:book_id>/<int:page>'")
|
||||
@login_required_if_no_ano
|
||||
def series(id, page):
|
||||
entries, random, pagination = fill_indexpage(page, db.Books, db.Books.series.any(db.Series.id == id),
|
||||
def series(book_id, page):
|
||||
entries, random, pagination = fill_indexpage(page, db.Books, db.Books.series.any(db.Series.id == book_id),
|
||||
db.Books.series_index)
|
||||
name=db.session.query(db.Series).filter(db.Series.id == id).first().name
|
||||
name=db.session.query(db.Series).filter(db.Series.id == book_id).first().name
|
||||
if entries:
|
||||
return render_title_template('index.html', random=random, pagination=pagination, entries=entries,
|
||||
title=_(u"Series: %(serie)s", serie=name))
|
||||
@@ -1133,52 +1133,52 @@ def language(name, page):
|
||||
@login_required_if_no_ano
|
||||
def category_list():
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
lang_filter = True
|
||||
entries = db.session.query(db.Tags, func.count('books_tags_link.book').label('count')).join(
|
||||
db.books_tags_link).join(db.Books).filter(
|
||||
filter).group_by('books_tags_link.tag').all()
|
||||
lang_filter).group_by('books_tags_link.tag').all()
|
||||
return render_title_template('list.html', entries=entries, folder='category', title=_(u"Category list"))
|
||||
|
||||
|
||||
@app.route("/category/<int:id>", defaults={'page': 1})
|
||||
@app.route('/category/<int:id>/<int:page>')
|
||||
@app.route("/category/<int:book_id>", defaults={'page': 1})
|
||||
@app.route('/category/<int:book_id>/<int:page>')
|
||||
@login_required_if_no_ano
|
||||
def category(id, page):
|
||||
entries, random, pagination = fill_indexpage(page, db.Books, db.Books.tags.any(db.Tags.id == id),
|
||||
def category(book_id, page):
|
||||
entries, random, pagination = fill_indexpage(page, db.Books, db.Books.tags.any(db.Tags.id == book_id),
|
||||
db.Books.timestamp.desc())
|
||||
|
||||
name=db.session.query(db.Tags).filter(db.Tags.id == id).first().name
|
||||
name=db.session.query(db.Tags).filter(db.Tags.id == book_id).first().name
|
||||
return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
|
||||
title=_(u"Category: %(name)s", name=name))
|
||||
|
||||
|
||||
@app.route("/ajax/toggleread/<int:id>", methods=['POST'])
|
||||
@app.route("/ajax/toggleread/<int:book_id>", methods=['POST'])
|
||||
@login_required
|
||||
def toggle_read(id):
|
||||
def toggle_read(book_id):
|
||||
book = ub.session.query(ub.ReadBook).filter(ub.and_(ub.ReadBook.user_id == int(current_user.id),
|
||||
ub.ReadBook.book_id == id)).first()
|
||||
ub.ReadBook.book_id == book_id)).first()
|
||||
if book:
|
||||
book.is_read=not book.is_read
|
||||
else:
|
||||
readBook=ub.ReadBook()
|
||||
readBook.user_id=int(current_user.id)
|
||||
readBook.book_id = id
|
||||
readBook.book_id = book_id
|
||||
readBook.is_read=True
|
||||
book=readBook
|
||||
ub.session.merge(book)
|
||||
ub.session.commit()
|
||||
return ""
|
||||
|
||||
@app.route("/book/<int:id>")
|
||||
@app.route("/book/<int:book_id>")
|
||||
@login_required_if_no_ano
|
||||
def show_book(id):
|
||||
def show_book(book_id):
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
entries = db.session.query(db.Books).filter(db.Books.id == id).filter(filter).first()
|
||||
lang_filter = True
|
||||
entries = db.session.query(db.Books).filter(db.Books.id == book_id).filter(lang_filter).first()
|
||||
if entries:
|
||||
for index in range(0, len(entries.languages)):
|
||||
try:
|
||||
@@ -1198,7 +1198,7 @@ def show_book(id):
|
||||
else:
|
||||
cc=tmpcc
|
||||
book_in_shelfs = []
|
||||
shelfs = ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == id).all()
|
||||
shelfs = ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book_id).all()
|
||||
for entry in shelfs:
|
||||
book_in_shelfs.append(entry.shelf)
|
||||
|
||||
@@ -1206,7 +1206,7 @@ def show_book(id):
|
||||
# title=entries.title, books_shelfs=book_in_shelfs)
|
||||
if not current_user.is_anonymous():
|
||||
matching_have_read_book=ub.session.query(ub.ReadBook).filter(ub.and_(ub.ReadBook.user_id == int(current_user.id),
|
||||
ub.ReadBook.book_id == id)).all()
|
||||
ub.ReadBook.book_id == book_id)).all()
|
||||
have_read=len(matching_have_read_book) > 0 and matching_have_read_book[0].is_read
|
||||
else:
|
||||
have_read=None
|
||||
@@ -1337,13 +1337,13 @@ def on_received_watch_confirmation():
|
||||
if not response['deleted'] and response['file']['title'] == 'metadata.db' and response['file']['md5Checksum'] != md5(dbpath):
|
||||
tmpDir=tempfile.gettempdir()
|
||||
app.logger.info ('Database file updated')
|
||||
copyfile (dbpath, tmpDir + "/metadata.db_" + str(current_milli_time()))
|
||||
copyfile (dbpath, os.path.join(tmpDir, "metadata.db_" + str(current_milli_time())))
|
||||
app.logger.info ('Backing up existing and downloading updated metadata.db')
|
||||
gdriveutils.downloadFile(Gdrive.Instance().drive, None, "metadata.db", tmpDir + "/tmp_metadata.db")
|
||||
gdriveutils.downloadFile(Gdrive.Instance().drive, None, "metadata.db", os.path.join(tmpDir, "tmp_metadata.db"))
|
||||
app.logger.info ('Setting up new DB')
|
||||
os.rename(tmpDir + "/tmp_metadata.db", dbpath)
|
||||
os.rename(os.path.join(tmpDir, "tmp_metadata.db"), dbpath)
|
||||
db.setup_db()
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
app.logger.exception(e)
|
||||
|
||||
updateMetaData()
|
||||
@@ -1395,14 +1395,14 @@ def search():
|
||||
term = request.args.get("query").strip()
|
||||
if term:
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
lang_filter = True
|
||||
entries = db.session.query(db.Books).filter(db.or_(db.Books.tags.any(db.Tags.name.like("%" + term + "%")),
|
||||
db.Books.series.any(db.Series.name.like("%" + term + "%")),
|
||||
db.Books.authors.any(db.Authors.name.like("%" + term + "%")),
|
||||
db.Books.publishers.any(db.Publishers.name.like("%" + term + "%")),
|
||||
db.Books.title.like("%" + term + "%"))).filter(filter).all()
|
||||
db.Books.title.like("%" + term + "%"))).filter(lang_filter).all()
|
||||
return render_title_template('search.html', searchterm=term, entries=entries)
|
||||
else:
|
||||
return render_title_template('search.html', searchterm="")
|
||||
@@ -1563,15 +1563,15 @@ def feed_unread_books():
|
||||
def unread_books(page):
|
||||
return render_read_books(page, False)
|
||||
|
||||
@app.route("/read/<int:book_id>/<format>")
|
||||
@app.route("/read/<int:book_id>/<book_format>")
|
||||
@login_required_if_no_ano
|
||||
def read_book(book_id, format):
|
||||
def read_book(book_id, book_format):
|
||||
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
||||
if book:
|
||||
book_dir = os.path.join(config.get_main_dir, "cps", "static", str(book_id))
|
||||
if not os.path.exists(book_dir):
|
||||
os.mkdir(book_dir)
|
||||
if format.lower() == "epub":
|
||||
if book_format.lower() == "epub":
|
||||
# check if mimetype file is exists
|
||||
mime_file = str(book_id) + "/mimetype"
|
||||
if not os.path.exists(mime_file):
|
||||
@@ -1586,9 +1586,7 @@ def read_book(book_id, format):
|
||||
try:
|
||||
os.makedirs(newDir)
|
||||
except OSError as exception:
|
||||
if exception.errno == errno.EEXIST:
|
||||
pass
|
||||
else:
|
||||
if not exception.errno == errno.EEXIST:
|
||||
raise
|
||||
if fileName:
|
||||
fd = open(os.path.join(newDir, fileName), "wb")
|
||||
@@ -1596,21 +1594,21 @@ def read_book(book_id, format):
|
||||
fd.close()
|
||||
zfile.close()
|
||||
return render_title_template('read.html', bookid=book_id, title=_(u"Read a Book"))
|
||||
elif format.lower() == "pdf":
|
||||
elif book_format.lower() == "pdf":
|
||||
all_name = str(book_id) + "/" + book.data[0].name + ".pdf"
|
||||
tmp_file = os.path.join(book_dir, book.data[0].name) + ".pdf"
|
||||
if not os.path.exists(tmp_file):
|
||||
pdf_file = os.path.join(config.config_calibre_dir, book.path, book.data[0].name) + ".pdf"
|
||||
copyfile(pdf_file, tmp_file)
|
||||
return render_title_template('readpdf.html', pdffile=all_name, title=_(u"Read a Book"))
|
||||
elif format.lower() == "txt":
|
||||
elif book_format.lower() == "txt":
|
||||
all_name = str(book_id) + "/" + book.data[0].name + ".txt"
|
||||
tmp_file = os.path.join(book_dir, book.data[0].name) + ".txt"
|
||||
if not os.path.exists(all_name):
|
||||
txt_file = os.path.join(config.config_calibre_dir, book.path, book.data[0].name) + ".txt"
|
||||
copyfile(txt_file, tmp_file)
|
||||
return render_title_template('readtxt.html', txtfile=all_name, title=_(u"Read a Book"))
|
||||
elif format.lower() == "cbr":
|
||||
elif book_format.lower() == "cbr":
|
||||
all_name = str(book_id) + "/" + book.data[0].name + ".cbr"
|
||||
tmp_file = os.path.join(book_dir, book.data[0].name) + ".cbr"
|
||||
if not os.path.exists(all_name):
|
||||
@@ -1622,13 +1620,13 @@ def read_book(book_id, format):
|
||||
flash(_(u"Error opening eBook. File does not exist or file is not accessible:"), category="error")
|
||||
return redirect(url_for("index"))
|
||||
|
||||
@app.route("/download/<int:book_id>/<format>")
|
||||
@app.route("/download/<int:book_id>/<book_format>")
|
||||
@login_required_if_no_ano
|
||||
@download_required
|
||||
def get_download_link(book_id, format):
|
||||
format = format.split(".")[0]
|
||||
def get_download_link(book_id, book_format):
|
||||
book_format = book_format.split(".")[0]
|
||||
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
||||
data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == format.upper()).first()
|
||||
data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == book_format.upper()).first()
|
||||
if data:
|
||||
# collect downloaded books only for registered user and not for anonymous user
|
||||
if current_user.is_authenticated:
|
||||
@@ -1639,25 +1637,25 @@ def get_download_link(book_id, format):
|
||||
file_name = helper.get_valid_filename(file_name)
|
||||
headers = Headers ()
|
||||
try:
|
||||
headers["Content-Type"] = mimetypes.types_map['.' + format]
|
||||
except:
|
||||
pass
|
||||
headers["Content-Disposition"] = "attachment; filename*=UTF-8''%s.%s" % (quote(file_name.encode('utf-8')), format)
|
||||
headers["Content-Type"] = mimetypes.types_map['.' + book_format]
|
||||
except KeyError:
|
||||
headers["Content-Type"] = "application/octet-stream"
|
||||
headers["Content-Disposition"] = "attachment; filename*=UTF-8''%s.%s" % (quote(file_name.encode('utf-8')), book_format)
|
||||
if config.config_use_google_drive:
|
||||
df=gdriveutils.getFileFromEbooksFolder(Gdrive.Instance().drive, book.path, '%s.%s' % (data.name, format))
|
||||
df=gdriveutils.getFileFromEbooksFolder(Gdrive.Instance().drive, book.path, '%s.%s' % (data.name, book_format))
|
||||
return do_gdrive_download(df, headers)
|
||||
else:
|
||||
response = make_response(send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + format))
|
||||
response = make_response(send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + book_format))
|
||||
response.headers=headers
|
||||
return response
|
||||
else:
|
||||
abort(404)
|
||||
|
||||
@app.route("/download/<int:book_id>/<format>/<anyname>")
|
||||
@app.route("/download/<int:book_id>/<book_format>/<anyname>")
|
||||
@login_required_if_no_ano
|
||||
@download_required
|
||||
def get_download_link_ext(book_id, format, anyname):
|
||||
return get_download_link(book_id, format)
|
||||
def get_download_link_ext(book_id, book_format, anyname):
|
||||
return get_download_link(book_id, book_format)
|
||||
|
||||
@app.route('/register', methods=['GET', 'POST'])
|
||||
def register():
|
||||
@@ -2370,10 +2368,10 @@ def edit_book(book_id):
|
||||
db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort)
|
||||
cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
|
||||
if current_user.filter_language() != "all":
|
||||
filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
|
||||
else:
|
||||
filter = True
|
||||
book = db.session.query(db.Books).filter(db.Books.id == book_id).filter(filter).first()
|
||||
lang_filter = True
|
||||
book = db.session.query(db.Books).filter(db.Books.id == book_id).filter(lang_filter).first()
|
||||
author_names = []
|
||||
if book:
|
||||
for index in range(0, len(book.languages)):
|
||||
@@ -2575,7 +2573,7 @@ def edit_book(book_id):
|
||||
if config.config_use_google_drive:
|
||||
updateGdriveCalibreFromLocal()
|
||||
if "detail_view" in to_save:
|
||||
return redirect(url_for('show_book', id=book.id))
|
||||
return redirect(url_for('show_book', book_id=book.id))
|
||||
else:
|
||||
return render_title_template('book_edit.html', book=book, authors=author_names, cc=cc,
|
||||
title=_(u"edit metadata"))
|
||||
@@ -2597,9 +2595,9 @@ def upload():
|
||||
db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort)
|
||||
db.session.connection().connection.connection.create_function('uuid4', 0, lambda: str(uuid4()))
|
||||
if request.method == 'POST' and 'btn-upload' in request.files:
|
||||
file = request.files['btn-upload']
|
||||
if '.' in file.filename:
|
||||
file_ext = file.filename.rsplit('.', 1)[-1].lower()
|
||||
requested_file = request.files['btn-upload']
|
||||
if '.' in requested_file.filename:
|
||||
file_ext = requested_file.filename.rsplit('.', 1)[-1].lower()
|
||||
if file_ext not in ALLOWED_EXTENSIONS:
|
||||
flash(
|
||||
_('File extension "%s" is not allowed to be uploaded to this server' %
|
||||
@@ -2610,7 +2608,7 @@ def upload():
|
||||
else:
|
||||
flash(_('File to be uploaded must have an extension'), category="error")
|
||||
return redirect(url_for('index'))
|
||||
meta = uploader.upload(file)
|
||||
meta = uploader.upload(requested_file)
|
||||
|
||||
title = meta.title
|
||||
author = meta.author
|
||||
|
||||
Reference in New Issue
Block a user