Exemple #1
0
def generate_post(args, logger, force=False):
    """
    Generates a new file with a skeleton for the blog post and
    saves it into the static posts directory.

    The file name is the slug.md. If the file already exists
    user is prompted whether to overwrite the file. This prompt is
    skipped if force is True
    """
    post_path = get_post_path(args.title)

    # Check to see if file exists, if it does prompt for overwite
    if not force and os.path.isfile(post_path):
        if not overwrite_file(post_path):
            logger.info("{} was not re-generated".format(post_path))
            return

    # Create post skeleton
    with open(post_path, 'w') as f:
        f.write("Author: {}\n".format(args.author))
        f.write("Title: {}\n".format(args.title))
        f.write("Tags: {}\n".format(', '.join(getattr(args, 'tags', ''))))
        f.write("\n") # Extra newline between metadata and content
        f.write(args.content)

    logger.info("Generated new file at {}".format(post_path))
Exemple #2
0
 def delete_from_dir(title):
     post_path = get_post_path(title)
     if os.path.isfile(post_path):
         os.remove(post_path)
         logger.info("Removed {}".format(post_path))
     else:
         logger.info("Error: Could not find {}".format(post_path))
Exemple #3
0
def publish_post(args, logger, force=False):
    """
    Persists post to database based on passed args and metadata from static
    post file. A post can be considered "published" once this function is run.

    There are two types of publishing:

    Normal -- sets published_dt to the current time. This post will be
              listed in the posts index and will be included in previous/next
              links.

     Draft -- sets published_dt to None. This post will not appear in the posts
              index and will not be included in previous/next links. However,
              this post can still be viewed through the slug url. This is
              useful if you want to publish your post for select review
              without displaying a link for everyone to see.

    In both cases the post model will be updated and committed to the db. Also,
    new tag models are created if a post includes tags that do not already exist
    in the db.

    Note: We use the post title and env defined post directory to locate which
    static file to use. This is done for convenience so a user can simply type
    in the name of their post instead of a full file path.
    """
    # Open the file and extract attributes
    post_path = get_post_path(args.title)
    try:
        with open(post_path, 'r') as f:
            author = f.readline().strip('n')
            _ = f.readline() # Title line, don't need
            tags = f.readline().strip('\n')
            _ = f.readline() # Line seperating content, don't need
            content = f.read()
    except IOError:
        logger.info("Error: Could not find {}".format(post_path))
        return

    # Process attributes
    author = parse_attr(author)
    slug = slugify(args.title)
    tags = parse_attr(tags).split(', ')
    if '' in tags:
        tags.remove('')

    # If this is a draft post, don't set the publish date
    published_dt = None if args.draft else func.now()

    # Backup posts in production
    if app_config.ENV == 'prod':
        make_backup(app_config.POSTS_DIR, app_config.BACKUP_POSTS_DIR, logger)

    # Prompt whether to delete post if already exists in db
    db = get_db()
    p = db.session.query(Post).filter_by(title=args.title).first()
    if p:
        if not force:
            resp = raw_input("Post with title '{}' already exists in db! Do you want to overwite (y/n)? "\
                   .format(args.title))
            if resp != 'y':
                logger.info("'{}' was not added to the db".format(args.title))
                return
        db.session.delete(p)
        db.session.commit()

    # Create post
    new_post = Post(author=author, title=args.title, slug=slug,
                    content=content, published_dt=published_dt)
    db.session.add(new_post)
    db.session.commit()

    if tags:
        # Add new tags to db
        new_tags = get_new_tags(db, tags)
        db.session.add_all(new_tags)
        db.session.commit()

        # Assign tags to post
        tags = get_tags(db, tags)
        new_post.tags = tags
        db.session.add(new_post)
        db.session.commit()

    action_msg = "Draft published" if args.draft else "Published"
    logger.info("{} {}!".format(action_msg, args.title))