core.utils

Module Contents

Functions

utcNow()

generateRandomString(length: int = 13) → str

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=254)

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

hmacSign(data: Any) → str

hmacVerify(data: Any, signature: str) → bool

downloadUrlFor(folder: str, fileName: str, derived: bool = False, expires: Union[datetime.timedelta, None] = timedelta(hours=1)) → str

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

seoUrlToEntry(module, entry=None, skelType=None, language=None)

seoUrlToFunction(module, function, render=None)

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

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

Attributes

currentRequest

currentRequestData

currentSession

currentLanguage

appVersion

versionHash

versionHash

projectBasePath

isLocalDevelopmentServer

core.utils.currentRequest
core.utils.currentRequestData
core.utils.currentSession
core.utils.currentLanguage
core.utils.appVersion
core.utils.versionHash
core.utils.versionHash
core.utils.projectBasePath
core.utils.isLocalDevelopmentServer
core.utils.utcNow()
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 (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.

Return type

dict | bool

Returns

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

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.

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: Any) str
core.utils.hmacVerify(data: Any, signature: str) bool
core.utils.downloadUrlFor(folder: str, fileName: str, derived: bool = False, expires: Union[datetime.timedelta, None] = timedelta(hours=1)) 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

Returns

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

core.utils.seoUrlToEntry(module, entry=None, skelType=None, language=None)
core.utils.seoUrlToFunction(module, function, render=None)
core.utils.normalizeKey(key: Union[None, viur.core.db.KeyClass]) Union[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.