Exemple #1
0
 def test_continue_url(self):
     post = Post(status=PostStatus.published, slug='test-post')
     self.assertEqual(post.continue_url, None)
     post.pub_date = datetime.date(2012, 4, 3)
     expected = '/blog/2012/04/03/test-post#{}'.format(
         self.app.config['POST_CONTINUE_LINK_FRAGMENT'])
     self.assertEqual(post.continue_url, expected)
Exemple #2
0
    def create_post(self, user, commit=True, **kwargs):
        post = Post(author=user, title=u'My First Post', slug=u'my-first-post',
                    body='Post body', **kwargs)

        if post.status == PostStatus.published and post.pub_date is None:
            post.pub_date = utc_now()

        db.session.add(post)
        if commit:
            db.session.commit()
        return post
Exemple #3
0
    def create_post(self, user, commit=True, **kwargs):
        default_fields = {"author": user, "title": u"My First Post", "slug": u"my-first-post", "body": u"Post body"}
        default_fields.update(kwargs)
        post = Post(**default_fields)

        if post.status == PostStatus.published and post.pub_date is None:
            post.pub_date = utc_now()

        db.session.add(post)
        if commit:
            db.session.commit()
        return post
Exemple #4
0
def posts_feed():
    base_url = url_for('general.index', _external=True)
    items = []
    posts = Post.get_published(num=10).all()

    for post in posts:
        post_url = urljoin(base_url, post.url)

        # TODO: Add a real description
        item = RSSItem(
            title=post.title,
            link=post_url,
            description=post.body.split('\r\n', 1)[0],
            author='{} ({})'.format(post.author.email, post.author.full_name),
            categories=[tag.name for tag in post.tags],
            guid=Guid(post_url),
            pubDate=post.pub_date
        )
        items.append(item)

    feed_config = current_app.config['BLOG_POSTS_FEED']
    rss2_feed = RSS2(
        title=feed_config['title'],
        link=base_url,
        description=feed_config['description'],
        language='en-us',
        webMaster=feed_config['webmaster'],
        lastBuildDate=posts[0].pub_date if posts else None,
        ttl=1440,
        items=items
    )
    return current_app.response_class(rss2_feed.to_xml(encoding='utf-8'),
                                      mimetype='application/rss+xml')
Exemple #5
0
def posts_feed():
    base_url = url_for('general.index', _external=True)
    items = []
    posts = Post.get_published(num=10).all()

    for post in posts:
        post_url = urljoin(base_url, post.url)

        # TODO: Add a real description
        item = RSSItem(title=post.title,
                       link=post_url,
                       description=post.body.split('\r\n', 1)[0],
                       author='{} ({})'.format(post.author.email,
                                               post.author.full_name),
                       categories=[tag.name for tag in post.tags],
                       guid=Guid(post_url),
                       pubDate=post.pub_date)
        items.append(item)

    feed_config = current_app.config['BLOG_POSTS_FEED']
    rss2_feed = RSS2(title=feed_config['title'],
                     link=base_url,
                     description=feed_config['description'],
                     language='en-us',
                     webMaster=feed_config['webmaster'],
                     lastBuildDate=posts[0].pub_date if posts else None,
                     ttl=1440,
                     items=items)
    return current_app.response_class(rss2_feed.to_xml(encoding='utf-8'),
                                      mimetype='application/rss+xml')
Exemple #6
0
    def create_post(self, user, commit=True, **kwargs):
        default_fields = {
            'author': user,
            'title': u'My First Post',
            'slug': u'my-first-post',
            'body': u'Post body'
        }
        default_fields.update(kwargs)
        post = Post(**default_fields)

        if post.status == PostStatus.published and post.pub_date is None:
            post.pub_date = utc_now()

        db.session.add(post)
        if commit:
            db.session.commit()
        return post
Exemple #7
0
def add_post():
    form = PostForm(request.form)

    if form.validate_on_submit():
        status = PostStatus.from_string(form.status.data)

        post = Post(author=current_user, title=form.title.data,
                    slug=form.slug.data, body=form.body.data,
                    status=status, tags=form.tags.data)

        if status == PostStatus.published:
            post.pub_date = utc_now()

        db.session.add(post)
        db.session.commit()

        message = u'Post "{}" successfully added.'.format(form.title.data)
        flash(message, 'success')
        return redirect(url_for('.view_posts'))

    return render_template('blog/post_add.html', form=form)
Exemple #8
0
def add_post():
    form = PostForm(request.form)

    if form.validate_on_submit():
        status = PostStatus.from_string(form.status.data)

        post = Post(author=current_user,
                    title=form.title.data,
                    slug=form.slug.data,
                    body=form.body.data,
                    status=status,
                    tags=form.tags.data)

        if status == PostStatus.published:
            post.pub_date = utc_now()

        db.session.add(post)
        db.session.commit()

        message = u'Post "{}" successfully added.'.format(form.title.data)
        flash(message, 'success')
        return redirect(url_for('.view_posts'))

    return render_template('blog/post_add.html', form=form)
Exemple #9
0
    def create_post(self, create_user=True, **kwargs):
        post_data = {
            'title': 'First Tiger on the Moon',
            'slug': 'first-tiger-moon',
            'body': 'The contents.'
        }

        if 'author' not in kwargs:
            user = User(email='*****@*****.**', short_name='Ryan')
            db.session.add(user)
            post_data['author'] = user

        post_data.update(kwargs)
        post = Post(**post_data)

        db.session.add(post)
        db.session.commit()

        return post
Exemple #10
0
 def test_post_tags_converted_to_comma_separated_string(self):
     post = Post(tags=[Tag('green'), Tag('blue')])
     form = PostForm(obj=post)
     self.assertEqual(form.tags._value(), 'green, blue')
Exemple #11
0
    def test_preview(self):
        post = Post(body=u'test <!-- preview -->')
        self.assertEqual(post.preview, u'test...')

        post = Post(body=u'test ')
        self.assertEqual(post.preview, u'test')
Exemple #12
0
def index(page):
    recent_posts = Post.get_recent(page)
    return render_template('general/index.html', posts=recent_posts)
Exemple #13
0
 def test_url(self):
     post = Post(status=PostStatus.published, slug='test-post')
     self.assertEqual(post.url, None)
     post.pub_date = datetime.date(2012, 4, 3)
     self.assertEqual(post.url, '/blog/2012/04/03/test-post')
Exemple #14
0
 def test_is_published(self):
     post = Post(status=PostStatus.published)
     self.assertTrue(post.is_published)
Exemple #15
0
 def test_get_published(self):
     post = self.create_post()
     self.assertEqual(len(Post.get_published().all()), 0)
     post.publish()
     self.assertEqual(len(Post.get_published().all()), 1)
Exemple #16
0
 def test_get_recent(self):
     post = self.create_post()
     self.assertEqual(len(Post.get_recent(1).items), 0)
     post.publish()
     self.assertEqual(len(Post.get_recent(1).items), 1)
Exemple #17
0
def index(page):
    recent_posts = Post.get_recent(page)
    return render_template('general/index.html', posts=recent_posts)