def test_simple_meta(self): """ Test simple meta-data conversions """ keywords = filter_field('keywords', self.meta) description = filter_field('description', self.meta) title = filter_field('title', self.meta) self.assertEqual(keywords, self.article.keywords) self.assertEqual(description, self.article.description) self.assertEqual(title, self.article.title)
def test_publish_new(self): """ Publish a new article """ # Make a different title title = 'Here is another article' pubstr = '2020-09-01 12:00' pubdate = filter_field('publish', { 'publish': pubstr }) contents = open(os.path.join(here, 'sample.rst')).read().replace( 'This is my title', title) f = StringIO(contents) a = publish(f, draft=True, publish=pubstr, debug=True) self.assertNotEqual(a.pk, self.article.pk) self.assertEqual(a.title, title) self.assertEqual(a.publish_date, pubdate) self.assertTrue('Some content' in a.content) followups = a.followup_for.all() self.assertTrue(self.article.pk in [a.pk for a in followups], followups) related = a.related_articles.all() # We have to use titles due to ambiguous slug self.assertTrue(self.article.title in [b.title for b in related], related)
def test_tags(self): """ Test ``tags`` filter """ tagqs = Tag.objects.all() self.assertEqual(list(tagqs), filter_field('tags', self.meta))
def test_status_default(self): """ Test ``status`` filter and use of conf defaults """ status = ArticleStatus.objects.get(name='Finished') self.assertEqual(filter_field('status', self.meta), status)
def test_article_slugs(self): """ Test ``article_slugs`` filter """ self.assertEqual(filter_field('related', self.meta)[0], self.article)
def publish(f, draft, by=None, publish=None, is_active=True, login_required=False, debug=False): """ Publishes an article. :param f: The file to parse :type f: file :param by: Author username :type by: str :param draft: Save as draft? :type draft: bool :param draft: Article active? :type draft: bool :param draft: Require login? :type draft: bool :param publish: When to publish :type publish: datetime fmt=YYYY-MM-DD HH:MM :param debug: Print debug data :type debug: bool :returns: Saved :class:`Article` """ meta, content = parse_meta_and_article(f.read()) meta.update({ 'is_active': is_active, 'login_required': login_required, }) if draft: meta['status'] = 'Draft' if publish: meta['publish'] = publish if by: meta['by'] = by slug = slugify(meta['title']) # New or updated? articles = Article.objects.using(DB).filter(slug=slug) if len(articles) > 1: if not publish: raise ConfigurationError('Title ambiguous; supply publish date') articles.filter(publish_date=meta['publish']) article = articles[0] elif len(articles) == 1: article = articles[0] else: article = Article() article.content = content article.markup = 'h' todo = [] keys = list(meta.keys()) for key in keys + [k for k in REQUIRED_FIELDS+FIELD_DEFAULTS.keys() if k not in keys]: value = filter_field(key, meta) if key not in SAVE_NEEDED: setfield(article, key, value, debug) else: todo.append((key, value)) article.save(using=DB) for key, value in todo: setfield(article, key, value, debug) article.save(using=DB) return article