Fix api crash when sending thumbnail path

For things with thumbnails that aren't images (just presentations I think)
This commit is contained in:
Daniel 2020-09-23 04:50:09 +00:00 committed by Raoul Snyman
parent 35c56cecdb
commit b5a2eef4e7
3 changed files with 38 additions and 9 deletions

View File

@ -275,7 +275,7 @@ class DisplayWindow(QtWidgets.QWidget, RegistryProperties, LogMixin):
:param script: The script to run, a string :param script: The script to run, a string
:param is_sync: Run the script synchronously. Defaults to False :param is_sync: Run the script synchronously. Defaults to False
""" """
log.debug(script) log.debug((script[:80] + '..') if len(script) > 80 else script)
# Wait for previous scripts to finish # Wait for previous scripts to finish
wait_for(lambda: self.__script_done) wait_for(lambda: self.__script_done)
if is_sync: if is_sync:

View File

@ -309,7 +309,7 @@ class ServiceItem(RegistryProperties):
verse_tag = verse_tag.upper() verse_tag = verse_tag.upper()
else: else:
# For items that don't have a verse tag, autoincrement the slide numbers # For items that don't have a verse tag, autoincrement the slide numbers
verse_tag = str(len(self.slides)) verse_tag = str(len(self.slides) + 1)
self.service_item_type = ServiceItemType.Text self.service_item_type = ServiceItemType.Text
title = text[:30].split('\n')[0] title = text[:30].split('\n')[0]
self.slides.append({'title': title, 'text': text, 'verse': verse_tag}) self.slides.append({'title': title, 'text': text, 'verse': verse_tag})
@ -897,8 +897,13 @@ class ServiceItem(RegistryProperties):
Registry().get('settings_thread').value('api/thumbnails'): Registry().get('settings_thread').value('api/thumbnails'):
# If the file is under our app directory tree send the portion after the match # If the file is under our app directory tree send the portion after the match
data_path = str(AppLocation.get_data_path()) data_path = str(AppLocation.get_data_path())
if frame['image'][0:len(data_path)] == data_path: try:
item['img'] = urllib.request.pathname2url(frame['image'][len(data_path):]) relative_file = frame['image'].relative_to(data_path)
except ValueError:
log.warning('Service item "{title}" is missing a thumbnail or has an invalid thumbnail path'
.format(title=self.title))
else:
item['img'] = urllib.request.pathname2url(os.path.sep + str(relative_file))
item['text'] = str(frame['title']) item['text'] = str(frame['title'])
item['html'] = str(frame['title']) item['html'] = str(frame['title'])
data_dict['slides'].append(item) data_dict['slides'].append(item)

View File

@ -24,7 +24,7 @@ Package to test the openlp.core.lib package.
import os import os
import pytest import pytest
from pathlib import Path from pathlib import Path
from unittest.mock import Mock, MagicMock, patch from unittest.mock import Mock, MagicMock, patch, ANY
from openlp.core.common import ThemeLevel, is_win from openlp.core.common import ThemeLevel, is_win
from openlp.core.common.enum import ServiceItemType from openlp.core.common.enum import ServiceItemType
@ -280,6 +280,26 @@ def test_service_item_load_image_from_local_service(mocked_get_section_data_path
'This service item should be able to have new items added to it' 'This service item should be able to have new items added to it'
def test_add_from_text_no_verse_tag():
"""
Test the Service Item - adding text slides with no verse tag
"""
# GIVEN: A service item and two slides
service_item = ServiceItem(None)
slide1 = "This is the first slide"
slide2 = "This is the second slide"
# WHEN: adding text slides to service_item
service_item.add_from_text(slide1)
service_item.add_from_text(slide2)
# THEN: Slides should be added with correctly numbered verse tags (Should start at 1)
assert service_item.slides == [
{'text': 'This is the first slide', 'title': 'This is the first slide', 'verse': '1'},
{'text': 'This is the second slide', 'title': 'This is the second slide', 'verse': '2'}
]
def test_add_from_command_for_a_presentation(): def test_add_from_command_for_a_presentation():
""" """
Test the Service Item - adding a presentation Test the Service Item - adding a presentation
@ -747,7 +767,8 @@ def test_to_dict_image_item(state_media, settings, service_item_env):
assert result == expected_dict assert result == expected_dict
def test_to_dict_presentation_item(state_media, settings, service_item_env): @patch('openlp.core.lib.serviceitem.AppLocation.get_data_path')
def test_to_dict_presentation_item(mocked_get_data_path, state_media, settings, service_item_env):
""" """
Test that the to_dict() method returns the correct data for the service item Test that the to_dict() method returns the correct data for the service item
""" """
@ -755,7 +776,9 @@ def test_to_dict_presentation_item(state_media, settings, service_item_env):
mocked_plugin = MagicMock() mocked_plugin = MagicMock()
mocked_plugin.name = 'presentations' mocked_plugin.name = 'presentations'
service_item = ServiceItem(mocked_plugin) service_item = ServiceItem(mocked_plugin)
service_item.capabilities = [ItemCapabilities.HasThumbnails]
presentation_name = 'test.pptx' presentation_name = 'test.pptx'
mocked_get_data_path.return_value = Path('/path/to/')
image = Path('thumbnails/abcd/slide1.png') image = Path('thumbnails/abcd/slide1.png')
display_title = 'DisplayTitle' display_title = 'DisplayTitle'
notes = 'Note1\nNote2\n' notes = 'Note1\nNote2\n'
@ -764,7 +787,7 @@ def test_to_dict_presentation_item(state_media, settings, service_item_env):
with patch('openlp.core.lib.serviceitem.sha256_file_hash') as mocked_sha256_file_hash,\ with patch('openlp.core.lib.serviceitem.sha256_file_hash') as mocked_sha256_file_hash,\
patch('openlp.core.lib.serviceitem.AppLocation.get_section_data_path') as mocked_get_section_data_path: patch('openlp.core.lib.serviceitem.AppLocation.get_section_data_path') as mocked_get_section_data_path:
mocked_sha256_file_hash.return_value = '4a067fed6834ea2bc4b8819f11636365' mocked_sha256_file_hash.return_value = '4a067fed6834ea2bc4b8819f11636365'
mocked_get_section_data_path.return_value = Path('.') mocked_get_section_data_path.return_value = Path('/path/to/presentations/')
service_item.add_from_command(TEST_PATH, presentation_name, image, display_title, notes) service_item.add_from_command(TEST_PATH, presentation_name, image, display_title, notes)
# WHEN: to_dict() is called # WHEN: to_dict() is called
@ -774,7 +797,7 @@ def test_to_dict_presentation_item(state_media, settings, service_item_env):
expected_dict = { expected_dict = {
'audit': '', 'audit': '',
'backgroundAudio': [], 'backgroundAudio': [],
'capabilities': [], 'capabilities': [21],
'footer': [], 'footer': [],
'fromPlugin': False, 'fromPlugin': False,
'isThemeOverwritten': False, 'isThemeOverwritten': False,
@ -786,7 +809,8 @@ def test_to_dict_presentation_item(state_media, settings, service_item_env):
'selected': False, 'selected': False,
'tag': 1, 'tag': 1,
'text': 'test.pptx', 'text': 'test.pptx',
'title': '' 'title': '',
'img': ANY
} }
], ],
'theme': None, 'theme': None,