contentdb/app/views/tasks.py

76 lines
2.1 KiB
Python
Raw Normal View History

2018-05-17 16:18:20 +02:00
# Content DB
# 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_user import *
2018-12-23 00:03:38 +01:00
import flask_menu as menu
from app import app, csrf
2018-05-11 16:04:17 +02:00
from app.models import *
from app.tasks import celery, TaskError
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 shouldReturnJson
2018-05-11 16:04:17 +02:00
# from celery.result import AsyncResult
2018-05-15 20:35:59 +02:00
from app.utils import *
2018-05-11 16:04:17 +02:00
@csrf.exempt
2018-05-11 16:14:35 +02:00
@app.route("/tasks/getmeta/new/", methods=["POST"])
@login_required
2018-05-11 16:04:17 +02:00
def new_getmeta_page():
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("check_task", id=aresult.id),
})
@app.route("/tasks/<id>/")
def check_task(id):
result = celery.AsyncResult(id)
status = result.status
traceback = result.traceback
result = result.result
info = None
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)