def convert_to_article(request, entry_id): def get_entry_author(entry): written_by_user = '******' % entry.user.username written_by_anon = 'Written by: Anonymous' if entry.hide_real_name: return written_by_anon return written_by_user entry = get_object_or_404(YourWordsCompetitionEntry, pk=entry_id) if not entry.article_page: competition_index_page = ( YourWordsCompetitionIndexPage.objects.live().first()) article = ArticlePage( title=entry.story_name, slug='yourwords-entry-%s' % cautious_slugify(entry.story_name), body=json.dumps([{ "type": "paragraph", "value": get_entry_author(entry), "type": "paragraph", "value": entry.story_text, }]) ) competition_index_page.add_child(instance=article) article.save_revision() article.unpublish() entry.article_page = article entry.save() return redirect('/admin/pages/%d/move/' % article.id) return redirect('/admin/pages/%d/edit/' % entry.article_page.id)
def test_escapes_non_latin_chars(self): test_cases = [ ('Straßenbahn', 'straxdfenbahn'), ('Спорт!', 'u0421u043fu043eu0440u0442'), ('〔山脈〕', 'u5c71u8108'), ] for (original, expected_result) in test_cases: self.assertEqual(cautious_slugify(original), expected_result)
def create_category_data_iter(self, count=2, locale='eng_UK', **kwargs): for i in range(count): data = {} data.update({ 'title': u'Test Category %s' % (i, ), 'language': locale }) data.update(kwargs) data.update({'slug': cautious_slugify(data['title'])}) yield data, i
def create_page_data_iter(self, count=2, locale='eng_UK', **kwargs): for i in range(count): data = {} data.update({ 'title': u'Test Page %s' % (i, ), 'content': u'this is sample content for pg %s' % (i, ), 'language': locale }) data.update(kwargs) data.update({'slug': cautious_slugify(data['title'])}) yield data, i
def test_behaves_same_as_slugify_for_latin_chars(self): test_cases = [ ('', ''), ('???', ''), ('Hello world', 'hello-world'), ('Hello_world', 'hello_world'), ('Hellö wörld', 'hello-world'), ('Hello world', 'hello-world'), (' Hello world ', 'hello-world'), ('Hello, world!', 'hello-world'), ('Hello*world', 'helloworld'), ('Hello☃world', 'helloworld'), ] for (original, expected_result) in test_cases: self.assertEqual(slugify(original), expected_result) self.assertEqual(cautious_slugify(original), expected_result)
def test_behaves_same_as_slugify_for_latin_chars(self): test_cases = [ ("", ""), ("???", ""), ("Hello world", "hello-world"), ("Hello_world", "hello_world"), ("Hellö wörld", "hello-world"), ("Hello world", "hello-world"), (" Hello world ", "hello-world"), ("Hello, world!", "hello-world"), ("Hello*world", "helloworld"), ("Hello☃world", "helloworld"), ] for (original, expected_result) in test_cases: self.assertEqual(slugify(original), expected_result) self.assertEqual(cautious_slugify(original), expected_result)
def generate_slug(text, tail_number=0): from wagtail.wagtailcore.models import Page """ Returns a new unique slug. Object must provide a SlugField called slug. URL friendly slugs are generated using django.template.defaultfilters' slugify. Numbers are added to the end of slugs for uniqueness. based on implementation in jmbo.utils https://github.com/praekelt/jmbo/blob/develop/jmbo/utils/__init__.py """ # Empty slugs are ugly (eg. '-1' may be generated) so force non-empty if not text: text = 'no-title' # use django slugify filter to slugify slug = cautious_slugify(text)[:255] values_list = Page.objects.filter( slug__startswith=slug ).values_list('id', 'slug') # Find highest suffix max = -1 for tu in values_list: if tu[1] == slug: if max == -1: # Set max to indicate a collision max = 0 # Update max if suffix is greater match = RE_NUMERICAL_SUFFIX.match(tu[1]) if match is not None: i = int(match.group(1)) if i > max: max = i if max >= 0: # There were collisions return "%s-%s" % (slug, max + 1) else: # No collisions return slug
def test_escapes_non_latin_chars(self): test_cases = [("Straßenbahn", "straxdfenbahn"), ("Спорт!", "u0421u043fu043eu0440u0442"), ("〔山脈〕", "u5c71u8108")] for (original, expected_result) in test_cases: self.assertEqual(cautious_slugify(original), expected_result)