Added a transpose endpoint to the web api

This commit is contained in:
Tomas Groth 2022-10-19 11:50:22 +00:00 committed by Tim Bentley
parent e14307111b
commit bca97f03a7
5 changed files with 61 additions and 20 deletions

View File

@ -40,6 +40,7 @@ def controller_live_items():
if current_item:
live_item = current_item.to_dict()
live_item['slides'][live_controller.selected_row]['selected'] = True
live_item['id'] = str(current_item.unique_identifier)
return jsonify(live_item)
@ -51,6 +52,7 @@ def controller_live_item():
live_item = {}
if current_item:
live_item = current_item.to_dict(True, live_controller.selected_row)
live_item['id'] = str(current_item.unique_identifier)
return jsonify(live_item)

View File

@ -21,11 +21,13 @@
##########################################################################
import logging
import re
from flask import abort, request, Blueprint, jsonify
from openlp.core.api.lib import login_required
from openlp.core.lib.plugin import PluginStatus
from openlp.core.common.registry import Registry
from openlp.plugins.songs.lib import transpose_lyrics
log = logging.getLogger(__name__)
@ -134,3 +136,45 @@ def set_search_option(plugin):
else:
log.error('Invalid option or value')
return '', 400
@plugins.route('/songs/transpose-live-item/<transpose_value>', methods=['GET'])
@login_required
def transpose(transpose_value):
log.debug('songs/transpose-live-item called')
if transpose_value:
try:
transpose_value = int(transpose_value)
except ValueError:
abort(400)
# Get lyrics from the live serviceitem in the live-controller and transpose it
live_controller = Registry().get('live_controller')
current_item = live_controller.service_item
# make sure an service item is currently displayed and that it is a song
if not current_item or current_item.name != 'songs':
abort(400)
previous_pages = {}
chord_song_text = ''
# re-create the song lyrics with OpenLP verse-tags to be able to transpose in one go so any keys are used
for raw_slide in current_item.slides:
verse_tag = raw_slide['verse']
if verse_tag in previous_pages and previous_pages[verse_tag][0] == raw_slide:
pages = previous_pages[verse_tag][1]
else:
pages = current_item.renderer.format_slide(raw_slide['text'], current_item)
previous_pages[verse_tag] = (raw_slide, pages)
for page in pages:
chord_song_text += '---[Verse:{verse_tag}]---\n'.format(verse_tag=verse_tag)
chord_song_text += page
chord_song_text += '\n'
# transpose
transposed_lyrics = transpose_lyrics(chord_song_text, transpose_value)
# re-split into verses
chord_slides = []
verse_list = re.split(r'---\[Verse:(.+?)\]---', transposed_lyrics)
# remove first blank entry
verse_list = verse_list[1:]
for i in range(0, len(verse_list), 2):
chord_slides.append({'chords': verse_list[i + 1].strip(), 'verse': verse_list[i]})
return jsonify(chord_slides), 200
abort(400)

View File

@ -224,7 +224,7 @@ class ServiceItem(RegistryProperties):
rendered_slide = {
'title': raw_slide['title'],
'text': render_tags(page),
'chords': render_tags(page, can_render_chords=True),
'chords': page,
'verse': index,
'footer': self.footer_html
}

View File

@ -32,6 +32,7 @@ def test_retrieve_live_items(flask_client, settings):
fake_live_controller = MagicMock()
fake_live_controller.service_item = MagicMock()
fake_live_controller.selected_row = 0
fake_live_controller.service_item.unique_identifier = 42
fake_live_controller.service_item.to_dict.return_value = {'slides': [{'selected': False}]}
Registry().register('live_controller', fake_live_controller)
@ -39,7 +40,7 @@ def test_retrieve_live_items(flask_client, settings):
res = flask_client.get('/api/v2/controller/live-items').get_json()
# THEN: The correct item data should be returned
assert res == {'slides': [{'selected': True}]}
assert res == {'slides': [{'selected': True}], 'id': '42'}
def test_controller_set_requires_login(settings, flask_client):

View File

@ -726,11 +726,10 @@ def test_to_dict_text_item(state_media, settings, service_item_env):
'notes': '',
'slides': [
{
'chords': '<span class="nochordline">'
'Amazing Grace! how sweet the sound\n'
'chords': 'Amazing Grace! how sweet the sound\n'
'That saved a wretch like me;\n'
'I once was lost, but now am found,\n'
'Was blind, but now I see.</span>',
'Was blind, but now I see.',
'html': 'Amazing Grace! how sweet the sound\n'
'That saved a wretch like me;\n'
'I once was lost, but now am found,\n'
@ -745,11 +744,10 @@ def test_to_dict_text_item(state_media, settings, service_item_env):
'footer': 'Amazing Grace<br>Written by: John Newton'
},
{
'chords': '<span class="nochordline">'
'Twas grace that taught my heart to fear,\n'
'chords': 'Twas grace that taught my heart to fear,\n'
'And grace my fears relieved;\n'
'How precious did that grace appear,\n'
'The hour I first believed!</span>',
'The hour I first believed!',
'html': 'Twas grace that taught my heart to fear,\n'
'And grace my fears relieved;\n'
'How precious did that grace appear,\n'
@ -764,11 +762,10 @@ def test_to_dict_text_item(state_media, settings, service_item_env):
'footer': 'Amazing Grace<br>Written by: John Newton'
},
{
'chords': '<span class="nochordline">'
'Through many dangers, toils and snares\n'
'chords': 'Through many dangers, toils and snares\n'
'I have already come;\n'
'Tis grace that brought me safe thus far,\n'
'And grace will lead me home.</span>',
'And grace will lead me home.',
'html': 'Through many dangers, toils and snares\n'
'I have already come;\n'
'Tis grace that brought me safe thus far,\n'
@ -783,11 +780,10 @@ def test_to_dict_text_item(state_media, settings, service_item_env):
'footer': 'Amazing Grace<br>Written by: John Newton'
},
{
'chords': '<span class="nochordline">'
'The Lord has promised good to me,\n'
'chords': 'The Lord has promised good to me,\n'
'His word my hope secures;\n'
'He will my shield and portion be\n'
'As long as life endures.</span>',
'As long as life endures.',
'html': 'The Lord has promised good to me,\n'
'His word my hope secures;\n'
'He will my shield and portion be\n'
@ -802,11 +798,10 @@ def test_to_dict_text_item(state_media, settings, service_item_env):
'footer': 'Amazing Grace<br>Written by: John Newton'
},
{
'chords': '<span class="nochordline">'
'Yes, when this heart and flesh shall fail,\n'
'chords': 'Yes, when this heart and flesh shall fail,\n'
'And mortal life shall cease,\n'
'I shall possess within the veil\n'
'A life of joy and peace.</span>',
'A life of joy and peace.',
'html': 'Yes, when this heart and flesh shall fail,\n'
'And mortal life shall cease,\n'
'I shall possess within the veil\n'
@ -821,11 +816,10 @@ def test_to_dict_text_item(state_media, settings, service_item_env):
'footer': 'Amazing Grace<br>Written by: John Newton'
},
{
'chords': '<span class="nochordline">'
'When weve been there a thousand years,\n'
'chords': 'When weve been there a thousand years,\n'
'Bright shining as the sun,\n'
'Weve no less days to sing Gods praise\n'
'Than when we first begun.</span>',
'Than when we first begun.',
'html': 'When weve been there a thousand years,\n'
'Bright shining as the sun,\n'
'Weve no less days to sing Gods praise\n'