refactored how comments are handled to fit in with the posts class

This commit is contained in:
0x01fe 2024-07-01 10:30:42 -05:00
parent 8712b8734e
commit abc4574988
5 changed files with 33 additions and 40 deletions

View File

@ -46,7 +46,7 @@ MUSIC_API_TOKEN = config['AUTH']['MUSIC_API_TOKEN']
MUSIC_API_URL = config['NETWORK']['MUSIC_API_URL'] MUSIC_API_URL = config['NETWORK']['MUSIC_API_URL']
statuses = {} 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}/*') post_files = glob.glob(f'{POSTS_FOLDER}/*')
try: try:
post_files.remove(f'{POSTS_FOLDER}/POST_TEMPLATE.md') 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) ordered_posts.append(most_recent)
posts.remove(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 return posts
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
def read_status_file() -> dict: def read_status_file() -> dict:
with open(STATUS_FILE, 'r', encoding='utf-8') as file: with open(STATUS_FILE, 'r', encoding='utf-8') as file:
@ -129,7 +119,7 @@ def get_status() -> str:
def index(): def index():
# Get posts # Get posts
posts_and_comments = get_posts_and_comments() posts = get_posts()
if 'username' in flask.session: if 'username' in flask.session:
user = flask.session['username'] user = flask.session['username']
@ -142,14 +132,14 @@ def index():
# Setup Comment Form # Setup Comment Form
form = comment.CommentForm() 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 # Posts
@app.route('/post/<string:post_name>') @app.route('/post/<string:post_name>')
def post(post_name: str): def post(post_name: str):
for post in get_posts_and_comments(): for post in get_posts():
if post[0]['title'] == post_name: if post['title'] == post_name:
if 'username' in flask.session: if 'username' in flask.session:
user = flask.session['username'] user = flask.session['username']
@ -168,7 +158,7 @@ def post(post_name: str):
def category_filter(category: str): def category_filter(category: str):
# Get posts # Get posts
posts_and_comments = get_posts_and_comments(category_filter=category) posts = get_posts(category_filter=category)
if 'username' in flask.session: if 'username' in flask.session:
user = flask.session['username'] user = flask.session['username']
@ -181,14 +171,14 @@ def category_filter(category: str):
# Setup Comment Form # Setup Comment Form
form = comment.CommentForm() 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 # Music Page
@app.route('/music/') @app.route('/music/')
def music(): def music():
# Get posts # Get posts
posts_and_comments = get_posts_and_comments(category_filter="music") posts = get_posts(category_filter="music")
if 'username' in flask.session: if 'username' in flask.session:
user = flask.session['username'] user = flask.session['username']
@ -219,14 +209,14 @@ def music():
# Setup Comment Form # Setup Comment Form
form = comment.CommentForm() 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 # Programming Page
@app.route('/programming/') @app.route('/programming/')
def programming(): def programming():
# Get posts # 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: if 'username' in flask.session:
user = flask.session['username'] user = flask.session['username']

View File

@ -1,15 +1,19 @@
import markdown import markdown
import datetime import datetime
import comment
class Post: class Post:
category : str category : str
author : str author : str
date : datetime.datetime date : datetime.datetime
date_str : str
body : str body : str
file : str file : str
title : str title : str
url : str url : str
comments : list[dict]
def __init__(self, file_path): def __init__(self, file_path):
self.file = file_path self.file = file_path
@ -24,8 +28,7 @@ class Post:
date = lines[3].split(":")[1].strip() date = lines[3].split(":")[1].strip()
self.date = datetime.datetime.strptime(date, "%d-%m-%Y") 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']) self.body = markdown.markdown(f'# [{self.title}]({self.url})\n' + ''.join(lines[7:]), extensions=['footnotes'])
self.comments = comment.get_comments(self.title)
def get_date(self) -> str:
return self.date.strftime("%B %d, %Y")

View File

@ -29,12 +29,12 @@
{% for post in posts %} {% for post in posts %}
<div class="post"> <div class="post">
<div class="post-date"> <div class="post-date">
{{ post[0].date }} {{ post.date_str }}
</div> </div>
{{ post[0].body|safe }} {{ post.body|safe }}
<div class="comment-container"> <div class="comment-container">
<h2>Comments</h2> <h2>Comments</h2>
{% for comment in post[1] %} {% for comment in post.comments %}
<div class="comment"> <div class="comment">
<h4>{{ comment.username }}</h4> <h4>{{ comment.username }}</h4>
<p>{{ comment.content }}</p> <p>{{ comment.content }}</p>
@ -43,7 +43,7 @@
{% if user %} {% if user %}
<div class="comment-editor"> <div class="comment-editor">
<h4>{{ user }}</h4> <h4>{{ user }}</h4>
<form method="post" action="/comment/{{ post[0].title }}"> <form method="post" action="/comment/{{ post.title }}">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ form.textbox }} {{ form.textbox }}
<input type="submit" value="Save"> <input type="submit" value="Save">

View File

@ -29,12 +29,12 @@
{% for post in posts %} {% for post in posts %}
<div class="post"> <div class="post">
<div class="post-date"> <div class="post-date">
{{ post[0].date }} {{ post.date_str }}
</div> </div>
{{ post[0].body|safe }} {{ post.body|safe }}
<div class="comment-container"> <div class="comment-container">
<h2>Comments</h2> <h2>Comments</h2>
{% for comment in post[1] %} {% for comment in post.comments %}
<div class="comment"> <div class="comment">
<h4>{{ comment.username }}</h4> <h4>{{ comment.username }}</h4>
<p>{{ comment.content }}</p> <p>{{ comment.content }}</p>
@ -43,7 +43,7 @@
{% if user %} {% if user %}
<div class="comment-editor"> <div class="comment-editor">
<h4>{{ user }}</h4> <h4>{{ user }}</h4>
<form method="post" action="/comment/{{ post[0].title }}"> <form method="post" action="/comment/{{ post.title }}">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ form.textbox }} {{ form.textbox }}
<input type="submit" value="Save"> <input type="submit" value="Save">

View File

@ -29,12 +29,12 @@
{% for post in posts %} {% for post in posts %}
<div class="post"> <div class="post">
<div class="post-date"> <div class="post-date">
{{ post[0].date }} {{ post.date_str }}
</div> </div>
{{ post[0].body|safe }} {{ post.body|safe }}
<div class="comment-container"> <div class="comment-container">
<h2>Comments</h2> <h2>Comments</h2>
{% for comment in post[1] %} {% for comment in post.comments %}
<div class="comment"> <div class="comment">
<h4>{{ comment.username }}</h4> <h4>{{ comment.username }}</h4>
<p>{{ comment.content }}</p> <p>{{ comment.content }}</p>
@ -43,7 +43,7 @@
{% if user %} {% if user %}
<div class="comment-editor"> <div class="comment-editor">
<h4>{{ user }}</h4> <h4>{{ user }}</h4>
<form method="post" action="/comment/{{ post[0].title }}"> <form method="post" action="/comment/{{ post.title }}">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ form.textbox }} {{ form.textbox }}
<input type="submit" value="Save"> <input type="submit" value="Save">