· 6 years ago · Apr 11, 2020, 02:16 PM
1from src.Model import *
2from src.Data import *
3
4#from Regosimport train_regex
5from controller import *
6
7# simple key for security of the API
8authentication_key = 'ALOshjfpqnwo3u78r9q3rfk2qpw3fj09q23ru'
9
10
11def authentication(key):
12 if key == authentication_key:
13 return True
14 else:
15 return False
16
17# Endpoints for Model class
18
19# Endpoint for adding model to the database
20
21@app.route('/model/<key>', methods = ['POST'])
22def add_model(key):
23 if authentication(key):
24 name = request.json['name']
25 path = './models/'
26 if name not in os.listdir(path):
27 new_file = open(path + name,'w')
28 new_file.close()
29 path+=name
30 model_type = request.json['model_type']
31 status = 'in queue'
32
33 new_model = Model(name, path, model_type, status)
34
35 db.session.add(new_model)
36 db.session.commit()
37
38 return model_schema.jsonify(new_model)
39 else:
40 return 'You cannot do it'
41
42# Get all models
43
44@app.route('/model/<key>', methods = ['GET'])
45def get_all_models(key):
46 if authentication(key):
47 all_models = Model.query.all()
48 return jsonify(models_schema.dump(all_models))
49 else:
50 return 'You cannot do it'
51
52
53
54# Get model based on id
55
56@app.route('/model/by_id/<key>/<id>', methods = ['GET'])
57def get_model_by_id(key, id):
58 if authentication(key):
59 model = Model.query.get(id)
60 return model_schema.jsonify(model)
61 else:
62 return 'You have no rights to access it'
63
64# Get model based on name
65
66@app.route('/model/by_name/<key>/<name>', methods = ['GET'])
67def get_model_by_name(key, name):
68 if authentication(key):
69 model = Model.query.filter_by(name = name).first_or_404()
70 return model_schema.jsonify(model)
71 else:
72 return 'You cannot do it'
73# train model
74
75
76@app.route('/model/train/<key>/<name>', methods = ['PUT'])
77def train_model_by_name(key, name):
78 if authentication(key):
79 return "training process finished"
80 else:
81 return 'Authentication failed'
82
83@app.route('/model/train/<key>/<id>', methods = ['PUT'])
84def train_model_by_id(key, id):
85 if authentication(key):
86 return "training process finished"
87 else:
88 return 'Authentication failed'
89
90
91# Remove model by id
92@app.route('/model/by_id/<key>/<id>', methods = ['DELETE'])
93def delete_model_by_id(key, id):
94 if authentication(key):
95 model = Model.query.get(id)
96 db.session.delete(model)
97 db.session.commit()
98 json_file = model_schema.jsonify(model)
99 path = dict(json_file)['path']
100 try:
101 os.remove(path)
102 except:
103 return 'Error occured, model fil not found'
104 else:
105 return 'It is impossible'
106
107
108# Endpoints for data class
109@app.route('/data/<key>', methods = ['POST'])
110def add_data(key):
111 if authentication(key):
112 new_datas = []
113 for json_file in request.json:
114 model_name = json_file['model_name']
115 string_data = json_file['string_data']
116 wanted_parts = json_file['wanted_parts']
117
118 new_data = Data(model_name, string_data, wanted_parts)
119 db.session.add(new_data)
120 db.session.commit()
121 new_datas.append(copy.deepcopy(data_schema.jsonify(new_data)))
122 del new_data
123 return '200'
124 else:
125 return 'Wrong authentication'
126
127@app.route('/data/<key>/<model_name>', methods = ['GET'])
128def get_data_for_model(key, model_name):
129 if authentication(key):
130 datas =Data.query.filter_by(model_name = model_name)
131 return datas_schema.jsonify(datas)
132 else:
133 return 'Wrong authentication'
134
135
136if __name__ == '__main__':
137 app.run(debug=True)