forked from phi-grib/namastox_API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.py
More file actions
86 lines (69 loc) · 3.25 KB
/
update.py
File metadata and controls
86 lines (69 loc) · 3.25 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from settings import *
from user import checkAccess
import json
import os
from namastox import update
from namastox import manage
from flask import request
from werkzeug.utils import secure_filename
def allowed_attachment(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in {'csv'}
# PUT GENERAL_INFO
@app.route(f'{url_base}{version}general_info/<string:ra_name>',methods=['PUT'])
@cross_origin()
def updateGeneralInfo(ra_name):
granted, access_result = checkAccess(ra_name,'write')
if not granted:
return access_result # this is the 403 JSON response
input_string = request.form['general']
input_dict = json.loads(input_string)
# check if the post request has the file part
if 'custom_workflow_file' in request.files:
file = request.files['custom_workflow_file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
return json.dumps(f'Failed to upload file, empty file nama'), 500, {'ContentType':'application/json'}
if file and allowed_attachment(file.filename):
filename = secure_filename(file.filename)
filename = filename.replace (' ','_')
success, data = manage.getPath (ra_name)
if not success:
return json.dumps(f'Failed to upload file, unable to access repository'), 500, {'ContentType':'application/json'}
file.save(os.path.join(data, filename))
success, data = update.action_update_general_info(ra_name, {'general':input_dict})
if success:
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
else:
return json.dumps(f'Failed to update General Info for {ra_name} with error: {data}'), 500, {'ContentType':'application/json'}
# PUT USERS
@app.route(f'{url_base}{version}users/<string:ra_name>',methods=['PUT'])
@cross_origin()
def updateUsers(ra_name):
granted, access_result = checkAccess(ra_name,'write')
if not granted:
return access_result # this is the 403 JSON response
users_read=None
users_write=None
if 'read' in request.form:
users_read = request.form['read'].replace(' ', '').strip().split(',')
if 'write' in request.form:
users_write = request.form['write'].replace(' ', '').strip().split(',')
manage.action_setusers(ra_name, users_read, users_write)
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
# PUT RESULT
@app.route(f'{url_base}{version}result/<string:ra_name>',methods=['PUT'])
@app.route(f'{url_base}{version}result/<string:ra_name>/<int:step>',methods=['PUT'])
@cross_origin()
def updateResult(ra_name, step=None):
granted, access_result = checkAccess(ra_name,'write')
if not granted:
return access_result # this is the 403 JSON response
input_string = request.form['result']
input_dict = json.loads(input_string)
success, data = update.action_update_result(ra_name, step, {'result':[input_dict]})
if success:
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
else:
return json.dumps(f'Failed to update Result for {ra_name} with error: {data}'), 500, {'ContentType':'application/json'}