From acb6e892a942768a52098336645f9308bf146494 Mon Sep 17 00:00:00 2001 From: Fernando Quant Date: Sat, 1 Aug 2020 19:34:08 +0000 Subject: [PATCH] Bible version api --- openlp/.version | 2 +- openlp/core/api/versions/v2/plugins.py | 44 ++++++++++++++ openlp/plugins/bibles/lib/mediaitem.py | 3 + .../openlp_core/api/v2/test_plugins.py | 60 +++++++++++++++++++ 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 tests/functional/openlp_core/api/v2/test_plugins.py diff --git a/openlp/.version b/openlp/.version index dedcc7d43..38d56e5e0 100644 --- a/openlp/.version +++ b/openlp/.version @@ -1 +1 @@ -2.9.1 +2.9.1 \ No newline at end of file diff --git a/openlp/core/api/versions/v2/plugins.py b/openlp/core/api/versions/v2/plugins.py index 60ba42b92..9f37ef16e 100644 --- a/openlp/core/api/versions/v2/plugins.py +++ b/openlp/core/api/versions/v2/plugins.py @@ -85,3 +85,47 @@ def live_view(plugin): id = data.get('id', -1) live(plugin, id) return '', 204 + + +@plugins.route('//search-options', methods=['GET']) +def search_options(plugin): + """ + Get the plugin's search options + """ + log.debug(f'{plugin}/search-options called') + if plugin == 'bibles': + bible_plugin = Registry().get('bible_plugin') + bibles = list(bible_plugin.manager.get_bibles().keys()) + primary = Registry().get('settings').value('bibles/primary bible') + return jsonify(primary=primary, bibles=bibles) + else: + return '', 501 + + +@plugins.route('//search-options', methods=['POST']) +@login_required +def set_search_option(plugin): + """ + Sets the plugin's search options + """ + log.debug(f'{plugin}/search-options-set called') + data = request.json + option = '' + if not data: + log.error('Missing request data') + abort(400) + elif type(data.get('option')) is not (str or int): + abort(400) + try: + option = data.get('option') + except ValueError: + log.error('Invalid data passed: ' + option) + abort(400) + + if plugin == 'bibles': + Registry().get('settings').setValue('bibles/primary bible', option) + Registry().execute('populate_bible_combo_boxes') + return '', 204 + else: + log.error('Unimplemented method') + return '', 501 diff --git a/openlp/plugins/bibles/lib/mediaitem.py b/openlp/plugins/bibles/lib/mediaitem.py index dff381595..018a21642 100755 --- a/openlp/plugins/bibles/lib/mediaitem.py +++ b/openlp/plugins/bibles/lib/mediaitem.py @@ -110,6 +110,7 @@ class BibleMediaItem(MediaManagerItem): self.search_timer.setSingleShot(True) self.search_timer.timeout.connect(self.on_search_timer_timeout) super().__init__(*args, **kwargs) + Registry().register_function('populate_bible_combo_boxes', self.populate_bible_combo_boxes) def setup_item(self): """ @@ -335,9 +336,11 @@ class BibleMediaItem(MediaManagerItem): bibles = self.plugin.manager.get_bibles() bibles = [(_f, bibles[_f]) for _f in bibles if _f] bibles.sort(key=lambda k: get_locale_key(k[0])) + self.version_combo_box.blockSignals(True) for bible in bibles: self.version_combo_box.addItem(bible[0], bible[1]) self.second_combo_box.addItem(bible[0], bible[1]) + self.version_combo_box.blockSignals(False) # set the default value bible = self.settings.value('bibles/primary bible') find_and_set_in_combo_box(self.version_combo_box, bible) diff --git a/tests/functional/openlp_core/api/v2/test_plugins.py b/tests/functional/openlp_core/api/v2/test_plugins.py new file mode 100644 index 000000000..887900299 --- /dev/null +++ b/tests/functional/openlp_core/api/v2/test_plugins.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- + +########################################################################## +# OpenLP - Open Source Lyrics Projection # +# ---------------------------------------------------------------------- # +# Copyright (c) 2008-2020 OpenLP Developers # +# ---------------------------------------------------------------------- # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +########################################################################## +from unittest.mock import MagicMock + +from openlp.core.common.registry import Registry + + +# Search options tests +def test_bibles_search_options_returns_bibles_list(flask_client, settings): + Registry().register('bible_plugin', MagicMock()) + res = flask_client.get('/api/v2/plugins/bibles/search-options').get_json() + assert res.get('primary') == "" + assert type(res.get('bibles')) is list + + +def test_bibles_set_search_options_sets_bible_version(flask_client, settings): + Registry().register('bible_plugin', MagicMock()) + res = flask_client.post('/api/v2/plugins/bibles/search-options', json=dict(option='foo')) + assert res.status_code == 204 + assert Registry().get('settings').value('bibles/primary bible') == 'foo' + + +def test_plugin_set_search_option_aborts_if_no_option(flask_client, settings): + res = flask_client.post('/api/v2/plugins/songs/search-options') + assert res.status_code == 400 + + +def test_plugin_set_search_option_aborts_if_invalid_option(flask_client, settings): + res1 = flask_client.post('/api/v2/plugins/songs/search-options', json=dict(option=[''])) + res2 = flask_client.post('/api/v2/plugins/songs/search-options', json=dict(option={1: '', 2: ''})) + assert res1.status_code == 400 + assert res2.status_code == 400 + + +def test_plugin_search_options_returns_plugin_exception(flask_client, settings): + res = flask_client.get('/api/v2/plugins/songs/search-options') + assert res.status_code == 501 + + +def test_plugin_set_search_option_returns_plugin_exception(flask_client, settings): + res = flask_client.post('/api/v2/plugins/songs/search-options', json=dict(option='')) + assert res.status_code == 501