Development Overview¶
Developing a CKAN extension requires organizing code into clean, decoupled architectural layers. Rather than building features inside monolithic files or mixing business logic into web views, modern CKAN extension development follows strict separation of responsibilities.
This guide outlines the overall extension architecture and the step-by-step process for developing new features.
Architectural Layers¶
Every feature in a CKAN extension is partitioned across predictable layers:
graph TD
A["Presentation (views, CLI, templates)"] --> B["Logic (actions, auth, validators)"]
B --> C["Data (SQLAlchemy models, migrations)"]
B --> D["Events & signals (ISignal subscribers)"]
E["Entry points (plugin.py & implementations/)"] --> A
E --> B
- Entry point: Subclasses
ckan.plugins.SingletonPluginto register capabilities with CKAN's interface system. - Logic: Encapsulates core domain operations in actions, enforces input validation, and guards access via auth functions.
- Data: Manages persistent state using SQLAlchemy database models and version-controlled database migrations.
- Presentation: Exposes HTTP routes via flask blueprints, administration tasks via CLI commands, and UI utilities via template helpers.
- Cross-cutting layer: Broadcasters and listeners decoupled via signals, runtime options managed via config management, and static safety enforced with typing.
The Feature Development Process¶
When building a new feature or extending existing CKAN capabilities, follow this standard workflow:
-
Data modeling & persistence
If the feature introduces new data entities or persistent state beyond core CKAN schemas:
- Declare SQLAlchemy models.
- Generate a migration script to handle schema creation and updates.
-
Business logic & action design
Write pure, reusable business logic in the logic layer:
- Define action functions inside
logic/action/named with your extension prefix (e.g.myext_item_create). - Annotate actions with
@tk.validate_action_data(schema)to enforce strict schema verification. - Always call
tk.check_access()inside action functions to enforce authorization checks.
- Define action functions inside
-
Plugin registration
Register entry points and hooks cleanly:
- Use
@tk.blanketdecorators on your plugin class inplugin.pyto auto-discover actions, auth functions, validators, blueprints, and template helpers. - Extract complex CKAN interface implementation methods (e.g.
IConfigurer,IPackageController) into animplementations/submodule rather than clutteringplugin.py.
- Use
-
Presentation & user interfaces
Expose your business actions to users:
- Create flask blueprints inside
views/for HTTP web routes. - Register CLI commands inside
cli.pyfor administrative or background operations. - Expose custom Jinja functions in
helpers.pyfor UI presentation needs. - Rule: Views and CLI commands must remain thin wrappers—they extract
parameters and invoke
tk.get_action(), never executing business logic or raw database queries directly.
- Create flask blueprints inside
-
Event decoupling & config integration
Complete the feature integration:
- Declare custom extension parameters in
config_declaration.pywith fallback defaults. - Use
ISignalto broadcast or react to system events without tight coupling. - Annotate interface implementations with
@overrideand type hints to maintain strict safety.
- Declare custom extension parameters in
Section Map¶
For detailed syntax, code patterns, and implementation examples, refer to the individual development guides:
- Defining Plugins — Plugin structure, blanket discovery, and interface implementation setup.
- Adding Actions — Writing actions, calling conventions, and context handling.
- Validators — Schema validation rules, custom validators, and
@tk.validate_action_data. - Adding Views — Flask Blueprints, route definitions, and request handling.
- CLI Commands — Click CLI command groups and terminal tools.
- Config Management — Declaration files, config options, and runtime retrieval.
- Database Models — SQLAlchemy v2 model definitions, session management, and dictize serialization.
- Database Migrations — Isolated Alembic migrations for extension tables.
- Signals — Event handling with
ISignalinterfaces. - Template Helpers — Exposing python helper functions to Jinja2 templates.
- Typing — Type hints,
@overrideannotations, and static checking.