-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
183 lines (137 loc) · 5.24 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
import os
from flask import Flask
from flask import render_template
from flask import request, redirect
from utils import *
from pprint import pprint
import re
from flask.ext.mongoengine import MongoEngine
from mongoengine import connect
from flask.ext.security import Security, MongoEngineUserDatastore, UserMixin, RoleMixin, login_required, current_user
from rq import Queue
from worker import conn
q = Queue(connection=conn)
import cooperhewitt.api.client
import json
import csv
access_token = os.environ['CH_API_KEY']
hostname = os.environ['CH_API_HOST']
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ['SECRET']
MONGO_URL = os.environ.get("MONGOHQ_URL")
if MONGO_URL:
credentials = re.sub(r"(.*?)//(.*?)(@hatch)", r"\2",MONGO_URL)
username = credentials.split(":")[0]
password = credentials.split(":")[1]
app.config["MONGODB_DB"] = MONGO_URL.split("/")[-1]
connect(
MONGO_URL.split("/")[-1],
host=MONGO_URL,
port=1043,
username=username,
password=password
)
else:
app.config['MONGODB_DB'] = os.environ['DB_NAME']
app.config['MONGODB_HOST'] = os.environ['DB_HOST']
app.config['MONGODB_PORT'] = os.environ['DB_PORT'] # 27017
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_SEND_REGISTER_EMAIL'] = False
app.config['SECURITY_PASSWORD_HASH'] = 'sha512_crypt'
app.config['SECURITY_PASSWORD_SALT'] = os.environ['SALT']
db = MongoEngine(app)
class Role(db.Document, RoleMixin):
name = db.StringField(max_length=80, unique=True)
description = db.StringField(max_length=255)
class User(db.Document, UserMixin):
email = db.StringField(max_length=255)
password = db.StringField(max_length=255)
active = db.BooleanField(default=True)
confirmed_at = db.DateTimeField()
roles = db.ListField(db.ReferenceField(Role), default=[])
class Log(db.Document):
method = db.StringField(max_length=300)
submitted_at = db.DateTimeField(default=datetime.datetime.now, required=True)
data = db.DynamicField()
# Setup Flask-Security
user_datastore = MongoEngineUserDatastore(db, User, Role)
security = Security(app, user_datastore)
@app.route('/')
def index():
#if current_user.get_id():
# user = user_datastore.find_user(id=current_user.get_id())
#if current_user:
# user = current_user
#else:
# user = ''
# api = cooperhewitt.api.client.OAuth2(access_token, hostname=hostname)
# method = 'cooperhewitt.exhibitions.getList'
# rsp = api.call(method)
# exhibitions = rsp['exhibitions']
exhibitions = {}
return render_template('index.html', exhibitions=exhibitions)
@app.route('/about/')
def about():
return render_template('about.html', title="About")
@app.route('/email/', methods=['GET', 'POST'])
def get_email():
if request.method == 'POST':
data = request.form['data']
method = request.form['method']
return render_template('get_email.html', title="Wait, we need your email!", method=method, data=data)
else:
return redirect('/')
@app.route('/thanks/')
def thanks():
return render_template('thanks.html', title="Thanks!")
@app.route('/random/', methods=['GET', 'POST'])
def random():
if request.method == 'POST' and request.form['data']:
meta = request.form['data']
email = request.form['email']
data = {}
data['meta'] = meta
data['email'] = email
job = Log(method='random_objects', data=data).save()
result = q.enqueue(
random_objects, data)
return redirect('/thanks/') ## should take us to a thanks page
else:
return redirect('/')
@app.route('/search/', methods=['GET', 'POST'])
def search():
if request.method == 'POST' and request.form['data']:
meta = request.form['data']
email = request.form['email']
data = {}
data['meta'] = meta
data['email'] = email
job = Log(method='search_objects', data=data).save()
result = q.enqueue(search_objects, args=(data,), timeout=3600)
return redirect('/thanks/') ## should take us to a thanks page
else:
return redirect('/')
@app.route('/list/', methods=['GET', 'POST'])
def list():
if request.method == 'POST' and request.form['data']:
meta = request.form['data']
email = request.form['email']
data = {}
data['meta'] = meta
data['email'] = email
job = Log(method='list_objects', data=data).save()
result = q.enqueue(
list_objects, data)
return redirect('/thanks/') ## should take us to a thanks page
else:
return redirect('/')
@app.route('/test/', methods=['GET', 'POST'])
@login_required
def test():
#result = user_datastore.create_role(name='admin', description='main man')
#result = user_datastore.add_role_to_user(current_user, 'admin')
api = cooperhewitt.api.client.OAuth2(access_token, hostname=hostname)
method = 'millerfox.objects.getInfo'
args = { 'accession_number': '7.2013.5' }
rsp = api.call(method, **args)
return render_template('test.html', rsp=json.dumps(rsp, indent=4,))