Example #1
0
def create_author() -> None:
    name = input("Author Name: ")
    biography = input("Biography: ")

    author = Author(name, biography)
    db.add(author)
    save_tip()
Example #2
0
def create_tag() -> None:
    tag_name = input("Tag Name: ")
    tag_short_desc = input("Tag short description: ")
    tag_long_desc = input("Tag long description: ")
    tag_main = yes_or_no("Is tag a section")
    tag = Tag(tag_name, tag_short_desc, tag_long_desc, tag_main)
    db.add(tag)
    save_tip()
Example #3
0
def create_friend() -> None:
    friend_name = input("Friend Name: ")
    friend_link = input("Link: ")
    friend_description = input("Description: ")

    friend = Friend(friend_name, friend_link, friend_description)
    db.add(friend)
    save_tip()
Example #4
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()
Example #5
0
        print(f"Deleted author \"{author.name}\".")
        save_tip()
        return

    print(f"No author with name: \"{author_name}\".")


def create_blog_post() -> None:
    author_name = input("Author Name: ")
    if not (author := db.query(Author).filter_by(name=author_name).first()):
        print(f"No author with name: \"{author_name}\".")
        exit()

    title = input("Blog Post Title: ")
    blog_post = BlogPost(title, author)
    db.add(blog_post)

    os.makedirs(blog_post.resources_path)
    open(blog_post.markdown_path, "w").close()

    save_tip()


def set_post_flags() -> None:
    blog_post = get_blog_post_by_id()
    blog_post.include_in_graph = yes_or_no("Enable graph annotations")
    blog_post.allow_comments = yes_or_no("Allow comments")
    blog_post.hidden = yes_or_no("Hide post")

    print(f"Changed flags of \"{blog_post.name}\".")
    save_tip()
Example #6
0
def register_referrer() -> None:
    # Save only the hostname (more could be dangerous privacy-wise) of the referrer url.
    if raw_hostname := urlparse(request.referrer).hostname:
        try_with_integrity_protection(
            lambda: db.add(ReferrerHostname(raw_hostname)))