-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
88 lines (69 loc) · 3.11 KB
/
run.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
from flask import Flask, render_template, request
from flask_wtf import FlaskForm
from flask_wtf.csrf import CSRFProtect
from wtforms import TextField, SelectField, SubmitField, RadioField
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from random import randint
import yaml
# Fetch configuration information
env_vars = yaml.load(open('config.yaml').read())
app = Flask(__name__)
app.secret_key = 'SoMeThInG000SeCrEt'
csrf = CSRFProtect(app)
app.config['MONGO_DBNAME'] = env_vars['database']['name']
app.config['MONGO_USERNAME'] = env_vars['database']['username']
app.config['MONGO_PASSWORD'] = env_vars['database']['pw']
app.config['MONGO_URI'] = env_vars['database']['uri']
mongo = PyMongo(app)
def get_random_id(docids):
id = randint(0, len(docids))
return docids[id]
class verifyAnswers(FlaskForm):
''' Form class for verification questions. '''
# Current test users
name = SelectField('Current user', choices=[('[email protected]', '[email protected]'), ('[email protected]', '[email protected]'), ('[email protected]', '[email protected]'), ('other', 'other')])
p_dam = RadioField('Predicted Dam', choices=[('1', 'Correct'), ('0', 'Incorrect'), ('-1', 'Skip')])
p_loc = RadioField('Predicted Dam', choices=[('1', 'Correct'), ('0', 'Incorrect'), ('-1', 'Skip')])
p_yr = RadioField('Predicted Dam', choices=[('1', 'Correct'), ('0', 'Incorrect'), ('-1', 'Skip')])
p_river = RadioField('Predicted Dam', choices=[('1', 'Correct'), ('0', 'Incorrect'), ('-1', 'Skip')])
id = TextField()
submit = SubmitField()
@app.route('/')
def main():
return render_template('index.html')
@app.route('/get', methods=['GET', 'POST'])
def get():
name = verifyAnswers() # Inst form
if request.method == 'GET':
docids = mongo.db.dams.distinct('_id') # Fetch mongodb doc ids
id = get_random_id(docids) # Randomly find one to review
name.id.data = id
dam = mongo.db.dams.find_one({'_id': ObjectId(id)})
# Attribute fields to bring over into the view for phrase context.
pred_dam = dam['dam']
pred_river = dam['river']
pred_yr = dam['year_removed']
pred_loc = dam['dam_location']
phrase = dam['phrase']
journal = dam['journal_title']
authors = dam['journal_authors']
return render_template('verify.html', dam=pred_dam, river=pred_river, yr=pred_yr, loc=pred_loc, name=name, phrase=phrase, journal=journal, authors=str(' '.join(authors)), id=id)
# Assign variables to the form data
dam = name.p_dam.data
loc = name.p_loc.data
river = name.p_river.data
yr = name.p_yr.data
id = name.id.data
usr = name.name.data
# Update
if '@' in usr:
usr = usr.split('@')[0]
try:
mongo.db.dams.update_one({'_id': ObjectId(id)}, {'$set': {'dam_verified-' + usr: dam, 'dam_location_verified-' + usr: loc, 'river_verified-' + usr: river, 'year_verified-' + usr: yr}})
except Exception as e:
print(e)
return('Sorry an error occured saving your results. Please contact us.')
return render_template('index.html')
if __name__ == "__main__":
app.run()