From a35c6359876e8a025e08355c8a5615751dce4031 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Wed, 18 Aug 2021 21:38:20 +0200 Subject: [PATCH] Update python metadata search --- cps/metadata_provider/scholar.py | 6 ----- cps/search_metadata.py | 19 ++++++++++------ cps/static/js/get_meta.js | 38 +++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/cps/metadata_provider/scholar.py b/cps/metadata_provider/scholar.py index 41a0e7f8..9302806c 100644 --- a/cps/metadata_provider/scholar.py +++ b/cps/metadata_provider/scholar.py @@ -20,12 +20,6 @@ from scholarly import scholarly from flask import url_for from cps.services.Metadata import Metadata -#try: - -#except ImportError: -# have_scholar = False -# pass - class scholar(Metadata): diff --git a/cps/search_metadata.py b/cps/search_metadata.py index bba22aab..4d648b1c 100644 --- a/cps/search_metadata.py +++ b/cps/search_metadata.py @@ -38,7 +38,7 @@ log = logger.create() new_list = list() meta_dir = os.path.join(constants.BASE_DIR, "cps", "metadata_provider") -modules = os.listdir(os.path.join(constants.BASE_DIR, "cps", "metadata_provider")) #glob.glob(join(dirname(__file__), "*.py")) +modules = os.listdir(os.path.join(constants.BASE_DIR, "cps", "metadata_provider")) for f in modules: if os.path.isfile(os.path.join(meta_dir, f)) and not f.endswith('__init__.py'): a = os.path.basename(f)[:-3] @@ -65,12 +65,14 @@ def metadata_provider(): active = current_user.view_settings.get('metadata', {}) provider = list() for c in cl: - provider.append({"name": c.__name__, "active": active.get(c.__id__, True), "id": c.__id__}) + ac = active.get(c.__id__, True) + provider.append({"name": c.__name__, "active": ac, "initial": ac, "id": c.__id__}) return Response(json.dumps(provider), mimetype='application/json') @meta.route("/metadata/provider", methods=['POST']) +@meta.route("/metadata/provider/", methods=['POST']) @login_required -def metadata_change_active_provider(): +def metadata_change_active_provider(prov_name): new_state = request.get_json() active = current_user.view_settings.get('metadata', {}) active[new_state['id']] = new_state['value'] @@ -84,10 +86,13 @@ def metadata_change_active_provider(): except (InvalidRequestError, OperationalError): log.error("Invalid request received: {}".format(request)) return "Invalid request", 400 - provider = list() - for c in cl: - provider.append({"name": c.__name__, "active": active.get(c.__id__, True), "id": c.__id__}) - return "" # Response(json.dumps(provider), mimetype='application/json') + if "initial" in new_state and prov_name: + for c in cl: + if c.__id__ == prov_name: + data = c.search(new_state.get('query', "")) + break + return Response(json.dumps(data), mimetype='application/json') + return "" @meta.route("/metadata/search", methods=['POST']) @login_required diff --git a/cps/static/js/get_meta.js b/cps/static/js/get_meta.js index 4b7f2873..f64be699 100644 --- a/cps/static/js/get_meta.js +++ b/cps/static/js/get_meta.js @@ -18,6 +18,7 @@ $(function () { var msg = i18nMsg; + var keyword = "" var templates = { bookResult: _.template( @@ -56,7 +57,6 @@ $(function () { data: {"query": keyword}, dataType: "json", success: function success(data) { - // console.log(data); $("#meta-info").html(""); data.forEach(function(book) { var $book = $(templates.bookResult(book)); @@ -80,13 +80,12 @@ $(function () { type: "get", dataType: "json", success: function success(data) { - // console.log(data); data.forEach(function(provider) { var checked = ""; if (provider.active) { checked = "checked"; } - var $provider_button = '' + var $provider_button = '' $("#metadata_provider").append($provider_button); }); }, @@ -94,20 +93,42 @@ $(function () { } $(document).on("change", ".pill", function () { - var id = $(this).data("control"); - var val = $(this).prop('checked'); + var element = $(this); + var id = element.data("control"); + var initial = element.data("initial"); + var val = element.prop('checked'); + var params = {id : id, value: val}; + if (!initial) { + params['initial'] = initial; + params['query'] = keyword; + } $.ajax({ method:"post", contentType: "application/json; charset=utf-8", dataType: "json", - url: getPath() + "/metadata/provider", - data: JSON.stringify({id : id, value: val}), + url: getPath() + "/metadata/provider/" + id, + data: JSON.stringify(params), + success: function success(data) { + element.data("initial", "true"); + data.forEach(function(book) { + var $book = $(templates.bookResult(book)); + $book.find("img").on("click", function () { + populateForm(book); + }); + $("#book-list").append($book); + }); + } }); }); $("#meta-search").on("submit", function (e) { e.preventDefault(); - var keyword = $("#keyword").val(); + keyword = $("#keyword").val(); + $('.pill').each(function(){ + // console.log($(this).data('control')); + $(this).data("initial", $(this).prop('checked')); + // console.log($(this).data('initial')); + }); doSearch(keyword); }); @@ -115,6 +136,7 @@ $(function () { populate_provider(); var bookTitle = $("#book_title").val(); $("#keyword").val(bookTitle); + keyword = bookTitle; doSearch(bookTitle); }); $("#metaModal").on("show.bs.modal", function(e) {