From 2a09b0bc8631f932f6790ce4075f611c2c9c083d Mon Sep 17 00:00:00 2001
From: tiyn
Date: Sun, 31 Jul 2022 01:12:43 +0200
Subject: [PATCH] src: rich text editor added
---
src/app.py | 9 +++++++--
src/content.py | 7 ++++---
src/database.py | 3 ++-
src/forms.py | 19 ++++++++++++++-----
src/templates/template.html | 1 +
src/templates/write.html | 3 +++
6 files changed, 31 insertions(+), 11 deletions(-)
diff --git a/src/app.py b/src/app.py
index a8bc10f..58e951e 100644
--- a/src/app.py
+++ b/src/app.py
@@ -1,5 +1,6 @@
from flask import Flask, flash, make_response, render_template, redirect, \
- abort, url_for
+ abort, url_for, request
+from flask_ckeditor import CKEditor
from flask_login import current_user, login_user, LoginManager, logout_user, \
login_required
from flask_wtf import CSRFProtect
@@ -14,6 +15,7 @@ from forms import LoginForm, RegisterForm, WriteForm
app = Flask(__name__)
csrf = CSRFProtect()
db = Database()
+ckeditor = CKEditor(app)
app.secret_key = os.urandom(32)
csrf.init_app(app)
@@ -131,8 +133,11 @@ def write():
if not current_user.is_authenticated:
return redirect(url_for("index"))
form = WriteForm()
+ print("data", form.text.data)
+ print("data", request.form.get("text"))
if form.validate_on_submit():
- db.insert_entry(form.name.data, form.date.data, form.text.data, form.rating.data, current_user.id)
+ db.insert_entry(form.name.data, form.date.data,
+ form.text.data, form.rating.data, current_user.id)
return redirect(url_for("index"))
return render_template("write.html", title=TITLE, form=form, style=STYLE)
diff --git a/src/content.py b/src/content.py
index 3aedf6c..f50944e 100644
--- a/src/content.py
+++ b/src/content.py
@@ -60,9 +60,9 @@ def gen_index_string():
username = db.get_user_by_id(entry[5])[1]
reviewed = entry[6]
content_string += "\n"
- content_string += "
" + title + " (" + year + ") - " + \
- str(rating) + "/100
\n"
+ str(rating) + "/100\n"
content_string += "[
" + "standalone" + "]
\n"
content_string += text
@@ -128,7 +128,8 @@ def get_rss_string():
username = db.get_user_by_id(entry[5])[1]
reviewed = entry[6]
content_string += "
- \n"
- content_string += "" + title + "(" + year + ") " + str(rating) + "/100 \n"
+ content_string += "" + title + \
+ "(" + year + ") " + str(rating) + "/100 \n"
content_string += "" + config.WEBSITE + \
"/index.html#" + str(ident) + "\n"
content_string += "" + reviewed + "\n"
diff --git a/src/database.py b/src/database.py
index cded5fa..ef7295e 100644
--- a/src/database.py
+++ b/src/database.py
@@ -3,6 +3,7 @@ import os
import sqlite3
from werkzeug.security import generate_password_hash, check_password_hash
+
class User():
def __init__(self, name, pass_hash=None):
@@ -129,4 +130,4 @@ class Database:
#db = Database()
#db.insert_entry("name", "2020", "text", 50, 1)
#res = db.get_entries()
-#print(res)
+# print(res)
diff --git a/src/forms.py b/src/forms.py
index d551894..95baf01 100644
--- a/src/forms.py
+++ b/src/forms.py
@@ -1,8 +1,10 @@
from datetime import date
+from flask_ckeditor import CKEditorField
from flask_wtf import FlaskForm
-from wtforms import StringField, PasswordField, SubmitField, TextAreaField
+from wtforms import StringField, PasswordField, SubmitField
from wtforms.fields.html5 import IntegerField
-from wtforms.validators import DataRequired, EqualTo, InputRequired, NumberRange
+from wtforms.validators import DataRequired, EqualTo, InputRequired, \
+ NumberRange, ValidationError
class LoginForm(FlaskForm):
@@ -18,9 +20,16 @@ class RegisterForm(FlaskForm):
"Repeat Password", validators=[DataRequired(), EqualTo("password")])
submit = SubmitField("Register")
+
class WriteForm(FlaskForm):
name = StringField("Name", validators=[DataRequired()])
- date = IntegerField("Release Year", default=date.today().year, validators=[DataRequired(), NumberRange(min=0, max=date.today().year, message="Year has to be valid.")])
- text = TextAreaField("Text", validators=[DataRequired()])
- rating = IntegerField("Rating", default=50, validators=[InputRequired(), NumberRange(min=0, max=100, message="Number has to be between 0 and 100.")])
+ date = IntegerField("Release Year", default=date.today().year, validators=[DataRequired(
+ ), NumberRange(min=0, max=date.today().year, message="Year has to be valid.")])
+ text = CKEditorField("Text", validators=[DataRequired()])
+ rating = IntegerField("Rating", default=50, validators=[InputRequired(
+ ), NumberRange(min=0, max=100, message="Number has to be between 0 and 100.")])
submit = SubmitField("Publish")
+
+ def validate_text(self, text):
+ if "
" in text.data or "
" in text.data:
+ raise ValidationError("Headings on level 1 are not permitted.")
diff --git a/src/templates/template.html b/src/templates/template.html
index e32cb37..d170056 100644
--- a/src/templates/template.html
+++ b/src/templates/template.html
@@ -32,6 +32,7 @@
Register
{% else %}
Logout
+ -
Write entry
{% endif %}
diff --git a/src/templates/write.html b/src/templates/write.html
index 7864627..e1db486 100644
--- a/src/templates/write.html
+++ b/src/templates/write.html
@@ -22,11 +22,14 @@
{% endfor %}
+
{{ form.text.label }}
{{ form.text }}
{% for error in form.text.errors %}
[{{ error }}]
{% endfor %}
+ {{ ckeditor.load() }}
+ {{ ckeditor.config(name="text") }}
{{ form.rating.label }}