Merge branch 'bibleversionapi' into 'master'

Bible version api

See merge request openlp/openlp!220
This commit is contained in:
Tomas Groth 2020-08-01 19:34:08 +00:00
commit 8fe519cada
4 changed files with 108 additions and 1 deletions

View File

@ -1 +1 @@
2.9.1 2.9.1

View File

@ -85,3 +85,47 @@ def live_view(plugin):
id = data.get('id', -1) id = data.get('id', -1)
live(plugin, id) live(plugin, id)
return '', 204 return '', 204
@plugins.route('/<plugin>/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('/<plugin>/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

View File

@ -110,6 +110,7 @@ class BibleMediaItem(MediaManagerItem):
self.search_timer.setSingleShot(True) self.search_timer.setSingleShot(True)
self.search_timer.timeout.connect(self.on_search_timer_timeout) self.search_timer.timeout.connect(self.on_search_timer_timeout)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
Registry().register_function('populate_bible_combo_boxes', self.populate_bible_combo_boxes)
def setup_item(self): def setup_item(self):
""" """
@ -335,9 +336,11 @@ class BibleMediaItem(MediaManagerItem):
bibles = self.plugin.manager.get_bibles() bibles = self.plugin.manager.get_bibles()
bibles = [(_f, bibles[_f]) for _f in bibles if _f] bibles = [(_f, bibles[_f]) for _f in bibles if _f]
bibles.sort(key=lambda k: get_locale_key(k[0])) bibles.sort(key=lambda k: get_locale_key(k[0]))
self.version_combo_box.blockSignals(True)
for bible in bibles: for bible in bibles:
self.version_combo_box.addItem(bible[0], bible[1]) self.version_combo_box.addItem(bible[0], bible[1])
self.second_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 # set the default value
bible = self.settings.value('bibles/primary bible') bible = self.settings.value('bibles/primary bible')
find_and_set_in_combo_box(self.version_combo_box, bible) find_and_set_in_combo_box(self.version_combo_box, bible)

View File

@ -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 <https://www.gnu.org/licenses/>. #
##########################################################################
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