def test_duplicate_title_and_language(self): """ Test that if user attempts to create an article with the same name and in the same language as another, it will not raise exceptions. """ title = "Sample Article" author = self.create_person() original_lang = settings.LANGUAGES[0][0] # Create an initial article in the first language article1 = Article( title=title, author=author, owner=author.user, app_config=self.app_config, publishing_date=now() ) article1.set_current_language(original_lang) article1.save() # Now try to create an article with the same title in every possible # language and every possible language contexts. for context_lang, _ in settings.LANGUAGES: with override(context_lang): for article_lang, _ in settings.LANGUAGES: try: article = Article(author=author, owner=author.user, app_config=self.app_config, publishing_date=now()) article.set_current_language(article_lang) article.title = title article.save() except Exception: self.fail('Creating article in process context "{0}" ' 'and article language "{1}" with identical name ' 'as another "{2}" article raises exception'.format( context_lang, article_lang, original_lang, ))
def test_auto_slugifies(self): activate(self.language) title = u'This is a title' author = self.create_person() article = Article.objects.create( title=title, author=author, owner=author.user, app_config=self.app_config, publishing_date=now(), is_published=True, ) article.save() self.assertEquals(article.slug, 'this-is-a-title') # Now, let's try another with the same title article_1 = Article( title=title.lower(), author=author, owner=author.user, app_config=self.app_config, publishing_date=now(), is_published=True, ) # Note, it cannot be the exact same title, else we'll fail the unique # constraint on the field. article_1.save() # Note that this should be "incremented" slug here. self.assertEquals(article_1.slug, 'this-is-a-title-1') article_2 = Article( title=title.upper(), author=author, owner=author.user, app_config=self.app_config, publishing_date=now(), is_published=True, ) article_2.save() self.assertEquals(article_2.slug, 'this-is-a-title-2')
def download_post(self, post_tuple): post = post_tuple[0] photo_key = post_tuple[1] translation.activate('ru') print("Post {} is being downloaded".format(post['id'])) title = self.truncate_text_to_title(post['text'], 50) try: attachment = post['attachments'][0] pic_url = attachment['photo'][photo_key] pic_response = requests.get(pic_url, allow_redirects=True) url_end_index = pic_url.find('.com') path_to_save = pic_url[url_end_index + 5:].replace('/', '_') pic_file = open( path_join(settings.MEDIA_ROOT, 'vk_pictures', path_to_save), 'w+b') pic_file.write(pic_response.content) pic_file.seek(0) importer = FileImporter() folder = FileImporter.get_or_create_folder(importer, ['base', 'subfolder']) file_obj = DjangoFile(pic_file, name=title + '.jpg') pic = importer.import_file(file_obj, folder) pic.save() pic_file.close() except KeyError: pic = None pass except: pic = None raise lead_in = SafeText(self.truncate_text_to_title(post['text'], 300)) try: user = User.objects.get(username='******') except django.contrib.auth.models.DoesNotExist: raise Exception("Должен быть создан пользователь с именем 'vk'") try: author = Person.objects.language('ru').get(user=user) except django.contrib.auth.models.DoesNotExist: raise Exception( "Должен быть создан Aldryn NewsBlog человек со ссылкой на пользователя 'vk'" ) try: config = NewsBlogConfig.objects.language('ru').all()[0] except IndexError: raise Exception("No available NewsBlog config!") publishing_date = datetime.fromtimestamp(int(post['date'])) article = Article(author=author, app_config=config, owner=user, title=title, publishing_date=publishing_date, featured_image=pic, lead_in=lead_in, is_published=True) article.set_current_language('ru') article.save() placeholder = article.content add_plugin(placeholder, 'TextPlugin', 'ru', body=post['text']) self.convert_hashtag_to_category(article) VkPost.objects.create(post_id=post['id'], date=post['date'], article=article) return article