Ejemplo n.º 1
0
def rename_post() -> None:
    blog_post = get_blog_post_by_id()
    old_slug_path = blog_post.slug_path
    blog_post.name = input(f"Rename post \"{blog_post.name}\" to: ")
    os.rename(old_slug_path, blog_post.slug_path)
    db.commit()
    done()
Ejemplo n.º 2
0
def save_changes() -> None:
    print("Saving changes... ", end="")

    try:
        db.commit()
        done()

    except IntegrityError:
        print("Error!\n\n" +
              "This error was caused by invalid data.\n" +
              "You probably created multiple records with non-unique names.")
Ejemplo n.º 3
0
def process_resource_files(blog_post: BlogPost) -> None:
    for root, _, files in os.walk(blog_post.resources_path, topdown=True):
        for file in files:
            file_path = os.path.join(root, file)
            extension = "." + file.rsplit(".", 1)[-1]

            # Hash file contents, this is the new file name
            file_hash = hash_file(file_path)
            file_title = file_name_to_title(file)

            # Optimize images for web (create thumbnails)
            if imghdr.what(file_path):
                full_size_file_name = f"{file_hash}.{IMAGE_FORMAT}"
                thumbnail_file_name = f"{file_hash}-thumb.{IMAGE_FORMAT}"

                db.add(
                    FileResource(full_size_file_name,
                                 "high-res-" + file,
                                 file_title,
                                 blog_post,
                                 is_image=True,
                                 is_thumbnail=False))

                db.add(
                    FileResource(thumbnail_file_name,
                                 file,
                                 file_title + " (Thumbnail)",
                                 blog_post,
                                 is_image=True,
                                 is_thumbnail=True))

                with Image.open(file_path) as image:
                    image.thumbnail((MAX_THUMBNAIL_WIDTH, -1))
                    image.save(in_res_path(thumbnail_file_name),
                               IMAGE_FORMAT,
                               optimize=True)

                with Image.open(file_path) as image:
                    image.save(in_res_path(full_size_file_name),
                               IMAGE_FORMAT,
                               optimize=True)

            else:
                # Copy the file to the resources dir, no processing
                new_file_name = file_hash + extension
                db.add(FileResource(new_file_name, file, file_title,
                                    blog_post))
                copyfile(file_path, in_res_path(new_file_name))

    db.commit()
Ejemplo n.º 4
0
def recreate_file_resources_for_post(blog_post: BlogPost):
    for file_resource in blog_post.file_resources:
        db.delete(file_resource)
    db.commit()
    process_resource_files(blog_post)
Ejemplo n.º 5
0
def try_with_integrity_protection(statement: Callable) -> None:
    try:
        statement()
        db.commit()
    except IntegrityError:
        db.rollback()  # Duplicate.
Ejemplo n.º 6
0
            lambda: blog_post.comments.append(comment))
        return redirect(
            url_for("home.route_blog_post",
                    blog_post_id=blog_post_id,
                    _name=_name))

    # The cache object is a function-static (like in C) variable
    blog_post_content = route_blog_post.file_cache.get_contents(
        blog_post.html_path)

    # Flush the ip tracker, and then, check if we should count this request as a true hit.
    route_blog_post.ip_tracker.remove_expired()
    if route_blog_post.ip_tracker.should_count_request(request.remote_addr,
                                                       blog_post_id):
        blog_post.hits += 1
        db.commit()

    # Select a random image to be served for opengraph embeds.
    image_resources = [
        fr for fr in blog_post.file_resources if fr.is_thumbnail
    ]
    open_graph_image = in_res_path(
        random.choice(image_resources).name) if image_resources else None

    return render_template("blog_post.html",
                           title=blog_post.name,
                           return_to_root=True,
                           blog_post=blog_post,
                           blog_post_content=blog_post_content,
                           form=CommentForm(),
                           open_graph_image=open_graph_image)