Esempio n. 1
0
 def test_should_replace_nonascii_chars_with_corresponding_ascii_chars(self):
     self.assertEquals(slug('áÁàÀãÃâÂäÄ'.decode('utf8')), 'aaaaaaaaaa')
     self.assertEquals(slug('éÉèÈẽẼêÊëË'.decode('utf8')), 'eeeeeeeeee')
     self.assertEquals(slug('íÍìÌĩĨîÎïÏ'.decode('utf8')), 'iiiiiiiiii')
     self.assertEquals(slug('óÓòÒõÕôÔöÖ'.decode('utf8')), 'oooooooooo')
     self.assertEquals(slug('úÚùÙũŨûÛüÜ'.decode('utf8')), 'uuuuuuuuuu')
     self.assertEquals(slug('ćĆĉĈçÇ'.decode('utf8')), 'cccccc')
Esempio n. 2
0
    def generate_slug(self, value):
        """Query the database for similarly matching values. Then
        increment the maximum trailing integer. In the future this
        will rely on map-reduce(?).

        This method first makes a basic slug from the given value.
        Then it checks to see if any documents in the database share
        that same value in the same field. If it finds matching
        results then it will attempt to increment the counter on the
        end of the slug.

        It uses pymongo directly because mongoengine's own querysets
        rely on each field's __set__ method, which results in endless
        recrusion.
        """
        collection = self.__class__.objects._collection
        slugVal = slug(value)
        slug_regex = '^%s' % slugVal
        existing_docs = [
            {'id': doc['_id'], 'slug': doc['slug']} for doc in
            collection.find({'slug': {'$regex':slug_regex}})
        ]
        matches = [int(re.search(r'-[\d]+$', doc['slug']).group()[-1:])
            for doc in existing_docs if re.search(r'-[\d]+$', doc['slug'])]

        # Four scenarios:
        # (1) No match is found, this is a brand new slug
        # (2) A matching document is found, but it's this one
        # (3) A matching document is found but without any number
        # (4) A matching document is found with an incrementing value
        next = 1
        if len(existing_docs) == 0:
            return slugVal
        elif self.id in [doc['id'] for doc in existing_docs]:
            return self['slug']
        elif not matches:
            return u'%s-%s' % (slugVal, next)
        else:
            next = max(matches) + 1
            return u'%s-%s' % (slugVal, next)
Esempio n. 3
0
 def test_should_accept_only_chars_in_permitted_chars_parameter(self):
     slugged_text = slug('''0987654321gfdsazxcvb''',
                         permitted_chars='abc123')
     self.assertEquals(slugged_text, '321acb')
Esempio n. 4
0
 def test_should_accept_only_ascii_letters_and_numbers(self):
     slugged_text = slug('''qwerty123456"'@#$%*()_+\|<>,.;:/?]~[`{}^ ''')
     self.assertEquals(slugged_text, 'qwerty123456')
Esempio n. 5
0
 def test_should_accept_other_input_encodings(self):
     slugged_text = slug(u'Álvaro Justen'.encode('utf16'), 'utf16')
     self.assertEquals(slugged_text, 'alvaro-justen')
Esempio n. 6
0
 def test_should_accept_unicode_text(self):
     self.assertEquals(slug(u'Álvaro Justen'), 'alvaro-justen')
Esempio n. 7
0
 def test_should_ignore_unecessary_spaces(self):
     self.assertEquals(slug('  alvaro   justen  '), 'alvaro-justen')
Esempio n. 8
0
 def test_should_replace_space_with_dash(self):
     self.assertEquals(slug('Alvaro Justen'), 'alvaro-justen')
Esempio n. 9
0
 def test_should_always_return_lowercase_words(self):
     self.assertEquals(slug('ALVAROJUSTEN'), 'alvarojusten')