partitioncloud-server/partitioncloud/modules/partition.py
augustin64 234e0f784f Option redirect=true pour une recherche en ligne
Modification du schéma sqlite:
`ALTER TABLE search_results ADD url TEXT;;`
2022-10-28 23:01:02 +02:00

58 lines
1.3 KiB
Python

#!/usr/bin/python3
"""
Partition module
"""
import os
from flask import Blueprint, abort, send_file, render_template, request, redirect
from .db import get_db
from .auth import login_required, admin_required
from .utils import get_all_partitions
bp = Blueprint("partition", __name__, url_prefix="/partition")
@bp.route("/<uuid>")
@login_required
def partition(uuid):
db = get_db()
partition = db.execute(
"""
SELECT * FROM partition
WHERE uuid = ?
""",
(uuid,)
).fetchone()
if partition is None:
abort(404)
return send_file(
os.path.join("partitions", f"{uuid}.pdf"),
download_name = f"{partition['name']}.pdf"
)
@bp.route("/search/<uuid>")
@login_required
def partition_search(uuid):
db = get_db()
partition = db.execute(
"""
SELECT uuid, url FROM search_results
WHERE uuid = ?
""",
(uuid,)
).fetchone()
if partition is None:
abort(404)
if request.args.get("redirect") == "true" and partition["url"] is not None:
return redirect(partition["url"])
return send_file(os.path.join("search-partitions", f"{uuid}.pdf"))
@bp.route("/")
@admin_required
def index():
partitions = get_all_partitions().fetchall()
return render_template("admin/partitions.html", partitions=partitions)