From 986f40b80a02b561588794af62a3e03bab44b445 Mon Sep 17 00:00:00 2001 From: OzzieIsaacs Date: Thu, 30 Mar 2017 21:17:18 +0200 Subject: [PATCH] Code cosmetics --- cps/db.py | 2 +- cps/epub.py | 4 +-- cps/helper.py | 6 ++-- cps/static/js/edit_books.js | 43 +++++++++++++----------- cps/static/js/get_meta.js | 65 ++++++++++++++++++------------------ cps/static/js/main.js | 29 ++++++++-------- cps/static/js/shelforder.js | 4 +-- cps/templates/book_edit.html | 2 +- cps/ub.py | 3 +- 9 files changed, 80 insertions(+), 78 deletions(-) diff --git a/cps/db.py b/cps/db.py index f5fbcbbf..c6e069e5 100755 --- a/cps/db.py +++ b/cps/db.py @@ -264,7 +264,7 @@ class Books(Base): class Custom_Columns(Base): __tablename__ = 'custom_columns' - + id = Column(Integer, primary_key=True) label = Column(String) name = Column(String) diff --git a/cps/epub.py b/cps/epub.py index 446efe9b..8d0b2817 100644 --- a/cps/epub.py +++ b/cps/epub.py @@ -57,7 +57,7 @@ def get_epub_info(tmp_file_path, original_file_name, original_file_extension): epub_metadata['description'] = "" if epub_metadata['language'] == "Unknown": - epub_metadata['language'] == "" + epub_metadata['language'] = "" else: lang = epub_metadata['language'].split('-', 1)[0].lower() if len(lang) == 2: @@ -87,7 +87,7 @@ def get_epub_info(tmp_file_path, original_file_name, original_file_extension): coverfile = extractCover(zip, filename, "", tmp_file_path) else: coverfile = extractCover(zip, coversection[0], coverpath, tmp_file_path) - + if epub_metadata['title'] is None: title = original_file_name else: diff --git a/cps/helper.py b/cps/helper.py index 68f1a296..577fa826 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -277,12 +277,12 @@ def update_dir_stucture(book_id, calibrepath): db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort) book = db.session.query(db.Books).filter(db.Books.id == book_id).first() path = os.path.join(calibrepath, book.path)#.replace('/',os.path.sep)).replace('\\',os.path.sep) - + authordir = book.path.split('/')[0] new_authordir = get_valid_filename(book.authors[0].name) titledir = book.path.split('/')[1] new_titledir = get_valid_filename(book.title) + " (" + str(book_id) + ")" - + if titledir != new_titledir: new_title_path = os.path.join(os.path.dirname(path), new_titledir) os.rename(path, new_title_path) @@ -348,7 +348,7 @@ class Updater(threading.Thread): if web.gevent_server: web.gevent_server.stop() else: - # stop tornado server + # stop tornado server server = IOLoop.instance() server.add_callback(server.stop) self.status=7 diff --git a/cps/static/js/edit_books.js b/cps/static/js/edit_books.js index e81a88b2..12199c0d 100644 --- a/cps/static/js/edit_books.js +++ b/cps/static/js/edit_books.js @@ -17,8 +17,8 @@ function prefixed_source(prefix, query, cb, bhAdapter) { }); } function get_path(){ - var jsFileLocation = $('script[src*=edit_books]').attr('src'); // the js file path - jsFileLocation = jsFileLocation.replace('/static/js/edit_books.js', ''); // the js folder path + 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; } @@ -29,7 +29,7 @@ var authors = new Bloodhound({ }, queryTokenizer: Bloodhound.tokenizers.whitespace, remote: { - url: get_path()+'/get_authors_json?q=%QUERY' + url: get_path()+"/get_authors_json?q=%QUERY" } }); @@ -58,7 +58,8 @@ var promise = authors.initialize(); highlight: true, minLength: 1, hint: true }, { - name: 'authors', displayKey: 'name', + name: "authors", + displayKey: "name", source: authors_source } ) @@ -73,10 +74,9 @@ var series = new Bloodhound({ return [query]; }, remote: { - url: get_path()+'/get_series_json?q=', + url: get_path()+"/get_series_json?q=", replace: function(url, query) { - url_query = url+encodeURIComponent(query); - return url_query; + return url+encodeURIComponent(query); } } }); @@ -87,7 +87,8 @@ var promise = series.initialize(); highlight: true, minLength: 0, hint: true }, { - name: 'series', displayKey: 'name', + name: "series", + displayKey: "name", source: series.ttAdapter() } ) @@ -99,9 +100,9 @@ var tags = new Bloodhound({ return [datum.name]; }, queryTokenizer: function(query) { - tokens = query.split(","); + var tokens = query.split(","); tokens = [tokens[tokens.length-1].trim()]; - return tokens + return tokens; }, remote: { url: get_path()+"/get_tags_json?q=%QUERY" @@ -131,7 +132,8 @@ var promise = tags.initialize(); highlight: true, minLength: 0, hint: true }, { - name: 'tags', displayKey: 'name', + name: "tags", + displayKey: "name", source: tag_source } ) @@ -146,7 +148,7 @@ var languages = new Bloodhound({ return [query]; }, remote: { - url: get_path()+'/get_languages_json?q=', + url: get_path()+"/get_languages_json?q=", replace: function(url, query) { url_query = url+encodeURIComponent(query); return url_query; @@ -177,23 +179,24 @@ var promise = languages.initialize(); highlight: true, minLength: 0, hint: true }, { - name: 'languages', displayKey: 'name', + name: "languages", + displayKey: "name", source: language_source } ) }); -$('form').on('change input typeahead:selected', function(data){ - form = $('form').serialize(); +$("form").on("change input typeahead:selected", function(data){ + var form = $("form").serialize(); $.getJSON( get_path()+"/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'))) { - $(this).addClass('disabled'); + $(".tags_click").each(function() { + if ($.inArray(parseInt($(this).children("input").first().val(), 10), data.tags) === -1 ) { + if (!($(this).hasClass("active"))) { + $(this).addClass("disabled"); } } else { - $(this).removeClass('disabled'); + $(this).removeClass("disabled"); } }); }); diff --git a/cps/static/js/get_meta.js b/cps/static/js/get_meta.js index e58e54bf..f9fa5b60 100644 --- a/cps/static/js/get_meta.js +++ b/cps/static/js/get_meta.js @@ -6,33 +6,33 @@ */ $(document).ready(function () { - var msg = i18n_msg; + var msg = i18nMsg; var douban = "https://api.douban.com"; - var db_search = '/v2/book/search'; - var db_get_info = '/v2/book/'; - var db_get_info_by_isbn = '/v2/book/isbn/ '; - var db_done = false; + var dbSearch = "/v2/book/search"; + var db_get_info = "/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 google = "https://www.googleapis.com/"; + var gg_search = "/books/v1/volumes"; + var gg_get_info = "/books/v1/volumes/"; var gg_done = false; var db_results = []; var gg_results = []; var show_flag = 0; String.prototype.replaceAll = function (s1, s2) {   - return this.replace(new RegExp(s1, "gm"), s2);   + return this.replace(new RegExp(s1, "gm"), s2); }; - gg_search_book = function (title) { - title = title.replaceAll(/\s+/, '+'); - var url = google + gg_search + '?q=' + title; + function gg_search_book (title) { + title = title.replaceAll(/\s+/, "+"); + var url = google + gg_search + "?q=" + title; $.ajax({ - url: url, + url, type: "GET", dataType: "jsonp", - jsonp: 'callback', + jsonp: "callback", success: function (data) { gg_results = data.items; }, @@ -43,15 +43,16 @@ $(document).ready(function () { }); } - get_meta = function (source, id) { + function get_meta (source, id) { var meta; - if (source == "google") { + var tags; + if (source === "google") { meta = gg_results[id]; $("#description").val(meta.volumeInfo.description); - $("#bookAuthor").val(meta.volumeInfo.authors.join(' & ')); + $("#bookAuthor").val(meta.volumeInfo.authors.join(" & ")); $("#book_title").val(meta.volumeInfo.title); if (meta.volumeInfo.categories) { - var tags = meta.volumeInfo.categories.join(','); + tags = meta.volumeInfo.categories.join(","); $("#tags").val(tags); } if (meta.volumeInfo.averageRating) { @@ -59,10 +60,10 @@ $(document).ready(function () { } return; } - if (source == "douban") { + if (source === "douban") { meta = db_results[id]; $("#description").val(meta.summary); - $("#bookAuthor").val(meta.author.join(' & ')); + $("#bookAuthor").val(meta.author.join(" & ")); $("#book_title").val(meta.title); var tags = ''; for (var i = 0; i < meta.tags.length; i++) { @@ -84,7 +85,7 @@ $(document).ready(function () { } db_search_book = function (title) { - var url = douban + db_search + '?q=' + title + '&fields=all&count=10'; + var url = douban + dbSearch + '?q=' + title + '&fields=all&count=10'; $.ajax({ url: url, type: "GET", @@ -97,18 +98,18 @@ $(document).ready(function () { $('#meta-info').html('

'+ msg.search_error+'!

'); }, complete: function () { - db_done = true; + dbDone = true; show_result(); } }); } - show_result = function () { + function show_result () { show_flag++; if (show_flag == 1) { $('#meta-info').html(''); } - if (gg_done && db_done) { + if (gg_done && dbDone) { if (!gg_results && !db_results) { $('#meta-info').html('

'+ msg.no_result +'

'); return; @@ -140,7 +141,7 @@ $(document).ready(function () { } gg_done = false; } - if (db_done && db_results.length > 0) { + if (dbDone && db_results.length > 0) { for (var i = 0; i < db_results.length; i++) { var book = db_results[i]; var book_html = '
  • ' + @@ -155,24 +156,24 @@ $(document).ready(function () { '

    ' + msg.description + ':' + book.summary + '

    ' + '

    ' + msg.source + ':Douban Books

    ' + '' + - '
  • '; + ""; $("#book-list").append(book_html); } - db_done = false; + dbDone = false; } } - $('#do-search').click(function () { - var keyword = $('#keyword').val(); + $("#do-search").click(function () { + var keyword = $("#keyword").val(); if (keyword) { do_search(keyword); } }); - $('#get_meta').click(function () { - var book_title = $('#book_title').val(); + $("#get_meta").click(function () { + var book_title = $("#book_title").val(); if (book_title) { - $('#keyword').val(book_title); + $("#keyword").val(book_title); do_search(book_title); } }); diff --git a/cps/static/js/main.js b/cps/static/js/main.js index c18f85e5..c20bd44a 100644 --- a/cps/static/js/main.js +++ b/cps/static/js/main.js @@ -3,7 +3,7 @@ var updateTimerID; var updateText; $(function() { - $('.discover .row').isotope({ + $(".discover .row").isotope({ // options itemSelector : ".book", layoutMode : "fitRows" @@ -20,28 +20,28 @@ $(function() { extraScrollPx: 300, // selector for all items you'll retrieve }, function(data){ - $(".load-more .row").isotope( 'appended', $(data), null ); + $(".load-more .row").isotope( "appended", $(data), null ); }); - $('#sendbtn').click(function(){ + $("#sendbtn").click(function(){ var $this = $(this); $this.text("Please wait..."); $this.addClass("disabled"); }); $("#restart").click(function() { $.ajax({ - dataType: 'json', + dataType: "json", url: window.location.pathname+"/../../shutdown", data: {"parameter":0}, success: function(data) { - $('#spinner').show(); + $("#spinner").show(); displaytext=data.text; setTimeout(restartTimer, 3000);} }); }); $("#shutdown").click(function() { $.ajax({ - dataType: 'json', + dataType: "json", url: window.location.pathname+"/../../shutdown", data: {"parameter":1}, success: function(data) { @@ -50,13 +50,13 @@ $(function() { }); $("#check_for_update").click(function() { var button_text = $("#check_for_update").html(); - $("#check_for_update").html('...'); + $("#check_for_update").html("..."); $.ajax({ - dataType: 'json', + dataType: "json", url: window.location.pathname+"/../../get_update_status", success: function(data) { $("#check_for_update").html(button_text); - if (data.status == true) { + if (data.status === true) { $("#check_for_update").addClass("hidden"); $("#perform_update").removeClass("hidden"); $("#update_info").removeClass("hidden"); @@ -82,13 +82,12 @@ $(function() { success: function(data) { updateText=data.text; $("#UpdateprogressDialog #Updatecontent").html(updateText[data.status]); - console.log(data.status); + // console.log(data.status); updateTimerID=setInterval(updateTimer, 2000);} }); }); }); - function restartTimer() { $("#spinner").hide(); $("#RestartDialog").modal("hide"); @@ -103,10 +102,10 @@ function updateTimer() { $("#UpdateprogressDialog #Updatecontent").html(updateText[data.status]); if (data.status >6){ clearInterval(updateTimerID); - $('#spinner2').hide(); - $('#UpdateprogressDialog #updateFinished').removeClass('hidden'); - $("#check_for_update").removeClass('hidden'); - $("#perform_update").addClass('hidden'); + $("#spinner2").hide(); + $("#UpdateprogressDialog #updateFinished").removeClass("hidden"); + $("#check_for_update").removeClass("hidden"); + $("#perform_update").addClass("hidden"); } }, error: function() { diff --git a/cps/static/js/shelforder.js b/cps/static/js/shelforder.js index ea970f9a..3102f080 100644 --- a/cps/static/js/shelforder.js +++ b/cps/static/js/shelforder.js @@ -1,4 +1,4 @@ -Sortable.create(sortTrue, { +var sortable = Sortable.create(sortTrue, { group: "sorting", sort: true }); @@ -9,7 +9,7 @@ function sendData(path){ var maxElements; var tmp=[]; - elements=Sortable.utils.find(sortTrue,"div"); + elements=sortable.utils.find(sortTrue,"div"); maxElements=elements.length; var form = document.createElement("form"); diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index 474cfaae..165512c7 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -138,7 +138,7 @@ {% block js %}