core.utils

Module Contents

Functions

utcNow(→ datetime.datetime)

generateRandomString(→ str)

Return a string containing random characters of given length.

getCurrentUser(→ Optional[SkeletonInstance])

Retrieve current user, if logged in.

markFileForDeletion(→ None)

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

escapeString(→ str)

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

hmacSign(→ str)

hmacVerify(→ bool)

sanitizeFileName(→ str)

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

downloadUrlFor(, downloadFileName)

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

srcSetFor(→ str)

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

seoUrlToEntry(→ str)

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

seoUrlToFunction(→ str)

normalizeKey(→ Union[None, viur.core.db.KeyClass])

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

__getattr__(attr)

Attributes

currentRequest

currentRequestData

currentSession

currentLanguage

projectBasePath

coreBasePath

__utils_conf_replacement

core.utils.currentRequest
core.utils.currentRequestData
core.utils.currentSession
core.utils.currentLanguage
core.utils.projectBasePath
core.utils.coreBasePath
core.utils.utcNow() datetime.datetime
core.utils.generateRandomString(length: int = 13) str

Return a string containing random characters of given length. Its safe to use this string in URLs or HTML.

Parameters:

length – The desired length of the generated string.

Returns:

A string with random characters of the given length.

core.utils.getCurrentUser() SkeletonInstance | None

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.

core.utils.markFileForDeletion(dlkey: str) None

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 – Unique download-key of the file that shall be marked for deletion.

core.utils.escapeString(val: str, maxLength: int = 254) str

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

Parameters:
  • val – The value to be escaped.

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

Returns:

The quoted string.

core.utils.hmacSign(data: Any) str
core.utils.hmacVerify(data: Any, signature: str) bool
core.utils.sanitizeFileName(fileName: str) str

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

core.utils.downloadUrlFor(folder: str, fileName: str, derived: bool = False, expires: datetime.timedelta | None = timedelta(hours=1), downloadFileName: str | None = None) str

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

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

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

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

  • expires – 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 – 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/…)

core.utils.srcSetFor(fileObj: dict, expires: int | None, width: int | None = None, height: int | None = None) str

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. :param fileObj: The file-bone (or if multiple=True a single value from it) to generate the srcset for :param expires: None if the file is supposed to be public (which causes it to be cached on the google ede

caches), otherwise it’s lifetime in seconds

Parameters:
  • width – A list of widths that should be included in the srcset. If a given width is not available, it will be skipped.

  • height – 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

core.utils.seoUrlToEntry(module: str, entry: SkeletonInstance | None = None, skelType: str | None = None, language: str | None = None) str

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

Parameters:
  • module – The module name.

  • entry – A skeleton instance or None, to get the path to the module.

  • skelType – # FIXME: Not used

  • language – For which language. If None, the language of the current request is used.

Returns:

The path (with a leading /).

core.utils.seoUrlToFunction(module: str, function: str, render: str | None = None) str
core.utils.normalizeKey(key: None | viur.core.db.KeyClass) None | viur.core.db.KeyClass

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

Parameters:

key – Key to be normalized.

Returns:

Normalized key in string representation.

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