From 76688b2d2c1e4cc47e865a9e66d6ae3f92b0315b Mon Sep 17 00:00:00 2001 From: 0x01fe Date: Wed, 3 Apr 2024 13:13:48 -0500 Subject: [PATCH] started working on the comments feature --- .gitignore | 5 ++++- app/app.py | 21 +++++++++++++++---- app/comment.py | 16 ++++++++++---- app/post.py | 5 ++++- app/posts/POST_TEMPLATE.md | 1 + app/static/style.css | 10 ++++----- app/templates/index.html | 3 +-- app/templates/user/register.html | 36 ++++++++++++++++++++++++++++++++ app/user.py | 25 ++++++++++++++++++++++ 9 files changed, 105 insertions(+), 17 deletions(-) create mode 100644 app/templates/user/register.html create mode 100644 app/user.py diff --git a/.gitignore b/.gitignore index 8b502d0..170b585 100644 --- a/.gitignore +++ b/.gitignore @@ -5,8 +5,11 @@ __pycache__ docker-compose.yaml *.sh -*.ini # Ignore images in posts *.jpg *.png + +# Flask Data & Config +*.ini +data diff --git a/app/app.py b/app/app.py index c5509d4..e7d9301 100644 --- a/app/app.py +++ b/app/app.py @@ -1,6 +1,7 @@ import glob import configparser import random +import base64 import flask import flask_wtf.csrf @@ -10,12 +11,13 @@ import markdown from post import Post import comment +import user app = flask.Flask(__name__, static_url_path='', static_folder='static') +app.register_blueprint(comment.comments) +app.register_blueprint(user.user) -csrf = flask_wtf.csrf.CSRFProtect() -csrf.init_app(app) - +# CONFIG CONFIG_PATH = "./config.ini" config = configparser.ConfigParser() config.read(CONFIG_PATH) @@ -25,6 +27,17 @@ STATUS_FILE = config['STATUS']['STATUS_FILE'] PORT = int(config['NETWORK']['PORT']) DEV = int(config['NETWORK']['DEV']) + +# CSRF Protect +app.config['SECRET_KEY'] = base64.b64decode(config["FLASK"]["SECRET"]) +csrf = flask_wtf.csrf.CSRFProtect() +csrf.init_app(app) + +# Session Setup +app.config['SESSION_TYPE'] = 'filesystem' +app.config['SESSION_FILE_DIR'] = './data/.flask_session/' +flask_session.Session(app) + def get_posts(category_filter : str | None = None) -> list[Post]: post_files = glob.glob(f'{POSTS_FOLDER}/*') try: @@ -85,7 +98,7 @@ def index(): # Setup Comment Form form = comment.CommentForm() - return flask.render_template('index.html', posts=posts_and_comments, status=status, form=form) + return flask.render_template('index.html', posts=posts_and_comments, status=status, form=form, user="yes") # Games Page @app.route('/games/') diff --git a/app/comment.py b/app/comment.py index 9b5d3b1..6eaa510 100644 --- a/app/comment.py +++ b/app/comment.py @@ -1,14 +1,22 @@ +import json + import flask import flask_session -import flask_wtf +import flask_wtf.csrf import wtforms - - +comments = flask.Blueprint('comment', __name__, template_folder='./templates') class CommentForm(flask_wtf.FlaskForm): - textbox = wtforms.TextAreaField() + textbox = wtforms.TextAreaField('Input') + +@comments.route('/comment/', methods=['POST']) +def comment(): + form = CommentForm(csrf_enabled=True) + + return flask.redirect('/') +def get_comments(post_id : int) -> list[dict]: diff --git a/app/post.py b/app/post.py index 1de822a..f087a29 100644 --- a/app/post.py +++ b/app/post.py @@ -8,6 +8,7 @@ class Post: date : datetime.datetime body : str file : str + id : int def __init__(self, file_path): self.file = file_path @@ -21,5 +22,7 @@ class Post: date = lines[3].split(":")[1].strip() self.date = datetime.datetime.strptime(date, "%d-%m-%Y") - self.body = markdown.markdown(''.join(lines[6:])) + self.id = int(lines[4].split(":")[1].strip()) + + self.body = markdown.markdown(''.join(lines[7:])) diff --git a/app/posts/POST_TEMPLATE.md b/app/posts/POST_TEMPLATE.md index 7fd0fb8..fa1630e 100644 --- a/app/posts/POST_TEMPLATE.md +++ b/app/posts/POST_TEMPLATE.md @@ -2,6 +2,7 @@ category: category author: author date: date +id: post id # POST ## TITLE diff --git a/app/static/style.css b/app/static/style.css index 3d83e15..68cbe2c 100644 --- a/app/static/style.css +++ b/app/static/style.css @@ -182,16 +182,16 @@ a:hover { margin: 1em; } -.comment-editor input { +.comment-editor textarea { width: 100%; - padding: 3em 0; - line-height: 140%; + height: 6em; + padding: 0.75em; border-style: solid; - /* border-color: var(--secondary50); */ + border-color: var(--secondary50); border-radius: var(--border-radius); } -.comment-editor input:focus { +.comment-editor textarea:focus { border: 3px solid var(--accent); } diff --git a/app/templates/index.html b/app/templates/index.html index 21daca3..d349f43 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -34,8 +34,7 @@ {% if user %}

{{ user }}

-
- + {{ form.textbox }}
diff --git a/app/templates/user/register.html b/app/templates/user/register.html new file mode 100644 index 0000000..27804b0 --- /dev/null +++ b/app/templates/user/register.html @@ -0,0 +1,36 @@ + + +
+ + + 0x01fe.net - Register +
+ +
+

0x01fe.net

+ {{ status|safe }} +
+
+ + + + + +
+

Register

+
+ {{ form.username }} + {{ form.password }} + +
+
+
+ + diff --git a/app/user.py b/app/user.py new file mode 100644 index 0000000..ff7c758 --- /dev/null +++ b/app/user.py @@ -0,0 +1,25 @@ +import flask +import flask_wtf.csrf +import wtforms + +user = flask.Blueprint('user', __name__, template_folder='./templates/user') + +class RegisterUserForm(flask_wtf.FlaskForm): + username = wtforms.StringField("Username", [ + wtforms.validators.Length(min=4, max=32), + wtforms.validators.DataRequired() + ]) + password = wtforms.PasswordField("Password", [ + wtforms.validators.Length(min=8, max=64), + wtforms.validators.DataRequired() + ]) + +@user.route('/user/add/', methods=["POST"]) +def add_user(): + pass + +@user.route('/user/register/') +def register_user(): + form = RegisterUserForm() + + return flask.render_template('register.html', form=form)