forked from KathiraveluLab/DHGWorkflow
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathworkflow.py
More file actions
70 lines (57 loc) · 2.32 KB
/
workflow.py
File metadata and controls
70 lines (57 loc) · 2.32 KB
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
from model.workflows import WorkFlowModel
from flask import request, make_response, Blueprint
import defusedxml.ElementTree as ET
workFlow = Blueprint('workflow', __name__)
workFlowModel = WorkFlowModel()
def isMissingWorkflow(graphml):
if graphml is None:
return True
# Backward-compatible guard for legacy model return type.
return isinstance(graphml, tuple) and len(graphml) > 0 and graphml[0] is False
def getLasteshActionHash(root):
xmlns = root.tag[root.tag.index('{')+1:root.tag.rindex('}')]
return root.find(f'{{{xmlns}}}graph')\
.findall(f'{{{xmlns}}}actionHistory')[-1]\
.find(f'{{{xmlns}}}hash')\
.text
def getAllActionHash(root):
xmlns = root.tag[root.tag.index('{')+1:root.tag.rindex('}')]
return list(map(lambda ah: ah.find(f'{{{xmlns}}}hash').text, root.find(f'{{{xmlns}}}graph').findall(f'{{{xmlns}}}actionHistory')))
@workFlow.route("/", methods=['POST'])
def postWorkflow():
try:
lastestHash = getLasteshActionHash(ET.fromstring(request.data))
except Exception:
return "Invalid GraphML", 400
graphML = request.data.decode('utf')
return workFlowModel.insert(graphML, lastestHash)
@workFlow.route("/<serverID>")
def getWorkflow(serverID):
graphml = workFlowModel.get(serverID)
if isMissingWorkflow(graphml):
return "Not Found", 404
if('X-Latest-Hash' in request.headers):
latestHash = request.headers['X-Latest-Hash']
allHash = getAllActionHash(ET.fromstring(graphml))
if(latestHash not in allHash):
return 'Different History', 400
r = make_response(graphml)
r.headers.set('Content-Type', "application/xml")
return r
@workFlow.route("/<serverID>", methods=['POST'])
def updateWorkflow(serverID):
forceUpdate = request.args.get(
'force') and request.args.get('force').lower() == 'true'
try:
root = ET.fromstring(request.data)
latestHash = getLasteshActionHash(root)
if(not forceUpdate):
allHash = getAllActionHash(root)
except Exception:
return "Invalid GraphML", 400
graphML = request.data.decode('utf')
if(forceUpdate):
res = workFlowModel.forceUpdate(serverID, graphML, latestHash)
else:
res = workFlowModel.update(serverID, graphML, latestHash, allHash)
return res[1], 200 if res[0] else 400