From eb3dc6b221723d6ca895bce9c927bf8b7cc4cbbb Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 20 Oct 2020 11:32:48 +0000 Subject: [PATCH] Fix first run issues --- openlp/core/api/deploy.py | 2 - openlp/core/common/httputils.py | 1 + openlp/core/lib/db.py | 14 +++--- openlp/core/ui/firsttimeform.py | 2 +- openlp/core/ui/mainwindow.py | 4 +- openlp/core/version.py | 8 ++-- tests/functional/openlp_core/test_version.py | 50 ++++++++++---------- 7 files changed, 41 insertions(+), 40 deletions(-) diff --git a/openlp/core/api/deploy.py b/openlp/core/api/deploy.py index a9cf3b0b2..cbdd977ca 100644 --- a/openlp/core/api/deploy.py +++ b/openlp/core/api/deploy.py @@ -75,8 +75,6 @@ class RemoteVersionWorker(ThreadWorker): self.no_internet.emit() if version_info and (QtCore.QVersionNumber.fromString(version_info['latest']['version']) > QtCore.QVersionNumber.fromString(self.current_version)): - Registry().get('settings').setValue('api/last version test', date.today().strftime('%Y-%m-%d')) - Registry().get('settings_form').api_tab.master_version = version_info['latest']['version'] self.new_version.emit(version_info['latest']['version']) self.quit.emit() diff --git a/openlp/core/common/httputils.py b/openlp/core/common/httputils.py index 7cd948430..87f36e55b 100644 --- a/openlp/core/common/httputils.py +++ b/openlp/core/common/httputils.py @@ -280,6 +280,7 @@ class DownloadWorker(ThreadWorker): log.exception('Unable to download %s', url) self.download_failed.emit() finally: + time.sleep(1) self.quit.emit() @QtCore.pyqtSlot() diff --git a/openlp/core/lib/db.py b/openlp/core/lib/db.py index 04c6a200d..143baea2f 100644 --- a/openlp/core/lib/db.py +++ b/openlp/core/lib/db.py @@ -358,6 +358,13 @@ class Manager(object): self.db_url = init_url(plugin_name, str(db_file_path)) # TOdO :PATHLIB else: self.db_url = init_url(plugin_name) + if not session: + try: + self.session = init_schema(self.db_url) + except (SQLAlchemyError, DBAPIError): + handle_db_error(plugin_name, db_file_path) + else: + self.session = session if upgrade_mod: try: db_ver, up_ver = upgrade_db(self.db_url, upgrade_mod) @@ -373,13 +380,6 @@ class Manager(object): db_up=up_ver, db_name=self.db_url)) return - if not session: - try: - self.session = init_schema(self.db_url) - except (SQLAlchemyError, DBAPIError): - handle_db_error(plugin_name, db_file_path) - else: - self.session = session def save_object(self, object_instance, commit=True): """ diff --git a/openlp/core/ui/firsttimeform.py b/openlp/core/ui/firsttimeform.py index a0f49e877..e2092f308 100644 --- a/openlp/core/ui/firsttimeform.py +++ b/openlp/core/ui/firsttimeform.py @@ -186,7 +186,6 @@ class FirstTimeForm(QtWidgets.QWizard, UiFirstTimeWizard, RegistryProperties): self.has_web_access = True self.application.process_events() self.downloading = translate('OpenLP.FirstTimeWizard', 'Downloading {name}...') - self.application.set_normal_cursor() self.is_index_downloaded = True def _parse_config(self, web_config): @@ -326,6 +325,7 @@ class FirstTimeForm(QtWidgets.QWizard, UiFirstTimeWizard, RegistryProperties): # Was the thread created. if self.thumbnail_download_threads: while any([not is_thread_finished(thread_name) for thread_name in self.thumbnail_download_threads]): + self.application.process_events() time.sleep(0.1) self.application.set_normal_cursor() Registry().remove_function('config_screen_changed', self.screen_selection_widget.load) diff --git a/openlp/core/ui/mainwindow.py b/openlp/core/ui/mainwindow.py index d72824f9f..8e8de1287 100644 --- a/openlp/core/ui/mainwindow.py +++ b/openlp/core/ui/mainwindow.py @@ -23,7 +23,7 @@ This is the main window, where all the action happens. """ import os import shutil -from datetime import datetime +from datetime import datetime, date from pathlib import Path from tempfile import gettempdir @@ -626,6 +626,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, LogMixin, RegistryPropert :param version: The Version to be displayed. """ + Registry().get('settings').setValue('api/last version test', date.today().strftime('%Y-%m-%d')) + Registry().get('settings_form').api_tab.master_version = version version_text = translate('OpenLP.MainWindow', 'Version {version} of the web remote is now available for ' 'download.\nTo download this version, go to the Remote settings and click the Upgrade ' 'button.').format(version=version) diff --git a/openlp/core/version.py b/openlp/core/version.py index f10efd362..39023cc69 100644 --- a/openlp/core/version.py +++ b/openlp/core/version.py @@ -27,10 +27,10 @@ import sys from collections import OrderedDict from datetime import date -import requests from PyQt5 import QtCore from openlp.core.common.applocation import AppLocation +from openlp.core.common.httputils import get_web_page from openlp.core.common.registry import Registry from openlp.core.threading import ThreadWorker, run_thread @@ -104,9 +104,9 @@ class VersionWorker(ThreadWorker): retries = 0 while retries < 3: try: - response = requests.get(download_url, headers=headers) - if response.status_code == 200: - remote_version = response.text.strip() + response = get_web_page(download_url, headers=headers) + if response: + remote_version = response.strip() log.debug('New version found: %s', remote_version) break except OSError: diff --git a/tests/functional/openlp_core/test_version.py b/tests/functional/openlp_core/test_version.py index 375017215..2254c4525 100644 --- a/tests/functional/openlp_core/test_version.py +++ b/tests/functional/openlp_core/test_version.py @@ -47,8 +47,8 @@ def test_worker_init(): @patch('openlp.core.version.platform') -@patch('openlp.core.version.requests') -def test_worker_start(mock_requests, mock_platform): +@patch('openlp.core.version.get_web_page') +def test_worker_start(mock_get_web_page, mock_platform): """ Test the VersionWorkder.start() method """ @@ -57,7 +57,7 @@ def test_worker_start(mock_requests, mock_platform): current_version = {'full': '2.0', 'version': '2.0', 'build': None} mock_platform.system.return_value = 'Linux' mock_platform.release.return_value = '4.12.0-1-amd64' - mock_requests.get.return_value = MagicMock(text='2.4.6', status_code=200) + mock_get_web_page.return_value = '2.4.6' worker = VersionWorker(last_check_date, current_version) # WHEN: The worker is run @@ -68,14 +68,14 @@ def test_worker_start(mock_requests, mock_platform): # THEN: The check completes and the signal is emitted expected_download_url = 'https://get.openlp.org/versions/version.txt' expected_headers = {'User-Agent': 'OpenLP/2.0 Linux/4.12.0-1-amd64; '} - mock_requests.get.assert_called_once_with(expected_download_url, headers=expected_headers) + mock_get_web_page.assert_called_once_with(expected_download_url, headers=expected_headers) mock_new_version.emit.assert_called_once_with('2.4.6') mock_quit.emit.assert_called_once_with() @patch('openlp.core.version.platform') -@patch('openlp.core.version.requests') -def test_worker_start_fail(mock_requests, mock_platform): +@patch('openlp.core.version.get_web_page') +def test_worker_start_fail(mock_get_web_page, mock_platform): """ Test the Version Workder.start() method with no response """ @@ -84,7 +84,7 @@ def test_worker_start_fail(mock_requests, mock_platform): current_version = {'full': '2.0', 'version': '2.0', 'build': None} mock_platform.system.return_value = 'Linux' mock_platform.release.return_value = '4.12.0-1-amd64' - mock_requests.get.return_value = MagicMock(text='2.4.6', status_code=500) + mock_get_web_page.return_value = None worker = VersionWorker(last_check_date, current_version) # WHEN: The worker is run @@ -95,14 +95,14 @@ def test_worker_start_fail(mock_requests, mock_platform): # THEN: The check completes and the signal is emitted expected_download_url = 'https://get.openlp.org/versions/version.txt' expected_headers = {'User-Agent': 'OpenLP/2.0 Linux/4.12.0-1-amd64; '} - mock_requests.get.assert_called_once_with(expected_download_url, headers=expected_headers) + mock_get_web_page.assert_called_once_with(expected_download_url, headers=expected_headers) mock_new_version.emit.assert_not_called() mock_quit.emit.assert_called_once_with() @patch('openlp.core.version.platform') -@patch('openlp.core.version.requests') -def test_worker_start_dev_version(mock_requests, mock_platform): +@patch('openlp.core.version.get_web_page') +def test_worker_start_dev_version(mock_get_web_page, mock_platform): """ Test the VersionWorkder.start() method for dev versions """ @@ -111,7 +111,7 @@ def test_worker_start_dev_version(mock_requests, mock_platform): current_version = {'full': '2.1.3', 'version': '2.1.3', 'build': None} mock_platform.system.return_value = 'Linux' mock_platform.release.return_value = '4.12.0-1-amd64' - mock_requests.get.return_value = MagicMock(text='2.4.6', status_code=200) + mock_get_web_page.return_value = '2.4.6' worker = VersionWorker(last_check_date, current_version) # WHEN: The worker is run @@ -122,14 +122,14 @@ def test_worker_start_dev_version(mock_requests, mock_platform): # THEN: The check completes and the signal is emitted expected_download_url = 'https://get.openlp.org/versions/dev_version.txt' expected_headers = {'User-Agent': 'OpenLP/2.1.3 Linux/4.12.0-1-amd64; '} - mock_requests.get.assert_called_once_with(expected_download_url, headers=expected_headers) + mock_get_web_page.assert_called_once_with(expected_download_url, headers=expected_headers) mock_new_version.emit.assert_called_once_with('2.4.6') mock_quit.emit.assert_called_once_with() @patch('openlp.core.version.platform') -@patch('openlp.core.version.requests') -def test_worker_start_nightly_version(mock_requests, mock_platform): +@patch('openlp.core.version.get_web_page') +def test_worker_start_nightly_version(mock_get_web_page, mock_platform): """ Test the VersionWorkder.start() method for nightlies """ @@ -138,7 +138,7 @@ def test_worker_start_nightly_version(mock_requests, mock_platform): current_version = {'full': '2.1-bzr2345', 'version': '2.1', 'build': '2345'} mock_platform.system.return_value = 'Linux' mock_platform.release.return_value = '4.12.0-1-amd64' - mock_requests.get.return_value = MagicMock(text='2.4.6', status_code=200) + mock_get_web_page.return_value = '2.4.6' worker = VersionWorker(last_check_date, current_version) # WHEN: The worker is run @@ -149,14 +149,14 @@ def test_worker_start_nightly_version(mock_requests, mock_platform): # THEN: The check completes and the signal is emitted expected_download_url = 'https://get.openlp.org/versions/nightly_version.txt' expected_headers = {'User-Agent': 'OpenLP/2.1-bzr2345 Linux/4.12.0-1-amd64; '} - mock_requests.get.assert_called_once_with(expected_download_url, headers=expected_headers) + mock_get_web_page.assert_called_once_with(expected_download_url, headers=expected_headers) mock_new_version.emit.assert_called_once_with('2.4.6') mock_quit.emit.assert_called_once_with() @patch('openlp.core.version.platform') -@patch('openlp.core.version.requests') -def test_worker_empty_response(mock_requests, mock_platform): +@patch('openlp.core.version.get_web_page') +def test_worker_empty_response(mock_get_web_page, mock_platform): """ Test the VersionWorkder.start() method for empty responses """ @@ -165,7 +165,7 @@ def test_worker_empty_response(mock_requests, mock_platform): current_version = {'full': '2.1-bzr2345', 'version': '2.1', 'build': '2345'} mock_platform.system.return_value = 'Linux' mock_platform.release.return_value = '4.12.0-1-amd64' - mock_requests.get.return_value = MagicMock(text='\n') + mock_get_web_page.return_value = '\n' worker = VersionWorker(last_check_date, current_version) # WHEN: The worker is run @@ -176,14 +176,14 @@ def test_worker_empty_response(mock_requests, mock_platform): # THEN: The check completes and the signal is emitted expected_download_url = 'https://get.openlp.org/versions/nightly_version.txt' expected_headers = {'User-Agent': 'OpenLP/2.1-bzr2345 Linux/4.12.0-1-amd64; '} - mock_requests.get.assert_called_once_with(expected_download_url, headers=expected_headers) + mock_get_web_page.assert_called_once_with(expected_download_url, headers=expected_headers) assert mock_new_version.emit.call_count == 0 mock_quit.emit.assert_called_once_with() @patch('openlp.core.version.platform') -@patch('openlp.core.version.requests') -def test_worker_start_connection_error(mock_requests, mock_platform): +@patch('openlp.core.version.get_web_page') +def test_worker_start_connection_error(mock_get_web_page, mock_platform): """ Test the VersionWorkder.start() method when a ConnectionError happens """ @@ -192,7 +192,7 @@ def test_worker_start_connection_error(mock_requests, mock_platform): current_version = {'full': '2.0', 'version': '2.0', 'build': None} mock_platform.system.return_value = 'Linux' mock_platform.release.return_value = '4.12.0-1-amd64' - mock_requests.get.side_effect = ConnectionError('Could not connect') + mock_get_web_page.side_effect = ConnectionError('Could not connect') worker = VersionWorker(last_check_date, current_version) # WHEN: The worker is run @@ -203,8 +203,8 @@ def test_worker_start_connection_error(mock_requests, mock_platform): # THEN: The check completes and the signal is emitted expected_download_url = 'https://get.openlp.org/versions/version.txt' expected_headers = {'User-Agent': 'OpenLP/2.0 Linux/4.12.0-1-amd64; '} - mock_requests.get.assert_called_with(expected_download_url, headers=expected_headers) - assert mock_requests.get.call_count == 3 + mock_get_web_page.assert_called_with(expected_download_url, headers=expected_headers) + assert mock_get_web_page.call_count == 3 mocked_no_internet.emit.assert_called_once_with() mocked_quit.emit.assert_called_once_with()