Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/utils.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2021 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 datetime import datetime, timezone | from datetime import datetime, timezone | ||||
import os | import os | ||||
import re | import re | ||||
from typing import Any, Dict, Optional | from typing import Any, Dict, Optional | ||||
from bs4 import BeautifulSoup | from bs4 import BeautifulSoup | ||||
from docutils.core import publish_parts | from docutils.core import publish_parts | ||||
import docutils.parsers.rst | import docutils.parsers.rst | ||||
import docutils.utils | import docutils.utils | ||||
from docutils.writers.html5_polyglot import HTMLTranslator, Writer | from docutils.writers.html5_polyglot import HTMLTranslator, Writer | ||||
from iso8601 import ParseError, parse_date | from iso8601 import ParseError, parse_date | ||||
from prometheus_client.registry import CollectorRegistry | from prometheus_client.registry import CollectorRegistry | ||||
from django.http import HttpRequest, QueryDict | from django.http import HttpRequest, QueryDict | ||||
from django.urls import reverse as django_reverse | from django.urls import reverse as django_reverse | ||||
from rest_framework.authentication import SessionAuthentication | |||||
from swh.web.common.exc import BadInputExc | from swh.web.common.exc import BadInputExc | ||||
from swh.web.common.typing import QueryParameters | from swh.web.common.typing import QueryParameters | ||||
from swh.web.config import ORIGIN_VISIT_TYPES, get_config | from swh.web.config import ORIGIN_VISIT_TYPES, get_config | ||||
SWH_WEB_METRICS_REGISTRY = CollectorRegistry(auto_describe=True) | SWH_WEB_METRICS_REGISTRY = CollectorRegistry(auto_describe=True) | ||||
swh_object_icons = { | swh_object_icons = { | ||||
▲ Show 20 Lines • Show All 242 Lines • ▼ Show 20 Lines | return { | ||||
server_name in site_base_url | server_name in site_base_url | ||||
for server_name in config["staging_server_names"] | for server_name in config["staging_server_names"] | ||||
] | ] | ||||
), | ), | ||||
"visit_types": ORIGIN_VISIT_TYPES, | "visit_types": ORIGIN_VISIT_TYPES, | ||||
} | } | ||||
class EnforceCSRFAuthentication(SessionAuthentication): | |||||
""" | |||||
Helper class to enforce CSRF validation on a DRF view | |||||
when a user is not authenticated. | |||||
""" | |||||
def authenticate(self, request): | |||||
user = getattr(request._request, "user", None) | |||||
self.enforce_csrf(request) | |||||
return (user, None) | |||||
def resolve_branch_alias( | def resolve_branch_alias( | ||||
snapshot: Dict[str, Any], branch: Optional[Dict[str, Any]] | snapshot: Dict[str, Any], branch: Optional[Dict[str, Any]] | ||||
) -> Optional[Dict[str, Any]]: | ) -> Optional[Dict[str, Any]]: | ||||
""" | """ | ||||
Resolve branch alias in snapshot content. | Resolve branch alias in snapshot content. | ||||
Args: | Args: | ||||
snapshot: a full snapshot content | snapshot: a full snapshot content | ||||
▲ Show 20 Lines • Show All 65 Lines • Show Last 20 Lines |