Rafał Pitoń 11 лет назад
Родитель
Сommit
07fea8a748

+ 18 - 1
misago/core/tests/test_utils.py

@@ -1,7 +1,8 @@
+#-*- coding: utf-8 -*-
 from django.core.urlresolvers import reverse
 from django.test import TestCase
 from django.test.client import RequestFactory
-from misago.core.utils import is_request_to_misago
+from misago.core.utils import is_request_to_misago, slugify
 
 
 VALID_PATHS = (
@@ -35,3 +36,19 @@ class IsRequestToMisagoTests(TestCase):
             self.assertFalse(
                 is_request_to_misago(request),
                 '"%s" is overlapped by "%s"' % (path, misago_prefix))
+
+
+class SlugifyTests(TestCase):
+    def test_valid_slugify_output(self):
+        """Misago's slugify correcly slugifies string"""
+        test_cases = (
+            (u'Bob', u'bob'),
+            (u'Eric The Fish', u'eric-the-fish'),
+            (u'John   Snow', u'john-snow'),
+            (u'J0n', u'j0n'),
+            (u'An###ne', u'anne'),
+            (u'S**t', u'st'),
+        )
+
+        for original, slug in test_cases:
+            self.assertEqual(slugify(original), slug)

+ 35 - 0
misago/core/tests/test_validators.py

@@ -0,0 +1,35 @@
+from django.core.exceptions import ValidationError
+from django.test import TestCase
+from misago.core.validators import validate_sluggable
+
+
+class ValidateSluggableTests(TestCase):
+    def test_error_messages_set(self):
+        """custom error messages are set and used"""
+        error_short = "I'm short custom error!"
+        error_long = "I'm long custom error!"
+
+        validator = validate_sluggable(error_short, error_long)
+
+        self.assertEqual(validator.error_short, error_short)
+        self.assertEqual(validator.error_long, error_long)
+
+    def test_faulty_input_validation(self):
+        """invalid values raise errors"""
+        validator = validate_sluggable()
+
+        with self.assertRaises(ValidationError):
+            validator('!#@! !@#@')
+        with self.assertRaises(ValidationError):
+            validator('!#@! !@#@ 1234567890 1234567890 1234567890 1234567890'
+                      '1234567890 1234567890 1234567890 1234567890 1234567890'
+                      '1234567890 1234567890 1234567890 1234567890 1234567890'
+                      '1234567890 1234567890 1234567890 1234567890 1234567890'
+                      '1234567890 1234567890 1234567890 1234567890 1234567890')
+
+    def test_valid_input_validation(self):
+        """valid values don't raise errors"""
+        validator = validate_sluggable()
+
+        validator('Bob')
+        validator('Lorem ipsum123!')

+ 8 - 0
misago/core/utils.py

@@ -1,4 +1,6 @@
+from unidecode import unidecode
 from django.core.urlresolvers import reverse
+from django.template.defaultfilters import slugify as django_slugify
 
 
 def _is_request_path_under_misago(request):
@@ -17,3 +19,9 @@ def is_request_to_misago(request):
     except AttributeError:
         request._request_to_misago = _is_request_path_under_misago(request)
         return request._request_to_misago
+
+
+def slugify(string):
+    string = unicode(string)
+    string = unidecode(string)
+    return django_slugify(string.replace('_', ' '))

+ 19 - 0
misago/core/validators.py

@@ -0,0 +1,19 @@
+from django.core.exceptions import ValidationError
+from django.utils.translation import ugettext_lazy as _
+from misago.core.utils import slugify
+
+
+class validate_sluggable(object):
+    def __init__(self, error_short=None, error_long=None):
+        self.error_short = error_short or _(
+            "Value has to contain alpha-numerical characters.")
+        self.error_long = error_long or _("Value is too long.")
+
+    def __call__(self, value):
+        slug = slugify(value)
+
+        if not slug.replace('-', ''):
+            raise ValidationError(self.error_short)
+
+        if len(slug) > 255:
+            raise ValidationError(self.error_long)