From 742f423747cf886fcca81146c5dfaee38ff15ee8 Mon Sep 17 00:00:00 2001
From: anonymous <anonymous>
Date: Wed, 20 Mar 2019 14:05:15 +0100
Subject: [PATCH] munges title to name for every created dataset

---
 ckanext/odsh/logic/__init__.py |  0
 ckanext/odsh/logic/action.py   | 25 +++++++++++++++++++++++++
 ckanext/odsh/plugin.py         |  7 +++++++
 3 files changed, 32 insertions(+)
 create mode 100644 ckanext/odsh/logic/__init__.py
 create mode 100644 ckanext/odsh/logic/action.py

diff --git a/ckanext/odsh/logic/__init__.py b/ckanext/odsh/logic/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/ckanext/odsh/logic/action.py b/ckanext/odsh/logic/action.py
new file mode 100644
index 00000000..b755a50f
--- /dev/null
+++ b/ckanext/odsh/logic/action.py
@@ -0,0 +1,25 @@
+import logging
+from ckan.logic.action.create import package_create
+import ckan.model as model
+
+log = logging.getLogger(__name__)
+
+
+def odsh_package_create(context, data_dict):
+    log.debug('in ODSH package_create')
+    munge_increment_name(data_dict)
+    return package_create(context, data_dict)
+
+def munge_increment_name(data_dict):
+    log.debug('IN MUNGE')
+    from ckan.lib.munge import munge_title_to_name
+
+    name_base = name = munge_title_to_name(data_dict['title'])
+    pkg = model.Package.get(name)
+    i = 0
+    while pkg:
+        i += 1
+        name = name_base + str(i)
+        pkg = model.Package.get(name)
+    log.debug('name: %s' % name)
+    data_dict['name'] = name
diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py
index c944fcf3..780fdb4b 100644
--- a/ckanext/odsh/plugin.py
+++ b/ckanext/odsh/plugin.py
@@ -11,6 +11,7 @@ import ckan.model as model
 from ckanext.odsh.lib.uploader import ODSHResourceUpload
 import ckan.lib.helpers as helpers
 import helpers as odsh_helpers
+import ckanext.odsh.logic.action as action
 
 from itertools import count
 from routes.mapper import SubMapper
@@ -298,6 +299,12 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
     plugins.implements(plugins.IDatasetForm)
     plugins.implements(plugins.IValidators)
     plugins.implements(plugins.IPackageController, inherit=True)
+    plugins.implements(plugins.IActions)
+
+    # IActions
+
+    def get_actions(self):
+        return {'package_create': action.odsh_package_create}
 
     # IConfigurer
 
-- 
GitLab