Skip to content

Commit

Permalink
Fix media update (#1005)
Browse files Browse the repository at this point in the history
Co-authored-by: wlorenzetti <[email protected]>
  • Loading branch information
wlorenzetti and wlorenzetti authored Jan 7, 2025
1 parent 2c41309 commit 2cb2434
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions g3w-admin/core/utils/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """

Expand All @@ -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]:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2cb2434

Please sign in to comment.