diff --git a/felt/core/layer_exporter.py b/felt/core/layer_exporter.py index 8a3dd20..c52a51c 100644 --- a/felt/core/layer_exporter.py +++ b/felt/core/layer_exporter.py @@ -90,6 +90,17 @@ class ZippedExportResult: ordering_key: Optional[int] = None +@dataclass +class ImportByUrlResult: + """ + Results of an import by URL operation + """ + layer_id: Optional[str] = None + error_message: Optional[str] = None + group_name: Optional[str] = None + ordering_key: Optional[int] = None + + class LayerExporter(QObject): """ Handles exports of layers to formats acceptable for Felt @@ -179,7 +190,8 @@ def layer_import_url(layer: QgsMapLayer) -> Optional[str]: @staticmethod def import_from_url(layer: QgsMapLayer, target_map: Map, - feedback: Optional[QgsFeedback] = None) -> Dict: + feedback: Optional[QgsFeedback] = None) \ + -> ImportByUrlResult: """ Imports a layer from URI to the given map """ @@ -192,7 +204,16 @@ def import_from_url(layer: QgsMapLayer, target_map: Map, blocking=True, feedback=feedback ) - return json.loads(reply.content().data().decode()) + response = json.loads(reply.content().data().decode()) + + res = ImportByUrlResult() + + if 'errors' in response: + res.error_message = response['errors'][0]['detail'] + return res + + res.layer_id = response['layer_id'] + return res @staticmethod def merge_dicts(tgt: Dict, enhancer: Dict) -> Dict: diff --git a/felt/core/map_uploader.py b/felt/core/map_uploader.py index ea10ee6..7055ca2 100644 --- a/felt/core/map_uploader.py +++ b/felt/core/map_uploader.py @@ -517,6 +517,7 @@ def run(self): return False to_upload = {} + imported_by_url = {} all_group_names = [] for layer_details in self.layers: @@ -532,17 +533,20 @@ def run(self): self.associated_map, multi_step_feedback) - if 'errors' in result: + if result.error_message: self.error_string = self.tr( 'Error occurred while exporting layer {}: {}').format( layer.name(), - result['errors'][0]['detail'] + result.error_message ) self.status_changed.emit(self.error_string) return False - layer.moveToThread(None) + + result.ordering_key = layer_details.ordering_key + result.group_name = layer_details.destination_group_name + imported_by_url[layer] = result else: self.status_changed.emit( @@ -768,6 +772,43 @@ def _upload_progress(sent, total): multi_step_feedback.step_finished() + for layer, details in imported_by_url.items(): + if self.isCanceled(): + return False + + self.status_changed.emit( + self.tr('Updating {}').format(layer.name()) + ) + + reply = None + if details.group_name: + group_id = group_ids[details.group_name] + reply = API_CLIENT.update_layer_details( + map_id=self.associated_map.id, + layer_id=details.layer_id, + layer_group_id=group_id, + ordering_key=details.ordering_key, + ) + elif details.ordering_key is not None: + reply = API_CLIENT.update_layer_details( + map_id=self.associated_map.id, + layer_id=details.layer_id, + ordering_key=details.ordering_key, + ) + + if reply and reply.error() != QNetworkReply.NoError: + self.error_string = reply.errorString() + Logger.instance().log_error_json( + { + 'type': Logger.MAP_EXPORT, + 'error': 'Error updating layer details: {}'.format( + self.error_string) + } + ) + return False + + multi_step_feedback.step_finished() + return True # pylint: enable=too-many-locals