예제 #1
0
def update_blogpost(blogpost_short_title):
    if not session.get('logged_in'):
        abort(401)
        
    old_bp = Blogpost.query.filter_by(short_title=blogpost_short_title)\
                     .first_or_404()
    blogpost_title = old_bp.title
    title = unicode(request.form['title'])
    if title!=blogpost_title and not is_blogpost_unique(title):
        flash('Title already exists. Please choose a different title' + \
              ' for your blogpost.')
        # TODO: send previous data
        return redirect(url_for('edit_blogpost', 
                                blogpost_title=blogpost_title))
    short_title = unicode(request.form['short_title']).replace(' ', '-')
    if short_title!=blogpost_short_title and not \
       is_blogpost_short_unique(short_title):
        flash('Short title already exists. Please choose a different ' + \
              'short title for your blogpost.')
        # TODO: send previous data
        return redirect(url_for('edit_blogpost', 
                                blogpost_title=blogpost_title))
    subtitle = unicode(request.form['subtitle'])
    
    text_markdown = unicode(request.form['text'])
    text_html = markdown.markdown(text_markdown, ['codehilite'])
    
    old_tags = list(old_bp.tags)
    blogpost_tags = []
    tags = unicode(request.form['tags'])
    for t in tags.split(','):
        t = t.strip()
        if t:
            blogpost_tags.append(t)           
    
    old_bp.title = title    
    old_bp.subtitle = subtitle
    old_bp.short_title = short_title
    old_bp.text_markdown = text_markdown
    old_bp.text_html = text_html
    old_bp.update_tags(blogpost_tags)
    old_bp.edited = datetime.utcnow()
    db.session.commit()
    delete_unused_tags(old_tags)

    flash('Blogpost has been updated.')
    return redirect(url_for('show_blogpost', 
                            blogpost_short_title=short_title))          
예제 #2
0
def add_blogpost():
    if not session.get('logged_in'):
        abort(401)
        
    title = unicode(request.form['title'])
    if not is_blogpost_unique(title):
        flash('Title already exists. Please choose a different title ' + \
              'for your blogpost.')
        # TODO: send previous data
        return redirect(url_for('create_blogpost'))
    short_title = unicode(request.form['short_title']).replace(' ', '-')
    if not is_blogpost_short_unique(short_title):
        flash('Short title already exists. Please choose a different ' + \
              'short title for your blogpost.')
        # TODO: send previous data
        return redirect(url_for('create_blogpost'))
    subtitle = unicode(request.form['subtitle'])
            
    text_markdown = unicode(request.form['text'])
    text_html = markdown.markdown(text_markdown, ['codehilite'])
    
    blogpost_tags = []
    tags = unicode(request.form['tags'])
    for t in tags.split(','):
        t = t.strip()
        if t:
            blogpost_tags.append(t)           

    user = User.query.filter_by(username=session['user']).first_or_404()
    blogpost = Blogpost(user, title, subtitle, short_title, text_markdown,
                        text_html, blogpost_tags, hidden=False)
    db.session.add(blogpost)       
    db.session.commit()

    flash('New blogpost has been added.')
    return redirect(url_for('show_blog'))