contentdb/app/blueprints/tasks/__init__.py

76 lines
2.1 KiB
Python
Raw Normal View History

2020-07-12 17:34:25 +02:00
# ContentDB
2021-01-30 17:59:42 +01:00
# Copyright (C) 2018-21 rubenwardy
2018-05-17 16:18:20 +02:00
#
# This program is free software: you can redistribute it and/or modify
2021-01-30 17:59:42 +01:00
# it under the terms of the GNU Affero General Public License as published by
2018-05-17 16:18:20 +02:00
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2021-01-30 17:59:42 +01:00
# GNU Affero General Public License for more details.
2018-05-17 16:18:20 +02:00
#
2021-01-30 17:59:42 +01:00
# You should have received a copy of the GNU Affero General Public License
2018-05-17 16:18:20 +02:00
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-06-19 20:32:36 +02:00
from flask import Blueprint, jsonify, url_for, request, redirect, render_template
from flask_login import login_required, current_user
2020-12-04 03:23:04 +01:00
from app import csrf
2023-06-19 20:32:36 +02:00
from app.models import UserRank
2020-12-04 03:23:04 +01:00
from app.tasks import celery
2023-06-19 22:27:49 +02:00
from app.tasks.importtasks import get_meta
from app.utils import should_return_json
2018-05-11 16:04:17 +02:00
bp = Blueprint("tasks", __name__)
@csrf.exempt
@bp.route("/tasks/getmeta/new/", methods=["POST"])
2018-05-11 16:14:35 +02:00
@login_required
def start_getmeta():
2023-06-19 20:32:36 +02:00
from flask import request
author = request.args.get("author")
author = current_user.forums_username if author is None else author
2023-06-19 22:27:49 +02:00
aresult = get_meta.delay(request.args.get("url"), author)
2018-05-11 16:04:17 +02:00
return jsonify({
"poll_url": url_for("tasks.check", id=aresult.id),
2018-05-11 16:04:17 +02:00
})
@bp.route("/tasks/<id>/")
def check(id):
2018-05-11 16:04:17 +02:00
result = celery.AsyncResult(id)
status = result.status
traceback = result.traceback
result = result.result
if isinstance(result, Exception):
info = {
'id': id,
2018-05-11 16:04:17 +02:00
'status': status,
}
2023-06-19 22:27:49 +02:00
if current_user.is_authenticated and current_user.rank.at_least(UserRank.ADMIN):
info["error"] = str(traceback)
elif str(result)[1:12] == "TaskError: ":
info["error"] = str(result)[12:-1]
else:
info["error"] = "Unknown server error"
2018-05-11 16:04:17 +02:00
else:
info = {
'id': id,
2018-05-11 16:04:17 +02:00
'status': status,
'result': result,
}
2023-06-19 22:27:49 +02:00
if should_return_json():
2018-05-11 16:04:17 +02:00
return jsonify(info)
else:
r = request.args.get("r")
if r is not None and status == "SUCCESS":
2018-05-11 16:04:17 +02:00
return redirect(r)
else:
return render_template("tasks/view.html", info=info)