diff --git a/src/qgis_geonode/apiclient/base.py b/src/qgis_geonode/apiclient/base.py index 229926b..5b5ce6b 100644 --- a/src/qgis_geonode/apiclient/base.py +++ b/src/qgis_geonode/apiclient/base.py @@ -123,28 +123,28 @@ def get_dataset_detail( self, dataset: typing.Union[models.BriefDataset, models.Dataset], get_style_too: bool = False, + authenticated: bool = False, ) -> None: - requests_to_perform = [ - network.RequestToPerform(url=self.get_dataset_detail_url(dataset.pk)) - ] - if get_style_too: - is_vector = ( - dataset.dataset_sub_type == models.GeonodeResourceType.VECTOR_LAYER - ) - should_load_vector_style = ( - models.ApiClientCapability.LOAD_VECTOR_LAYER_STYLE in self.capabilities - ) - if is_vector and should_load_vector_style: - sld_url = QtCore.QUrl(dataset.default_style.sld_url) - requests_to_perform.append(network.RequestToPerform(url=sld_url)) + + auth_manager = qgis.core.QgsApplication.authManager() + auth_provider_name = auth_manager.configAuthMethodKey(self.auth_config).lower() + + if auth_provider_name == "basic": + authenticated = True self.network_fetcher_task = network.NetworkRequestTask( - requests_to_perform, + [network.RequestToPerform(url=self.get_dataset_detail_url(dataset.pk))], self.network_requests_timeout, self.auth_config, description="Get dataset detail", ) - self.network_fetcher_task.task_done.connect(self.handle_dataset_detail) + self.network_fetcher_task.task_done.connect( + partial( + self.handle_dataset_detail, + get_style_too=get_style_too, + authenticated=authenticated, + ) + ) qgis.core.QgsApplication.taskManager().addTask(self.network_fetcher_task) def handle_dataset_detail(self, result: bool): diff --git a/src/qgis_geonode/apiclient/geonode_api_v2.py b/src/qgis_geonode/apiclient/geonode_api_v2.py index 33fc684..d1cc8e6 100644 --- a/src/qgis_geonode/apiclient/geonode_api_v2.py +++ b/src/qgis_geonode/apiclient/geonode_api_v2.py @@ -251,7 +251,12 @@ def handle_dataset_list(self, task_result: bool) -> None: ) self.dataset_list_received.emit(brief_datasets, pagination_info) - def handle_dataset_detail(self, task_result: bool) -> None: + def handle_dataset_detail( + self, + task_result: bool, + get_style_too: bool = False, + authenticated: bool = False, + ) -> None: log("inside the API client's handle_dataset_detail") deserialized_resource = self._retrieve_response( task_result, 0, self.dataset_detail_error_received @@ -267,21 +272,23 @@ def handle_dataset_detail(self, task_result: bool) -> None: debug=False, ) else: - try: - style_response_contents = ( - self.network_fetcher_task.response_contents[1] + # check if the request is from a WFS to see if it will retrieve the style + if get_style_too and authenticated: + is_vector = ( + dataset.dataset_sub_type + == models.GeonodeResourceType.VECTOR_LAYER ) - except IndexError: - pass + should_load_vector_style = ( + models.ApiClientCapability.LOAD_VECTOR_LAYER_STYLE + in self.capabilities + ) + # Check if the layer is vector and if it has the permissions to read the style + if is_vector and should_load_vector_style: + self.get_dataset_style( + dataset, emit_dataset_detail_received=True + ) else: - ( - sld_named_layer, - error_message, - ) = geonode_styles.get_usable_sld(style_response_contents) - if sld_named_layer is None: - raise RuntimeError(error_message) - dataset.default_style.sld = sld_named_layer - self.dataset_detail_received.emit(dataset) + self.dataset_detail_received.emit(dataset) def handle_dataset_style( self, diff --git a/src/qgis_geonode/gui/geonode_map_layer_config_widget.py b/src/qgis_geonode/gui/geonode_map_layer_config_widget.py index d9d0a4b..96546ec 100644 --- a/src/qgis_geonode/gui/geonode_map_layer_config_widget.py +++ b/src/qgis_geonode/gui/geonode_map_layer_config_widget.py @@ -495,12 +495,17 @@ def _toggle_style_controls(self, enabled: bool) -> None: models.GeonodePermission.CHANGE_DATASET_STYLE in dataset.permissions ) is_service = self.layer.dataProvider().name().lower() in ("wfs", "wcs") - has_style_url = dataset.default_style.sld_url is not None - if can_load_style and has_style_url and is_service: + has_geonode_style = dataset.default_style.sld is not None + if can_load_style and has_geonode_style and is_service: widgets.append(self.download_style_pb) else: self.download_style_pb.setEnabled(False) - if allowed_to_modify and can_modify_style and has_style_url and is_service: + if ( + allowed_to_modify + and can_modify_style + and has_geonode_style + and is_service + ): widgets.append(self.upload_style_pb) else: self.upload_style_pb.setEnabled(False) diff --git a/src/qgis_geonode/gui/search_result_widget.py b/src/qgis_geonode/gui/search_result_widget.py index 7be16c6..02eb17e 100644 --- a/src/qgis_geonode/gui/search_result_widget.py +++ b/src/qgis_geonode/gui/search_result_widget.py @@ -224,11 +224,14 @@ def prepare_loaded_layer(self): self.layer = self.dataset_loader_task.layer self.api_client.dataset_detail_received.connect(self.handle_layer_detail) self.api_client.dataset_detail_error_received.connect(self.handle_loading_error) + self.api_client.style_detail_error_received.connect(self.handle_style_error) self.api_client.get_dataset_detail( self.brief_dataset, get_style_too=self.layer.dataProvider().name() != "wms" ) - def handle_layer_detail(self, dataset: typing.Optional[models.Dataset]): + def handle_layer_detail( + self, dataset: typing.Optional[models.Dataset], retrieved_style: bool = False + ): self.api_client.dataset_detail_received.disconnect(self.handle_layer_detail) self.layer.setCustomProperty( models.DATASET_CUSTOM_PROPERTY_KEY, @@ -245,7 +248,11 @@ def handle_layer_detail(self, dataset: typing.Optional[models.Dataset]): can_load_style = models.loading_style_supported( self.layer.type(), self.api_client.capabilities ) - if can_load_style and dataset.default_style: + + if dataset.default_style.sld is not None: + retrieved_style = True + + if can_load_style and retrieved_style: error_message = "" loaded_sld = self.layer.readSld(dataset.default_style.sld, error_message) if not loaded_sld: @@ -257,10 +264,16 @@ def handle_loading_error(self): self.data_source_widget.show_message(message, level=qgis.core.Qgis.Critical) self.handle_layer_load_end(clear_message_bar=False) + def handle_style_error(self): + message = f"Unable to retrieve the style of {self.brief_dataset.title}" + self.data_source_widget.show_message(message, level=qgis.core.Qgis.Critical) + self.handle_layer_load_end(clear_message_bar=False) + def add_layer_to_project(self): self.api_client.dataset_detail_error_received.disconnect( self.handle_loading_error ) + self.api_client.style_detail_error_received.disconnect(self.handle_style_error) self.project.addMapLayer(self.layer) self.handle_layer_load_end()