change most double quotes to single quotes
This commit is contained in:
@@ -20,35 +20,35 @@ import json
|
||||
|
||||
def create_default_avatar():
|
||||
if Avatars.count() == 0:
|
||||
print("Creating default avatar reference")
|
||||
print('Creating default avatar reference')
|
||||
Avatars.create({
|
||||
"file_path": "/static/avatars/default.webp",
|
||||
"uploaded_at": int(time.time())
|
||||
'file_path': '/static/avatars/default.webp',
|
||||
'uploaded_at': int(time.time())
|
||||
})
|
||||
|
||||
def create_admin():
|
||||
username = "admin"
|
||||
if Users.count({"username": username}) == 0:
|
||||
print("!!!!!Creating admin account!!!!!")
|
||||
username = 'admin'
|
||||
if Users.count({'username': username}) == 0:
|
||||
print('!!!!!Creating admin account!!!!!')
|
||||
password_length = 16
|
||||
password = secrets.token_urlsafe(password_length)
|
||||
hashed = digest(password)
|
||||
Users.create({
|
||||
"username": username,
|
||||
"password_hash": hashed,
|
||||
"permission": PermissionLevel.ADMIN.value,
|
||||
'username': username,
|
||||
'password_hash': hashed,
|
||||
'permission': PermissionLevel.ADMIN.value,
|
||||
})
|
||||
print(f"!!!!!Administrator account created, use '{username}' as the login and '{password}' as the password. This will only be shown once!!!!!")
|
||||
|
||||
def create_deleted_user():
|
||||
username = "DeletedUser"
|
||||
if Users.count({"username": username.lower()}) == 0:
|
||||
print("Creating DeletedUser")
|
||||
username = 'DeletedUser'
|
||||
if Users.count({'username': username.lower()}) == 0:
|
||||
print('Creating DeletedUser')
|
||||
Users.create({
|
||||
"username": username.lower(),
|
||||
"display_name": username,
|
||||
"password_hash": "",
|
||||
"permission": PermissionLevel.SYSTEM.value,
|
||||
'username': username.lower(),
|
||||
'display_name': username,
|
||||
'password_hash': '',
|
||||
'permission': PermissionLevel.SYSTEM.value,
|
||||
})
|
||||
|
||||
def reparse_babycode():
|
||||
@@ -167,26 +167,26 @@ def create_app():
|
||||
except FileNotFoundError:
|
||||
print('No configuration file found, leaving defaults.')
|
||||
|
||||
if os.getenv("PYROM_PROD") is None:
|
||||
app.static_folder = os.path.join(os.path.dirname(__file__), "../data/static")
|
||||
if os.getenv('PYROM_PROD') is None:
|
||||
app.static_folder = os.path.join(os.path.dirname(__file__), '../data/static')
|
||||
app.debug = True
|
||||
app.config["DB_PATH"] = "data/db/db.dev.sqlite"
|
||||
app.config["SERVER_NAME"] = "localhost:8080"
|
||||
app.config['DB_PATH'] = 'data/db/db.dev.sqlite'
|
||||
app.config['SERVER_NAME'] = 'localhost:8080'
|
||||
load_dotenv()
|
||||
else:
|
||||
app.config["DB_PATH"] = "data/db/db.prod.sqlite"
|
||||
if not app.config["SERVER_NAME"]:
|
||||
app.config['DB_PATH'] = 'data/db/db.prod.sqlite'
|
||||
if not app.config['SERVER_NAME']:
|
||||
raise SiteNameMissingException()
|
||||
|
||||
app.config["SECRET_KEY"] = os.getenv("FLASK_SECRET_KEY")
|
||||
app.config['SECRET_KEY'] = os.getenv('FLASK_SECRET_KEY')
|
||||
|
||||
app.config['AVATAR_UPLOAD_PATH'] = 'data/static/avatars/'
|
||||
app.config['BADGES_PATH'] = 'data/static/badges/'
|
||||
app.config['BADGES_UPLOAD_PATH'] = 'data/static/badges/user/'
|
||||
app.config['MAX_CONTENT_LENGTH'] = 3 * 1000 * 1000 # 3M total, subject to further limits per route
|
||||
|
||||
os.makedirs(os.path.dirname(app.config["DB_PATH"]), exist_ok = True)
|
||||
os.makedirs(os.path.dirname(app.config["BADGES_UPLOAD_PATH"]), exist_ok = True)
|
||||
os.makedirs(os.path.dirname(app.config['DB_PATH']), exist_ok = True)
|
||||
os.makedirs(os.path.dirname(app.config['BADGES_UPLOAD_PATH']), exist_ok = True)
|
||||
|
||||
if app.config['CACHE_TYPE'] == 'FileSystemCache':
|
||||
cache_dir = app.config.get('CACHE_DIR', 'data/_cached')
|
||||
@@ -217,21 +217,21 @@ def create_app():
|
||||
def make_session_permanent():
|
||||
session.permanent = True
|
||||
|
||||
commit = ""
|
||||
commit = ''
|
||||
with open('.git/refs/heads/main') as f:
|
||||
commit = f.read().strip()
|
||||
|
||||
@app.context_processor
|
||||
def inject_constants():
|
||||
return {
|
||||
"InfoboxHTMLClass": InfoboxHTMLClass,
|
||||
"InfoboxKind": InfoboxKind,
|
||||
"PermissionLevel": PermissionLevel,
|
||||
"__commit": commit,
|
||||
"__emoji": EMOJI,
|
||||
"REACTION_EMOJI": REACTION_EMOJI,
|
||||
"MOTD_BANNED_TAGS": MOTD_BANNED_TAGS,
|
||||
"SIG_BANNED_TAGS": SIG_BANNED_TAGS,
|
||||
'InfoboxHTMLClass': InfoboxHTMLClass,
|
||||
'InfoboxKind': InfoboxKind,
|
||||
'PermissionLevel': PermissionLevel,
|
||||
'__commit': commit,
|
||||
'__emoji': EMOJI,
|
||||
'REACTION_EMOJI': REACTION_EMOJI,
|
||||
'MOTD_BANNED_TAGS': MOTD_BANNED_TAGS,
|
||||
'SIG_BANNED_TAGS': SIG_BANNED_TAGS,
|
||||
}
|
||||
|
||||
@app.context_processor
|
||||
@@ -241,18 +241,18 @@ def create_app():
|
||||
'get_time_now': lambda: int(time.time()),
|
||||
}
|
||||
|
||||
@app.template_filter("ts_datetime")
|
||||
@app.template_filter('ts_datetime')
|
||||
def ts_datetime(ts, format):
|
||||
return datetime.utcfromtimestamp(ts or int(time.time())).strftime(format)
|
||||
|
||||
@app.template_filter("pluralize")
|
||||
def pluralize(subject, num=1, singular = "", plural = "s"):
|
||||
@app.template_filter('pluralize')
|
||||
def pluralize(subject, num=1, singular = '', plural = 's'):
|
||||
if int(num) == 1:
|
||||
return subject + singular
|
||||
|
||||
return subject + plural
|
||||
|
||||
@app.template_filter("permission_string")
|
||||
@app.template_filter('permission_string')
|
||||
def permission_string(term):
|
||||
return permission_level_string(term)
|
||||
|
||||
@@ -291,7 +291,7 @@ def create_app():
|
||||
# sooo... /shrug
|
||||
@app.template_filter('cachebust')
|
||||
def cachebust(subject):
|
||||
return f"{subject}?v={str(int(time.time()))}"
|
||||
return f'{subject}?v={str(int(time.time()))}'
|
||||
|
||||
@app.template_filter('theme_name')
|
||||
def get_theme_name(subject: str):
|
||||
|
||||
@@ -70,8 +70,8 @@ class InfoboxKind(IntEnum):
|
||||
ERROR = 3
|
||||
|
||||
InfoboxHTMLClass = {
|
||||
InfoboxKind.INFO: "",
|
||||
InfoboxKind.LOCK: "warn",
|
||||
InfoboxKind.WARN: "warn",
|
||||
InfoboxKind.ERROR: "critical",
|
||||
InfoboxKind.INFO: '',
|
||||
InfoboxKind.LOCK: 'warn',
|
||||
InfoboxKind.WARN: 'warn',
|
||||
InfoboxKind.ERROR: 'critical',
|
||||
}
|
||||
|
||||
@@ -53,11 +53,11 @@ def run_migrations():
|
||||
)
|
||||
""")
|
||||
if len(MIGRATIONS) == 0:
|
||||
print("No migrations defined.")
|
||||
print('No migrations defined.')
|
||||
return
|
||||
print("Running migrations...")
|
||||
print('Running migrations...')
|
||||
ran = 0
|
||||
completed = {int(row["id"]) for row in db.query("SELECT id FROM _migrations")}
|
||||
completed = {int(row['id']) for row in db.query('SELECT id FROM _migrations')}
|
||||
to_run = {idx: migration_obj for idx, migration_obj in enumerate(MIGRATIONS) if idx not in completed}
|
||||
if not to_run:
|
||||
print('No migrations need to run.')
|
||||
@@ -74,4 +74,4 @@ def run_migrations():
|
||||
|
||||
db.execute('INSERT INTO _migrations (id) VALUES (?)', migration_id)
|
||||
ran += 1
|
||||
print(f"Ran {ran} migrations.")
|
||||
print(f'Ran {ran} migrations.')
|
||||
|
||||
@@ -4,10 +4,10 @@ from flask import current_app
|
||||
import time
|
||||
|
||||
class Users(Model):
|
||||
table = "users"
|
||||
table = 'users'
|
||||
|
||||
def get_avatar_url(self):
|
||||
return Avatars.find({"id": self.avatar_id}).file_path
|
||||
return Avatars.find({'id': self.avatar_id}).file_path
|
||||
|
||||
def is_default_avatar(self):
|
||||
return int(Avatars.find({'id': self.avatar_id}).id) == 1
|
||||
@@ -104,7 +104,7 @@ class Users(Model):
|
||||
|
||||
|
||||
class Topics(Model):
|
||||
table = "topics"
|
||||
table = 'topics'
|
||||
|
||||
@classmethod
|
||||
def get_list(_cls):
|
||||
@@ -131,12 +131,12 @@ class Topics(Model):
|
||||
topics.sort_order ASC"""
|
||||
return db.query(q)
|
||||
|
||||
def get_threads(self, per_page, page, sort_by = "activity"):
|
||||
order_clause = ""
|
||||
if sort_by == "thread":
|
||||
order_clause = "ORDER BY threads.is_stickied DESC, threads.created_at DESC"
|
||||
def get_threads(self, per_page, page, sort_by = 'activity'):
|
||||
order_clause = ''
|
||||
if sort_by == 'thread':
|
||||
order_clause = 'ORDER BY threads.is_stickied DESC, threads.created_at DESC'
|
||||
else:
|
||||
order_clause = "ORDER BY threads.is_stickied DESC, latest_post_created_at DESC"
|
||||
order_clause = 'ORDER BY threads.is_stickied DESC, latest_post_created_at DESC'
|
||||
|
||||
q = """
|
||||
SELECT
|
||||
@@ -168,7 +168,7 @@ class Topics(Model):
|
||||
users u ON u.id = posts.user_id
|
||||
WHERE
|
||||
threads.topic_id = ?
|
||||
""" + order_clause + " LIMIT ? OFFSET ?"
|
||||
""" + order_clause + ' LIMIT ? OFFSET ?'
|
||||
|
||||
return db.query(q, self.id, per_page, (page - 1) * per_page)
|
||||
|
||||
@@ -206,10 +206,10 @@ class Topics(Model):
|
||||
|
||||
|
||||
class Threads(Model):
|
||||
table = "threads"
|
||||
table = 'threads'
|
||||
|
||||
def get_posts(self, limit, offset):
|
||||
q = Posts.FULL_POSTS_QUERY + " WHERE posts.thread_id = ? ORDER BY posts.created_at ASC LIMIT ? OFFSET ?"
|
||||
q = Posts.FULL_POSTS_QUERY + ' WHERE posts.thread_id = ? ORDER BY posts.created_at ASC LIMIT ? OFFSET ?'
|
||||
return db.query(q, self.id, limit, offset)
|
||||
|
||||
def get_posts_rss(self):
|
||||
@@ -263,23 +263,23 @@ class Posts(Model):
|
||||
LEFT JOIN
|
||||
user_badges ON users.id = user_badges.user_id"""
|
||||
|
||||
table = "posts"
|
||||
table = 'posts'
|
||||
|
||||
def get_full_post_view(self):
|
||||
q = f'{self.FULL_POSTS_QUERY} WHERE posts.id = ?'
|
||||
return db.fetch_one(q, self.id)
|
||||
|
||||
class PostHistory(Model):
|
||||
table = "post_history"
|
||||
table = 'post_history'
|
||||
|
||||
class Sessions(Model):
|
||||
table = "sessions"
|
||||
table = 'sessions'
|
||||
|
||||
class Avatars(Model):
|
||||
table = "avatars"
|
||||
table = 'avatars'
|
||||
|
||||
class Subscriptions(Model):
|
||||
table = "subscriptions"
|
||||
table = 'subscriptions'
|
||||
|
||||
def get_unread_count(self):
|
||||
q = """SELECT COUNT(*) AS unread_count
|
||||
@@ -316,15 +316,15 @@ class APIRateLimits(Model):
|
||||
return False
|
||||
|
||||
class Reactions(Model):
|
||||
table = "reactions"
|
||||
table = 'reactions'
|
||||
|
||||
@classmethod
|
||||
def for_post(cls, post_id):
|
||||
qb = db.QueryBuilder(cls.table)\
|
||||
.select("reaction_text, COUNT(*) as c")\
|
||||
.where({"post_id": post_id})\
|
||||
.group_by("reaction_text")\
|
||||
.order_by("c", False)
|
||||
.select('reaction_text, COUNT(*) as c')\
|
||||
.where({'post_id': post_id})\
|
||||
.group_by('reaction_text')\
|
||||
.order_by('c', False)
|
||||
result = qb.all()
|
||||
return result if result else []
|
||||
|
||||
@@ -342,7 +342,7 @@ class Reactions(Model):
|
||||
|
||||
|
||||
class PasswordResetLinks(Model):
|
||||
table = "password_reset_links"
|
||||
table = 'password_reset_links'
|
||||
|
||||
|
||||
class InviteKeys(Model):
|
||||
@@ -446,7 +446,7 @@ class BadgeUploads(Model):
|
||||
|
||||
@classmethod
|
||||
def get_for_user(cls, user_id):
|
||||
q = "SELECT * FROM badge_uploads WHERE user_id = ? OR user_id IS NULL ORDER BY uploaded_at"
|
||||
q = 'SELECT * FROM badge_uploads WHERE user_id = ? OR user_id IS NULL ORDER BY uploaded_at'
|
||||
res = db.query(q, int(user_id))
|
||||
return [cls.from_data(row) for row in res]
|
||||
|
||||
|
||||
@@ -159,39 +159,39 @@ SCHEMA = [
|
||||
)""",
|
||||
|
||||
# INDEXES
|
||||
"CREATE INDEX IF NOT EXISTS idx_post_history_post_id ON post_history(post_id)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_posts_thread ON posts(thread_id, created_at, id)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_posts_thread_id ON posts(thread_id)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_rate_limit_user_method ON api_rate_limits (user_id, method)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_subscription_user_thread ON subscriptions (user_id, thread_id)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_threads_slug ON threads(slug)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_threads_topic_id ON threads(topic_id)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_topics_slug ON topics(slug)",
|
||||
"CREATE INDEX IF NOT EXISTS session_keys ON sessions(key)",
|
||||
"CREATE INDEX IF NOT EXISTS sessions_user_id ON sessions(user_id)",
|
||||
'CREATE INDEX IF NOT EXISTS idx_post_history_post_id ON post_history(post_id)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_posts_thread ON posts(thread_id, created_at, id)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_posts_thread_id ON posts(thread_id)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_rate_limit_user_method ON api_rate_limits (user_id, method)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_subscription_user_thread ON subscriptions (user_id, thread_id)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_threads_slug ON threads(slug)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_threads_topic_id ON threads(topic_id)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_topics_slug ON topics(slug)',
|
||||
'CREATE INDEX IF NOT EXISTS session_keys ON sessions(key)',
|
||||
'CREATE INDEX IF NOT EXISTS sessions_user_id ON sessions(user_id)',
|
||||
|
||||
"CREATE INDEX IF NOT EXISTS reaction_post_text ON reactions(post_id, reaction_text)",
|
||||
"CREATE INDEX IF NOT EXISTS reaction_user_post_text ON reactions(user_id, post_id, reaction_text)",
|
||||
'CREATE INDEX IF NOT EXISTS reaction_post_text ON reactions(post_id, reaction_text)',
|
||||
'CREATE INDEX IF NOT EXISTS reaction_user_post_text ON reactions(user_id, post_id, reaction_text)',
|
||||
|
||||
"CREATE INDEX IF NOT EXISTS idx_bookmark_collections_user_id ON bookmark_collections(user_id)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_bookmark_collections_user_default ON bookmark_collections(user_id, is_default) WHERE is_default = 1",
|
||||
'CREATE INDEX IF NOT EXISTS idx_bookmark_collections_user_id ON bookmark_collections(user_id)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_bookmark_collections_user_default ON bookmark_collections(user_id, is_default) WHERE is_default = 1',
|
||||
|
||||
"CREATE INDEX IF NOT EXISTS idx_bookmarked_posts_collection ON bookmarked_posts(collection_id)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_bookmarked_posts_post ON bookmarked_posts(post_id)",
|
||||
'CREATE INDEX IF NOT EXISTS idx_bookmarked_posts_collection ON bookmarked_posts(collection_id)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_bookmarked_posts_post ON bookmarked_posts(post_id)',
|
||||
|
||||
"CREATE INDEX IF NOT EXISTS idx_bookmarked_threads_collection ON bookmarked_threads(collection_id)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_bookmarked_threads_thread ON bookmarked_threads(thread_id)",
|
||||
'CREATE INDEX IF NOT EXISTS idx_bookmarked_threads_collection ON bookmarked_threads(collection_id)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_bookmarked_threads_thread ON bookmarked_threads(thread_id)',
|
||||
|
||||
"CREATE INDEX IF NOT EXISTS idx_mentioned_user ON mentions(mentioned_user_id)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_mention_revision_id ON mentions(revision_id)",
|
||||
'CREATE INDEX IF NOT EXISTS idx_mentioned_user ON mentions(mentioned_user_id)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_mention_revision_id ON mentions(revision_id)',
|
||||
|
||||
"CREATE INDEX IF NOT EXISTS idx_badge_upload_user ON badge_uploads(user_id)",
|
||||
"CREATE INDEX IF NOT EXISTS idx_badge_user ON badges(user_id)",
|
||||
'CREATE INDEX IF NOT EXISTS idx_badge_upload_user ON badge_uploads(user_id)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_badge_user ON badges(user_id)',
|
||||
]
|
||||
|
||||
def create():
|
||||
print("Creating schema...")
|
||||
print('Creating schema...')
|
||||
with db.transaction():
|
||||
for stmt in SCHEMA:
|
||||
db.execute(stmt)
|
||||
print("Schema completed.")
|
||||
print('Schema completed.')
|
||||
|
||||
Reference in New Issue
Block a user