Added basic theme API

This commit is contained in:
Fernando Quant 2020-04-01 18:26:57 +00:00 committed by Raoul Snyman
parent b85d6e0ded
commit 5e4559cdb3
5 changed files with 158 additions and 0 deletions

1
.gitignore vendored
View File

@ -48,3 +48,4 @@ package-lock.json
tags
test
openlp-test-projectordb.sqlite
Chromium_80.0.3987_(Linux_0.0.0)/

View File

@ -23,6 +23,7 @@ import urllib.request
from pathlib import Path
from openlp.core.api.lib import login_required
from openlp.core.common import ThemeLevel
from openlp.core.common.registry import Registry
from openlp.core.common.applocation import AppLocation
from openlp.core.lib import create_thumb
@ -103,3 +104,98 @@ def controller_direction():
getattr(Registry().get('live_controller'), 'slidecontroller_live_{action}'.
format(action=action)).emit()
return '', 204
@controller_views.route('/theme-level', methods=['GET'])
@login_required
def get_theme_level():
theme_level = Registry().get('settings').value('themes/theme level')
if theme_level == ThemeLevel.Global:
theme_level = 'global'
elif theme_level == ThemeLevel.Service:
theme_level = 'service'
elif theme_level == ThemeLevel.Song:
theme_level = 'song'
return jsonify(theme_level)
@controller_views.route('/theme-level', methods=['POST'])
@login_required
def set_theme_level():
data = request.json
if not data:
abort(400)
theme_level = ''
try:
theme_level = str(data.get("level"))
except ValueError:
abort(400)
if theme_level == 'global':
Registry().get('settings').setValue('themes/theme level', 1)
elif theme_level == 'service':
Registry().get('settings').setValue('themes/theme level', 2)
elif theme_level == 'song':
Registry().get('settings').setValue('theme/theme level', 3)
else:
abort(400)
return '', 204
@controller_views.route('/themes', methods=['GET'])
@login_required
def get_themes():
theme_level = Registry().get('settings').value('themes/theme level')
theme_list = []
current_theme = ''
if theme_level == ThemeLevel.Global:
current_theme = Registry().get('theme_manager').global_theme
if theme_level == ThemeLevel.Service:
current_theme = Registry().get('service_manager').service_theme
# Gets and appends theme list
themes = Registry().execute('get_theme_names')
try:
for theme in themes[0]:
theme_list.append({
'name': theme,
'selected': False
})
for i in theme_list:
if i["name"] == current_theme:
i["selected"] = True
except IndexError:
pass
return jsonify(theme_list)
@controller_views.route('/theme', methods=['POST'])
@login_required
def set_theme():
data = request.json
theme = ''
theme_level = Registry().get('settings').value('themes/theme level')
if not data:
abort(400)
try:
theme = str(data.get('theme'))
except ValueError:
abort(400)
if theme_level == ThemeLevel.Global:
Registry().get('settings').setValue('themes/global theme', theme)
Registry().execute('theme_update_global')
elif theme_level == ThemeLevel.Service:
Registry().get('settings').setValue('servicemanager/service theme', theme)
Registry().execute('theme_update_service')
elif theme_level == ThemeLevel.Song:
return '', 501
return '', 204

View File

@ -302,6 +302,7 @@ class Ui_ServiceManager(object):
Registry().register_function('theme_update_list', self.update_theme_list)
Registry().register_function('config_screen_changed', self.regenerate_service_items)
Registry().register_function('theme_update_global', self.theme_change)
Registry().register_function('theme_update_service', self.service_theme_change)
Registry().register_function('mediaitem_suffix_reset', self.reset_supported_suffixes)
@ -1284,6 +1285,14 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi
self.toolbar.actions['theme_label'].setVisible(visible)
self.regenerate_service_items()
def service_theme_change(self):
"""
Set the theme for the current service remotely
"""
self.service_theme = self.settings.value(self.main_window.service_manager_settings_section + '/service theme')
find_and_set_in_combo_box(self.theme_combo_box, self.service_theme)
self.regenerate_service_items(True)
def regenerate_service_items(self, changed=False):
"""
Rebuild the service list as things have changed and a repaint is the easiest way to do this.

View File

@ -129,6 +129,8 @@ class Ui_ThemeManager(object):
self.theme_list_widget.doubleClicked.connect(self.change_global_from_screen)
self.theme_list_widget.currentItemChanged.connect(self.check_list_state)
Registry().register_function('get_theme_names', self.get_theme_names)
class ThemeManager(QtWidgets.QWidget, RegistryBase, Ui_ThemeManager, LogMixin, RegistryProperties):
"""

View File

@ -73,3 +73,53 @@ def test_controller_direction_calls_service_manager(flask_client, settings):
res = flask_client.post('/api/v2/controller/progress', json=dict(action='next'))
assert res.status_code == 204
fake_live_controller.slidecontroller_live_next.emit.assert_called_once()
# Themes tests
def test_controller_get_theme_level_returns_valid_theme_level(flask_client, settings):
res = flask_client.get('/api/v2/controller/theme-level').get_json()
assert res == ('global' or 'service' or 'song')
def test_controller_set_theme_level_aborts_if_no_theme_level(flask_client, settings):
res = flask_client.post('/api/v2/controller/theme-level')
assert res.status_code == 400
def test_controller_set_theme_level_aborts_if_invalid_theme_level(flask_client, settings):
res = flask_client.post('/api/v2/controller/theme-level', json=dict(level='foo'))
assert res.status_code == 400
def test_controller_set_theme_level_sets_theme_level(flask_client, settings):
res = flask_client.post('/api/v2/controller/theme-level', json=dict(level='service'))
assert res.status_code == 204
assert Registry().get('settings').value('themes/theme level') == 2
def test_controller_get_themes_retrieves_themes_list(flask_client, settings):
Registry().register('theme_manager', MagicMock())
Registry().register('service_manager', MagicMock())
res = flask_client.get('api/v2/controller/themes').get_json()
assert type(res) is list
def test_controller_set_theme_does_not_accept_get(flask_client):
res = flask_client.get('/api/v2/controller/theme')
assert res.status_code == 405
def test_controller_set_theme_aborts_if_no_theme(flask_client, settings):
res = flask_client.post('/api/v2/controller/theme')
assert res.status_code == 400
def test_controller_set_theme_sets_theme(flask_client, settings):
res = flask_client.post('/api/v2/controller/theme', json=dict(theme='test'))
assert res.status_code == 204
def test_controller_set_theme_returns_song_exception(flask_client, settings):
Registry().get('settings').setValue('themes/theme level', 3)
res = flask_client.post('/api/v2/controller/theme', json=dict(theme='test'))
assert res.status_code == 501