diff --git a/api/helpers.py b/api/helpers.py index af181e9..c1e961c 100644 --- a/api/helpers.py +++ b/api/helpers.py @@ -1,9 +1,6 @@ # coding=UTF-8 import json from flask import request, current_app, Response -import unidecode -import re - def check_api_key(): api_keys = current_app.config.get("API_KEYS") @@ -16,27 +13,4 @@ def check_api_key(): return Response( response, 401, {'WWW-Authenticate': 'Basic realm="Login Required"'} - ) - - -def slugify(text): - """Converts a string to a human-readable url""" - - # translate - text = unidecode.unidecode(text) - - # lowercase - text = text.lower() - - # replace unwanted chars - numbers = re.compile('(?<=\d),(?=\d)') - text = numbers.sub('', text) - unwanted_chars = re.compile(r'[^_a-z0-9/]+') - text = unwanted_chars.sub('_', text) - - # remove redundant _ - duplicated_underscores = re.compile('_{2,}') - text = duplicated_underscores.sub('_', text) - text = text.strip('_') - - return text + ) \ No newline at end of file diff --git a/api/lib/slugify.py b/api/lib/slugify.py new file mode 100644 index 0000000..bd3bed1 --- /dev/null +++ b/api/lib/slugify.py @@ -0,0 +1,26 @@ +# coding=UTF-8 +import unidecode +import re + + +def slugify(text): + """Converts a string to a human-readable url""" + + # translate + text = unidecode.unidecode(text) + + # lowercase + text = text.lower() + + # replace unwanted chars + numbers = re.compile('(?<=\d),(?=\d)') + text = numbers.sub('', text) + unwanted_chars = re.compile(r'[^_a-z0-9/]+') + text = unwanted_chars.sub('_', text) + + # remove redundant _ + duplicated_underscores = re.compile('_{2,}') + text = duplicated_underscores.sub('_', text) + text = text.strip('_') + + return text diff --git a/api/models/category.py b/api/models/category.py index 5a307e1..e90edd4 100644 --- a/api/models/category.py +++ b/api/models/category.py @@ -1,5 +1,5 @@ from api import db -from api.helpers import slugify +from api.lib.slugify import slugify from api.models.locale import Locale diff --git a/api/models/designer.py b/api/models/designer.py index 6558a02..31d08d5 100644 --- a/api/models/designer.py +++ b/api/models/designer.py @@ -1,5 +1,5 @@ from api import db -from api.helpers import slugify +from api.lib.slugify import slugify class Designer(db.Model): diff --git a/tests/lib/__init__.py b/tests/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/lib/test_slugify.py b/tests/lib/test_slugify.py new file mode 100644 index 0000000..e9dc0f0 --- /dev/null +++ b/tests/lib/test_slugify.py @@ -0,0 +1,20 @@ +# coding=UTF-8 + +import unittest2 +from api.lib.slugify import slugify + + +class TestHelpers(unittest2.TestCase): + + def test_slugify(self): + self.assertEqual(slugify('HELLO'), 'hello') + self.assertEqual(slugify('this is a test'), 'this_is_a_test') + self.assertEqual(slugify('___This is a test___'), 'this_is_a_test') + self.assertEqual(slugify('this-is-a-test'), 'this_is_a_test') + self.assertEqual(slugify('this_is_a__test'), 'this_is_a_test') + self.assertEqual(slugify('404'), '404') + self.assertEqual(slugify('1,000 reasons you are #1'), + '1000_reasons_you_are_1') + self.assertEqual(slugify('Nín hǎo'), 'nin_hao') + self.assertEqual(slugify('räksmörgås'), 'raksmorgas') + self.assertEqual(slugify('RÄKSMÖRGÅS'), 'raksmorgas') diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 309be1e..e69de29 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1,20 +0,0 @@ -# coding=UTF-8 - -import unittest2 -from api.helpers import slugify - - -class TestHelpers(unittest2.TestCase): - - def test_slugify(self): - self.assertEqual(slugify('HELLO'), 'hello') - self.assertEqual(slugify('this is a test'), 'this_is_a_test') - self.assertEqual(slugify('___This is a test___'), 'this_is_a_test') - self.assertEqual(slugify('this-is-a-test'), 'this_is_a_test') - self.assertEqual(slugify('this_is_a__test'), 'this_is_a_test') - self.assertEqual(slugify('404'), '404') - self.assertEqual(slugify('1,000 reasons you are #1'), - '1000_reasons_you_are_1') - self.assertEqual(slugify('Nín hǎo'), 'nin_hao') - self.assertEqual(slugify('räksmörgås'), 'raksmorgas') - self.assertEqual(slugify('RÄKSMÖRGÅS'), 'raksmorgas')