-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
400 lines (322 loc) · 14.6 KB
/
app.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import ast
import StringIO
import base64
from urllib import urlencode
from requests import get, post
import json
from itsdangerous import URLSafeTimedSerializer
from flask import Flask, request, render_template, url_for, redirect, flash, \
session
from flask_mail import Mail, Message
import dotenv
from getenv import env
import drest
from api import MetpetAPI
from forms import LoginForm, RequestPasswordResetForm, PasswordResetForm
from utilities import paginate_model
app = Flask(__name__)
app.config.from_object('config')
mail = Mail(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/search/')
def search():
email = session.get('email', None)
api_key = session.get('api_key', None)
api = MetpetAPI(email, api_key).api
filters = dict(request.args)
filter_dictionary = {}
for key in filters:
if filters[key][0]:
if key != "resource":
filter_dictionary[key] = ",".join(filters[key])
if request.args.get('resource') == 'sample':
url = url_for('samples') + '?' + urlencode(filter_dictionary)
return redirect(url)
elif request.args.get('resource') == 'chemicalanalysis':
if request.args.get('search_filters') == 'samples':
request_obj = drest.api.API(baseurl=env('API_HOST'))
if email and api_key:
headers = {'email': email, 'api_key': api_key}
else:
headers = None
response = request_obj.\
make_request('GET',
'/get-chem-analyses-given-sample-filters/',
params=filter_dictionary,
headers=headers)
ids = response.data['chemical_analysis_ids']
url = url_for('chemical_analyses') + '?' + \
urlencode({'chemical_analysis_id__in': ids})
return redirect(url)
else:
url = url_for('chemical_analyses') + '?' + urlencode(filter_dictionary)
return redirect(url)
owner_list = []
region_list = []
rock_type_list = []
collector_list = []
country_list = []
reference_list = []
number_list = []
igsn_list = []
metamorphic_region_list = []
metamorphic_grade_list = []
regions = api.region.get(params={'order_by': 'name', 'limit': 0}).data['objects']
rock_types = api.rock_type.get(params={'order_by': 'rock_type', 'limit': 0}).data['objects']
references = api.reference.get(params={'order_by': 'name', 'limit': 0}).data['objects']
metamorphic_regions = api.metamorphic_region.get(params={'order_by': 'name', 'limit': 0}).data['objects']
metamorphic_grades = api.metamorphic_grade.get(params={'limit': 0}).data['objects']
samples = api.sample.get(params={'fields': 'user__user_id,user__name,collector,number,sesar_number,country,public_data',
'limit': 0}).data['objects']
mineral_relationships = api.mineral_relationship.get(\
params={'limit': 0,
'fields':'parent_mineral__mineral_id,parent_mineral__name,child_mineral__mineral_id,child_mineral__name'}).\
data['objects']
mineralroots = []
parents = set()
children = set()
for m in mineral_relationships:
parents.add((m['parent_mineral__name'], m['parent_mineral__mineral_id']))
children.add((m['child_mineral__name'], m['child_mineral__mineral_id']))
mineralroots = set(parents) - set(children)
mineralnodes = []
for (name, mid) in mineralroots:
mineralnodes.append({"id": name, "parent": "#", "text": name, "mineral_id": mid})
for m in mineral_relationships:
node = {"id": m['child_mineral__name'], "parent": m['parent_mineral__name'], "text": m['child_mineral__name'], "mineral_id": m['child_mineral__mineral_id']}
mineralnodes.append(node)
for region in regions:
region_list.append(region['name'])
for rock_type in rock_type_list:
rock_type_list.append(rock_type['name'])
for ref in references:
reference_list.append(ref['name'])
for mmr in metamorphic_regions:
metamorphic_region_list.append(mmr['name'])
for mmg in metamorphic_grades:
metamorphic_grade_list.append(mmg['name'])
owner_dict = {}
if email:
logged_in_user = api.user.get(params={'email': email,
'fields': 'user_id,name'}).data['objects']
owner_dict[logged_in_user[0]['user_id']] = logged_in_user[0]['name']
for sample in samples:
collector_list.append(unicode(sample['collector']))
number_list.append(sample['number'])
igsn_list.append(sample['sesar_number'])
country_list.append(sample['country'])
"""The 'owners' list in the Provenance tab should contain only users
with public samples"""
if sample['public_data'] == 'Y':
if not sample['user__user_id'] in owner_dict:
owner_dict[sample['user__user_id']] = sample['user__name']
collector_list = list (set ( collector_list ))
country_list = list(set (country_list))
return render_template('search_form.html',
query='',
regions=region_list,
mineralrelationships=json.dumps(mineral_relationships),
mineral_nodes=json.dumps(mineralnodes),
rock_types=rock_types,
provenances=collector_list,
references=reference_list,
numbers=number_list,
igsns=igsn_list,
countries=country_list,
owners = owner_dict,
metamorphic_regions=metamorphic_region_list,
metamorphic_grades=metamorphic_grade_list)
@app.route('/login', methods=['GET', 'POST'])
def login():
if session.get('api_key'):
return redirect(url_for('search'))
form = LoginForm()
if form.validate_on_submit():
payload = {'email': form.email.data,
'password': form.password.data}
response = post(env('API_HOST') + '/authenticate/', data=payload)
if response.status_code == 200:
data = json.loads(response.text)
session['email'] = data['email']
session['api_key'] = data['api_key']
flash('Login successful!')
return redirect(url_for('search'))
else:
flash('Authentication failed. Please try again.')
return render_template('login.html',
form=form)
@app.route('/logout')
def logout():
session.pop('email', None)
session.pop('api_key', None)
flash('Logout successful.')
return redirect(url_for('search'))
@app.route('/request-password-reset', methods=['GET', 'POST'])
def request_reset_password():
form = RequestPasswordResetForm()
if form.validate_on_submit():
payload = {'email': form.email.data}
response = post(env('API_HOST') + '/reset-password/', data=payload)
if response.status_code == 200:
data = json.loads(response.text)
message = Message("Metpetdb: Reset Password",
sender=env('DEFAULT_MAIL_SENDER'),
recipients=[form.email.data])
reset_url = url_for('reset_password', token=data['reset_token'],
_external=True)
message.body = render_template('reset_password_email.html',
reset_url=reset_url)
mail.send(message)
flash('Please check your email for a link to reset your password')
return redirect(url_for('login'))
else:
flash("Invalid email. Please try again.")
return render_template('request_password_reset.html', form=form)
@app.route('/reset-password/<string:token>', methods=['GET', 'POST'])
def reset_password(token):
form = PasswordResetForm()
if form.validate_on_submit():
payload = {'token': form.token.data,
'password': form.password.data}
response = post(env('API_HOST') + '/reset-password/', data=payload)
if response.status_code == 200:
data = json.loads(response.text)
session['email'] = data['email']
session['api_key'] = data['api_key']
flash('Password reset successful!')
return redirect(url_for('search'))
else:
flash('Password reset failed. Please try again.')
return redirect(url_for('request_reset_password'))
if token:
response = get(env('API_HOST') + '/reset-password/' + token)
if response.status_code == 200:
form = PasswordResetForm(token=token)
return render_template('reset_password.html', form=form)
flash('Password reset failed. Please try again.')
return redirect(url_for('request_reset_password'))
@app.route('/samples/')
def samples():
email = session.get('email', None)
api_key = session.get('api_key', None)
api = MetpetAPI(email, api_key).api
filters = ast.literal_eval(json.dumps(request.args))
offset = request.args.get('offset', 0)
filters['offset'] = offset
data = api.sample.get(params=filters)
next, previous, last, total_count = paginate_model('samples', data, filters)
samples = data.data['objects']
for sample in samples:
if 'minerals' in samples:
mineral_names = [mineral['name'] for mineral in sample['minerals']]
sample['mineral_list'] = (', ').join(mineral_names)
first_page_filters = filters
del first_page_filters['offset']
if filters:
first_page_url = url_for('samples') + '?' + urlencode(first_page_filters)
else:
first_page_url = url_for('samples') + urlencode(first_page_filters)
return render_template('samples.html',
samples=samples,
next_url=next,
prev_url=previous,
total=total_count,
first_page=first_page_url,
last_page=last)
@app.route('/sample/<int:id>')
def sample(id):
email = session.get('email', None)
api_key = session.get('api_key', None)
api = MetpetAPI(email, api_key).api
sample = api.sample.get(id).data
location = sample['location'].split(" ")
longtitude = location[1].replace("(","")
latitude = location[2].replace(")","")
loc = [longtitude, latitude]
filter = {"sample__sample_id": sample['sample_id'], "limit": "0"}
subsamples = api.subsample.get(params=filter).data['objects']
aliases = api.sample_alias.get(params=filter).data['objects']
aliases_str = [alias['alias'] for alias in aliases]
regions = [region['name'] for region in sample['regions']]
metamorphic_regions = [metamorphic_region['name'] for metamorphic_region in sample['metamorphic_regions']]
metamorphic_grades = [metamorphic_grade['name'] for metamorphic_grade in sample['metamorphic_grades']]
references = [reference['name'] for reference in sample['references']]
minerals = [mineral['name'] for mineral in sample['minerals']]
if sample:
return render_template('sample.html',
sample=sample,
location=loc,
minerals=(', ').join(minerals),
regions=(', ').join(regions),
references=(', ').join(references),
metamorphic_grades=(', ').join(metamorphic_grades),
metamorphic_regions=(', ').join(metamorphic_regions),
aliases=(', ').join(aliases_str),
subsamples=subsamples)
else:
return HttpResponse("Sample does not Exist")
@app.route('/subsample/<int:id>')
def subsample(id):
email = session.get('email', None)
api_key = session.get('api_key', None)
api = MetpetAPI(email, api_key).api
subsample = api.subsample.get(id).data
user = api.user.get(subsample['user']['user_id']).data
filter = {"subsample__subsample_id": subsample['subsample_id'],
"limit": "0"}
chemical_analyses = api.chemical_analysis.get(params=filter).data['objects']
if subsample:
return render_template('subsample.html',
subsample=subsample,
user=user,
chemical_analyses=chemical_analyses,
sample_id=subsample['sample'].split('/')[-2])
else:
return HttpResponse("Subsample does not Exist")
@app.route('/chemical_analyses/')
def chemical_analyses():
email = session.get('email', None)
api_key = session.get('api_key', None)
api = MetpetAPI(email, api_key).api
filters = ast.literal_eval(json.dumps(request.args))
offset = request.args.get('offset', 0)
filters['offset'] = offset
data = api.chemical_analysis.get(params=filters)
next, previous, last, total_count = paginate_model('chemical_analyses',
data, filters)
chemical_analyses = data.data['objects']
first_page_filters = filters
del first_page_filters['offset']
if filters:
first_page_url = url_for('chemical_analyses') + '?' + urlencode(first_page_filters)
else:
first_page_url = url_for('chemical_analyses') + urlencode(first_page_filters)
return render_template('chemical_analyses.html',
chemical_analyses=chemical_analyses,
next_url=next,
prev_url=previous,
total=total_count,
first_page=first_page_url,
last_page=last)
@app.route('/chemical_analysis/<int:id>')
def chemical_analysis(id):
email = session.get('email', None)
api_key = session.get('api_key', None)
payload = {'email': email, 'api_key': api_key}
url = env('API_HOST') + '/chemical_analysis/{0}'.format(id)
response = get(url, params=payload)
return render_template('chemical_analysis.html',
data=response.json())
@app.route('/user/<int:id>')
def user(id):
api = MetpetAPI(None, None).api
user = api.user.get(id).data
if sample:
return render_template('user.html', user=user)
else:
return HttpResponse("User does not Exist")
if __name__ == '__main__':
dotenv.read_dotenv('../app_variables.env')
app.run(debug=True)