]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/tasks/__init__.py
8d002dba988686d969604f240b9a3e19a0c5824d
[cheatdb.git] / app / blueprints / tasks / __init__.py
1 # Content DB
2 # Copyright (C) 2018  rubenwardy
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17
18 from flask import *
19 from flask_user import *
20 import flask_menu as menu
21 from app import csrf
22 from app.models import *
23 from app.tasks import celery, TaskError
24 from app.tasks.importtasks import getMeta
25 from app.utils import shouldReturnJson
26 from app.utils import *
27
28 bp = Blueprint("tasks", __name__)
29
30 @csrf.exempt
31 @bp.route("/tasks/getmeta/new/", methods=["POST"])
32 @login_required
33 def start_getmeta():
34         author = request.args.get("author")
35         author = current_user.forums_username if author is None else author
36         aresult = getMeta.delay(request.args.get("url"), author)
37         return jsonify({
38                 "poll_url": url_for("tasks.check", id=aresult.id),
39         })
40
41 @bp.route("/tasks/<id>/")
42 def check(id):
43         result = celery.AsyncResult(id)
44         status = result.status
45         traceback = result.traceback
46         result = result.result
47
48         info = None
49         if isinstance(result, Exception):
50                 info = {
51                                 'id': id,
52                                 'status': status,
53                         }
54
55                 if current_user.is_authenticated and current_user.rank.atLeast(UserRank.ADMIN):
56                         info["error"] = str(traceback)
57                 elif str(result)[1:12] == "TaskError: ":
58                         info["error"] = str(result)[12:-1]
59                 else:
60                         info["error"] = "Unknown server error"
61         else:
62                 info = {
63                                 'id': id,
64                                 'status': status,
65                                 'result': result,
66                         }
67
68         if shouldReturnJson():
69                 return jsonify(info)
70         else:
71                 r = request.args.get("r")
72                 if r is not None and status == "SUCCESS":
73                         return redirect(r)
74                 else:
75                         return render_template("tasks/view.html", info=info)