[ckan-changes] [okfn/ckan] ec1d21: extend plugin.toolkit functions

GitHub noreply at github.com
Thu Apr 19 14:50:14 UTC 2012


  Branch: refs/heads/master
  Home:   https://github.com/okfn/ckan
  Commit: ec1d2113a66ed66312ad6b56fe441afb633cf8b1
      https://github.com/okfn/ckan/commit/ec1d2113a66ed66312ad6b56fe441afb633cf8b1
  Author: Toby <toby.junk at gmail.com>
  Date:   2012-04-19 (Thu, 19 Apr 2012)

  Changed paths:
    M ckan/plugins/toolkit.py

  Log Message:
  -----------
  extend plugin.toolkit functions


diff --git a/ckan/plugins/toolkit.py b/ckan/plugins/toolkit.py
index 64a80d7..59de1c9 100644
--- a/ckan/plugins/toolkit.py
+++ b/ckan/plugins/toolkit.py
@@ -1,21 +1,28 @@
-## This file is intended to make functions consistently available to
-## plugins whilst giving developers the ability move code around or
-## change underlying frameworks etc. It should not be used internaly
-## within ckan only by extensions. Functions should only be removed from
-## this file after reasonable depreciation notice has been given.
+## This file is intended to make functions/objects consistently
+## available to plugins whilst giving developers the ability move code
+## around or change underlying frameworks etc. It should not be used
+## internaly within ckan only by extensions. Functions should only be
+## removed from this file after reasonable depreciation notice has
+## been given.
 
 import inspect
 import os
+import re
 
 import pylons
 import paste.deploy.converters as converters
 import webhelpers.html.tags
 
-import lib.base as base
+import ckan
+import ckan.lib.base as base
+import ckan.logic as logic
+import ckan.lib.cli as cli
+
 
 
 __all__ = [
-    ## Imported functions ##
+    ## Imported functions/objects ##
+    '_',                    # i18n translation
     'c',                    # template context
     'request',              # http request object
     'render',               # template render function
@@ -25,12 +32,22 @@
     'asint',                # converts an object to an integer
     'aslist',               # converts an object to a list
     'literal',              # stop tags in a string being escaped
+    'get_action',           # get logic action function
+    'check_access',         # check logic function authorisation
+    'NotFound',             # action not found exception
+    'NotAuthorized',        # action not authourized exception
+    'ValidationError',      # model update validation error
+    'CkanCommand',          # class for providing cli interfaces
 
-    ## Functions fully defined here ##
+    ## Fully defined in this file ##
     'add_template_directory',
     'add_public_directory',
+    'requires_ckan_version',
+    'check_ckan_version',
+    'CkanVersionException',
 ]
 
+_ = pylons.i18n._
 c = pylons.c
 request = pylons.request
 render = base.render
@@ -40,6 +57,13 @@
 aslist = converters.aslist
 literal = webhelpers.html.tags.literal
 
+get_action = logic.get_action
+check_access = logic.check_access
+NotFound = logic.NotFound
+NotAuthorized = logic.NotAuthorized
+ValidationError = logic.ValidationError
+
+CkanCommand = cli.CkanCommand
 
 # wrappers
 def render_snippet(template, data=None):
@@ -73,3 +97,38 @@ def _add_served_directory(config, relative_path, config_var):
             config[config_var] += ',' + absolute_path
         else:
             config[config_var] = absolute_path
+
+class CkanVersionException(Exception):
+    ''' Exception raised if required ckan version is not available. '''
+    pass
+
+
+def _version_str_2_list(v_str):
+    ''' conver a version string into a list of ints
+    eg 1.6.1b --> [1, 6, 1] '''
+    v_str = re.sub(r'[^0-9.]', '', v_str)
+    return [int(part) for part in v_str.split('.')]
+
+def check_ckan_version(min_version=None, max_version=None):
+    ''' Check that the ckan version is correct for the plugin. '''
+    current = _version_str_2_list(ckan.__version__)
+
+    if min_version:
+        min_required = _version_str_2_list(min_version)
+        if current < min_required:
+            return False
+    if max_version:
+        max_required = _version_str_2_list(max_version)
+        if current > max_required:
+            return False
+    return True
+
+def requires_ckan_version(min_version, max_version=None):
+    ''' Check that the ckan version is correct for the plugin. '''
+    if not check_ckan_version(min_version=min_version, max_version=max_version):
+        if not max_version:
+            error = 'Requires ckan version %s or higher' % min_version
+        else:
+            error = 'Requires ckan version  between %s and %s' % \
+                        (min_version, max_version)
+        raise CkanVersionException(error)


================================================================



More information about the ckan-changes mailing list