-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodes.py
141 lines (101 loc) · 3.32 KB
/
nodes.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import re
from flask import request, Blueprint, render_template, abort, g
from werkzeug.exceptions import NotFound
from app import app
from carddb import MAINTAINER
nodes = Blueprint('nodes', __name__, url_prefix='/<int:node>')
@nodes.url_value_preprocessor
def check_node(endpoint, values):
try:
request.nodeid = values.pop('node')
request.node = app.nodes[request.nodeid]
except KeyError:
raise NotFound
@nodes.route('/status', methods=['GET', 'PUT'])
def status():
if request.method == 'PUT':
status = request.data
if status not in ('1', '0'):
return 'Invalid status', 400
request.node.status = status
return 'OK'
else:
return str(request.node.status)
@nodes.route('/tooluse', methods=['GET', 'PUT'])
def tooluse():
if request.method == 'GET':
return str(request.node.tooluse)
m = re.match('^([0-9]+),([A-Z0-9]+)$', request.data)
if not m:
return 'Invalid arguments', 400
tooluse, uid = m.groups()
access = node.checkCard(uid)
if not access:
abort(403)
if tooluse in ('1', '0'):
request.node.tooluse = int(tooluse)
return 'OK'
@nodes.route('/tooluse/time/', methods=['GET'])
def get_tool_time():
pass
@nodes.route('/tooluse/time/', methods=['POST'])
def add_tool_time():
if request.headers['Content-type'] == 'text/plain':
m = re.match('^([0-9]+),([A-Z0-9]+)$', request.data)
if not m:
return 'Invalid content', 400
tooluse, uid = m.groups()
else:
abort(415)
access = request.node.checkCard(uid)
if not access:
abort(403)
if tooluse not in ('1', '0'):
return 'Invalid tooluse', 400
request.node.tooluse = int(tooluse)
return 'OK'
@nodes.route('/case', methods=['GET', 'PUT'])
def case():
if request.method == 'GET':
return str(request.node.case)
case = request.data
if case not in ('1', '0'):
return 'Invalid case', 400
request.node.case = int(case)
return 'OK'
@nodes.route('/card/<uid>', methods=['GET', 'POST'])
def card(uid):
if request.method == 'GET':
access = request.node.checkCard(g.db, uid)
return str(access)
granter_uid = request.data
granter_access = request.node.checkCard(g.db, granter_uid)
if granter_access != MAINTAINER:
abort(403)
user_access = request.node.checkCard(g.db, uid)
if user_access: # no change
return 'OK (was %s)' % user_access
else:
request.node.addCard(g.db, uid, granter_uid)
return 'OK', 201
@nodes.route('/sync/')
@nodes.route('/sync/<from_uid>')
def sync(from_uid=None):
json, text, html = 'application/json', 'text/plain', 'text/html'
# In order of preference
best_match = request.accept_mimetypes.best_match([html, json, text])
if not best_match:
return not_acceptable
# If no DB, return 204
if best_match == text:
# Use the embedded protocol (i.e. only one card at a time)
next_card = request.node.getCard(g.db, from_uid)
if not next_card:
return 'END'
return next_card
elif best_match == json:
card = request.node.card
return jsonify(cards)
else:
# Not implemented yet
return render_template('cards.html', cards=request.node.cards)