Skip to content

Commit

Permalink
QF-2711 Fine tune error handling in the files API endpoint (#684)
Browse files Browse the repository at this point in the history
* Rebasing on main
* dropping libqfieldsync changes
* Update docker-app/qfieldcloud/core/exceptions.py

Co-authored-by: Ivan Ivanov <[email protected]>

* renamed exception
* improved clarity around ensuring the existence of bucket resource
* new test
* doctrings; assertions
* cleanup
* removed argument
* leftover

Co-authored-by: Ivan Ivanov <[email protected]>
  • Loading branch information
why-not-try-calmer and suricactus authored Jun 26, 2023
1 parent 9da861c commit 503f0e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
17 changes: 13 additions & 4 deletions docker-app/qfieldcloud/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,22 @@ def get_s3_session() -> boto3.Session:


def get_s3_bucket() -> mypy_boto3_s3.service_resource.Bucket:
"""Get a new S3 Bucket instance using Django settings"""
"""
Get a new S3 Bucket instance using Django settings.
"""

session = get_s3_session()
bucket_name = settings.STORAGE_BUCKET_NAME

assert bucket_name, "Expected `bucket_name` to be non-empty string!"

# Get the bucket objects
session = get_s3_session()
s3 = session.resource("s3", endpoint_url=settings.STORAGE_ENDPOINT_URL)
return s3.Bucket(settings.STORAGE_BUCKET_NAME)

# Ensure the bucket exists
s3.meta.client.head_bucket(Bucket=bucket_name)

# Get the bucket resource
return s3.Bucket(bucket_name)


def get_s3_client() -> mypy_boto3_s3.Client:
Expand Down
17 changes: 4 additions & 13 deletions docker-app/qfieldcloud/core/views/files_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
purge_old_file_versions,
)
from rest_framework import permissions, status, views
from rest_framework.exceptions import NotFound, server_error
from rest_framework.exceptions import NotFound
from rest_framework.parsers import MultiPartParser
from rest_framework.request import Request
from rest_framework.response import Response

logger = logging.getLogger(__name__)
Expand All @@ -42,23 +43,13 @@ class ListFilesView(views.APIView):

permission_classes = [permissions.IsAuthenticated, ListFilesViewPermissions]

def get(self, request, projectid):
def get(self, request: Request, projectid: str) -> Response:
try:
project = Project.objects.get(id=projectid)
bucket = utils.get_s3_bucket()
if not bucket.creation_date:
# Let DRF return 500 when bucket does not exist, since it needed but
# not what the client tried to get
return server_error(
request=request,
reason=f"Unable to fetch needed resource for {projectid}",
)
except ObjectDoesNotExist:
# map failure to get from db into failure to GET from API
raise NotFound(detail=projectid)
except Exception as unknown_reason:
return server_error(request=request, reason=str(unknown_reason))

bucket = utils.get_s3_bucket()
prefix = f"projects/{projectid}/files/"

files = {}
Expand Down

0 comments on commit 503f0e7

Please sign in to comment.