src: rich text editor added

master
tiyn 2 years ago
parent 2d25888ee3
commit 2a09b0bc86

@ -1,5 +1,6 @@
from flask import Flask, flash, make_response, render_template, redirect, \ 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, \ from flask_login import current_user, login_user, LoginManager, logout_user, \
login_required login_required
from flask_wtf import CSRFProtect from flask_wtf import CSRFProtect
@ -14,6 +15,7 @@ from forms import LoginForm, RegisterForm, WriteForm
app = Flask(__name__) app = Flask(__name__)
csrf = CSRFProtect() csrf = CSRFProtect()
db = Database() db = Database()
ckeditor = CKEditor(app)
app.secret_key = os.urandom(32) app.secret_key = os.urandom(32)
csrf.init_app(app) csrf.init_app(app)
@ -131,8 +133,11 @@ def write():
if not current_user.is_authenticated: if not current_user.is_authenticated:
return redirect(url_for("index")) return redirect(url_for("index"))
form = WriteForm() form = WriteForm()
print("data", form.text.data)
print("data", request.form.get("text"))
if form.validate_on_submit(): 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 redirect(url_for("index"))
return render_template("write.html", title=TITLE, form=form, style=STYLE) return render_template("write.html", title=TITLE, form=form, style=STYLE)

@ -60,9 +60,9 @@ def gen_index_string():
username = db.get_user_by_id(entry[5])[1] username = db.get_user_by_id(entry[5])[1]
reviewed = entry[6] reviewed = entry[6]
content_string += "<div class=\"entry\">\n" content_string += "<div class=\"entry\">\n"
content_string += "<h2 id=\"" + \ content_string += "<h1 id=\"" + \
str(ident) + "\">" + title + " (" + year + ") - " + \ str(ident) + "\">" + title + " (" + year + ") - " + \
str(rating) + "/100</h2>\n" str(rating) + "/100</h1>\n"
content_string += "[<a href=\"" + "/entry/" + \ content_string += "[<a href=\"" + "/entry/" + \
str(ident) + "\">" + "standalone" + "</a>]<br>\n" str(ident) + "\">" + "standalone" + "</a>]<br>\n"
content_string += text content_string += text
@ -128,7 +128,8 @@ def get_rss_string():
username = db.get_user_by_id(entry[5])[1] username = db.get_user_by_id(entry[5])[1]
reviewed = entry[6] reviewed = entry[6]
content_string += "<item>\n" content_string += "<item>\n"
content_string += "<title>" + title + "(" + year + ") " + str(rating) + "/100 </title>\n" content_string += "<title>" + title + \
"(" + year + ") " + str(rating) + "/100 </title>\n"
content_string += "<guid>" + config.WEBSITE + \ content_string += "<guid>" + config.WEBSITE + \
"/index.html#" + str(ident) + "</guid>\n" "/index.html#" + str(ident) + "</guid>\n"
content_string += "<pubDate>" + reviewed + "</pubDate>\n" content_string += "<pubDate>" + reviewed + "</pubDate>\n"

@ -3,6 +3,7 @@ import os
import sqlite3 import sqlite3
from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.security import generate_password_hash, check_password_hash
class User(): class User():
def __init__(self, name, pass_hash=None): def __init__(self, name, pass_hash=None):
@ -129,4 +130,4 @@ class Database:
#db = Database() #db = Database()
#db.insert_entry("name", "2020", "text", 50, 1) #db.insert_entry("name", "2020", "text", 50, 1)
#res = db.get_entries() #res = db.get_entries()
#print(res) # print(res)

@ -1,8 +1,10 @@
from datetime import date from datetime import date
from flask_ckeditor import CKEditorField
from flask_wtf import FlaskForm 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.fields.html5 import IntegerField
from wtforms.validators import DataRequired, EqualTo, InputRequired, NumberRange from wtforms.validators import DataRequired, EqualTo, InputRequired, \
NumberRange, ValidationError
class LoginForm(FlaskForm): class LoginForm(FlaskForm):
@ -18,9 +20,16 @@ class RegisterForm(FlaskForm):
"Repeat Password", validators=[DataRequired(), EqualTo("password")]) "Repeat Password", validators=[DataRequired(), EqualTo("password")])
submit = SubmitField("Register") submit = SubmitField("Register")
class WriteForm(FlaskForm): class WriteForm(FlaskForm):
name = StringField("Name", validators=[DataRequired()]) 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.")]) date = IntegerField("Release Year", default=date.today().year, validators=[DataRequired(
text = TextAreaField("Text", validators=[DataRequired()]) ), NumberRange(min=0, max=date.today().year, message="Year has to be valid.")])
rating = IntegerField("Rating", default=50, validators=[InputRequired(), NumberRange(min=0, max=100, message="Number has to be between 0 and 100.")]) 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") submit = SubmitField("Publish")
def validate_text(self, text):
if "<h1>" in text.data or "</h1>" in text.data:
raise ValidationError("Headings on level 1 are not permitted.")

@ -32,6 +32,7 @@
<a href="{{ url_for('register') }}">Register</a> <a href="{{ url_for('register') }}">Register</a>
{% else %} {% else %}
<a href="{{ url_for('logout') }}">Logout</a> <a href="{{ url_for('logout') }}">Logout</a>
-
<a href="{{ url_for('write') }}">Write entry</a> <a href="{{ url_for('write') }}">Write entry</a>
{% endif %} {% endif %}
</div> </div>

@ -22,11 +22,14 @@
{% endfor %} {% endfor %}
</p> </p>
<p> <p>
{{ form.text.label }}<br> {{ form.text.label }}<br>
{{ form.text }} {{ form.text }}
{% for error in form.text.errors %} {% for error in form.text.errors %}
<span style="color: red;">[{{ error }}]</span> <span style="color: red;">[{{ error }}]</span>
{% endfor %} {% endfor %}
{{ ckeditor.load() }}
{{ ckeditor.config(name="text") }}
</p> </p>
<p> <p>
{{ form.rating.label }}<br> {{ form.rating.label }}<br>

Loading…
Cancel
Save