add versioning to markup languages and reparse old format version posts
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
from flask import Flask, session
|
||||
from dotenv import load_dotenv
|
||||
from .models import Avatars, Users
|
||||
from .models import Avatars, Users, PostHistory, Posts
|
||||
from .auth import digest
|
||||
from .routes.users import is_logged_in, get_active_user
|
||||
from .routes.threads import get_post_url
|
||||
@ -9,7 +9,7 @@ from .constants import (
|
||||
InfoboxKind, InfoboxIcons, InfoboxHTMLClass,
|
||||
REACTION_EMOJI,
|
||||
)
|
||||
from .lib.babycode import babycode_to_html, EMOJI
|
||||
from .lib.babycode import babycode_to_html, EMOJI, BABYCODE_VERSION
|
||||
from datetime import datetime
|
||||
import os
|
||||
import time
|
||||
@ -48,6 +48,23 @@ def create_deleted_user():
|
||||
"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():
|
||||
app = Flask(__name__)
|
||||
app.config.from_file('../config/pyrom_config.toml', load=tomllib.load, text=False)
|
||||
@ -76,6 +93,8 @@ def create_app():
|
||||
create_admin()
|
||||
create_deleted_user()
|
||||
|
||||
reparse_posts()
|
||||
|
||||
from app.routes.app import bp as app_bp
|
||||
from app.routes.topics import bp as topics_bp
|
||||
from app.routes.threads import bp as threads_bp
|
||||
|
@ -202,9 +202,9 @@ class Model:
|
||||
|
||||
|
||||
@classmethod
|
||||
def findall(cls, condition):
|
||||
def findall(cls, condition, operator='='):
|
||||
rows = db.QueryBuilder(cls.table)\
|
||||
.where(condition)\
|
||||
.where(condition, operator)\
|
||||
.all()
|
||||
res = []
|
||||
for row in rows:
|
||||
|
@ -2,6 +2,8 @@ from .babycode_parser import Parser
|
||||
from markupsafe import escape
|
||||
import re
|
||||
|
||||
BABYCODE_VERSION = 1
|
||||
|
||||
NAMED_COLORS = [
|
||||
'black', 'silver', 'gray', 'white', 'maroon', 'red',
|
||||
'purple', 'fuchsia', 'green', 'lime', 'olive', 'yellow',
|
||||
@ -146,7 +148,7 @@ def babycode_to_html(s):
|
||||
parser.valid_emotes = EMOJI.keys()
|
||||
|
||||
elements = parser.parse()
|
||||
print(elements)
|
||||
# print(elements)
|
||||
out = ""
|
||||
def fold(element, nobr):
|
||||
if isinstance(element, str):
|
||||
|
@ -10,6 +10,7 @@ MIGRATIONS = [
|
||||
migrate_old_avatars,
|
||||
'DELETE FROM sessions', # delete old lua porom sessions
|
||||
'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():
|
||||
|
@ -2,7 +2,7 @@ from flask import (
|
||||
Blueprint, redirect, url_for, flash, render_template, request
|
||||
)
|
||||
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 ..db import db
|
||||
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"):
|
||||
parsed_content = babycode_to_html(content)
|
||||
with db.transaction():
|
||||
post = Posts.create({
|
||||
"thread_id": thread_id,
|
||||
@ -20,10 +21,11 @@ def create_post(thread_id, user_id, content, markup_language="babycode"):
|
||||
|
||||
revision = PostHistory.create({
|
||||
"post_id": post.id,
|
||||
"content": babycode_to_html(content),
|
||||
"content": parsed_content,
|
||||
"is_initial_revision": True,
|
||||
"original_markup": content,
|
||||
"markup_language": markup_language,
|
||||
"format_version": BABYCODE_VERSION,
|
||||
})
|
||||
|
||||
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'):
|
||||
parsed_content = babycode_to_html(content)
|
||||
with db.transaction():
|
||||
post = Posts.find({'id': post_id})
|
||||
new_revision = PostHistory.create({
|
||||
'post_id': post.id,
|
||||
'content': babycode_to_html(new_content),
|
||||
'content': parsed_content,
|
||||
'is_initial_revision': False,
|
||||
'original_markup': new_content,
|
||||
'markup_language': markup_language,
|
||||
'format_version': BABYCODE_VERSION,
|
||||
})
|
||||
|
||||
post.update({'current_revision_id': new_revision.id})
|
||||
|
Reference in New Issue
Block a user