Пример #1
0
    def test_save_and_get_by_id(self):
        self.assertIsNone(Article.get_by_id(1))

        now = int(time.time())
        article = Article(
            title='test title',
            url='test-url',
            content=u'测试',
            category='test',
            tags=('test1', 'test2'),
        )
        self.assertRaises(PropertyError, article.save)
        article.save(inserting=True)
        self.assertEqual(article.id, 1)

        saved_article = Article.get_by_id(1)
        self.assertEqual(article, saved_article)
        self.assertGreaterEqual(saved_article.pub_time, now)
        self.assertGreaterEqual(saved_article.mod_time, now)
        self.assertIsNone(Article.get_by_id(2))

        saved_article.title = u'测试'
        saved_article.save()
        saved_article = Article.get_by_id(1)
        self.assertEqual(saved_article.title, u'测试')
        self.assertIsNone(Article.get_by_id(2))
        self.assertRaises(PropertyError, saved_article.save, inserting=True)

        article2 = Article(
            title='test',
            category='test',
            tags=('test1', 'test2'),
            pub_time=1,
            mod_time=2
        )
        article2.save(inserting=True)
        self.assertEqual(article2.id, 2)
        saved_article2 = Article.get_by_id(2)
        self.assertEqual(article2, saved_article2)
Пример #2
0
    def post(self):
        title = self.get_argument('title', None)
        if not title:
            self.finish('发表失败,标题不能为空')
            return

        pub_time = self.get_argument('pub_time', None)
        if pub_time:
            pub_time = parse_time(pub_time)
        if not pub_time:
            pub_time = datetime.utcnow()
        pub_timestamp = datetime_to_timestamp(pub_time)

        mod_time = self.get_argument('mod_time', None)
        if mod_time:
            mod_time = parse_time(mod_time)
        if not mod_time:
            mod_timestamp = pub_timestamp
        else:
            mod_timestamp = datetime_to_timestamp(mod_time)

        url = self.get_argument('url', None)
        # todo: check url pattern
        if not url:
            formatted_date = formatted_date_for_url(pub_time)
            if CONFIG.REPLACE_SPECIAL_CHARACTERS_FOR_URL:
                url = formatted_date + replace_special_characters_for_url(title)
            else:
                url = formatted_date + title
        if Article.exist_url(url):
            self.finish('发表失败,同链接的文章已存在')
            return

        public = self.get_argument('public', None) == 'on'
        bbcode = self.get_argument('bbcode', None) == 'on'
        html = self.get_argument('html', None) == 'on'
        format = bbcode * ContentFormatFlag.BBCODE + html * ContentFormatFlag.HTML
        content = self.get_argument('content').replace('\r\n', '\n').replace('\r', '\n')

        category_name = self.get_argument('category', None)
        if category_name:
            if not Category.exists(category_name):
                self.finish('发表失败,分类不存在')
                return

        tag_names = self.get_arguments('tags')
        if tag_names:
            tag_names = set(tag_names)
            tag_names.discard('')

        keywords = self.get_argument('keywords', None)
        if keywords:
            keywords = keywords.lower()

        article = Article(
            title=title,
            url=url,
            content=content,
            format=format,
            category=category_name or None,
            tags=sorted(tag_names) if tag_names else None,
            keywords=keywords,
            public=public,
            pub_time=pub_timestamp,
            mod_time=mod_timestamp
        )
        try:
            article.save(inserting=True)
        except IntegrityError:
            quoted_url = article.quoted_url()
            self.finish('发表失败,已有<a href="%s%s">相同链接的文章</a>存在' % (CONFIG.BLOG_HOME_RELATIVE_PATH, quoted_url))
        except Exception:
            logging.exception('failed to save new article')
            self.finish('发表失败')
        else:
            quoted_url = article.quoted_url()
            self.finish('发表成功,查看<a href="%s%s">发表后的文章</a>' % (CONFIG.BLOG_HOME_RELATIVE_PATH, quoted_url))