From 2cb24348daea1a35863190a61eb1f20cb7f9ee6f Mon Sep 17 00:00:00 2001 From: Walter Lorenzetti Date: Tue, 7 Jan 2025 09:44:54 +0100 Subject: [PATCH] Fix media update (#1005) Co-authored-by: wlorenzetti --- g3w-admin/core/utils/vector.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/g3w-admin/core/utils/vector.py b/g3w-admin/core/utils/vector.py index 3555e56c8..44cc340b6 100644 --- a/g3w-admin/core/utils/vector.py +++ b/g3w-admin/core/utils/vector.py @@ -89,6 +89,15 @@ def get_domain(self): else: return '{}://{}'.format(schema, self.request.get_host()) + def _new_path(self, file_name): + """ Build new path to save media file """ + + return reverse('user-media', kwargs={ + 'project_type': self.type, + 'layer_id': self.layer.pk, + 'file_name': file_name + }) + def new_value(self, change=False): """ Build and save media value from client """ @@ -109,7 +118,7 @@ def new_value(self, change=False): # Check if it is a user-media view url_path = urllib.parse.urlparse(self.feature_properties[field]).path - is_media_view = bool(url_path) and url_path.startswith('/media/temp_uploads/') + is_media_view = bool(url_path) and url_path.startswith(f'{settings.MEDIA_URL}temp_uploads/') if change: if self.feature_properties[field]: @@ -162,11 +171,17 @@ def new_value(self, change=False): # build new value self.feature_properties[field] = '{}{}'.format(self.get_domain(), - reverse('user-media', kwargs={ - 'project_type': self.type, - 'layer_id': self.layer.pk, - 'file_name': file_name - })) + self._new_path(file_name)) + else: + if is_media_view: + + value = current_field_value + # Try to fix old record saved with temp_uploads path + if os.path.exists(path_file_to_save): + value = self._new_path(file_name) + + # Restore to current_feature value + self.feature_properties[field] = value if delete_old: to_delete = '{}/{}'.format(path_to_save, current_file_name)