-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
160 lines (114 loc) · 4.11 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
# Copyright 2021 DAI Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Public version #
from flask import Flask, request, render_template, jsonify
from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
import atexit
from views.main_view import main_page_view, main_page_data
from views.address_views import address_page_view, address_data_view
from views.yay_views import yay_page_view, yay_data_view
from views.poll_views import poll_page_view, poll_data_view
from views.proxy_views import proxy_page_view, proxy_data_view
from connectors.sf import sf, sf_disconnect
from config import API_TOKEN, GUI_PASSWORD1, GUI_PASSWORD2
import api
app = Flask(__name__)
app.config['JSON_SORT_KEYS'] = False
auth = HTTPBasicAuth()
token_auth = HTTPTokenAuth(scheme='Bearer')
users = {
"legal": generate_password_hash(GUI_PASSWORD1),
"internal": generate_password_hash(GUI_PASSWORD2),
"community": generate_password_hash(GUI_PASSWORD2)
}
# LOGIN authorization
@auth.verify_password
def verify_password(username, password):
if username in users and \
check_password_hash(users.get(username), password):
return username
# ToDo: IRV voting
# API authorization
@token_auth.verify_token
def verify_token(token):
if 'access_token' in request.args.to_dict().keys():
if token == API_TOKEN or request.args.to_dict()['access_token'] == API_TOKEN:
return True
else:
if token == API_TOKEN:
return True
# API endpoints --------------------------------------------
@app.route("/api/last_block", methods=['GET'])
# @token_auth.login_required
def api_get_last_block():
return api.get_last_block()
@app.route("/api/last_time", methods=['GET'])
# @token_auth.login_required
def api_get_last_time():
return api.get_last_time()
# HTML endpoints -------------------------------------------
@app.route('/')
# @auth.login_required
def main_page():
return main_page_view(sf)
@app.route('/address/<address>')
# @auth.login_required
def address_page(address):
return address_page_view(sf, address.lower())
@app.route('/proxy/<proxy>')
# @auth.login_required
def proxy_page(proxy):
return proxy_page_view(sf, proxy.lower())
@app.route('/yay/<yay_id>')
# @auth.login_required
def yay_page(yay_id):
return yay_page_view(sf, yay_id)
@app.route('/poll/<poll_id>')
# @auth.login_required
def poll_page(poll_id):
return poll_page_view(sf, poll_id)
# DATA endpoints -------------------------------------------
@app.route("/data/main", methods=["GET"])
# @auth.login_required
def get_main_page_data():
dataset = main_page_data(sf)
return jsonify(dataset)
@app.route("/data/address/<address>", methods=["GET"])
# @auth.login_required
def get_address_page_data(address):
dataset = address_data_view(sf, address.lower())
return jsonify(dataset)
@app.route("/data/proxy/<proxy>", methods=["GET"])
# @auth.login_required
def get_proxy_page_data(proxy):
dataset = proxy_data_view(sf, proxy.lower())
return jsonify(dataset)
@app.route("/data/yay/<yay>", methods=["GET"])
# @auth.login_required
def get_yay_page_data(yay):
dataset = yay_data_view(sf, yay)
return jsonify(dataset)
@app.route("/data/poll/<poll>", methods=["GET"])
# @auth.login_required
def get_poll_page_data(poll):
dataset = poll_data_view(sf, poll)
return jsonify(dataset)
# cleanup tasks
def cleanup_task():
if not sf.is_closed():
sf_disconnect(sf)
print('SF connection closed.')
atexit.register(cleanup_task)
if __name__ == '__main__':
app.run(debug=False)