From 7c6d527a556540b100e6514e4fc827f7b4867556 Mon Sep 17 00:00:00 2001
From: OzzieIsaacs
Date: Sun, 2 Apr 2017 10:05:07 +0200
Subject: [PATCH] Code cosmetics
---
cps.py | 2 +-
cps/helper.py | 1 +
cps/static/js/edit_books.js | 21 ++--
cps/static/js/get_meta.js | 186 ++++++++++++++--------------
cps/static/js/main.js | 12 +-
cps/templates/book_edit.html | 2 +-
cps/templates/detail.html | 12 +-
cps/templates/discover.html | 4 +-
cps/templates/feed.xml | 8 +-
cps/templates/index.html | 8 +-
cps/templates/json.txt | 4 +-
cps/templates/list.html | 2 +-
cps/templates/search.html | 4 +-
cps/templates/shelf.html | 4 +-
cps/templates/user_edit.html | 2 +-
cps/ub.py | 3 +-
cps/web.py | 234 +++++++++++++++++------------------
17 files changed, 254 insertions(+), 255 deletions(-)
diff --git a/cps.py b/cps.py
index 180be500..523246b2 100755
--- a/cps.py
+++ b/cps.py
@@ -22,7 +22,7 @@ except ImportError:
if __name__ == '__main__':
if web.ub.DEVELOPMENT:
- web.app.run(host="0.0.0.0", port=web.ub.config.config_port, debug=True)
+ web.app.run(port=web.ub.config.config_port, debug=True)
else:
if gevent_present:
web.app.logger.info('Attempting to start gevent')
diff --git a/cps/helper.py b/cps/helper.py
index 7f561319..71cacd74 100755
--- a/cps/helper.py
+++ b/cps/helper.py
@@ -13,6 +13,7 @@ import os
import traceback
import re
import unicodedata
+
try:
from StringIO import StringIO
from email.MIMEBase import MIMEBase
diff --git a/cps/static/js/edit_books.js b/cps/static/js/edit_books.js
index 07d25148..810869a5 100644
--- a/cps/static/js/edit_books.js
+++ b/cps/static/js/edit_books.js
@@ -8,7 +8,7 @@
Takes a prefix, query typeahead callback, Bloodhound typeahead adapter
and returns the completions it gets from the bloodhound engine prefixed.
*/
-function prefixed_source(prefix, query, cb, bhAdapter) {
+function prefixedSource(prefix, query, cb, bhAdapter) {
bhAdapter(query, function(retArray){
var matches = [];
for (var i = 0; i < retArray.length; i++) {
@@ -18,7 +18,7 @@ function prefixed_source(prefix, query, cb, bhAdapter) {
cb(matches);
});
}
-function get_path(){
+function getPath(){
var jsFileLocation = $("script[src*=edit_books]").attr("src"); // the js file path
jsFileLocation = jsFileLocation.replace("/static/js/edit_books.js", ''); // the js folder path
return jsFileLocation;
@@ -31,7 +31,7 @@ var authors = new Bloodhound({
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
- url: get_path()+"/get_authors_json?q=%QUERY"
+ url: getPath()+"/get_authors_json?q=%QUERY"
}
});
@@ -44,7 +44,7 @@ var series = new Bloodhound({
return [query];
},
remote: {
- url: get_path()+"/get_series_json?q=",
+ url: getPath()+"/get_series_json?q=",
replace: function(url, query) {
return url+encodeURIComponent(query);
}
@@ -63,7 +63,7 @@ var tags = new Bloodhound({
return tokens;
},
remote: {
- url: get_path()+"/get_tags_json?q=%QUERY"
+ url: getPath()+"/get_tags_json?q=%QUERY"
}
});
@@ -76,10 +76,9 @@ var languages = new Bloodhound({
return [query];
},
remote: {
- url: get_path()+"/get_languages_json?q=",
+ url: getPath()+"/get_languages_json?q=",
replace: function(url, query) {
- url_query = url+encodeURIComponent(query);
- return url_query;
+ return url+encodeURIComponent(query);
}
}
});
@@ -101,7 +100,7 @@ function sourceSplit(query, cb, split, source) {
for (var i = 0; i < tokens.length; i++) {
prefix += tokens[i].trim() + newSplit;
}
- prefixed_source(prefix, currentSource, cb, bhAdapter);
+ prefixedSource(prefix, currentSource, cb, bhAdapter);
}
var promiseAuthors = authors.initialize();
@@ -130,7 +129,7 @@ var promiseSeries = series.initialize();
displayKey: "name",
source: series.ttAdapter()
}
- )
+ );
});
var promiseTags = tags.initialize();
@@ -165,7 +164,7 @@ var promiseLanguages = languages.initialize();
$("form").on("change input typeahead:selected", function(data){
var form = $("form").serialize();
- $.getJSON( get_path()+"/get_matching_tags", form, function( data ) {
+ $.getJSON( getPath()+"/get_matching_tags", form, function( data ) {
$(".tags_click").each(function() {
if ($.inArray(parseInt($(this).children("input").first().val(), 10), data.tags) === -1 ) {
if (!($(this).hasClass("active"))) {
diff --git a/cps/static/js/get_meta.js b/cps/static/js/get_meta.js
index 583abea2..316d2651 100644
--- a/cps/static/js/get_meta.js
+++ b/cps/static/js/get_meta.js
@@ -9,36 +9,95 @@ $(document).ready(function () {
var msg = i18nMsg;
var douban = "https://api.douban.com";
var dbSearch = "/v2/book/search";
- var db_get_info = "/v2/book/";
- var db_get_info_by_isbn = "/v2/book/isbn/ ";
+ // var dbGetInfo = "/v2/book/";
+ // var db_get_info_by_isbn = "/v2/book/isbn/ ";
var dbDone = false;
var google = "https://www.googleapis.com/";
- var gg_search = "/books/v1/volumes";
- var gg_get_info = "/books/v1/volumes/";
- var gg_done = false;
+ var ggSearch = "/books/v1/volumes";
+ // var gg_get_info = "/books/v1/volumes/";
+ var ggDone = false;
- var db_results = [];
- var gg_results = [];
- var show_flag = 0;
- String.prototype.replaceAll = function (s1, s2) {
+ var dbResults = [];
+ var ggResults = [];
+ var showFlag = 0;
+ String.prototype.replaceAll = function (s1, s2) {
return this.replace(new RegExp(s1, "gm"), s2);
};
- function gg_search_book (title) {
+ function showResult () {
+ showFlag++;
+ if (showFlag === 1) {
+ $("#meta-info").html('');
+ }
+ if (ggDone && dbDone) {
+ if (!ggResults && !dbResults) {
+ $("#meta-info").html(''+ msg.no_result +"
");
+ return;
+ }
+ }
+ if (ggDone && ggResults.length > 0) {
+ for (var i = 0; i < ggResults.length; i++) {
+ var book = ggResults[i];
+ var bookCover;
+ if (book.volumeInfo.imageLinks) {
+ bookCover = book.volumeInfo.imageLinks.thumbnail;
+ } else {
+ bookCover = "/static/generic_cover.jpg";
+ }
+ var bookHtml = '' +
+ ' ' +
+ '' +
+ '
' +
+ "
"+ msg.author +":" + book.volumeInfo.authors + "
" +
+ "
"+ msg.publisher + ":" + book.volumeInfo.publisher + "
" +
+ "
"+ msg.description + ":" + book.volumeInfo.description + "
" +
+ "
"+ msg.source + ':Google Books
' +
+ "
" +
+ " ";
+ $("#book-list").append(bookHtml);
+ }
+ ggDone = false;
+ }
+ if (dbDone && dbResults.length > 0) {
+ for (var i = 0; i < dbResults.length; i++) {
+ var book = dbResults[i];
+ var bookHtml = '' +
+ ' ' +
+ '' +
+ '
" +
+ "
" + msg.author + ":" + book.author + "
" +
+ "
" + msg.publisher + ":" + book.publisher + "
" +
+ "
" + msg.description + ":" + book.summary + "
" +
+ "
" + msg.source + ':Douban Books
' +
+ "
" +
+ " ";
+ $("#book-list").append(bookHtml);
+ }
+ dbDone = false;
+ }
+ }
+
+ function ggSearchBook (title) {
title = title.replaceAll(/\s+/, "+");
- var url = google + gg_search + "?q=" + title;
+ var url = google + ggSearch + "?q=" + title;
$.ajax({
url,
type: "GET",
dataType: "jsonp",
jsonp: "callback",
success: function (data) {
- gg_results = data.items;
+ ggResults = data.items;
},
complete: function () {
- gg_done = true;
- show_result();
+ ggDone = true;
+ showResult();
}
});
}
@@ -47,7 +106,7 @@ $(document).ready(function () {
var meta;
var tags;
if (source === "google") {
- meta = gg_results[id];
+ meta = ggResults[id];
$("#description").val(meta.volumeInfo.description);
$("#bookAuthor").val(meta.volumeInfo.authors.join(" & "));
$("#book_title").val(meta.volumeInfo.title);
@@ -61,13 +120,13 @@ $(document).ready(function () {
return;
}
if (source === "douban") {
- meta = db_results[id];
+ meta = dbResults[id];
$("#description").val(meta.summary);
$("#bookAuthor").val(meta.author.join(" & "));
$("#book_title").val(meta.title);
- tags = '';
+ tags = "";
for (var i = 0; i < meta.tags.length; i++) {
- tags = tags + meta.tags[i].title + ',';
+ tags = tags + meta.tags[i].title + ",";
}
$("#tags").val(tags);
$("#rating").val(Math.round(meta.rating.average / 2));
@@ -75,91 +134,32 @@ $(document).ready(function () {
}
}
- function do_search (keyword) {
- show_flag = 0;
+ function doSearch (keyword) {
+ showFlag = 0;
$("#meta-info").text(msg.loading);
- var keyword = $("#keyword").val();
+ // var keyword = $("#keyword").val();
if (keyword) {
- db_search_book(keyword);
- gg_search_book(keyword);
+ dbSearchBook(keyword);
+ ggSearchBook(keyword);
}
}
- function show_result () {
- show_flag++;
- if (show_flag == 1) {
- $('#meta-info').html('');
- }
- if (gg_done && dbDone) {
- if (!gg_results && !db_results) {
- $('#meta-info').html(''+ msg.no_result +'
');
- return;
- }
- }
- if (gg_done && gg_results.length > 0) {
- for (var i = 0; i < gg_results.length; i++) {
- var book = gg_results[i];
- var book_cover;
- if (book.volumeInfo.imageLinks) {
- book_cover = book.volumeInfo.imageLinks.thumbnail;
- } else {
- book_cover = '/static/generic_cover.jpg';
- }
- var book_html = '' +
- ' ' +
- '' +
- '
' +
- '
'+ msg.author +':' + book.volumeInfo.authors + '
' +
- '
'+ msg.publisher + ':' + book.volumeInfo.publisher + '
' +
- '
'+ msg.description + ':' + book.volumeInfo.description + '
' +
- '
'+ msg.source + ':Google Books
' +
- '
' +
- ' ';
- $("#book-list").append(book_html);
- }
- gg_done = false;
- }
- if (dbDone && db_results.length > 0) {
- for (var i = 0; i < db_results.length; i++) {
- var book = db_results[i];
- var book_html = '' +
- ' ' +
- '' +
- '
' +
- '
' + msg.author + ':' + book.author + '
' +
- '
' + msg.publisher + ':' + book.publisher + '
' +
- '
' + msg.description + ':' + book.summary + '
' +
- '
' + msg.source + ':Douban Books
' +
- '
' +
- " ";
- $("#book-list").append(book_html);
- }
- dbDone = false;
- }
- }
-
- db_search_book = function (title) {
- var url = douban + dbSearch + '?q=' + title + '&fields=all&count=10';
+ function dbSearchBook (title) {
+ var url = douban + dbSearch + "?q=" + title + "&fields=all&count=10";
$.ajax({
url: url,
type: "GET",
dataType: "jsonp",
- jsonp: 'callback',
+ jsonp: "callback",
success: function (data) {
- db_results = data.books;
+ dbResults = data.books;
},
error: function () {
- $('#meta-info').html(''+ msg.search_error+'!
');
+ $("#meta-info").html(''+ msg.search_error+"!
");
},
complete: function () {
dbDone = true;
- show_result();
+ showResult();
}
});
}
@@ -167,15 +167,15 @@ $(document).ready(function () {
$("#do-search").click(function () {
var keyword = $("#keyword").val();
if (keyword) {
- do_search(keyword);
+ doSearch(keyword);
}
});
$("#get_meta").click(function () {
- var book_title = $("#book_title").val();
- if (book_title) {
- $("#keyword").val(book_title);
- do_search(book_title);
+ var bookTitle = $("#book_title").val();
+ if (bookTitle) {
+ $("#keyword").val(bookTitle);
+ doSearch(bookTitle);
}
});
diff --git a/cps/static/js/main.js b/cps/static/js/main.js
index 8494973f..10e4dc08 100644
--- a/cps/static/js/main.js
+++ b/cps/static/js/main.js
@@ -49,13 +49,13 @@ $(function() {
});
});
$("#check_for_update").click(function() {
- var button_text = $("#check_for_update").html();
+ var buttonText = $("#check_for_update").html();
$("#check_for_update").html("...");
$.ajax({
dataType: "json",
url: window.location.pathname+"/../../get_update_status",
success: function(data) {
- $("#check_for_update").html(button_text);
+ $("#check_for_update").html(buttonText);
if (data.status === true) {
$("#check_for_update").addClass("hidden");
$("#perform_update").removeClass("hidden");
@@ -67,7 +67,7 @@ $(function() {
});
$("#restart_database").click(function() {
$.ajax({
- dataType: 'json',
+ dataType: "json",
url: window.location.pathname+"/../../shutdown",
data: {"parameter":2}
});
@@ -76,7 +76,7 @@ $(function() {
$("#spinner2").show();
$.ajax({
type: "POST",
- dataType: 'json',
+ dataType: "json",
data: { start: "True"},
url: window.location.pathname+"/../../get_updater_status",
success: function(data) {
@@ -94,10 +94,10 @@ $(function() {
function updateTimer() {
$.ajax({
- dataType: 'json',
+ dataType: "json",
url: window.location.pathname+"/../../get_updater_status",
success: function(data) {
- console.log(data.status);
+ // console.log(data.status);
$("#UpdateprogressDialog #Updatecontent").html(updateText[data.status]);
if (data.status >6){
clearInterval(updateTimerID);
diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html
index 165512c7..b94ddbb2 100644
--- a/cps/templates/book_edit.html
+++ b/cps/templates/book_edit.html
@@ -106,7 +106,7 @@
{{_('Get metadata')}}
{{_('Submit')}}
- {{_('Back')}}
+ {{_('Back')}}
{% endif %}
diff --git a/cps/templates/detail.html b/cps/templates/detail.html
index e0a7ef01..44fc8f55 100644
--- a/cps/templates/detail.html
+++ b/cps/templates/detail.html
@@ -15,7 +15,7 @@
{{entry.title}}
{% for author in entry.authors %}
- {{author.name}}
+ {{author.name}}
{% if not loop.last %}
&
{% endif %}
@@ -37,7 +37,7 @@
{% endif %}
{% if entry.series|length > 0 %}
-
{{_('Book')}} {{entry.series_index}} {{_('of')}} {{entry.series[0].name}}
+ {{_('Book')}} {{entry.series_index}} {{_('of')}} {{entry.series[0].name}}
{% endif %}
{% if entry.languages.__len__() > 0 %}
@@ -65,7 +65,7 @@
{% for tag in entry.tags %}
- {{tag.name}}
+ {{tag.name}}
{%endfor%}
@@ -110,7 +110,7 @@
{% if not g.user.is_anonymous() %}
-
@@ -136,7 +136,7 @@
@@ -154,7 +154,7 @@
diff --git a/cps/templates/discover.html b/cps/templates/discover.html
index c340cfc0..32f89a47 100644
--- a/cps/templates/discover.html
+++ b/cps/templates/discover.html
@@ -8,14 +8,14 @@
{% if entry.has_cover is defined %}
-
+
{% endif %}