from flask import Blueprint
from flask import request
from flask import jsonify
from flask import session
from flask import current_app
from flask import render_template
from flask import request
from app.utils import render_markdown
from sqlmodel import Session as SQLSession
from sqlmodel import select

from app.models.server import Catagory, Organization
from app.utils.decorators import admin_required

bp = Blueprint("admin", __name__)


@bp.route("/")
@admin_required
def index():
    return render_markdown(
        "page.html",
        file="admin.md",
        session=session,
    )


@bp.route("/organizations", methods=["GET"])
def get_organizations():
    with SQLSession(current_app.engine) as s:
        orgs = select(Organization)
        results = s.exec(orgs).all()

        return render_template(
            "admin/organizations.html", session=session, organizations=results
        )


@bp.route("/server/organization", methods=["POST", "DELETE"])
def post_server():
    if request.method == "POST":
        data = request.form

        org_id = data.get("id").strip()
        title = data.get("title").strip()
        return jsonify({}), 200


@bp.route("/catagory", methods=["POST"])
@admin_required
def get_catagory():
    data = request.form

    cat_id = data.get("id")
    title = data.get("title")
    color = data.get("color")

    with SQLSession(current_app.engine) as s:
        if cat_id:
            _catagory = s.exec(select(Catagory).where(Catagory.id == cat_id)).one()
            _catagory.title = data.get("title")
            _catagory.meta_ref = data.get("title").lower().replace(" ", "-")
            _catagory.color = data.get("color")
            s.add(_catagory)
            s.commit()

        else:
            _catagory = Catagory(
                title=data.get("title"),
                meta_ref=data.get("title").lower().replace(" ", "-"),
                color=data.get("color"),
            )
            s.add(_catagory)
            s.commit()

    return jsonify({"result": "Operate successfully"})


@bp.route("/catagories", methods=["GET", "POST"])
def get_post_catagories():
    if request.method == "GET":
        with SQLSession(current_app.engine) as s:
            results = s.exec(select(Catagory)).all()

            return render_template(
                "admin/catagories.html", sesssion=session, catagories=results
            )
    else:
        data = request.get_json()

flask.render_template app.utils.render_markdown flask.request.get_json sqlmodel.select sqlmodel.Session flask.Blueprint flask.jsonify
[((413, 441), 'flask.Blueprint', 'Blueprint', (['"""admin"""', '__name__'], {}), "('admin', __name__)\n", (422, 441), False, 'from flask import Blueprint\n'), ((499, 561), 'app.utils.render_markdown', 'render_markdown', (['"""page.html"""'], {'file': '"""admin.md"""', 'session': 'session'}), "('page.html', file='admin.md', session=session)\n", (514, 561), False, 'from app.utils import render_markdown\n'), ((2011, 2054), 'flask.jsonify', 'jsonify', (["{'result': 'Operate successfully'}"], {}), "({'result': 'Operate successfully'})\n", (2018, 2054), False, 'from flask import jsonify\n'), ((674, 704), 'sqlmodel.Session', 'SQLSession', (['current_app.engine'], {}), '(current_app.engine)\n', (684, 704), True, 'from sqlmodel import Session as SQLSession\n'), ((726, 746), 'sqlmodel.select', 'select', (['Organization'], {}), '(Organization)\n', (732, 746), False, 'from sqlmodel import select\n'), ((800, 888), 'flask.render_template', 'render_template', (['"""admin/organizations.html"""'], {'session': 'session', 'organizations': 'results'}), "('admin/organizations.html', session=session, organizations=\n    results)\n", (815, 888), False, 'from flask import render_template\n'), ((1367, 1397), 'sqlmodel.Session', 'SQLSession', (['current_app.engine'], {}), '(current_app.engine)\n', (1377, 1397), True, 'from sqlmodel import Session as SQLSession\n'), ((2423, 2441), 'flask.request.get_json', 'request.get_json', ([], {}), '()\n', (2439, 2441), False, 'from flask import request\n'), ((1148, 1159), 'flask.jsonify', 'jsonify', (['{}'], {}), '({})\n', (1155, 1159), False, 'from flask import jsonify\n'), ((2179, 2209), 'sqlmodel.Session', 'SQLSession', (['current_app.engine'], {}), '(current_app.engine)\n', (2189, 2209), True, 'from sqlmodel import Session as SQLSession\n'), ((2289, 2367), 'flask.render_template', 'render_template', (['"""admin/catagories.html"""'], {'sesssion': 'session', 'catagories': 'results'}), "('admin/catagories.html', sesssion=session, catagories=results)\n", (2304, 2367), False, 'from flask import render_template\n'), ((2245, 2261), 'sqlmodel.select', 'select', (['Catagory'], {}), '(Catagory)\n', (2251, 2261), False, 'from sqlmodel import select\n'), ((1454, 1470), 'sqlmodel.select', 'select', (['Catagory'], {}), '(Catagory)\n', (1460, 1470), False, 'from sqlmodel import select\n')]