core.utils

Module Contents

Functions

utcNow()

generateRandomString([length])

Return a string containing random characters of given length.

getCurrentUser()

Retrieve current user, if logged in.

markFileForDeletion(dlkey)

Adds a marker to the data store that the file specified as dlkey can be deleted.

escapeString(val[, maxLength])

Quotes several characters and removes "\n" and "\0" to prevent XSS injection.

hmacSign(data)

hmacVerify(data, signature)

sanitizeFileName(fileName)

Sanitize the filename so it can be safely downloaded or be embedded into html

downloadUrlFor(folder, fileName[, derived, expires, ...])

Utility function that creates a signed download-url for the given folder/filename combination

srcSetFor(fileObj, expires[, width, height])

Generates a string suitable for use as the srcset tag in html. This functionality provides the browser

seoUrlToEntry(module[, entry, skelType, language])

Return the seo-url to a skeleton instance or the module.

seoUrlToFunction(module, function[, render])

normalizeKey(key)

Normalizes a datastore key (replacing _application with the current one)

parse_bool(value[, truthy_values])

Parse a value into a boolean based on accepted truthy values.

__getattr__(attr)

Attributes

__utils_conf_replacement

__utils_current_replacement

core.utils.utcNow()
Return type:

datetime.datetime

core.utils.generateRandomString(length=13)

Return a string containing random characters of given length. It’s safe to use this string in URLs or HTML. Because we use the secrets module it could be used for security purposes as well

Parameters:

length (int) – The desired length of the generated string.

Returns:

A string with random characters of the given length.

Return type:

str

core.utils.getCurrentUser()

Retrieve current user, if logged in. If a user is logged in, this function returns a dict containing user data. If no user is logged in, the function returns None.

Returns:

A SkeletonInstance containing information about the logged-in user, None if no user is logged in.

Return type:

Optional[SkeletonInstance]

core.utils.markFileForDeletion(dlkey)

Adds a marker to the data store that the file specified as dlkey can be deleted.

Once the mark has been set, the data store is checked four times (default: every 4 hours) if the file is in use somewhere. If it is still in use, the mark goes away, otherwise the mark and the file are removed from the datastore. These delayed checks are necessary due to database inconsistency.

Parameters:

dlkey (str) – Unique download-key of the file that shall be marked for deletion.

Return type:

None

core.utils.escapeString(val, maxLength=254)

Quotes several characters and removes “\n” and “\0” to prevent XSS injection.

Parameters:
  • val (str) – The value to be escaped.

  • maxLength (int) – Cut-off after maxLength characters. A value of 0 means “unlimited”.

Returns:

The quoted string.

Return type:

str

core.utils.hmacSign(data)
Parameters:

data (Any) –

Return type:

str

core.utils.hmacVerify(data, signature)
Parameters:
  • data (Any) –

  • signature (str) –

Return type:

bool

core.utils.sanitizeFileName(fileName)

Sanitize the filename so it can be safely downloaded or be embedded into html

Parameters:

fileName (str) –

Return type:

str

core.utils.downloadUrlFor(folder, fileName, derived=False, expires=timedelta(hours=1), downloadFileName=None)

Utility function that creates a signed download-url for the given folder/filename combination

Parameters:
  • folder (str) – The GCS-Folder (= the download-key) for that file

  • fileName (str) – The name of that file. Either the original filename as uploaded or the name of a dervived file

  • derived (bool) – True, if it points to a derived file, False if it points to the original uploaded file

  • expires (Union[datetime.timedelta, None]) – None if the file is supposed to be public (which causes it to be cached on the google ede caches), otherwise a timedelta of how long that link should be valid

  • downloadFileName (Optional[str]) – If set, we’ll force to browser to download this blob with the given filename

Returns:

THe signed download-url relative to the current domain (eg /download/…)

Return type:

str

core.utils.srcSetFor(fileObj, expires, width=None, height=None)

Generates a string suitable for use as the srcset tag in html. This functionality provides the browser with a list of images in different sizes and allows it to choose the smallest file that will fill it’s viewport without upscaling.

Parameters:
  • fileObj (dict) – The file-bone (or if multiple=True a single value from it) to generate the srcset.

  • expires (Optional[int]) – None if the file is supposed to be public (which causes it to be cached on the google edecaches), otherwise it’s lifetime in seconds

  • width (Optional[int]) – A list of widths that should be included in the srcset. If a given width is not available, it will be skipped.

  • height (Optional[int]) – A list of heights that should be included in the srcset. If a given height is not available, it will be skipped.

Returns:

The srctag generated or an empty string if a invalid file object was supplied

Return type:

str

core.utils.seoUrlToEntry(module, entry=None, skelType=None, language=None)

Return the seo-url to a skeleton instance or the module.

Parameters:
  • module (str) – The module name.

  • entry (Optional[SkeletonInstance]) – A skeleton instance or None, to get the path to the module.

  • skelType (Optional[str]) – # FIXME: Not used

  • language (Optional[str]) – For which language. If None, the language of the current request is used.

Returns:

The path (with a leading /).

Return type:

str

core.utils.seoUrlToFunction(module, function, render=None)
Parameters:
  • module (str) –

  • function (str) –

  • render (Optional[str]) –

Return type:

str

core.utils.normalizeKey(key)

Normalizes a datastore key (replacing _application with the current one)

Parameters:

key (Union[None, viur.core.db.KeyClass]) – Key to be normalized.

Returns:

Normalized key in string representation.

Return type:

Union[None, viur.core.db.KeyClass]

core.utils.parse_bool(value, truthy_values=('true', 'yes', '1'))

Parse a value into a boolean based on accepted truthy values.

This method takes a value, converts it to a lowercase string, removes whitespace, and checks if it matches any of the provided truthy values. :param value: The value to be parsed into a boolean. :param truthy_values: An iterable of strings representing truthy values.

Default is (“true”, “yes”, “1”).

Returns:

True if the value matches any of the truthy values, False otherwise.

Parameters:
  • value (Any) –

  • truthy_values (Iterable) –

Return type:

bool

core.utils.__utils_conf_replacement
core.utils.__utils_current_replacement
core.utils.__getattr__(attr)