diff --git a/swh/web/common/identifiers.py b/swh/web/common/identifiers.py --- a/swh/web/common/identifiers.py +++ b/swh/web/common/identifiers.py @@ -115,7 +115,7 @@ if query_params and len(query_params) > 0: for k in sorted(query_params.keys()): - query_dict[k] = query_params[k] + query_dict[k] = str(query_params[k]) if swhid_parsed.origin: origin_url = unquote(swhid_parsed.origin) @@ -150,14 +150,11 @@ query_dict["path"] = hash_to_hex(directory) + query_dict["path"] else: # remove leading slash from SWHID content path - query_dict["path"] = query_dict["path"][1:] + query_dict["path"] = str(query_dict["path"]).lstrip("/") elif object_type == ObjectType.DIRECTORY: object_id = directory # remove leading and trailing slashes from SWHID directory path - if query_dict["path"].endswith("/"): - query_dict["path"] = query_dict["path"][1:-1] - else: - query_dict["path"] = query_dict["path"][1:] + query_dict["path"] = str(query_dict["path"]).strip("/") # snapshot context if swhid_parsed.visit: diff --git a/swh/web/common/utils.py b/swh/web/common/utils.py --- a/swh/web/common/utils.py +++ b/swh/web/common/utils.py @@ -101,7 +101,7 @@ if query_params and len(query_params) > 0: query_dict = QueryDict("", mutable=True) for k in sorted(query_params.keys()): - query_dict[k] = query_params[k] + query_dict[k] = str(query_params[k]) url += "?" + query_dict.urlencode(safe="/;:") if request is not None: diff --git a/swh/web/tests/conftest.py b/swh/web/tests/conftest.py --- a/swh/web/tests/conftest.py +++ b/swh/web/tests/conftest.py @@ -21,7 +21,7 @@ from django.contrib.auth.models import User from django.core.cache import cache -from django.test.utils import setup_databases # type: ignore +from django.test.utils import setup_databases from rest_framework.test import APIClient, APIRequestFactory from swh.model.hashutil import ( diff --git a/swh/web/tests/utils.py b/swh/web/tests/utils.py --- a/swh/web/tests/utils.py +++ b/swh/web/tests/utils.py @@ -7,7 +7,7 @@ from django.contrib.auth.models import Permission from django.contrib.contenttypes.models import ContentType -from django.http import HttpResponse, StreamingHttpResponse +from django.http.response import HttpResponse, HttpResponseBase, StreamingHttpResponse from django.test.client import Client from rest_framework.response import Response from rest_framework.test import APIClient @@ -16,8 +16,8 @@ def _assert_http_response( - response: HttpResponse, status_code: int, content_type: str -) -> HttpResponse: + response: HttpResponseBase, status_code: int, content_type: str +) -> HttpResponseBase: if isinstance(response, Response): drf_response = cast(Response, response) @@ -28,7 +28,7 @@ ) elif isinstance(response, StreamingHttpResponse): error_context = getattr(response, "traceback", response.streaming_content) - else: + elif isinstance(response, HttpResponse): error_context = getattr(response, "traceback", response.content) assert response.status_code == status_code, error_context @@ -44,7 +44,7 @@ content_type: str = "*/*", http_origin: Optional[str] = None, server_name: Optional[str] = None, -) -> HttpResponse: +) -> HttpResponseBase: """Helper function to check HTTP response for a GET request. Args: @@ -77,7 +77,7 @@ request_content_type="application/json", data: Optional[Dict[str, Any]] = None, http_origin: Optional[str] = None, -) -> HttpResponse: +) -> HttpResponseBase: """Helper function to check HTTP response for a POST request. Args: @@ -140,7 +140,7 @@ status_code: int, content_type: str = "*/*", data: Optional[Dict[str, Any]] = None, -) -> HttpResponse: +) -> HttpResponseBase: """Helper function to check Web API response for a POST request for all accepted content types. @@ -201,7 +201,7 @@ template_used: Optional[str] = None, http_origin: Optional[str] = None, server_name: Optional[str] = None, -) -> HttpResponse: +) -> HttpResponseBase: """Helper function to check HTML responses for a GET request. Args: