Exemple #1
0
def update( file_name, interactive=False ):
    '''update( file_name, interactive=False )

    Formats and updates a post in a restblog from the given `file_name`.

    Parameters:
    - file_name     Input file name with a post written in reStructuredText.
    - interactive   Prompts for missing credentials if set to True. False by
                    default.
    '''

    try:
        html_file_name = post.createFormattedPost( file_name )
        metadata, contents = post.getPostContents( html_file_name )
        
        id = int( metadata.attrib[ 'id' ] )
        publish = metadata.attrib.get( 'publish', 'yes' ) == 'yes'
        blog = server.connect( interactive=interactive )
        if metadata.attrib[ 'type' ] == 'page':
            success = blog.editPage( id, contents, publish=publish )
        else:
            success = blog.editPost( id, contents, publish=publish )
        if not success:
            raise RuntimeError, 'Unexpected error when updating post'
    except Exception, ex:
        print 'Unable to update restblog post from file %(file_name)s. Error: %(ex)s' % locals()
Exemple #2
0
def delete( post_id, interactive=False, force=False ):
    '''delete( post_id, interactive=False, force=False )

    Deletes the post with the given `post_id` from a restblog.

    Parameters:

    - post_id: Post ID to delete.
    - interactive: Prompts for missing credentials if set to True. False by
      default.
    - force: Force deletion without confirmation.
    '''

    try:
        blog = server.connect( interactive=interactive )
        if not force:
            prompt = 'Delete post number %(post_id)s? [y/N]: ' % locals()
            confirmed = raw_input( prompt )
            if confirmed.lower() != 'y':
                logger.info( 'Post will not be deleted.' )
                return
        logger.info( 'Deleting post %(post_id)s', locals() )
        blog.deletePost( post_id, publish=True )
        logger.info( 'Done.' )
    except Exception, ex:
        print 'Unable to delete post %(post_id)s from restblog. Error: %(ex)s' % locals()
Exemple #3
0
def insert( file_name, interactive=False ):
    '''insert( file_name, interactive=False )

    Formats and inserts a new post in a restblog from the given `file_name`.

    Parameters:
    - file_name     Input file name with a post written in reStructuredText.
    - interactive   Prompts for missing credentials if set to True. False by
                    default.
    '''

    try:
        html_file_name = post.createFormattedPost( file_name )
        metadata, contents = post.getPostContents( html_file_name )
                
        publish = metadata.attrib.get( 'publish', 'yes' ) == 'yes'
        blog = server.connect( interactive=interactive )
        if metadata.attrib[ 'type' ] == 'page':
            id = blog.newPage( contents, publish=publish )
        else:
            id = blog.newPost( contents, publish=publish )
        metadata.attrib[ 'id' ] = id
        post.updateSourceMetadata( file_name, metadata )
    except Exception, ex:
        print 'Unable to insert post into restblog from file %(file_name)s. Error: %(ex)s' % locals()
Exemple #4
0
def listPosts( url, count, header, user, password, interactive=False ):
    blog = server.connect( url, user=user, password=password, interactive=interactive )
    posts = blog.getRecentPostTitles( count )
    if not posts:
        print 'No posts found.'
        return
    maximum_width = findMaximumWidth( posts, padding=2 )
    format = \
        '%%(postid)%(postid)ds  ' \
        '%%(title)-%(title)ds  ' \
        '%%(restblog_timestamp)-%(restblog_timestamp)ds' \
        % maximum_width
    if header:
        text = format % dict( postid='id', title='title', restblog_timestamp='created' )
        print text
        print '-'*len( text )
    for post in posts:
        postid = post[ 'postid' ]
        title = post[ 'title' ]
        restblog_timestamp = str( post[ 'dateCreated' ] ).replace( 'T', ' ' )
        print format % locals()