contentdb/app/blueprints/tasks/__init__.py

74 lines
2.0 KiB
Python
Raw Normal View History

2020-07-12 17:34:25 +02:00
# ContentDB
2018-05-17 16:18:20 +02:00
# Copyright (C) 2018 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# 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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-05-11 16:04:17 +02:00
from flask import *
from flask_login import login_required
2020-12-04 03:23:04 +01:00
from app import csrf
2020-12-04 03:23:04 +01:00
from app.tasks import celery
2018-05-11 16:04:17 +02:00
from app.tasks.importtasks import getMeta
2018-05-15 20:35:59 +02:00
from app.utils import *
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():
author = request.args.get("author")
author = current_user.forums_username if author is None else author
aresult = getMeta.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
2020-12-04 03:23:04 +01:00
None
2018-05-11 16:04:17 +02:00
if isinstance(result, Exception):
info = {
'id': id,
2018-05-11 16:04:17 +02:00
'status': status,
}
if current_user.is_authenticated and current_user.rank.atLeast(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,
}
if shouldReturnJson():
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)