port mod app

This commit is contained in:
Lera Elvoé 2025-06-30 22:13:12 +03:00
parent 453aeff95a
commit c22aa1036f
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc
8 changed files with 80 additions and 41 deletions

View File

@ -71,9 +71,11 @@ def create_app():
from app.routes.app import bp as app_bp
from app.routes.topics import bp as topics_bp
from app.routes.users import bp as users_bp
from app.routes.mod import bp as mod_bp
app.register_blueprint(app_bp)
app.register_blueprint(topics_bp)
app.register_blueprint(users_bp)
app.register_blueprint(mod_bp)
app.config['SESSION_COOKIE_SECURE'] = True

View File

@ -4,13 +4,12 @@ from flask import current_app
class DB:
def __init__(self):
self._transaction_depth = 0
self._connection = None
@contextmanager
def _get_connection(self):
if self._connection and self._transaction_depth > 0:
if self._connection:
yield self._connection
return
@ -21,48 +20,24 @@ class DB:
try:
yield conn
finally:
if self._transaction_depth == 0:
conn.close()
@contextmanager
def transaction(self):
"""Transaction context."""
self.begin()
tr_connection = sqlite3.connect(current_app.config["DB_PATH"])
tr_connection.row_factory = sqlite3.Row
tr_connection.execute("PRAGMA FOREIGN_KEYS = 1")
tr_connection.execute("BEGIN")
try:
yield
self.commit()
tr_connection.execute("COMMIT")
except Exception:
self.rollback()
tr_connection.execute("ROLLBACK")
raise
def begin(self):
"""Begins a new transaction."""
if self._transaction_depth == 0:
if not self._connection:
self._connection = sqlite3.connect(current_app.config["DB_PATH"])
self._connection.row_factory = sqlite3.Row
self._connection.execute("PRAGMA FOREIGN_KEYS = 1")
self._connection.execute("BEGIN")
self._transaction_depth += 1
def commit(self):
"""Commits the current transaction."""
if self._transaction_depth > 0:
self._transaction_depth -= 1
if self._transaction_depth == 0:
self._connection.commit()
def rollback(self):
"""Rolls back the current transaction."""
if self._transaction_depth > 0:
self._transaction_depth = 0
self._connection.rollback()
def query(self, sql, *args):
"""Executes a query and returns a list of dictionaries."""
with self._get_connection() as conn:
@ -209,6 +184,13 @@ class Model:
return result["c"] if result else 0
@classmethod
def select(cls, sel = "*"):
qb = db.QueryBuilder(cls.table).select(sel)
result = qb.all()
return result if result else []
def update(self, data):
qb = db.QueryBuilder(self.table)\
.where({"id": self._data["id"]})

33
app/routes/mod.py Normal file
View File

@ -0,0 +1,33 @@
from flask import (
Blueprint, render_template, request, redirect, url_for
)
from .users import login_required, mod_only
from ..models import Users
from ..db import db, DB
bp = Blueprint("mod", __name__, url_prefix = "/mod/")
@bp.get("/sort-topics")
@login_required
@mod_only("topics.all_topics")
def sort_topics():
topics = db.query("SELECT * FROM topics ORDER BY sort_order ASC")
return render_template("mod/sort-topics.html", topics = topics)
@bp.post("/sort-topics")
@login_required
@mod_only("topics.all_topics")
def sort_topics_post():
with db.transaction():
for topic_id, new_order in request.form.items():
db.execute("UPDATE topics SET sort_order = ? WHERE id = ?", new_order, topic_id)
return redirect(url_for(".sort_topics"))
@bp.get("/user-list")
@login_required
@mod_only("users.page", username = lambda: get_active_user().username)
def user_list():
users = Users.select()
return render_template("mod/user-list.html", users = users)

View File

@ -186,13 +186,6 @@ def inbox(username):
return "stub"
@bp.get("/list")
@login_required
@mod_only(".page", username = lambda: get_active_user().username)
def user_list():
return "stub"
@bp.post("/log_out")
def log_out():
pass

View File

@ -14,7 +14,7 @@
<a href="{{ url_for("users.inbox", username = user.username) }}">Inbox</a>
{% if user.is_mod() %}
&bullet;
<a href="{{ url_for("users.user_list") }}">User list</a>
<a href="{{ url_for("mod.user_list") }}">User list</a>
{% endif %}
{% endwith %}
{% endif %}

View File

@ -0,0 +1,18 @@
{% extends "base.html" %}
{% block content %}
<div class="darkbg settings-container">
<h1>Change topics order</h1>
<p>Drag topic titles to reoder them. Press submit when done. The topics will appear to users in the order set here.</p>
<form method="post" id=topics-container>
{% for topic in topics %}
<div draggable="true" class="draggable-topic" ondragover="dragOver(event)" ondragstart="dragStart(event)" ondragend="dragEnd()">
<div class="thread-title">{{ topic['name'] }}</div>
<div>{{ topic.description }}</div>
<input type="hidden" name="{{ topic['id'] }}" value="{{ topic['sort-order'] }}" class="topic-input">
</div>
{% endfor %}
<input type=submit value="Save order">
</form>
</div>
<script src="/static/js/sort-topics.js"></script>
{% endblock %}

View File

@ -0,0 +1,10 @@
{% extends "base.html" %}
{% block content %}
<div class="darkbg settings-container">
<ul>
{% for user in users %}
<li><a href="{{url_for("users.page", username=user['username'])}}">{{user['username']}}</a>
{% endfor %}
</ul>
</div>
{% endblock %}

View File

@ -5,6 +5,7 @@
<h1 class="thread-title">All topics</h1>
{% if active_user and active_user.is_mod() %}
<a class="linkbutton" href={{ url_for("topics.create") }}>Create new topic</a>
<a class="linkbutton" href={{ url_for("mod.sort_topics") }}>Sort topics</a>
{% endif %}
</nav>
{% if topic_list | length == 0 %}