From b5a2eef4e78c7bac87dceb17a4bc32f5126515d7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 23 Sep 2020 04:50:09 +0000 Subject: [PATCH] Fix api crash when sending thumbnail path For things with thumbnails that aren't images (just presentations I think) --- openlp/core/display/window.py | 2 +- openlp/core/lib/serviceitem.py | 11 ++++-- .../openlp_core/lib/test_serviceitem.py | 34 ++++++++++++++++--- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/openlp/core/display/window.py b/openlp/core/display/window.py index 1374b20cb..3e2dfc218 100644 --- a/openlp/core/display/window.py +++ b/openlp/core/display/window.py @@ -275,7 +275,7 @@ class DisplayWindow(QtWidgets.QWidget, RegistryProperties, LogMixin): :param script: The script to run, a string :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(lambda: self.__script_done) if is_sync: diff --git a/openlp/core/lib/serviceitem.py b/openlp/core/lib/serviceitem.py index d61fa6ec1..d00448610 100644 --- a/openlp/core/lib/serviceitem.py +++ b/openlp/core/lib/serviceitem.py @@ -309,7 +309,7 @@ class ServiceItem(RegistryProperties): verse_tag = verse_tag.upper() else: # 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 title = text[:30].split('\n')[0] self.slides.append({'title': title, 'text': text, 'verse': verse_tag}) @@ -897,8 +897,13 @@ class ServiceItem(RegistryProperties): Registry().get('settings_thread').value('api/thumbnails'): # If the file is under our app directory tree send the portion after the match data_path = str(AppLocation.get_data_path()) - if frame['image'][0:len(data_path)] == data_path: - item['img'] = urllib.request.pathname2url(frame['image'][len(data_path):]) + try: + 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['html'] = str(frame['title']) data_dict['slides'].append(item) diff --git a/tests/functional/openlp_core/lib/test_serviceitem.py b/tests/functional/openlp_core/lib/test_serviceitem.py index 31253df10..14fafa739 100644 --- a/tests/functional/openlp_core/lib/test_serviceitem.py +++ b/tests/functional/openlp_core/lib/test_serviceitem.py @@ -24,7 +24,7 @@ Package to test the openlp.core.lib package. import os import pytest 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.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' +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(): """ 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 -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 """ @@ -755,7 +776,9 @@ def test_to_dict_presentation_item(state_media, settings, service_item_env): mocked_plugin = MagicMock() mocked_plugin.name = 'presentations' service_item = ServiceItem(mocked_plugin) + service_item.capabilities = [ItemCapabilities.HasThumbnails] presentation_name = 'test.pptx' + mocked_get_data_path.return_value = Path('/path/to/') image = Path('thumbnails/abcd/slide1.png') display_title = 'DisplayTitle' 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,\ patch('openlp.core.lib.serviceitem.AppLocation.get_section_data_path') as mocked_get_section_data_path: 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) # WHEN: to_dict() is called @@ -774,7 +797,7 @@ def test_to_dict_presentation_item(state_media, settings, service_item_env): expected_dict = { 'audit': '', 'backgroundAudio': [], - 'capabilities': [], + 'capabilities': [21], 'footer': [], 'fromPlugin': False, 'isThemeOverwritten': False, @@ -786,7 +809,8 @@ def test_to_dict_presentation_item(state_media, settings, service_item_env): 'selected': False, 'tag': 1, 'text': 'test.pptx', - 'title': '' + 'title': '', + 'img': ANY } ], 'theme': None,