fairdm.contrib.plugins.utils#

Helpers for plugin slugs, edit permission checks and address resolution.

Module Contents#

Functions#

slugify

Convert a class name or phrase to a URL-safe slug.

class_to_slug

Convert a class, an instance or a string to a slug. Prefer slugify in new code.

check_has_edit_permission

Check whether the user may edit the object.

sample_check_has_edit_permission

Allow editing a sample.

reverse

Resolve a plugin address for a record.

API#

slugify(text: str) → str[source]#

Convert a class name or phrase to a URL-safe slug.

Args: text: The class name or phrase.

Returns: The slug.

Example: >>> slugify(“URLTestPlugin”) ‘url-test-plugin’ >>> slugify(“My Plugin_Name”) ‘my-plugin-name’

class_to_slug(name: str | object | type) → str[source]#

Convert a class, an instance or a string to a slug. Prefer slugify in new code.

Args: name: A string, or an object whose __name__ is used.

Returns: The slug.

check_has_edit_permission(request, instance, **kwargs)[source]#

Check whether the user may edit the object.

Args: request: The current request. instance: The object being edited. **kwargs: Unused.

Returns: True for a superuser or the object itself, otherwise the user’s change permission on the object. None when there is no object.

sample_check_has_edit_permission(request, instance, **kwargs)[source]#

Allow editing a sample.

Args: request: The current request. instance: The sample. **kwargs: Unused.

Returns: Always True.

reverse(instance, view_name, *args, **kwargs)[source]#

Resolve a plugin address for a record.

Args: instance: The record. view_name: The plugin’s URL name. *args: Positional URL arguments. **kwargs: Keyword URL arguments. Those in the record’s declared addressing are filled in.

Returns: The URL.