Skip to content

Commit

Permalink
Fix max size in uploadhandler.py
Browse files Browse the repository at this point in the history
* Update uploadhandler.py

By default even the layer used the document upload size milit

* Update uploadhandler.py

* Update forms.py

* Fix broken upload tests

* Test fix replace layer

* Test fix replace layer

* Test fix replace layer

* Test fix LGTM issues

* Test fix LGTM issues
  • Loading branch information
mattiagiupponi authored May 5, 2022
1 parent d9e5933 commit df8ec3b
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
2 changes: 2 additions & 0 deletions geonode/layers/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import tempfile
from unittest.mock import patch
from django.conf import settings
from django.test import override_settings

import gisdata
from django.conf.urls import url
Expand Down Expand Up @@ -142,6 +143,7 @@ def test_layer_replace_should_raise_error_if_layer_does_not_exists(self):
self.assertDictEqual(expected, response.json())

@patch("geonode.layers.views.validate_input_source")
@override_settings(ASYNC_SIGNALS=False)
def test_layer_replace_should_work(self, _validate_input_source):

_validate_input_source.return_value = True
Expand Down
11 changes: 4 additions & 7 deletions geonode/layers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,9 +1208,9 @@ def layer_replace(request, layername, template='layers/layer_replace.html'):
if resource_is_valid:
# Create a new upload session
request.GET = {"layer_id": layer.id}
steps = [None, "check", "final"] if layer.is_vector() else [None, "final"]
steps = [None, "check", "final"] if layer.is_vector() and settings.ASYNC_SIGNALS else [None, "final"]
for _step in steps:
if _step != 'final':
if _step != 'final' or (_step == 'final' and not settings.ASYNC_SIGNALS):
response, cat, valid = UploadViewSet()._emulate_client_upload_step(
request,
_step
Expand All @@ -1220,11 +1220,8 @@ def layer_replace(request, layername, template='layers/layer_replace.html'):
else:
logger.error("starting final step for Replace Layer")
from geonode.upload.tasks import finalize_incomplete_session_uploads
if settings.ASYNC_SIGNALS:
logger.error("async starting")
finalize_incomplete_session_uploads.apply_async()
else:
finalize_incomplete_session_uploads.apply()
logger.error("async starting")
finalize_incomplete_session_uploads.apply_async()

set_geowebcache_invalidate_cache(layer.typename)

Expand Down
2 changes: 1 addition & 1 deletion geonode/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ def tearDown(self):
from django.conf import settings
if settings.OGC_SERVER['default'].get(
"GEOFENCE_SECURITY_ENABLED", False):
from geonode.security.utils import purge_geofence_all
from geonode.geoserver.security import purge_geofence_all
purge_geofence_all()
4 changes: 2 additions & 2 deletions geonode/upload/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def test_rest_uploads_with_size_limit(self, mocked_uploaded_file, mocked_validat

mocked_validation_error.side_effect = ValidationError(expected_error)
upload_size_limit_obj, created = UploadSizeLimit.objects.get_or_create(
slug="total_upload_size_sum",
slug="dataset_upload_size",
defaults={
"description": "The sum of sizes for the files of a dataset upload.",
"max_size": 1,
Expand Down Expand Up @@ -723,7 +723,7 @@ def test_rest_uploads_with_size_limit_before_upload(self, mocked_uploaded_file,

mocked_validation_error.side_effect = ValidationError(expected_error)
upload_size_limit_obj, created = UploadSizeLimit.objects.get_or_create(
slug="total_upload_size_sum",
slug="dataset_upload_size",
defaults={
"description": "The sum of sizes for the files of a dataset upload.",
"max_size": 1,
Expand Down
2 changes: 1 addition & 1 deletion geonode/upload/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def validate_files_sum_of_sizes(self, file_dict):

def _get_uploads_max_size(self):
try:
max_size_db_obj = UploadSizeLimit.objects.get(slug="total_upload_size_sum")
max_size_db_obj = UploadSizeLimit.objects.get(slug="dataset_upload_size")
except UploadSizeLimit.DoesNotExist:
max_size_db_obj = UploadSizeLimit.objects.create_default_limit()
return max_size_db_obj.max_size
Expand Down
5 changes: 3 additions & 2 deletions geonode/upload/uploadhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

class SizeRestrictedFileUploadHandler(FileUploadHandler):
"""
Upload handler that avoid streaming data when the file is bigged than a given size.
Upload handler exhaust the stream to avoit overload the memory when the file is bigger than the limit set.
The upload will be blocked afterwards by the Upload Form
It only applies for elegible urls set in ``elegible_url_names`` property.
"""

Expand All @@ -35,7 +36,7 @@ def handle_raw_input(self, input_data, META, content_length, boundary, encoding=

# If the post is too large, we create a empty UploadedFile, otherwise another handler will take care or it.
if self.is_view_elegible_for_size_restriction:
file_type = 'dataset_upload_size' if 'uploads/upload' in input_data.path else 'document_upload_size'
file_type = 'dataset_upload_size' if '/documents/upload' not in input_data.path else 'document_upload_size'
self.max_size_allowed = self._get_max_size(file_type)
self.activated = content_length > self.max_size_allowed
if self.activated:
Expand Down

0 comments on commit df8ec3b

Please sign in to comment.