-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebapp.py
218 lines (167 loc) · 6.23 KB
/
webapp.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
import flask
import json
import database
import psycopg2
from flask_sqlalchemy import SQLAlchemy
import settings as ss
import os
import json
import subprocess
from flask import request, redirect, url_for, flash, render_template, session
from utils import *
CURRENT_FILE = os.path.abspath(__file__)
CURRENT_DIR = os.path.dirname(CURRENT_FILE)
UPLOAD_FOLDER = CURRENT_DIR + '/static/data/image/'
DB_URL = 'postgresql+psycopg2://{user}:{pw}@{url}:{port}/{db}'.format(
user=ss.DATABASE_ADMIN, pw=ss.DATABASE_PWD, url=ss.DATABASE_ADDRESS, port=ss.DATABASE_PORT, db=ss.DATABASE_NAME)
#
DB_CON = psycopg2.connect(database="open-collectives", user="postgres",
password="LARM_ghol6lick.jusm",
host="daraja-test.cclr5pzf7wtq.us-east-1.rds.amazonaws.com",
port=5432)
# DB_CON.rollback()
webapp = flask.Flask(
__name__,
static_url_path="",
static_folder="./static",
template_folder="templates"
)
webapp.config["DEBUG"] = True
webapp.secret_key = "change this later!"
webapp.config['SQLALCHEMY_DATABASE_URI'] = DB_URL
# silence the deprecation warning
webapp.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(webapp)
webapp.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
cur = DB_CON.cursor()
@webapp.route("/")
def index():
return render_template("index.html")
@webapp.route("/film")
def film():
return render_template("film.html")
@webapp.route("/care")
def care():
return render_template("care.html")
@webapp.route("/living")
def living():
return render_template("living.html")
@webapp.route("/market")
def market():
return render_template("market.html")
@webapp.route("/labor")
def labor():
return render_template("labor.html")
@webapp.route("/voices", methods=["POST", "GET"])
def voices():
# answers = {}
# if request.method == "GET":
# answers['labor'] = request.args.getlist("labor")
# answers['market'] = request.args.get("market")
# answers['care'] = request.args.get("care")
# answers['living'] = request.args.getlist("living")
# print(answers)
# for category in answers:
# if answers[category]:
# if isinstance(answers[category], str):
# pass
# elif isinstance(answers[category], list):
# answers[category] = convert_to_csv(answers[category])
# else:
# pass
# sql = '''INSERT INTO responses (labor, market, care, living) VALUES (%(labor)s, %(market)s, %(care)s, %(living)s) RETURNING response_id'''
# cur = DB_CON.cursor()
# query_data = {
# 'labor': answers['labor'],
# 'market': answers['market'],
# 'care': answers['care'],
# 'living': answers['living']
# }
# cur.execute(sql, query_data)
# DB_CON.commit()
return render_template("voices.html")
@webapp.route("/about")
def about():
return render_template("about.html")
@webapp.route("/responses")
def responses():
cur.execute('''SELECT response_id, labor, market, care, living, country, admin1, city FROM responses_final''')
response = cur.fetchall()
# DB_CON.commit()
count = 0
data = []
for res in response:
if not all_false(res):
pass
else:
count += 1
data.append({"responseID": count,
"labor": convert_to_array(res[1]),
"market": res[2],
'care': res[3],
'living': convert_to_array(res[4]),
'country': res[5],
'admin1': res[6],
'city': res[7]})
return flask.Response(json.dumps(data), mimetype="application/json")
@webapp.route("/survey", methods=["GET"])
def survey():
answers = {}
answers['rowID'] = request.args.get("rowID")
answers['country'] = request.args.get("country")
answers['admin1'] = request.args.get("admin1")
answers['city'] = request.args.get("city")
answers['labor'] = request.args.getlist("labor")
answers['market'] = request.args.get("market")
answers['care'] = request.args.get("care")
answers['living'] = request.args.getlist("living")
print(answers)
for category in answers:
if answers[category]:
if isinstance(answers[category], str):
pass
elif isinstance(answers[category], list):
answers[category] = convert_to_csv(answers[category])
else:
pass
sql = '''INSERT INTO responses_final (labor, market, care, living, country, admin1, city)
VALUES (%(labor)s, %(market)s, %(care)s, %(living)s, %(country)s, %(admin1)s, %(city)s)
RETURNING response_id'''
query_data = {
'labor': answers['labor'],
'market': answers['market'],
'care': answers['care'],
'living': answers['living'],
'country': answers['country'],
'admin1': answers['admin1'],
'city': answers['city']
}
cur.execute(sql, query_data)
DB_CON.commit()
answers['rowID'] = cur.fetchone()
return flask.Response(json.dumps(answers), mimetype="application/json")
@webapp.route("/dimensions")
def home():
return render_template("dimensions.html")
@webapp.route("/post_collective", methods =['POST'])
#Function to post form input to the postgres database
def post_collective():
success = database.post_collective()
# submission_successful = True
# Redirects back to the home page for flashed message
response = {'success': success, 'data': database.make_api_from_db()}
return json.dumps(response)
# return redirect(url_for('home'))
@webapp.route("/data")
def data():
# database.upload_json_to_db() # Only call this once to upload the data to database
return database.make_api_from_db()
@webapp.route("/pull")
def pull():
os.chdir('/home/mitcivicdata/webapps/collective_dimensions')
subprocess.run(['git', 'reset', '--hard', 'HEAD'])
response = subprocess.check_output(['git','pull'])
subprocess.run(['touch', 'htdocs/index.py'])
return response
if __name__ == "__main__":
webapp.run(debug=True)