-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
230 lines (169 loc) · 5.28 KB
/
backend.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from flask import Flask, render_template, request, json, redirect
from pymongo import MongoClient
from model.gen import gen
import random
import base64
import cStringIO
import elastic
#import model.convert_mol as cml
app = Flask(__name__)
class safe:
def set(self, values):
self.values = values
return
my_safe = safe()
@app.route('/')
def hello_world():
return render_template('index.html')
@app.route('/search')
def search():
return render_template('search.html')
@app.route('/home')
def home():
print(my_safe.values)
return render_template('home.html', projects=my_safe.values['json'])
@app.route('/upload')
def uploadFile():
return render_template('upload.html')
@app.route('/analytics')
def analytics():
return render_template('analytics.html')
uri = 'mongodb://user:[email protected]:23658/novogen'
def mongo_users():
client = MongoClient(uri)
db = client['novogen']
users = db['users']
return users
def mongo_projects():
client = MongoClient(uri)
db = client['novogen']
projects = db['projects']
return projects
@app.route('/api/account/register', methods=['POST'])
def register():
credentials = request.get_json()
username = credentials['username']
password = credentials['password']
cookie = int(username) + int(password)
users = mongo_users()
registration = {'user': cookie,
'projects': []}
registration_id = users.insert_one(registration).inserted_id
print(registration_id)
response = app.response_class(
response=json.dumps(registration),
status=200,
mimetype='application/json'
)
return response
@app.route('/api/account/user', methods=['GET'])
def user():
cookie = request.args.get('cookie', '')
users = mongo_users()
u = users.find_one({'user': cookie})
if u is None:
response = app.response_class(
response=json.dumps({'error_message': 'This user does not exist'}),
status=400,
mimetype='application/json'
)
return response
res = {}
projects = mongo_projects()
for pid in u['projects']:
p = projects.find_one({'project': pid})
res[pid] = p['name']
j = res
response = app.response_class(
response=json.dumps(res),
status=200,
mimetype='application/json'
)
return response
@app.route('/api/project', methods=['GET'])
def project():
cookie = request.args.get('cookie', '')
users = mongo_users()
u = users.find_one({'user': cookie})
project_id = request.args.get('project_id', '')
if project_id not in u['projects']:
response = app.response_class(
response=json.dumps({'error_message': 'This user does not have permission to access this project'}),
status=200,
mimetype='application/json'
)
return response
projects = mongo_projects()
p = projects.find_one({'project_id': project_id})
if p is None:
response = app.response_class(
response=json.dumps({'error_message': 'This project does not exist'}),
status=400,
mimetype='application/json'
)
return response
response = app.response_class(
response=json.dumps({'molecules': p['molecules']}),
status=200,
mimetype='application/json'
)
return response
@app.route('/api/project/upload', methods=['POST'])
def upload():
lines = request.get_json(force=True)['file']
print(lines)
molecules = gen(lines)
res = {'json': []}
for m in molecules:
buffered = cStringIO.StringIO()
m.molecular_img.save(buffered, format='PNG')
img_str = base64.b64encode(buffered.getvalue())
molecule = {'molecule': m.smiles,
'log_p': m.log_p,
'tpsa': m.tpsa,
'num_h_donors': m.num_h_donors,
'num_h_acceptors': m.num_h_acceptors,
'molecular_weight': m.molecular_weight,
'molecular_img': img_str
#'threeD': cml.convert(m.smiles)
}
res['json'].append(molecule)
'''
projects = mongo_projects()
molecules = gen(lines)
project_id = projects.insert_one({'molecules': lines}).inserted_id
response = app.response_class(
response=json.dumps({'project_id': project_id,
'molecules': molecules}),
status=200,
mimetype='application/json'
)
cookie = request.args.get('cookie', '')
users = mongo_users()
u = users.find_one({'user': cookie})
users.update(
{'_id': u['_id']},
{
'$push': {'projects': project_id}
}
)
'''
my_safe.values = res
response = app.response_class(
response=json.dumps(res),
status=200,
mimetype='application/json'
)
return response
@app.route('/api/search', methods=['GET'])
def perform_query():
query = request.args.get('query', '')
hits = elastic.perform_query(query)
response = app.response_class(
response=json.dumps(hits),
status=200,
mimetype='application/json'
)
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)