add versioning to markup languages and reparse old format version posts

This commit is contained in:
2025-08-16 05:28:44 +03:00
parent 382080ceaa
commit cf4bf3caa3
5 changed files with 34 additions and 8 deletions

View File

@ -1,6 +1,6 @@
from flask import Flask, session from flask import Flask, session
from dotenv import load_dotenv from dotenv import load_dotenv
from .models import Avatars, Users from .models import Avatars, Users, PostHistory, Posts
from .auth import digest from .auth import digest
from .routes.users import is_logged_in, get_active_user from .routes.users import is_logged_in, get_active_user
from .routes.threads import get_post_url from .routes.threads import get_post_url
@ -9,7 +9,7 @@ from .constants import (
InfoboxKind, InfoboxIcons, InfoboxHTMLClass, InfoboxKind, InfoboxIcons, InfoboxHTMLClass,
REACTION_EMOJI, REACTION_EMOJI,
) )
from .lib.babycode import babycode_to_html, EMOJI from .lib.babycode import babycode_to_html, EMOJI, BABYCODE_VERSION
from datetime import datetime from datetime import datetime
import os import os
import time import time
@ -48,6 +48,23 @@ def create_deleted_user():
"permission": PermissionLevel.SYSTEM.value, "permission": PermissionLevel.SYSTEM.value,
}) })
def reparse_posts():
from .db import db
post_histories = PostHistory.findall([
('markup_language', '=', 'babycode'),
('format_version', 'IS NOT', BABYCODE_VERSION)
])
if len(post_histories) == 0:
return
print('Re-parsing babycode, this may take a while...')
with db.transaction():
for ph in post_histories:
ph.update({
'content': babycode_to_html(ph['original_markup']),
'format_version': BABYCODE_VERSION,
})
print('Re-parsing done.')
def create_app(): def create_app():
app = Flask(__name__) app = Flask(__name__)
app.config.from_file('../config/pyrom_config.toml', load=tomllib.load, text=False) app.config.from_file('../config/pyrom_config.toml', load=tomllib.load, text=False)
@ -76,6 +93,8 @@ def create_app():
create_admin() create_admin()
create_deleted_user() create_deleted_user()
reparse_posts()
from app.routes.app import bp as app_bp from app.routes.app import bp as app_bp
from app.routes.topics import bp as topics_bp from app.routes.topics import bp as topics_bp
from app.routes.threads import bp as threads_bp from app.routes.threads import bp as threads_bp

View File

@ -202,9 +202,9 @@ class Model:
@classmethod @classmethod
def findall(cls, condition): def findall(cls, condition, operator='='):
rows = db.QueryBuilder(cls.table)\ rows = db.QueryBuilder(cls.table)\
.where(condition)\ .where(condition, operator)\
.all() .all()
res = [] res = []
for row in rows: for row in rows:

View File

@ -2,6 +2,8 @@ from .babycode_parser import Parser
from markupsafe import escape from markupsafe import escape
import re import re
BABYCODE_VERSION = 1
NAMED_COLORS = [ NAMED_COLORS = [
'black', 'silver', 'gray', 'white', 'maroon', 'red', 'black', 'silver', 'gray', 'white', 'maroon', 'red',
'purple', 'fuchsia', 'green', 'lime', 'olive', 'yellow', 'purple', 'fuchsia', 'green', 'lime', 'olive', 'yellow',
@ -146,7 +148,7 @@ def babycode_to_html(s):
parser.valid_emotes = EMOJI.keys() parser.valid_emotes = EMOJI.keys()
elements = parser.parse() elements = parser.parse()
print(elements) # print(elements)
out = "" out = ""
def fold(element, nobr): def fold(element, nobr):
if isinstance(element, str): if isinstance(element, str):

View File

@ -10,6 +10,7 @@ MIGRATIONS = [
migrate_old_avatars, migrate_old_avatars,
'DELETE FROM sessions', # delete old lua porom sessions 'DELETE FROM sessions', # delete old lua porom sessions
'ALTER TABLE "users" ADD COLUMN "invited_by" INTEGER REFERENCES users(id)', # invitation system 'ALTER TABLE "users" ADD COLUMN "invited_by" INTEGER REFERENCES users(id)', # invitation system
'ALTER TABLE "post_history" ADD COLUMN "format_version" INTEGER DEFAULT NULL',
] ]
def run_migrations(): def run_migrations():

View File

@ -2,7 +2,7 @@ from flask import (
Blueprint, redirect, url_for, flash, render_template, request Blueprint, redirect, url_for, flash, render_template, request
) )
from .users import login_required, get_active_user from .users import login_required, get_active_user
from ..lib.babycode import babycode_to_html from ..lib.babycode import babycode_to_html, BABYCODE_VERSION
from ..constants import InfoboxKind from ..constants import InfoboxKind
from ..db import db from ..db import db
from ..models import Posts, PostHistory, Threads, Topics from ..models import Posts, PostHistory, Threads, Topics
@ -11,6 +11,7 @@ bp = Blueprint("posts", __name__, url_prefix = "/post")
def create_post(thread_id, user_id, content, markup_language="babycode"): def create_post(thread_id, user_id, content, markup_language="babycode"):
parsed_content = babycode_to_html(content)
with db.transaction(): with db.transaction():
post = Posts.create({ post = Posts.create({
"thread_id": thread_id, "thread_id": thread_id,
@ -20,10 +21,11 @@ def create_post(thread_id, user_id, content, markup_language="babycode"):
revision = PostHistory.create({ revision = PostHistory.create({
"post_id": post.id, "post_id": post.id,
"content": babycode_to_html(content), "content": parsed_content,
"is_initial_revision": True, "is_initial_revision": True,
"original_markup": content, "original_markup": content,
"markup_language": markup_language, "markup_language": markup_language,
"format_version": BABYCODE_VERSION,
}) })
post.update({"current_revision_id": revision.id}) post.update({"current_revision_id": revision.id})
@ -31,14 +33,16 @@ def create_post(thread_id, user_id, content, markup_language="babycode"):
def update_post(post_id, new_content, markup_language='babycode'): def update_post(post_id, new_content, markup_language='babycode'):
parsed_content = babycode_to_html(content)
with db.transaction(): with db.transaction():
post = Posts.find({'id': post_id}) post = Posts.find({'id': post_id})
new_revision = PostHistory.create({ new_revision = PostHistory.create({
'post_id': post.id, 'post_id': post.id,
'content': babycode_to_html(new_content), 'content': parsed_content,
'is_initial_revision': False, 'is_initial_revision': False,
'original_markup': new_content, 'original_markup': new_content,
'markup_language': markup_language, 'markup_language': markup_language,
'format_version': BABYCODE_VERSION,
}) })
post.update({'current_revision_id': new_revision.id}) post.update({'current_revision_id': new_revision.id})