Start implementing comments
This commit is contained in:
6
src/.buildinfo.json
Normal file
6
src/.buildinfo.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"tag": "jc-ng-localtest:",
|
||||
"date": "2025-11-02",
|
||||
"host": "jake-e580",
|
||||
"user": "jake"
|
||||
}
|
||||
30
src/comments.py
Normal file
30
src/comments.py
Normal file
@@ -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/<article>', 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()])
|
||||
@@ -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()
|
||||
0
src/static/js/get_comments.js
Normal file
0
src/static/js/get_comments.js
Normal file
@@ -6,5 +6,21 @@
|
||||
<hr />
|
||||
{{post|safe}}
|
||||
</section>
|
||||
<section id="comments">
|
||||
<h2>{{ comments | length }} comments</h2>
|
||||
{% for comment in comments %}
|
||||
<div class="comment">
|
||||
<strong>{{ comment[2] }}</strong>
|
||||
<p>{{ comment[3] }}</p>
|
||||
</div>
|
||||
{% endfor %}
|
||||
<form action="/comments/{{ metadata.id }}" method="post">
|
||||
<label for="name">Name:</label>
|
||||
<input type="text" name="name" />
|
||||
<label for="comment">Comment:</label>
|
||||
<textarea name="comment"></textarea>
|
||||
<input type="submit" name="submit" value="Submit">
|
||||
</form>
|
||||
</section>
|
||||
</main>
|
||||
{% include 'footer.html' %}
|
||||
Reference in New Issue
Block a user