utils package

Submodules

utils.authentication module

Module containing a custom OAuth2 authentication class.

class utils.authentication.GPFOAuth2Authentication[source]

Bases: OAuth2Authentication

Provide custom OAuth2 authentication.

This authentication class that will return a 401 error in the case that an invalid OAuth token has been provided. We need this behaviour as we have routes which will allow unauthenticated requests, which in turn prevents distinguishing requests with expired tokens from those that bear no authentication headers.

authenticate(request: HttpRequest) tuple | None[source]

Returns two-tuple of (user, token) if authentication succeeds, or None otherwise.

class utils.authentication.ReadOnlyDBAuthentication[source]

Bases: BaseAuthentication

Authentication class intended to be used as a safeguard when we want the database to be read-only.

This is designed to be used when we want to migrate the production instance soon, and want to ensure that the database state is frozen, so that no new data can be missed during the migration.

authenticate(_request: Request) None[source]

Authenticate the request.

class utils.authentication.SessionAuthenticationWithUnauthenticatedCSRF[source]

Bases: SessionAuthentication

Session authentication with unauthenticated CSRF.

authenticate(request: Request) tuple | None[source]

Return the currently logged-in user or None otherwise.

class utils.authentication.SessionAuthenticationWithoutCSRF[source]

Bases: SessionAuthentication

enforce_csrf(request: Request) None[source]

Enforce CSRF validation for session based authentication.

Middleware patch to allow storing OAuth tokens as cookies.

utils.cached_response module

In-process memoization of pre-rendered JSON responses.

GET endpoints that serialize large, instance-static payloads (e.g. the genomic scores registry with all of its histograms) are expensive to re-render on every request even though the result only changes when the GPF instance is reloaded.

This module memoizes the rendered JSON bytes keyed by the instance id and the instance-load timestamp, so the serialization runs once per instance load. The helper is designed to compose under the existing etag(get_instance_timestamp_etag) decorator: that decorator handles conditional 304 short-circuiting before the view body runs, so the build function here only ever executes for a 200.

utils.cached_response.cached_json_response(instance_id: str, build: Callable[[], Any], *extra: Any) HttpResponse[source]

Return a cached application/json response.

The rendered body is memoized in-process keyed by (instance_id, get_instance_timestamp(), extra). Including instance_id prevents cross-fixture leakage in tests; extra carries any view-varying parameter (e.g. an optional path segment).

The response carries Cache-Control: public, max-age=3600.

utils.datasets module

utils.datasets.find_dataset_id_in_dict(dictionary: dict) str | None[source]

Find dataset_id in a dictionary.

utils.datasets.find_dataset_id_in_request(request: Any) str | None[source]

Find dataset_id in a DRF request.

utils.email_regex module

utils.email_regex.is_email_valid(email: str) bool[source]

utils.expand_gene_set module

utils.expand_gene_set.expand_gene_set(data: dict) dict[source]

Expand gene set to list of gene symbols.

utils.expand_gene_set.expand_gene_syms(data: dict) dict[source]

Expand gene set symbols.

utils.gene_sets module

utils.gene_sets.get_denovo_gene_set_spec(gene_sets_types: list[Any]) dict[str, Any][source]

Convert typescript style gene sets types into python dict.

utils.logger module

utils.logger.log_filter(request: Request | WSGIRequest, message: str, *args: Any) str[source]

Filter request info for logging.

utils.logger.request_logging(logger: Logger) Callable[[F], F][source]

Automatically log request info on views.

utils.logger.request_logging_function_view(logger: Logger) Callable[[F], F][source]

Automatically log request info on view functions.

utils.pagination module

class utils.pagination.WdaePageNumberPagination[source]

Bases: PageNumberPagination

Custom pagination class that handles empty pages.

get_paginated_response(data: list[Any]) Response[source]
page_size_query_param = 'page_size'
paginate_queryset(queryset: Any, request: Request, view: APIView | None = None) list[Any] | None[source]

Paginate and handle empty pages by returning 204.

utils.password_requirements module

utils.password_requirements.is_password_valid(password: str) bool[source]

Validate a password.

A password is considered valid if it is at least ten symbols long and is not a commonly used (blacklisted) password.

utils.query_params module

utils.query_params.parse_query_params(data: dict) dict[source]

utils.streaming_response_util module

utils.streaming_response_util.convert(obj: Any) int | float[source]
utils.streaming_response_util.iterator_to_json(variants: Generator[list[Any] | None, None, None]) Generator[str, None, None][source]

Convert an iterator of dictionaries to a JSON array string generator.

utils.testing module

utils.testing.setup_t4c8_grr(root_path: Path) GenomicResourceRepo[source]
utils.testing.setup_t4c8_instance(root_path: Path) GPFInstance[source]
utils.testing.setup_wgpf_instance(root_path: Path) WGPFInstance[source]

Module contents