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 7f5741f..0000000 Binary files a/app/data/.flask_session/2029240f6d1128be89ddc32729463129 and /dev/null differ diff --git a/app/data/.flask_session/aa43147407c8a8c678cd91f678f2a023 b/app/data/.flask_session/aa43147407c8a8c678cd91f678f2a023 deleted file mode 100644 index d818306..0000000 Binary files a/app/data/.flask_session/aa43147407c8a8c678cd91f678f2a023 and /dev/null differ diff --git a/app/data/comments.json b/app/data/comments.json deleted file mode 100644 index 964e9aa..0000000 --- a/app/data/comments.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "2" : [ - { - "username" : "0x01FE", - "content" : "Hello, this is an example comment!" - } - ] -} diff --git a/app/data/users.json b/app/data/users.json deleted file mode 100644 index c3227f9..0000000 --- a/app/data/users.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "users" : { - "0x01FE" : "cGFzc3dvcmQ=" - } -} diff --git a/app/post.py b/app/post.py index 92dce5d..ea9156e 100644 --- a/app/post.py +++ b/app/post.py @@ -8,7 +8,6 @@ class Post: date : datetime.datetime body : str file : str - id : int title : str url : str @@ -20,13 +19,11 @@ class Post: self.category = lines[1].split(":")[1].strip() self.author = lines[2].split(":")[1].strip() - self.title = lines[6][2:-1] + self.title = lines[6].replace('#', '').strip() self.url = '/post/' + self.title.replace(' ', '-') date = lines[3].split(":")[1].strip() self.date = datetime.datetime.strptime(date, "%d-%m-%Y") - self.id = int(lines[4].split(":")[1].strip()) - self.body = markdown.markdown(f'# [{self.title}]({self.url})\n' + ''.join(lines[7:])) diff --git a/app/static/style.css b/app/static/style.css index 6e4dab4..14450f9 100644 --- a/app/static/style.css +++ b/app/static/style.css @@ -216,13 +216,12 @@ a:hover { } .comment-editor textarea { - width: 100%; + width: 80%; height: 6em; padding: 0.75em; border-style: solid; border-color: var(--secondary50); - border-radius: var(--border-radius); } .comment-editor textarea:focus { diff --git a/app/templates/about.html b/app/templates/about.html index ce7774c..88ff118 100644 --- a/app/templates/about.html +++ b/app/templates/about.html @@ -19,7 +19,8 @@ Motion 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('/')