Cleaned up the controller plugin and started removing some cruft.
[ckanext-datagovau.git] / ckanext / example / controller_plugin.py
blob:a/ckanext/example/controller_plugin.py -> blob:b/ckanext/example/controller_plugin.py
--- a/ckanext/example/controller_plugin.py
+++ b/ckanext/example/controller_plugin.py
@@ -1,23 +1,24 @@
-import logging
-from ckan.lib.base import BaseController, render, c, model, abort, request
-from ckan.lib.base import redirect, _, config, h
+import os, logging
+from ckan.authz import Authorizer
 import ckan.logic.action.create as create
 import ckan.logic.action.update as update
 import ckan.logic.action.get as get
 from ckan.logic.converters import date_to_db, date_to_form, convert_to_extras, convert_from_extras
-from ckan.lib.navl.dictization_functions import DataError, flatten_dict, unflatten
 from ckan.logic import NotFound, NotAuthorized, ValidationError
 from ckan.logic import tuplize_dict, clean_dict, parse_params
+import ckan.logic.schema as default_schema
 from ckan.logic.schema import package_form_schema
-from ckan.plugins import IDatasetForm
-from ckan.plugins import implements, SingletonPlugin
+import ckan.logic.validators as val
+from ckan.lib.base import BaseController, render, c, model, abort, request
+from ckan.lib.base import redirect, _, config, h
 from ckan.lib.package_saver import PackageSaver
 from ckan.lib.field_types import DateType, DateConvertError
-from ckan.authz import Authorizer
 from ckan.lib.navl.dictization_functions import Invalid
 from ckan.lib.navl.dictization_functions import validate, missing
-import ckan.logic.validators as val
-import ckan.logic.schema as default_schema
+from ckan.lib.navl.dictization_functions import DataError, flatten_dict, unflatten
+from ckan.plugins import IDatasetForm, IConfigurer
+from ckan.plugins import implements, SingletonPlugin
+
 from ckan.lib.navl.validators import (ignore_missing,
                                       not_empty,
                                       empty,
@@ -30,11 +31,33 @@
 
 
 class ExampleDatasetForm(SingletonPlugin):
+    """This plugin demonstrates how a theme packaged as a CKAN
+    extension might extend CKAN behaviour.
+
+    In this case, we implement three extension interfaces:
+
+      - ``IConfigurer`` allows us to override configuration normally
+        found in the ``ini``-file.  Here we use it to specify where the
+        form templates can be found.
+      - ``IDatasetForm`` allows us to provide a custom form for a dataset
+        based on the type_name that may be set for a package.  Where the 
+        type_name matches one of the values in package_types then this 
+        class will be used. 
     """
-
-    """
-
     implements(IDatasetForm, inherit=True)
+    implements(IConfigurer, inherit=True)    
+    
+    def update_config(self, config):
+        """
+        This IConfigurer implementation causes CKAN to look in the
+        ```templates``` directory when looking for the package_form()
+        """
+        here = os.path.dirname(__file__)
+        rootdir = os.path.dirname(os.path.dirname(here))
+        template_dir = os.path.join(rootdir, 'ckanext',
+                                    'example', 'theme', 'templates')
+        config['extra_template_paths'] = ','.join([template_dir,
+                config.get('extra_template_paths', '')])
 
     def package_form(self):
         """
@@ -68,10 +91,11 @@
 
     def setup_template_variables(self, context, data_dict=None):
         """
-        Add variables to c just prior to the template being rendered.
+        Adds variables to c just prior to the template being rendered that can
+        then be used within the form
         """        
         c.licences = [('', '')] + model.Package.get_license_options()
-        c.publishers = self.get_publishers()
+        c.publishers = [('Example publisher', 'Example publisher 2')]
         c.is_sysadmin = Authorizer().is_sysadmin(c.user)
         c.resource_columns = model.Resource.get_columns()
 
@@ -149,14 +173,8 @@
 
     def check_data_dict(self, data_dict):
         """
-        Check if the return data is correct.
-
-        raise a DataError if not.
+        Check if the return data is correct and raises a DataError if not.
         """
         return
 
-    def get_publishers(self):
-        """
-        """
-        return [('pub1', 'pub2')]