From 9cd68f404880784955b4344043414b80cae8edf1 Mon Sep 17 00:00:00 2001 From: 0x01fe Date: Wed, 26 Jun 2024 15:53:17 -0500 Subject: [PATCH] finished up the comments/user system --- .gitignore | 2 + app/app.py | 27 +++--- app/comment.py | 42 +++++++++- .../2029240f6d1128be89ddc32729463129 | Bin 9 -> 0 bytes .../aa43147407c8a8c678cd91f678f2a023 | Bin 90 -> 0 bytes app/data/comments.json | 8 -- app/data/users.json | 5 -- app/post.py | 5 +- app/static/style.css | 3 +- app/templates/about.html | 3 +- app/templates/games.html | 3 +- app/templates/index.html | 13 ++- app/templates/motion-pictures.html | 3 +- app/templates/music.html | 3 +- app/templates/programming.html | 3 +- app/templates/user/login.html | 40 +++++++++ app/templates/user/register.html | 24 +++--- app/user.py | 77 +++++++++++++++++- 18 files changed, 200 insertions(+), 61 deletions(-) delete mode 100644 app/data/.flask_session/2029240f6d1128be89ddc32729463129 delete mode 100644 app/data/.flask_session/aa43147407c8a8c678cd91f678f2a023 delete mode 100644 app/data/comments.json delete mode 100644 app/data/users.json create mode 100644 app/templates/user/login.html diff --git a/.gitignore b/.gitignore index 0db4e3d..8e92c84 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,8 @@ docker-compose.yaml # Flask Data & Config *.ini data +.flask_session +*.json *.gif # Writing diff --git a/app/app.py b/app/app.py index b672af8..416bf54 100644 --- a/app/app.py +++ b/app/app.py @@ -116,12 +116,21 @@ def index(): # Get posts posts = get_posts() - # Get Comments - comments = [] + if 'username' in flask.session: + user = flask.session['username'] + else: + user = 'Anon' + # Get Comments posts_and_comments = [] for post in posts: - posts_and_comments.append((post.body, comments)) + + comments = comment.get_comments(post.title) + posts_and_comments.append(({ + "body" : post.body, + "title" : post.title + }, + comments)) # Get status status = get_status() @@ -129,17 +138,7 @@ def index(): # Setup Comment Form form = comment.CommentForm() - return flask.render_template('index.html', posts=posts_and_comments, status=status, form=form, user="yes") - -# Posts -@app.route('/post/') -def post(post_name: str): - - for post in get_posts(): - if post.title.replace(' ', '-') == post_name: - return flask.render_template('index.html', posts=[post.body], status=get_status()) - - flask.abort(404) + return flask.render_template('index.html', posts=posts_and_comments, status=status, form=form, user=user) # Posts @app.route('/post/') diff --git a/app/comment.py b/app/comment.py index 6eaa510..0e27ea6 100644 --- a/app/comment.py +++ b/app/comment.py @@ -1,22 +1,56 @@ import json import flask -import flask_session import flask_wtf.csrf import wtforms +COMMENTS_PATH = "./data/comments.json" comments = flask.Blueprint('comment', __name__, template_folder='./templates') class CommentForm(flask_wtf.FlaskForm): textbox = wtforms.TextAreaField('Input') -@comments.route('/comment/', methods=['POST']) -def comment(): +@comments.route('/comment/', methods=['POST']) +def comment(post_title: str): form = CommentForm(csrf_enabled=True) + save_comment(form.textbox.data, post_title) + return flask.redirect('/') +def save_comment(content: str, post_title: str): + with open(COMMENTS_PATH, 'r') as file: + comment_data = json.loads(file.read()) -def get_comments(post_id : int) -> list[dict]: + # See if user is logged in, otherwise setup as anon + if 'username' in flask.session: + username = flask.session['username'] + else: + username = 'Anon' + + comment = { + "username" : username, + "content" : content + } + + # Add comment to JSON data + if post_title in comment_data: + comment_data[post_title].append(comment) + else: + comment_data[post_title] = [comment] + + # Save JSON data + with open(COMMENTS_PATH, 'w') as file: + file.write(json.dumps(comment_data)) + +def get_comments(post_title : int) -> list[dict]: + + with open(COMMENTS_PATH, 'r') as file: + comment_data = json.loads(file.read()) + + if post_title in comment_data: + return comment_data[post_title] + else: + return [] diff --git a/app/data/.flask_session/2029240f6d1128be89ddc32729463129 b/app/data/.flask_session/2029240f6d1128be89ddc32729463129 deleted file mode 100644 index 7f5741f13017ee705ea34021d222a06a6ee2a6c9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 9 QcmZQzU|?uq^=8ro00XcA0RR91 diff --git a/app/data/.flask_session/aa43147407c8a8c678cd91f678f2a023 b/app/data/.flask_session/aa43147407c8a8c678cd91f678f2a023 deleted file mode 100644 index d818306312c02b6939f76f09975e7cd3b355c42e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 90 zcmeB))lF+)o$Ad10ku;!dbr{XQj2mE^HTFlrgZdhB^MW^#h2t~r{+!R(J-_yH#Sa5 oH8xK%HZ!wKO-(j2Ha1VQ1o9116D`cmEmPBs%u|z-rMotion Picture
Programming
Writing
- About + About
+ Login diff --git a/app/templates/games.html b/app/templates/games.html index 0ffded7..69587d9 100644 --- a/app/templates/games.html +++ b/app/templates/games.html @@ -19,7 +19,8 @@ Motion Picture
Programming
Writing
- About + About
+ Login diff --git a/app/templates/index.html b/app/templates/index.html index 4c79bc5..f0114c4 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -19,7 +19,8 @@ Motion Picture
Programming
Writing
- About + About
+ Login @@ -27,15 +28,19 @@
{% for post in posts %}
- {{ post[0]|safe }} + {{ post[0].body|safe }}
{% for comment in post[1] %} -
{{ comment|safe }}
+
+

{{ comment.username }}

+

{{ comment.content }}

+
{% endfor %} {% if user %}

{{ user }}

-
+ + {{ form.hidden_tag() }} {{ form.textbox }}
diff --git a/app/templates/motion-pictures.html b/app/templates/motion-pictures.html index 87d36f9..ef72b79 100644 --- a/app/templates/motion-pictures.html +++ b/app/templates/motion-pictures.html @@ -19,7 +19,8 @@ Motion Picture
Programming
Writing
- About + About
+ Login
diff --git a/app/templates/music.html b/app/templates/music.html index 5ea89f3..71c868a 100644 --- a/app/templates/music.html +++ b/app/templates/music.html @@ -19,7 +19,8 @@ Motion Picture
Programming
Writing
- About + About
+ Login
diff --git a/app/templates/programming.html b/app/templates/programming.html index d146ccd..0bf970e 100644 --- a/app/templates/programming.html +++ b/app/templates/programming.html @@ -19,7 +19,8 @@ Motion Picture
Programming
Writing
- About + About
+ Login
diff --git a/app/templates/user/login.html b/app/templates/user/login.html new file mode 100644 index 0000000..9659b9c --- /dev/null +++ b/app/templates/user/login.html @@ -0,0 +1,40 @@ + + +
+ + + 0x01fe.net - Login +
+ +
+

0x01fe.net

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

Login

+
+ {{ form.hidden_tag() }} + Username: {{ form.username }}
+ Password: {{ form.password }}
+ +
+
+ +

Need to Register?

+ Register +
+
+ + diff --git a/app/templates/user/register.html b/app/templates/user/register.html index 27804b0..b5cc4da 100644 --- a/app/templates/user/register.html +++ b/app/templates/user/register.html @@ -13,22 +13,22 @@
- -

Register

-
- {{ form.username }} - {{ form.password }} - + + {{ form.hidden_tag() }} + Username: {{ form.username }}
+ Password: {{ form.password }}
+
diff --git a/app/user.py b/app/user.py index ff7c758..6c6411b 100644 --- a/app/user.py +++ b/app/user.py @@ -1,9 +1,12 @@ +import base64 +import json + import flask import flask_wtf.csrf import wtforms user = flask.Blueprint('user', __name__, template_folder='./templates/user') - +USERS_PATH = "./data/users.json" class RegisterUserForm(flask_wtf.FlaskForm): username = wtforms.StringField("Username", [ wtforms.validators.Length(min=4, max=32), @@ -14,12 +17,80 @@ class RegisterUserForm(flask_wtf.FlaskForm): wtforms.validators.DataRequired() ]) +class LoginUserForm(flask_wtf.FlaskForm): + username = wtforms.StringField("Username", [ + wtforms.validators.DataRequired() + ]) + password = wtforms.PasswordField("Password", [ + wtforms.validators.DataRequired() + ]) + @user.route('/user/add/', methods=["POST"]) def add_user(): - pass + + # Get form data + form = RegisterUserForm(csrf_enabled=True) + + username = form.username.data + password = form.password.data + + # Read existing user data + with open(USERS_PATH, 'r') as file: + user_data = json.loads(file.read()) + + # check if user exists + if username in user_data: + flask.abort(400) + + # Store password / server side cookie + user_data[username] = base64.b64encode(password.encode()).decode() + flask.session['username'] = username + + # Write user data + with open(USERS_PATH, 'w') as file: + file.write(json.dumps(user_data)) + + flask.redirect('/') @user.route('/user/register/') -def register_user(): +def register_page(): form = RegisterUserForm() return flask.render_template('register.html', form=form) + +@user.route('/user/login/', methods=["POST"]) +def login_user(): + form = LoginUserForm(csrf_enabled=True) + + username = form.username.data + password = base64.b64encode(form.password.data.encode()).decode() + + # Read existing user data + with open(USERS_PATH, 'r') as file: + user_data = json.loads(file.read()) + + # check if user exists + if username not in user_data: + flask.abort(400) + + # Does password match? + if user_data[username] != password: + flask.abort(400) + + flask.session['username'] = username + + return flask.redirect('/') + +@user.route('/login/') +def login_page(): + form = LoginUserForm() + + return flask.render_template('login.html', form=form) + +@user.route('/logout/') +def logout_user(): + + if 'username' in flask.session: + flask.session.pop('username') + + return flask.redirect('/')