refactored how comments are handled to fit in with the posts class
This commit is contained in:
parent
8712b8734e
commit
abc4574988
40
app/app.py
40
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/<string:post_name>')
|
||||
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']
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -29,12 +29,12 @@
|
||||
{% for post in posts %}
|
||||
<div class="post">
|
||||
<div class="post-date">
|
||||
{{ post[0].date }}
|
||||
{{ post.date_str }}
|
||||
</div>
|
||||
{{ post[0].body|safe }}
|
||||
{{ post.body|safe }}
|
||||
<div class="comment-container">
|
||||
<h2>Comments</h2>
|
||||
{% for comment in post[1] %}
|
||||
{% for comment in post.comments %}
|
||||
<div class="comment">
|
||||
<h4>{{ comment.username }}</h4>
|
||||
<p>{{ comment.content }}</p>
|
||||
@ -43,7 +43,7 @@
|
||||
{% if user %}
|
||||
<div class="comment-editor">
|
||||
<h4>{{ user }}</h4>
|
||||
<form method="post" action="/comment/{{ post[0].title }}">
|
||||
<form method="post" action="/comment/{{ post.title }}">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ form.textbox }}
|
||||
<input type="submit" value="Save">
|
||||
|
||||
@ -29,12 +29,12 @@
|
||||
{% for post in posts %}
|
||||
<div class="post">
|
||||
<div class="post-date">
|
||||
{{ post[0].date }}
|
||||
{{ post.date_str }}
|
||||
</div>
|
||||
{{ post[0].body|safe }}
|
||||
{{ post.body|safe }}
|
||||
<div class="comment-container">
|
||||
<h2>Comments</h2>
|
||||
{% for comment in post[1] %}
|
||||
{% for comment in post.comments %}
|
||||
<div class="comment">
|
||||
<h4>{{ comment.username }}</h4>
|
||||
<p>{{ comment.content }}</p>
|
||||
@ -43,7 +43,7 @@
|
||||
{% if user %}
|
||||
<div class="comment-editor">
|
||||
<h4>{{ user }}</h4>
|
||||
<form method="post" action="/comment/{{ post[0].title }}">
|
||||
<form method="post" action="/comment/{{ post.title }}">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ form.textbox }}
|
||||
<input type="submit" value="Save">
|
||||
|
||||
@ -29,12 +29,12 @@
|
||||
{% for post in posts %}
|
||||
<div class="post">
|
||||
<div class="post-date">
|
||||
{{ post[0].date }}
|
||||
{{ post.date_str }}
|
||||
</div>
|
||||
{{ post[0].body|safe }}
|
||||
{{ post.body|safe }}
|
||||
<div class="comment-container">
|
||||
<h2>Comments</h2>
|
||||
{% for comment in post[1] %}
|
||||
{% for comment in post.comments %}
|
||||
<div class="comment">
|
||||
<h4>{{ comment.username }}</h4>
|
||||
<p>{{ comment.content }}</p>
|
||||
@ -43,7 +43,7 @@
|
||||
{% if user %}
|
||||
<div class="comment-editor">
|
||||
<h4>{{ user }}</h4>
|
||||
<form method="post" action="/comment/{{ post[0].title }}">
|
||||
<form method="post" action="/comment/{{ post.title }}">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ form.textbox }}
|
||||
<input type="submit" value="Save">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user