started working on the comments feature

This commit is contained in:
0x01fe 2024-04-03 13:13:48 -05:00
parent f569620fce
commit 76688b2d2c
9 changed files with 105 additions and 17 deletions

5
.gitignore vendored
View File

@ -5,8 +5,11 @@ __pycache__
docker-compose.yaml docker-compose.yaml
*.sh *.sh
*.ini
# Ignore images in posts # Ignore images in posts
*.jpg *.jpg
*.png *.png
# Flask Data & Config
*.ini
data

View File

@ -1,6 +1,7 @@
import glob import glob
import configparser import configparser
import random import random
import base64
import flask import flask
import flask_wtf.csrf import flask_wtf.csrf
@ -10,12 +11,13 @@ import markdown
from post import Post from post import Post
import comment import comment
import user
app = flask.Flask(__name__, static_url_path='', static_folder='static') 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() # CONFIG
csrf.init_app(app)
CONFIG_PATH = "./config.ini" CONFIG_PATH = "./config.ini"
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(CONFIG_PATH) config.read(CONFIG_PATH)
@ -25,6 +27,17 @@ STATUS_FILE = config['STATUS']['STATUS_FILE']
PORT = int(config['NETWORK']['PORT']) PORT = int(config['NETWORK']['PORT'])
DEV = int(config['NETWORK']['DEV']) 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]: def get_posts(category_filter : str | None = None) -> list[Post]:
post_files = glob.glob(f'{POSTS_FOLDER}/*') post_files = glob.glob(f'{POSTS_FOLDER}/*')
try: try:
@ -85,7 +98,7 @@ def index():
# Setup Comment Form # Setup Comment Form
form = comment.CommentForm() 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 # Games Page
@app.route('/games/') @app.route('/games/')

View File

@ -1,14 +1,22 @@
import json
import flask import flask
import flask_session import flask_session
import flask_wtf import flask_wtf.csrf
import wtforms import wtforms
comments = flask.Blueprint('comment', __name__, template_folder='./templates')
class CommentForm(flask_wtf.FlaskForm): 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]:

View File

@ -8,6 +8,7 @@ class Post:
date : datetime.datetime date : datetime.datetime
body : str body : str
file : str file : str
id : int
def __init__(self, file_path): def __init__(self, file_path):
self.file = file_path self.file = file_path
@ -21,5 +22,7 @@ class Post:
date = lines[3].split(":")[1].strip() date = lines[3].split(":")[1].strip()
self.date = datetime.datetime.strptime(date, "%d-%m-%Y") 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:]))

View File

@ -2,6 +2,7 @@
category: category category: category
author: author author: author
date: date date: date
id: post id
# POST # POST
## TITLE ## TITLE

View File

@ -182,16 +182,16 @@ a:hover {
margin: 1em; margin: 1em;
} }
.comment-editor input { .comment-editor textarea {
width: 100%; width: 100%;
padding: 3em 0; height: 6em;
line-height: 140%; padding: 0.75em;
border-style: solid; border-style: solid;
/* border-color: var(--secondary50); */ border-color: var(--secondary50);
border-radius: var(--border-radius); border-radius: var(--border-radius);
} }
.comment-editor input:focus { .comment-editor textarea:focus {
border: 3px solid var(--accent); border: 3px solid var(--accent);
} }

View File

@ -34,8 +34,7 @@
{% if user %} {% if user %}
<div class="comment-editor"> <div class="comment-editor">
<h3>{{ user }}</h3> <h3>{{ user }}</h3>
<form method="post" action="/comment"> <form method="post" action="/comment/">
<!-- <input type="text" class="commenter"> -->
{{ form.textbox }} {{ form.textbox }}
<input type="submit" value="Save"> <input type="submit" value="Save">
</form> </form>

View File

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<header>
<link rel="shortcut icon" href="index_favicon.ico">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<title>0x01fe.net - Register</title>
</header>
<body>
<div class="header">
<h1>0x01fe.net</h1>
{{ status|safe }}
</div>
<div class="container">
<!-- Sidebar -->
<div class="sidebar">
<a href="."><u>Home</u></a><br>
<a href="./games/">Games</a><br>
<a href="./music/">Music</a><br>
<a href="./motion-pictures/">Motion Picture</a><br>
<a href="./programming/">Programming</a><br>
<a href="./about/">About</a>
</div>
<!-- Main Page -->
<!-- Get it? D-Log? Like digital log? -->
<div class="dlog">
<h1>Register</h1>
<form method="post" action="/user/register/">
{{ form.username }}
{{ form.password }}
<input type="submit" value="Register">
</form>
</div>
</div>
</body>
</html>

25
app/user.py Normal file
View File

@ -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)