Пример #1
0
def dump_user_comments(user_dir, user):
    comments = Comment.objects.filter(author=user).select_related(
        "author", "post")

    for comment in comments:
        comment_dir = os.path.join(user_dir, f"comments/{comment.id}")
        os.makedirs(comment_dir)

        with open(os.path.join(comment_dir, f"{comment.id}.md"),
                  "w",
                  encoding="utf-8") as f:
            f.write(comment_to_md(comment))

        with open(os.path.join(comment_dir, f"{comment.id}.json"),
                  "w",
                  encoding="utf-8") as f:
            f.write(json.dumps(comment_to_json(comment), ensure_ascii=False))

        # dump replies
        comment_replies = Comment.objects.filter(
            reply_to=comment).select_related("author", "post")
        with open(os.path.join(comment_dir, f"replies.json"),
                  "w",
                  encoding="utf-8") as f:
            f.write(
                json.dumps(comments_to_json(comment_replies),
                           ensure_ascii=False))
Пример #2
0
def dump_user_posts(user_dir, user):
    posts = Post.objects.filter(author=user).select_related("author", "topic")

    for post in posts:
        post_dir = os.path.join(user_dir, f"posts/{post.slug}")
        os.makedirs(post_dir)

        with open(os.path.join(post_dir, f"{post.slug}.md"),
                  "w",
                  encoding="utf-8") as f:
            f.write(post_to_md(post))

        with open(os.path.join(post_dir, f"{post.slug}.json"),
                  "w",
                  encoding="utf-8") as f:
            f.write(json.dumps(post_to_json(post), ensure_ascii=False))

        # dump post comments
        post_comments = Comment.objects.filter(post=post).select_related(
            "author", "post")
        with open(os.path.join(post_dir, f"comments.json"),
                  "w",
                  encoding="utf-8") as f:
            f.write(
                json.dumps(comments_to_json(post_comments),
                           ensure_ascii=False))