From 4b5014d381c55819a1cc81e1e68257e9e1225c89 Mon Sep 17 00:00:00 2001 From: David Read Date: Mon, 23 Nov 2015 18:27:04 +0000 Subject: [PATCH] Fix test for older ckan. --- ckanext/harvest/harvesters/base.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ckanext/harvest/harvesters/base.py b/ckanext/harvest/harvesters/base.py index c2670a7..01a7943 100644 --- a/ckanext/harvest/harvesters/base.py +++ b/ckanext/harvest/harvesters/base.py @@ -24,11 +24,22 @@ from ckanext.harvest.interfaces import IHarvester if p.toolkit.check_ckan_version(min_version='2.3'): from ckan.lib.munge import munge_tag else: - # Fallback for older ckan versions which don't have a decent munger + # Fallback munge_tag for older ckan versions which don't have a decent + # munger + def _munge_to_length(string, min_length, max_length): + '''Pad/truncates a string''' + if len(string) < min_length: + string += '_' * (min_length - len(string)) + if len(string) > max_length: + string = string[:max_length] + return string + def munge_tag(tag): tag = substitute_ascii_equivalents(tag) tag = tag.lower().strip() - return re.sub(r'[^a-zA-Z0-9\- ]', '', tag).replace(' ', '-') + tag = re.sub(r'[^a-zA-Z0-9\- ]', '', tag).replace(' ', '-') + tag = _munge_to_length(tag, model.MIN_TAG_LENGTH, model.MAX_TAG_LENGTH) + return tag log = logging.getLogger(__name__)