Exemple #1
0
def create_post(slug):
    """Create a new or edit an existing blog post"""
    next = request.values.get('next', '')
    post = None
    if slug:
        post = Post.query.filter_by(slug=slug).first()
    if slug and not post:
        flash('Invalid slug', 'error')
        return redirect(next)

    if request.method == 'GET':
        return render_template('admin/compose.html',
                               post=post,
                               categories=Category.query.all())

    if request.method == 'POST':
        if request.form['action'] == 'Cancel':
            return redirect(next)

        title = request.form['title']
        markup = request.form['markup']
        tags = normalize_tags(request.form['tags'])
        comments_allowed = bool(request.values.get('comments_allowed', False))
        visible = bool(request.values.get('visible', False))
        #: Contains the ids of the categories
        categories = []
        for name, id in request.form.iteritems():
            if 'category-' in name:
                categories.append(id)

        if title == '':
            flash('You must provide a title', 'error')
            return render_template('admin/compose.html')
        elif request.form['action'] == 'Publish':
            post = Post(title, markup, comments_allowed, visible)
            post.tags = tags
            post.categories = categories
            db.session.add(post)
            db.session.commit()
            signals.post_created.send(post)
            flash('New post was successfully posted')
            return redirect(url_for('main.show_posts'))
        elif request.form['action'] == 'Update':
            post.title = title
            post.markup = markup
            post.comments_allowed = comments_allowed
            post.visible = visible
            post.tags = tags
            post.categories = categories
            db.session.commit()
            signals.post_updated.send(post)
            flash('Post was successfully updated')
            return redirect(url_for('main.show_post', slug=post.slug))
Exemple #2
0
def create_post(slug):
    """Create a new or edit an existing blog post"""
    next = request.values.get('next', '')
    post = None
    if slug:
        post = Post.query.filter_by(slug=slug).first()
    if slug and not post:
        flash('Invalid slug', 'error')
        return redirect(next)
    
    if request.method == 'GET':
        return render_template('admin/compose.html', post=post,
            categories=Category.query.all())
            
    if request.method == 'POST':
        if request.form['action'] == 'Cancel':
            return redirect(next)
        
        title = request.form['title']
        markup = request.form['markup']
        tags = normalize_tags(request.form['tags'])
        comments_allowed = bool(request.values.get('comments_allowed', False))
        visible = bool(request.values.get('visible', False))
        #: Contains the ids of the categories
        categories = []
        for name, id in request.form.iteritems():
            if 'category-' in name:
                categories.append(id)
                
        if title == '':
            flash('You must provide a title', 'error')
            return render_template('admin/compose.html')
        elif request.form['action'] == 'Publish':
            post = Post(title, markup, comments_allowed, visible)
            post.tags = tags
            post.categories = categories
            db.session.add(post)
            db.session.commit()
            signals.post_created.send(post)
            flash('New post was successfully posted')
            return redirect(url_for('main.show_posts'))
        elif request.form['action'] == 'Update':
            post.title = title
            post.markup = markup
            post.comments_allowed = comments_allowed
            post.visible = visible
            post.tags = tags
            post.categories = categories
            db.session.commit()
            signals.post_updated.send(post)
            flash('Post was successfully updated')
            return redirect(url_for('main.show_post', slug=post.slug))
Exemple #3
0
def preview():
    """Returns a preview of a blog post. Use with an Ajax request"""
    args = request.form
    # Mimic post object
    post = dict(
        slug=normalize(args['title']), 
        title=args['title'],
        markup=args['markup'],
        visible=True,
        html=convert_markup(args['markup']),
        datetime=datetime.datetime.fromtimestamp(int(args['datetime'])),
        # Mimic the tag relationship field of post
        tags=[dict(name=tag) for tag in normalize_tags(args['tags'])],
        # Mimic the category relationship field of post
        categories=[dict(name=name) for name in 
            filter(lambda x: x != '', set(args['categories'].split(',')))],
    )
    return render_template('admin/_preview.html', post=post)
Exemple #4
0
def preview():
    """Returns a preview of a blog post. Use with an Ajax request"""
    args = request.form
    # Mimic post object
    post = dict(
        slug=normalize(args['title']),
        title=args['title'],
        markup=args['markup'],
        visible=True,
        html=convert_markup(args['markup']),
        datetime=datetime.datetime.fromtimestamp(int(args['datetime'])),
        # Mimic the tag relationship field of post
        tags=[dict(name=tag) for tag in normalize_tags(args['tags'])],
        # Mimic the category relationship field of post
        categories=[
            dict(name=name) for name in filter(
                lambda x: x != '', set(args['categories'].split(',')))
        ],
    )
    return render_template('admin/_preview.html', post=post)
Exemple #5
0
def test_tags_normalizing():
    """Test the correct interpretation of a string of comma separated tags"""
    assert_equal(helpers.normalize_tags(''), [])
    assert_equal(helpers.normalize_tags(','), [])
    assert_equal(helpers.normalize_tags('cool'), ['cool'])
    assert_equal(helpers.normalize_tags('cool,cool'), ['cool'])
    assert_equal(helpers.normalize_tags('cool, cooler'), ['cool', 'cooler'])
    assert_equal(helpers.normalize_tags('cool, cooler '), ['cool', 'cooler'])
    assert_equal(helpers.normalize_tags('cool, cooler ,'), ['cool', 'cooler'])
    assert_equal(helpers.normalize_tags('cool, cooler ,  '),
                 ['cool', 'cooler'])
    assert_equal(helpers.normalize_tags(',cool, cooler ,  '),
                 ['cool', 'cooler'])
    assert_equal(helpers.normalize_tags(' ,cool, cooler ,,  '),
                 ['cool', 'cooler'])
    assert_equal(helpers.normalize_tags("django, franz und bertha,vil/bil"),
                 ['django', 'franz-und-bertha', 'vil-bil'])
Exemple #6
0
def test_tags_normalizing():
    """Test the correct interpretation of a string of comma separated tags"""
    assert_equal(helpers.normalize_tags(''), [])
    assert_equal(helpers.normalize_tags(','), [])
    assert_equal(helpers.normalize_tags('cool'), ['cool'])
    assert_equal(helpers.normalize_tags('cool,cool'), ['cool'])
    assert_equal(helpers.normalize_tags('cool, cooler'), ['cool', 'cooler'])
    assert_equal(helpers.normalize_tags('cool, cooler '), ['cool', 'cooler'])
    assert_equal(helpers.normalize_tags('cool, cooler ,'), ['cool', 'cooler'])
    assert_equal(helpers.normalize_tags('cool, cooler ,  '), ['cool', 'cooler'])
    assert_equal(
        helpers.normalize_tags(',cool, cooler ,  '), ['cool', 'cooler'])
    assert_equal(
        helpers.normalize_tags(' ,cool, cooler ,,  '), ['cool', 'cooler'])
    assert_equal(
        helpers.normalize_tags("django, franz und bertha,vil/bil"),
        ['django','franz-und-bertha','vil-bil'])