home / docs / sections

sections: plugin_hooks:plugin-register-actions

This data as json

id page ref title content breadcrumbs references
plugin_hooks:plugin-register-actions plugin_hooks plugin-register-actions register_actions(datasette) If your plugin needs to register actions that can be checked with Datasette's new resource-based permission system, return a list of those actions from this hook. Actions define what operations can be performed on resources (like viewing a table, executing SQL, or custom plugin actions). from datasette import hookimpl from datasette.permissions import Action, Resource class DocumentCollectionResource(Resource): """A collection of documents.""" name = "document-collection" parent_name = None def __init__(self, collection: str): super().__init__(parent=collection, child=None) @classmethod def resources_sql(cls) -> str: return """ SELECT collection_name AS parent, NULL AS child FROM document_collections """ class DocumentResource(Resource): """A document in a collection.""" name = "document" parent_name = "document-collection" def __init__(self, collection: str, document: str): super().__init__(parent=collection, child=document) @classmethod def resources_sql(cls) -> str: return """ SELECT collection_name AS parent, document_id AS child FROM documents """ @hookimpl def register_actions(datasette): return [ Action( name="list-documents", abbr="ld", description="List documents in a collection", resource_class=DocumentCollectionResource, ), Action( name="view-document", abbr="vdoc", description="View document", resource_class=DocumentResource, ), Action( name="edit-document", abbr="edoc", description="Edit document", resource_class=DocumentResource, ), ] The fields of the Action dataclass are as follows: name - string The name of the action, e.g. view-document . This should be unique across all plugins. abbr - string or None An abbreviation of the action, e.g. vdoc . This is optional. Since this needs to be unique across all installed plugins it's best to choose carefully or omit it entirely (same as setting it to None .) description - string or None A human-readable description of what the action allows you to do. resource_class - type[Resource] or None The Resource subclass that defines what kind of resource this action applies to. Omit this (or set to None ) for global actions that apply only at the instance level with no associated resources (like debug-menu or permissions-debug ). Your Resource subclass must: Define a name class attribute (e.g., "document" ) Define a parent_class class attribute ( None for top-level resources like databases, or the parent Resource subclass for child resources) Implement a resources_sql() classmethod that returns SQL returning all resources as (parent, child) columns Have an __init__ method that accepts appropriate parameters and calls super().__init__(parent=..., child=...) ["Plugin hooks"] []
Powered by Datasette · Queries took 2.257ms