Skip to content

Commit

Permalink
Ensure groups and ordering key are set for layers imported by URL
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 25, 2024
1 parent 674066d commit 37bb98d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
25 changes: 23 additions & 2 deletions felt/core/layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
"""
Expand All @@ -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:
Expand Down
47 changes: 44 additions & 3 deletions felt/core/map_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ def run(self):
return False

to_upload = {}
imported_by_url = {}

all_group_names = []
for layer_details in self.layers:
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 37bb98d

Please sign in to comment.