swh.web.common.utils module

swh.web.common.utils.reverse(viewname: str, url_args: Optional[Dict[str, Any]] = None, query_params: Optional[Union[Dict[str, Any], django.http.request.QueryDict]] = None, current_app: Optional[str] = None, urlconf: Optional[str] = None, request: Optional[django.http.request.HttpRequest] = 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.common.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.common.utils.parse_timestamp(timestamp)[source]

Given a time or timestamp (as string), parse the result as UTC datetime.

Returns

a timezone-aware datetime representing the

parsed value or None if the parsing fails.

Return type

datetime.datetime

Samples:
  • 2016-01-12

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

  • Today is January 1, 2047 at 8:21:00AM

  • 1452591542

swh.web.common.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.common.utils.format_utc_iso_date(iso_date, fmt='%d %B %Y, %H:%M UTC')[source]

Turns a string representation of an ISO 8601 date 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.common.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.common.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.common.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.common.utils.context_processor(request)[source]

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

class swh.web.common.utils.EnforceCSRFAuthentication[source]

Bases: rest_framework.authentication.SessionAuthentication

Helper class to enforce CSRF validation on a DRF view when a user is not authenticated.

authenticate(request)[source]

Returns a User if the request session currently has a logged in user. Otherwise returns None.

swh.web.common.utils.resolve_branch_alias(snapshot: Dict[str, Any], branch: Optional[Dict[str, Any]]) → Optional[Dict[str, Any]][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.common.utils.rst_to_html(rst: str) → 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.common.utils.prettify_html(html: str) → str[source]

Prettify an HTML document.

Parameters

html – Input HTML document

Returns

The prettified HTML document