-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebapp.py
90 lines (72 loc) · 2.51 KB
/
webapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
from flask import Flask, render_template, jsonify, url_for
import tasks
import config
from sh import git
app = Flask(__name__)
config.configure(app)
@app.route('/ripdisk')
def rip_disk():
"""Initializes the ripping of a disk"""
task = tasks.rip_disk.apply_async()
url = url_for('ripstatus', task_id=task.id)
return "<a href=\"%s\">%s</a>" % (url, url)
@app.route('/rip/status/<task_id>')
def ripstatus(task_id):
"""Shows info about the status of an on-going disk ripping"""
task = tasks.rip_disk.AsyncResult(task_id)
if task.state == 'PENDING':
response = {
'state': task.state,
'current': 0,
'total': 1,
'status': 'Pending...'
}
elif task.state != 'FAILURE':
response = {
'state': task.state,
'info': task.info
}
if 'result' in task.info:
response['result'] = task.info['result']
else:
# something went wrong in the background job
response = {
'state': task.state,
'current': 1,
'total': 1,
'status': str(task.info), # this is the exception raised
}
return jsonify(response)
@app.route("/")
def hello():
"""Displays the main page"""
return render_template("index.html")
@app.route("/changer/status")
def init_changer_status():
"""Checks the status of the changer"""
task = tasks.mtx_command.apply_async(["get_status"])
return jsonify({'updates': url_for('changer_updates', task_id=task.id)})
@app.route("/changer/updates/<task_id>")
def changer_updates(task_id):
"""Displays information about the state of a changer task"""
task = tasks.mtx_command.AsyncResult(task_id)
response = {'task_id': task_id, "state": task.state}
if task.state != "FAILURE":
response['info'] = task.info
return jsonify(response)
@app.route("/changer/eject/<slot>")
def init_changer_eject(slot):
"""Ejects a disc from the changer"""
task = tasks.mtx_command.apply_async(["eject"], {"slot": slot})
return jsonify({'updates': url_for('changer_updates', task_id=task.id)})
@app.route("/changer/load/<slot>", defaults={'slot': None})
def init_changer_load(slot):
task = tasks.mtx_command.apply_async(["load"], {"slot": slot})
return jsonify({'updates': url_for('changer_updates', task_id=task.id)})
@app.route('/githook', methods=["POST"])
def githook():
git('pull')
return "kthx"
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')