swh.web.utils package#

Subpackages#

Submodules#

Module contents#

swh.web.utils.reverse(viewname: str, url_args: Dict[str, Any] | None = None, query_params: Mapping[str, str | None] | None = None, current_app: str | None = None, urlconf: str | None = None, request: HttpRequest | None = None) str[source]#

An override of django reverse function supporting query parameters.

Parameters:
  • viewname – the name of the django view from which to compute a url

  • url_args – dictionary of url arguments indexed by their names

  • query_params – dictionary of query parameters to append to the reversed url

  • current_app – the name of the django app tighten to the view

  • urlconf – url configuration module

  • request – build an absolute URI if provided

Returns:

the url of the requested view with processed arguments and query parameters

Return type:

str

swh.web.utils.datetime_to_utc(date)[source]#

Returns datetime in UTC without timezone info

Parameters:

date (datetime.datetime) – input datetime with timezone info

Returns:

datetime in UTC without timezone info

Return type:

datetime.datetime

swh.web.utils.parse_iso8601_date_to_utc(iso_date: str) datetime[source]#

Given an ISO 8601 datetime string, parse the result as UTC datetime.

Returns:

a timezone-aware datetime representing the parsed date

Raises:

swh.web.utils.exc.BadInputExc – provided date does not respect ISO 8601 format

Samples:
  • 2016-01-12

  • 2016-01-12T09:19:12+0100

  • 2007-01-14T20:34:22Z

swh.web.utils.shorten_path(path)[source]#

Shorten the given path: for each hash present, only return the first 8 characters followed by an ellipsis

swh.web.utils.format_utc_iso_date(iso_date, fmt='%d %B %Y, %H:%M:%S UTC')[source]#

Turns a string representation of an ISO 8601 datetime string to UTC and format it into a more human readable one.

For instance, from the following input string: ‘2017-05-04T13:27:13+02:00’ the following one is returned: ‘04 May 2017, 11:27 UTC’. Custom format string may also be provided as parameter

Parameters:
  • iso_date (str) – a string representation of an ISO 8601 date

  • fmt (str) – optional date formatting string

Returns:

a formatted string representation of the input iso date

Return type:

str

swh.web.utils.gen_path_info(path)[source]#

Function to generate path data navigation for use with a breadcrumb in the swh web ui.

For instance, from a path /folder1/folder2/folder3, it returns the following list:

[{'name': 'folder1', 'path': 'folder1'},
 {'name': 'folder2', 'path': 'folder1/folder2'},
 {'name': 'folder3', 'path': 'folder1/folder2/folder3'}]
Parameters:

path – a filesystem path

Returns:

a list of path data for navigation as illustrated above.

Return type:

list

swh.web.utils.parse_rst(text, report_level=2)[source]#

Parse a reStructuredText string with docutils.

Parameters:
  • text (str) – string with reStructuredText markups in it

  • report_level (int) – level of docutils report messages to print (1 info 2 warning 3 error 4 severe 5 none)

Returns:

a parsed docutils document

Return type:

docutils.nodes.document

swh.web.utils.get_client_ip(request)[source]#

Return the client IP address from an incoming HTTP request.

Parameters:

request (django.http.HttpRequest) – the incoming HTTP request

Returns:

The client IP address

Return type:

str

swh.web.utils.is_swh_web_development(request: HttpRequest) bool[source]#

Indicate if we are running a development version of swh-web.

swh.web.utils.is_swh_web_staging(request: HttpRequest) bool[source]#

Indicate if we are running a staging version of swh-web.

swh.web.utils.is_swh_web_production(request: HttpRequest) bool[source]#

Indicate if we are running the public production version of swh-web.

swh.web.utils.context_processor(request)[source]#

Django context processor used to inject variables in all swh-web templates.

swh.web.utils.resolve_branch_alias(snapshot: Dict[str, Any], branch: Dict[str, Any] | None) Dict[str, Any] | None[source]#

Resolve branch alias in snapshot content.

Parameters:
  • snapshot – a full snapshot content

  • branch – a branch alias contained in the snapshot

Returns:

The real snapshot branch that got aliased.

swh.web.utils.rst_to_html(rst: str, raw_enabled: bool = False) str[source]#

Convert reStructuredText document into HTML.

Parameters:

rst – A string containing a reStructuredText document

Returns:

Body content of the produced HTML conversion.

swh.web.utils.cache_set(cache_key: str, obj: ~typing.Any, timeout: int = <object object>, extra_encoders: ~typing.List[~typing.Tuple[type, str, ~typing.Callable]] | None = None) None[source]#

Set a value in django cache.

For optimizing cache size, the value to cache is serialized to binary using msgpack and then compressed with gzip.

Parameters:
  • cache_key – string key for the value to set in cache

  • obj – value to store in cache

  • timeout – the duration in seconds after which the cache expires

  • extra_encoders – optional encoders for serializing types that are not default supported by msgpack, see swh.core.api.serializers

swh.web.utils.cache_get(cache_key: str, extra_decoders: Dict[str, Callable] | None = None) Any | None[source]#

Get a value from the django cache.

For optimizing cache size, values to cache are serialized to binary using msgpack and then compressed with gzip.

Parameters:
  • cache_key – string key for the value to get from cache

  • extra_decoders – optional decoders for deserializing types that are not default supported by msgpack, see swh.core.api.serializers

Returns:

the cached value or None if it does not exist

swh.web.utils.django_cache(timeout: int = <object object>, catch_exception: bool = False, exception_return_value: ~typing.Any = None, invalidate_cache_pred: ~typing.Callable[[~typing.Any], bool] = <function <lambda>>, extra_encoders: ~typing.List[~typing.Tuple[type, str, ~typing.Callable]] | None = None, extra_decoders: ~typing.Dict[str, ~typing.Callable] | None = None)[source]#

Decorator to put the result of a function call in Django cache, subsequent calls will directly return the cached value.

For optimizing cache size, values to cache are serialized to binary using msgpack and then compressed with gzip.

Parameters:
  • timeout – The number of seconds value will be hold in cache

  • catch_exception – If True, any thrown exception by the decorated function will be caught and not reraised

  • exception_return_value – The value to return if previous parameter is set to True

  • invalidate_cache_pred – A predicate function enabling to invalidate the cache under certain conditions, decorated function will then be called again

  • extra_encoders – optional encoders for serializing types that are not default supported by msgpack, see swh.core.api.serializers

  • extra_decoders – optional decoders for deserializing types that are not default supported by msgpack, see swh.core.api.serializers

Returns:

The returned value of the decorated function for the specified parameters

swh.web.utils.get_deposits_list(username: str | None = None) List[Dict[str, Any]][source]#

Return the list of software deposits using swh-deposit API

swh.web.utils.origin_visit_types(use_cache: bool = True) List[str][source]#

Return the exhaustive list of visit types for origins ingested into the archive.

Parameters:

use_cache – if True, store visit types in django cache for 24 hours.

swh.web.utils.redirect_to_new_route(request, new_route, permanent=True)[source]#

Redirect a request to another route with url args and query parameters eg: /origin/<url:url-val>/log?path=test can be redirected as /log?url=<url-val>&path=test. This can be used to deprecate routes

swh.web.utils.demangle_url(url: str) str[source]#

Fix URL where the :// character sequence was mangled into :/ by HTTP clients