diff --git a/app/__init__.py b/app/__init__.py index 752409a..548bae0 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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 diff --git a/app/db.py b/app/db.py index 2ab8762..030692a 100644 --- a/app/db.py +++ b/app/db.py @@ -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() + 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"]}) diff --git a/app/routes/mod.py b/app/routes/mod.py new file mode 100644 index 0000000..7dcb80c --- /dev/null +++ b/app/routes/mod.py @@ -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) diff --git a/app/routes/users.py b/app/routes/users.py index 3464681..30e9479 100644 --- a/app/routes/users.py +++ b/app/routes/users.py @@ -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 diff --git a/app/templates/common/topnav.html b/app/templates/common/topnav.html index 35c66b5..126f44f 100644 --- a/app/templates/common/topnav.html +++ b/app/templates/common/topnav.html @@ -14,7 +14,7 @@ Inbox {% if user.is_mod() %} • - User list + User list {% endif %} {% endwith %} {% endif %} diff --git a/app/templates/mod/sort-topics.html b/app/templates/mod/sort-topics.html new file mode 100644 index 0000000..7e03b33 --- /dev/null +++ b/app/templates/mod/sort-topics.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} +{% block content %} +
+

Change topics order

+

Drag topic titles to reoder them. Press submit when done. The topics will appear to users in the order set here.

+
+ {% for topic in topics %} +
+
{{ topic['name'] }}
+
{{ topic.description }}
+ +
+ {% endfor %} + +
+
+ +{% endblock %} diff --git a/app/templates/mod/user-list.html b/app/templates/mod/user-list.html new file mode 100644 index 0000000..e8e7f07 --- /dev/null +++ b/app/templates/mod/user-list.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} +{% block content %} +
+ +
+{% endblock %} diff --git a/app/templates/topics/topics.html b/app/templates/topics/topics.html index 58473cd..dd60874 100644 --- a/app/templates/topics/topics.html +++ b/app/templates/topics/topics.html @@ -5,6 +5,7 @@

All topics

{% if active_user and active_user.is_mod() %} Create new topic + Sort topics {% endif %} {% if topic_list | length == 0 %}