diff --git a/src/.buildinfo.json b/src/.buildinfo.json new file mode 100644 index 0000000..61a8160 --- /dev/null +++ b/src/.buildinfo.json @@ -0,0 +1,6 @@ +{ + "tag": "jc-ng-localtest:", + "date": "2025-11-02", + "host": "jake-e580", + "user": "jake" +} diff --git a/src/comments.py b/src/comments.py new file mode 100644 index 0000000..bd47d61 --- /dev/null +++ b/src/comments.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +import sqlite3 +from os import path +from datetime import datetime +import json +from flask import request, Response, redirect +from index import app +from projects import md_directory, get_by_meta_key + +database = '/tmp/db' #path.join(md_directory, 'comments.db') +with sqlite3.Connection(database) as db: + db.execute('CREATE TABLE IF NOT EXISTS comments (date INTEGER, article TEXT, name TEXT, comment TEXT)') + db.commit() + +@app.route('/comments/
', methods=['GET', 'POST']) +def post_comments(article: str): + match request.method: + case 'POST': + if len(get_by_meta_key(md_directory, 'id', article)) == 0: + return Response(status=404) + with sqlite3.Connection(database) as db: + db.execute('INSERT INTO comments (date, article, name, comment) VALUES (?, ?, ?, ?)', (datetime.now(), article, request.form.get('name'), request.form.get('comment'))) + db.commit() + return redirect(f'/projects/{article}') + case 'GET': + if len(get_by_meta_key(md_directory, 'id', article)) == 1: + with sqlite3.Connection(database) as db: + res = db.execute('SELECT * FROM comments WHERE `article` = ?', (article,)) + return json.dumps([{'author': x[2], 'date': x[0], 'comment': x[3]} for x in res.fetchall()]) diff --git a/src/index.py b/src/index.py index 98057c4..96eb5e0 100755 --- a/src/index.py +++ b/src/index.py @@ -10,9 +10,10 @@ from flask import Flask, render_template, Response app = Flask(__name__) # These imports need to come after our app is defined as they add routes to it. +import sitemap # pylint: disable=wrong-import-position,unused-import import projects # pylint: disable=wrong-import-position,unused-import import contact # pylint: disable=wrong-import-position,unused-import -import sitemap # pylint: disable=wrong-import-position,unused-import +import comments # pylint: disable=wrong-import-position,unused-import class DiscordLogger(logging.Handler): ''' Simple logging handler to send a message to Discord ''' @@ -82,3 +83,6 @@ def error(code) -> str: return render_template('error.html', error=f'{code}: {error_definitions.get(int(code))}', description=error_desc.get(int(code))) + +if __name__ == '__main__': + app.run() \ No newline at end of file diff --git a/src/static/js/get_comments.js b/src/static/js/get_comments.js new file mode 100644 index 0000000..e69de29 diff --git a/src/templates/article.html b/src/templates/article.html index 6aba5b7..a5cc387 100755 --- a/src/templates/article.html +++ b/src/templates/article.html @@ -6,5 +6,21 @@
{{post|safe}} +
+

{{ comments | length }} comments

+ {% for comment in comments %} +
+ {{ comment[2] }} +

{{ comment[3] }}

+
+ {% endfor %} +
+ + + + + +
+
{% include 'footer.html' %} \ No newline at end of file