Skip to main content

Utilities for ckan extensions.

Project description

ckantools

CKAN Python

Utilities and common methods for CKAN extensions.

Overview

A collection of methods, decorators, and anything else that might be useful.

Installation

pip install ckantools

Usage

Actions

Use the @action decorator to add actions:

# logic/actions/module_name.py

from ckantools.decorators import action

schema = {
        'parameter_1': [not_missing, str],
        'parameter_2': [ignore_missing, int_validator]
    }

helptext = 'This action only exists as an example, so does not actually anything.'

@action(schema, helptext)
def example_action(parameter_1, parameter_2):
    # ...

And then load the action(s) in plugin.py:

# plugin.py

from .logic.actions import module_name
from ckantools.loaders import create_actions
from ckan.plugins import implements, interfaces, SingletonPlugin

class ExamplePlugin(SingletonPlugin):
    implements(interfaces.IActions)
    
    # IActions
    def get_actions(self):
        return create_actions(module_name)

Main benefits to using the decorator:

  • automatically calls relevant auth function
  • injects items defined in schema as function args
  • allows you to define long or complex schemas and helptexts without cluttering up code and/or affecting readability
  • neater and easier to maintain than having to list out all of the actions you want to load, e.g.
          ## IActions
    def get_actions(self):
      return {
          'example_action': module_name.example_action,
          'other_action': module_name.other_action,
          'other_other_action': module_name.other_other_action,
          # etc
      }
    

Auth

Loading auth functions is similar to actions, i.e. use the @auth decorator.

The decorator has three args, all of which are optional:

  • proxy: the name of an existing auth function to call that function first
  • keymapping: if the keys are different between this auth function and the proxy auth function, use this to rename them
  • anon: boolean, if true, apply the toolkit.auth_allow_anonymous_access decorator.
# logic/auth/module_name.py

from ckantools.decorators import auth

# all of the args are optional
@auth(anon=True)
def example_action(context, data_dict):
    # no proxy
    # anonymous access is allowed
    # then the custom auth logic:
    return {'success': data_dict.get('parameter_2') == 'carrots'}

# with args
@auth('example_action', {'param_1': 'parameter_2'})
def other_action(context, data_dict):
    # checks access to example_action first
    # the arg param_1 from this action is the same as parameter_2 in example_action (not all args/parameters have to be mapped, just the relevant ones)
    # anonymous access is not allowed
    # if it passes all that:
    return {'success': True}

The auth functions can then be loaded in plugin.py:

# plugin.py

from .logic.auth import module_name
from ckantools.loaders import create_auth
from ckan.plugins import implements, interfaces, SingletonPlugin

class ExamplePlugin(SingletonPlugin):
    implements(interfaces.IActions)
    
    # IAuthFunctions
    def get_auth_functions(self):
        return create_auth(module_name)

Main benefits to using the decorator:

  • reduces repetition of complex auth logic
  • as with the action decorator, it's neater and easier to maintain than having to list out all of the auth functions to load

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ckantools-0.0.5.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

ckantools-0.0.5-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file ckantools-0.0.5.tar.gz.

File metadata

  • Download URL: ckantools-0.0.5.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for ckantools-0.0.5.tar.gz
Algorithm Hash digest
SHA256 79d7428aa0433dadc61cbd531f409d0e485de0cc7c4ac22ae9d22669daccc28c
MD5 db7decc058dbc06fec928584de24a5bd
BLAKE2b-256 10f9548ca136bcb07853c47890c697d7a7cd24b6be3810474291443b363d5f96

See more details on using hashes here.

File details

Details for the file ckantools-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: ckantools-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for ckantools-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c83cdac9a7b69c1cc05dbf2f142a797ee817126c5652b3756a3f62277a1b49f6
MD5 7598df7017e9ede9c1e358ff00d08901
BLAKE2b-256 60bf6c367e8cf8b30cb68f3baf58bdb2260dee5caa542be781b7cd86dde704b6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page