diff --git a/swh/web/api/views/add_forge_now.py b/swh/web/api/views/add_forge_now.py --- a/swh/web/api/views/add_forge_now.py +++ b/swh/web/api/views/add_forge_now.py @@ -350,8 +350,8 @@ response["headers"]["link-prev"] = reverse( "api-1-add-forge-request-list", query_params={ - "page": page.previous_page_number(), - "per_page": per_page, + "page": str(page.previous_page_number()), + "per_page": str(per_page), }, request=request, ) @@ -359,7 +359,10 @@ if page.has_next(): response["headers"]["link-next"] = reverse( "api-1-add-forge-request-list", - query_params={"page": page.next_page_number(), "per_page": per_page}, + query_params={ + "page": str(page.next_page_number()), + "per_page": str(per_page), + }, request=request, ) diff --git a/swh/web/api/views/origin.py b/swh/web/api/views/origin.py --- a/swh/web/api/views/origin.py +++ b/swh/web/api/views/origin.py @@ -105,7 +105,7 @@ if next_page_token is not None: headers["link-next"] = reverse( "api-1-origins", - query_params={"page_token": next_page_token, "origin_count": limit}, + query_params={"page_token": next_page_token, "origin_count": str(limit)}, request=request, ) return {"results": origins, "headers": headers} diff --git a/swh/web/api/views/snapshot.py b/swh/web/api/views/snapshot.py --- a/swh/web/api/views/snapshot.py +++ b/swh/web/api/views/snapshot.py @@ -97,8 +97,8 @@ url_args={"snapshot_id": snapshot_id}, query_params={ "branches_from": results["next_branch"], - "branches_count": branches_count, - "target_types": target_types, + "branches_count": str(branches_count), + "target_types": ",".join(target_types) if target_types else None, }, request=request, ) diff --git a/swh/web/browse/identifiers.py b/swh/web/browse/identifiers.py --- a/swh/web/browse/identifiers.py +++ b/swh/web/browse/identifiers.py @@ -15,6 +15,6 @@ The url that points to it is :http:get:`/(swhid)/`. """ - swhid_resolved = resolve_swhid(swhid, query_params=request.GET) + swhid_resolved = resolve_swhid(swhid, query_params=request.GET.dict()) assert swhid_resolved["browse_url"] return redirect(swhid_resolved["browse_url"]) diff --git a/swh/web/browse/views/revision.py b/swh/web/browse/views/revision.py --- a/swh/web/browse/views/revision.py +++ b/swh/web/browse/views/revision.py @@ -256,8 +256,8 @@ "browse-revision-log", url_args={"sha1_git": sha1_git}, query_params={ - "per_page": per_page, - "offset": offset + per_page, + "per_page": str(per_page), + "offset": str(offset + per_page), "revs_ordering": revs_ordering or None, }, ) @@ -268,8 +268,8 @@ "browse-revision-log", url_args={"sha1_git": sha1_git}, query_params={ - "per_page": per_page, - "offset": offset - per_page, + "per_page": str(per_page), + "offset": str(offset - per_page), "revs_ordering": revs_ordering or None, }, ) diff --git a/swh/web/browse/views/snapshot.py b/swh/web/browse/views/snapshot.py --- a/swh/web/browse/views/snapshot.py +++ b/swh/web/browse/views/snapshot.py @@ -44,7 +44,7 @@ browse_snapshot_url = reverse( "browse-snapshot-directory", url_args={"snapshot_id": snapshot_id}, - query_params=request.GET, + query_params=request.GET.dict(), ) return redirect(browse_snapshot_url) 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 @@ -8,15 +8,12 @@ from typing_extensions import TypedDict -from django.http import QueryDict - from swh.model.exceptions import ValidationError from swh.model.hashutil import hash_to_bytes, hash_to_hex from swh.model.swhids import ObjectType, QualifiedSWHID from swh.web.common import archive from swh.web.common.exc import BadInputExc from swh.web.common.typing import ( - QueryParameters, SnapshotContext, SWHIDContext, SWHIDInfo, @@ -87,7 +84,7 @@ def resolve_swhid( - swhid: str, query_params: Optional[QueryParameters] = None + swhid: str, query_params: Optional[Mapping[str, str]] = None ) -> ResolvedSWHID: """ Try to resolve a SoftWare Heritage persistent IDentifier into an url for @@ -109,13 +106,10 @@ object_id = swhid_parsed.object_id browse_url = None url_args = {} - query_dict = QueryDict("", mutable=True) fragment = "" process_lines = object_type == ObjectType.CONTENT - if query_params and len(query_params) > 0: - for k in sorted(query_params.keys()): - query_dict[k] = str(query_params[k]) + query_dict: Dict[str, str] = dict(query_params or {}) if swhid_parsed.origin: origin_url = unquote(swhid_parsed.origin) @@ -148,13 +142,13 @@ # when no origin or revision context, content objects need to have # their path prefixed by root directory id for breadcrumbs display query_dict["path"] = hash_to_hex(directory) + query_dict["path"] - else: + elif query_dict["path"] is not None: # remove leading slash from SWHID content path - query_dict["path"] = str(query_dict["path"]).lstrip("/") - elif object_type == ObjectType.DIRECTORY: + query_dict["path"] = query_dict["path"].lstrip("/") + elif object_type == ObjectType.DIRECTORY and query_dict["path"] is not None: object_id = directory # remove leading and trailing slashes from SWHID directory path - query_dict["path"] = str(query_dict["path"]).strip("/") + query_dict["path"] = query_dict["path"].strip("/") # snapshot context if swhid_parsed.visit: diff --git a/swh/web/common/typing.py b/swh/web/common/typing.py --- a/swh/web/common/typing.py +++ b/swh/web/common/typing.py @@ -3,17 +3,13 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -from typing import Any, Dict, List, Optional, TypeVar, Union +from typing import Any, Dict, List, Optional, TypeVar from typing_extensions import TypedDict -from django.http import QueryDict - from swh.core.api.classes import PagedResult as CorePagedResult from swh.model.swhids import ObjectType -QueryParameters = Union[Dict[str, Any], QueryDict] - class OriginInfo(TypedDict): url: str @@ -103,7 +99,7 @@ """optional origin info associated to the snapshot""" origin_visits_url: Optional[str] """optional origin visits URL""" - query_params: QueryParameters + query_params: Dict[str, Optional[str]] """common query parameters when browsing snapshot content""" release: Optional[str] """optional release name set when browsing snapshot in that scope""" 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 @@ -7,7 +7,7 @@ import functools import os import re -from typing import Any, Callable, Dict, List, Optional +from typing import Any, Callable, Dict, List, Mapping, Optional import urllib.parse from bs4 import BeautifulSoup @@ -34,7 +34,6 @@ MAILMAP_ADMIN_PERMISSION, ) from swh.web.common.exc import BadInputExc, sentry_capture_exception -from swh.web.common.typing import QueryParameters from swh.web.config import SWH_WEB_SERVER_NAME, get_config, search SWH_WEB_METRICS_REGISTRY = CollectorRegistry(auto_describe=True) @@ -67,7 +66,7 @@ def reverse( viewname: str, url_args: Optional[Dict[str, Any]] = None, - query_params: Optional[QueryParameters] = None, + query_params: Optional[Mapping[str, Optional[str]]] = None, current_app: Optional[str] = None, urlconf: Optional[str] = None, request: Optional[HttpRequest] = None, @@ -95,13 +94,13 @@ viewname, urlconf=urlconf, kwargs=url_args, current_app=current_app ) + params: Dict[str, str] = {} if query_params: - query_params = {k: v for k, v in query_params.items() if v is not None} + params = {k: v for k, v in query_params.items() if v is not None} - if query_params and len(query_params) > 0: + if params: query_dict = QueryDict("", mutable=True) - for k in sorted(query_params.keys()): - query_dict[k] = str(query_params[k]) + query_dict.update(dict(sorted(params.items()))) url += "?" + query_dict.urlencode(safe="/;:") if request is not None: diff --git a/swh/web/misc/iframe.py b/swh/web/misc/iframe.py --- a/swh/web/misc/iframe.py +++ b/swh/web/misc/iframe.py @@ -139,7 +139,7 @@ "swhid-iframe", url_args={"swhid": str(root_dir_swhid)}, query_params={ - "focus_swhid": focus_swhid + "focus_swhid": str(focus_swhid) if focus_swhid != root_dir_swhid else None }, @@ -165,7 +165,7 @@ "url": reverse( "swhid-iframe", url_args={"swhid": str(dir_swhid)}, - query_params={"focus_swhid": focus_swhid}, + query_params={"focus_swhid": str(focus_swhid)}, ), } )