Allow anonymous access to datastore API search
[ckanext-datagovau.git] / ckanext / datagovau / plugin.py
blob:a/ckanext/datagovau/plugin.py -> blob:b/ckanext/datagovau/plugin.py
--- a/ckanext/datagovau/plugin.py
+++ b/ckanext/datagovau/plugin.py
@@ -6,6 +6,9 @@
 import ckan.plugins.toolkit as tk
 import ckan.model as model
 from pylons import config
+
+from sqlalchemy import orm
+import ckan.model
 
 #parse the activity feed for last active non-system user
 def get_last_active_user(id):
@@ -26,6 +29,17 @@
 				lib.helpers.get_action('user_activity_list',{'id':user_dict['id']}) if x['data'].get('package')]
     return created_datasets_list + active_datasets_list
 
+def get_dga_stats():
+	connection = model.Session.connection()
+        res = connection.execute("SELECT 'organization', count(*) from \"group\" where type = 'organization' and state = 'active' 		union select 'package', count(*) from package where state='active' or state='draft' or state='draft-complete' 		union select 'resource', count(*) from resource where state='active' 		union select name||role, 0 from user_object_role inner join \"user\" on user_object_role.user_id = \"user\".id where name not in ('logged_in','visitor') group by name,role;")
+        return res
+
+
+def get_activity_counts():
+	connection = model.Session.connection()
+        res = connection.execute("select to_char(timestamp, 'YYYY-MM') as month,activity_type, count(*) from activity group by month, activity_type order by month;").fetchall();
+        return res
+
 
 class DataGovAuPlugin(plugins.SingletonPlugin,
                                 tk.DefaultDatasetForm):
@@ -37,14 +51,6 @@
     plugins.implements(plugins.IConfigurer, inherit=False)
     plugins.implements(plugins.IDatasetForm, inherit=False)
     plugins.implements(plugins.ITemplateHelpers, inherit=False)
-    plugins.implements(plugins.IAuthFunctions)
-
-    def datastore_search(context, data_dict):
-        return {'success': True} # allow all
-
-    def get_auth_functions(self):
-        return {'datastore_search': datastore_search}
-
 
     def update_config(self, config):
         # Add this plugin's templates dir to CKAN's extra_template_paths, so
@@ -58,7 +64,7 @@
         # config['licenses_group_url'] = 'http://%(ckan.site_url)/licenses.json'
 
     def get_helpers(self):
-        return {'get_last_active_user': get_last_active_user, 'get_user_datasets': get_user_datasets}
+        return {'get_last_active_user': get_last_active_user, 'get_user_datasets': get_user_datasets, 'get_dga_stats': get_dga_stats, 'get_activity_counts': get_activity_counts}
 
     def is_fallback(self):
         # Return True to register this plugin as the default handler for
@@ -72,17 +78,17 @@
 
 
     def create_package_schema(self):
-        schema = super(ExampleIDatasetFormPlugin, self).create_package_schema()
+        schema = super(DataGovAuPlugin, self).create_package_schema()
         schema = self._modify_package_schema(schema)
         return schema
 
     def update_package_schema(self):
-        schema = super(ExampleIDatasetFormPlugin, self).update_package_schema()
+        schema = super(DataGovAuPlugin, self).update_package_schema()
         schema = self._modify_package_schema(schema)
         return schema
 
     def show_package_schema(self):
-        schema = super(ExampleIDatasetFormPlugin, self).show_package_schema()
+        schema = super(DataGovAuPlugin, self).show_package_schema()
 
         # Don't show vocab tags mixed in with normal 'free' tags
         # (e.g. on dataset pages, or on the search page)
@@ -145,25 +151,25 @@
     # called.
 
     def setup_template_variables(self, context, data_dict):
-        return super(ExampleIDatasetFormPlugin, self).setup_template_variables(
+        return super(DataGovAuPlugin, self).setup_template_variables(
             context, data_dict)
 
     def new_template(self):
-        return super(ExampleIDatasetFormPlugin, self).new_template()
+        return super(DataGovAuPlugin, self).new_template()
 
     def read_template(self):
-        return super(ExampleIDatasetFormPlugin, self).read_template()
+        return super(DataGovAuPlugin, self).read_template()
 
     def edit_template(self):
-        return super(ExampleIDatasetFormPlugin, self).edit_template()
+        return super(DataGovAuPlugin, self).edit_template()
 
     def search_template(self):
-        return super(ExampleIDatasetFormPlugin, self).search_template()
+        return super(DataGovAuPlugin, self).search_template()
 
     def history_template(self):
-        return super(ExampleIDatasetFormPlugin, self).history_template()
+        return super(DataGovAuPlugin, self).history_template()
 
     def package_form(self):
-        return super(ExampleIDatasetFormPlugin, self).package_form()
+        return super(DataGovAuPlugin, self).package_form()