Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/browseurls.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from typing import List, Optional | |||||
from swh.web.common.urlsindex import UrlsIndex | from swh.web.common.urlsindex import UrlsIndex | ||||
class BrowseUrls(UrlsIndex): | class BrowseUrls(UrlsIndex): | ||||
""" | """ | ||||
Class to manage swh-web browse application urls. | Class to manage swh-web browse application urls. | ||||
""" | """ | ||||
scope = "browse" | scope = "browse" | ||||
def browse_route(*url_patterns, view_name=None, checksum_args=None): | def browse_route( | ||||
*url_patterns: str, | |||||
view_name: Optional[str] = None, | |||||
checksum_args: Optional[List[str]] = None, | |||||
): | |||||
""" | """ | ||||
Decorator to ease the registration of a swh-web browse endpoint | Decorator to ease the registration of a swh-web browse endpoint | ||||
Args: | Args: | ||||
url_patterns: list of url patterns used by Django to identify the | url_patterns: list of url patterns used by Django to identify the | ||||
browse routes | browse routes | ||||
view_name: the name of the Django view associated to the routes used | view_name: the name of the Django view associated to the routes used | ||||
to reverse the url | to reverse the url | ||||
""" | """ | ||||
url_patterns = ["^" + url_pattern + "$" for url_pattern in url_patterns] | url_patterns = tuple("^" + url_pattern + "$" for url_pattern in url_patterns) | ||||
view_name = view_name | view_name = view_name | ||||
def decorator(f): | def decorator(f): | ||||
# register the route and its view in the browse endpoints index | # register the route and its view in the browse endpoints index | ||||
for url_pattern in url_patterns: | for url_pattern in url_patterns: | ||||
BrowseUrls.add_url_pattern(url_pattern, f, view_name) | BrowseUrls.add_url_pattern(url_pattern, f, view_name) | ||||
if checksum_args: | if checksum_args: | ||||
BrowseUrls.add_redirect_for_checksum_args( | BrowseUrls.add_redirect_for_checksum_args( | ||||
view_name, url_patterns, checksum_args | view_name, url_patterns, checksum_args | ||||
) | ) | ||||
return f | return f | ||||
return decorator | return decorator |