From abc4574988c9ea93ac349812641188558604b6af Mon Sep 17 00:00:00 2001 From: 0x01fe Date: Mon, 1 Jul 2024 10:30:42 -0500 Subject: [PATCH] refactored how comments are handled to fit in with the posts class --- app/app.py | 40 +++++++++++++--------------------- app/post.py | 9 +++++--- app/templates/index.html | 8 +++---- app/templates/music.html | 8 +++---- app/templates/programming.html | 8 +++---- 5 files changed, 33 insertions(+), 40 deletions(-) diff --git a/app/app.py b/app/app.py index 3e81752..037de4f 100644 --- a/app/app.py +++ b/app/app.py @@ -46,7 +46,7 @@ MUSIC_API_TOKEN = config['AUTH']['MUSIC_API_TOKEN'] MUSIC_API_URL = config['NETWORK']['MUSIC_API_URL'] statuses = {} -def get_posts_and_comments(category_filter : str | None = None) -> list[tuple[dict, list]]: +def get_posts(category_filter : str | None = None) -> list[tuple[dict, list]]: post_files = glob.glob(f'{POSTS_FOLDER}/*') try: post_files.remove(f'{POSTS_FOLDER}/POST_TEMPLATE.md') @@ -76,22 +76,12 @@ def get_posts_and_comments(category_filter : str | None = None) -> list[tuple[di ordered_posts.append(most_recent) posts.remove(most_recent) - posts = reversed(ordered_posts) + # Convert to dict + posts = [] + for post in reversed(ordered_posts): + posts.append(post.__dict__) - # Get Comments - posts_and_comments = [] - - for post in posts: - - comments = comment.get_comments(post.title) - posts_and_comments.append(({ - "body" : post.body, - "title" : post.title, - "date" : post.get_date() - }, - comments)) - - return posts_and_comments + return posts def read_status_file() -> dict: with open(STATUS_FILE, 'r', encoding='utf-8') as file: @@ -129,7 +119,7 @@ def get_status() -> str: def index(): # Get posts - posts_and_comments = get_posts_and_comments() + posts = get_posts() if 'username' in flask.session: user = flask.session['username'] @@ -142,14 +132,14 @@ def index(): # Setup Comment Form form = comment.CommentForm() - return flask.render_template('index.html', posts=posts_and_comments, status=status, form=form, user=user, title='0x01fe.net') + return flask.render_template('index.html', posts=posts, status=status, form=form, user=user, title='0x01fe.net') # Posts @app.route('/post/') def post(post_name: str): - for post in get_posts_and_comments(): - if post[0]['title'] == post_name: + for post in get_posts(): + if post['title'] == post_name: if 'username' in flask.session: user = flask.session['username'] @@ -168,7 +158,7 @@ def post(post_name: str): def category_filter(category: str): # Get posts - posts_and_comments = get_posts_and_comments(category_filter=category) + posts = get_posts(category_filter=category) if 'username' in flask.session: user = flask.session['username'] @@ -181,14 +171,14 @@ def category_filter(category: str): # Setup Comment Form form = comment.CommentForm() - return flask.render_template('index.html', posts=posts_and_comments, status=status, form=form, user=user, title=category.replace('-', ' ')) + return flask.render_template('index.html', posts=posts, status=status, form=form, user=user, title=category.replace('-', ' ')) # Music Page @app.route('/music/') def music(): # Get posts - posts_and_comments = get_posts_and_comments(category_filter="music") + posts = get_posts(category_filter="music") if 'username' in flask.session: user = flask.session['username'] @@ -219,14 +209,14 @@ def music(): # Setup Comment Form form = comment.CommentForm() - return flask.render_template('music.html', posts=posts_and_comments, status=status, top_albums=top_albums, form=form, user=user) + return flask.render_template('music.html', posts=posts, status=status, top_albums=top_albums, form=form, user=user) # Programming Page @app.route('/programming/') def programming(): # Get posts - posts_and_comments = get_posts_and_comments(category_filter="programming") + posts_and_comments = get_posts(category_filter="programming") if 'username' in flask.session: user = flask.session['username'] diff --git a/app/post.py b/app/post.py index 9accc64..24e4113 100644 --- a/app/post.py +++ b/app/post.py @@ -1,15 +1,19 @@ import markdown import datetime +import comment + class Post: category : str author : str date : datetime.datetime + date_str : str body : str file : str title : str url : str + comments : list[dict] def __init__(self, file_path): self.file = file_path @@ -24,8 +28,7 @@ class Post: date = lines[3].split(":")[1].strip() self.date = datetime.datetime.strptime(date, "%d-%m-%Y") + self.date_str = self.date.strftime("%B %d, %Y") self.body = markdown.markdown(f'# [{self.title}]({self.url})\n' + ''.join(lines[7:]), extensions=['footnotes']) - - def get_date(self) -> str: - return self.date.strftime("%B %d, %Y") + self.comments = comment.get_comments(self.title) diff --git a/app/templates/index.html b/app/templates/index.html index a395ad5..6110f5c 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -29,12 +29,12 @@ {% for post in posts %}
- {{ post[0].body|safe }} + {{ post.body|safe }}

Comments

- {% for comment in post[1] %} + {% for comment in post.comments %}

{{ comment.username }}

{{ comment.content }}

@@ -43,7 +43,7 @@ {% if user %}

{{ user }}

-
+ {{ form.hidden_tag() }} {{ form.textbox }} diff --git a/app/templates/music.html b/app/templates/music.html index ce6f874..df91746 100644 --- a/app/templates/music.html +++ b/app/templates/music.html @@ -29,12 +29,12 @@ {% for post in posts %}
- {{ post[0].body|safe }} + {{ post.body|safe }}

Comments

- {% for comment in post[1] %} + {% for comment in post.comments %}

{{ comment.username }}

{{ comment.content }}

@@ -43,7 +43,7 @@ {% if user %}

{{ user }}

- + {{ form.hidden_tag() }} {{ form.textbox }} diff --git a/app/templates/programming.html b/app/templates/programming.html index 516b171..cb81887 100644 --- a/app/templates/programming.html +++ b/app/templates/programming.html @@ -29,12 +29,12 @@ {% for post in posts %}
- {{ post[0].body|safe }} + {{ post.body|safe }}

Comments

- {% for comment in post[1] %} + {% for comment in post.comments %}

{{ comment.username }}

{{ comment.content }}

@@ -43,7 +43,7 @@ {% if user %}

{{ user }}

- + {{ form.hidden_tag() }} {{ form.textbox }}