Pyramid views

exception sol.views.ExcessiveOffset
exception sol.views.UnacceptableOffset
class sol.views.LoggerAdapter(logger, extra=None, merge_extra=False)

Add username and remote IP to the logged message

process(msg: str, kwargs)

Process the logging message and keyword arguments passed in to a logging call to insert contextual information. You can either manipulate the message itself, the keyword args or both. Return the message and kwargs modified (or not) to suit your needs.

Normally, you'll only need to override this one method in a LoggerAdapter subclass for your specific needs.

sol.views.compute_elapsed_time(since: int | None, max_minutes: int) → int | None

Compute elapsed milliseconds since since timestamp, up to max_minutes.

sol.views.get_request_logger(request, logger)

Get a specialized logger for a Pyramid request

sol.views.json_decode(*args, **kwargs)

Custom JSON decoder.

sol.views.json_encode(*args, **kwargs)

Custom JSON encoder.

sol.views.json_script_encode(value: Any) → str

Encode value as JSON that can be embedded verbatim in a <script> element.

The content of a <script> element is not HTML-escaped: the browser scans it as raw text, looking only for the sequence that ends it. So any string coming from the database (a tourney description, a club name…) could break out of the element and inject markup: a </script> closes the element early, and what follows is parsed as HTML, other <script> elements included; a <!-- followed by <script puts the parser in a state where the element does not end where expected.

Writing <, > and & as JSON unicode escapes makes those sequences impossible, whatever the data. The result is still valid JSON and JSON.parse() returns the original strings: no data is altered, as stripping tags would do.

This is what Django's json_script and Jinja's tojson filter do too:

>>> print(json_script_encode({'name': '</script>'}))
{"name":"\u003C/script\u003E"}
sol.views.only_for_admins(f)

Ensure only admins can perform the operation.

sol.views.unauthorized_for_guest(f)

Prevent guest users to perform the operation.