/
app.py
253 lines (207 loc) · 7.31 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
"""
bitrich-www
~~~~~~~~~~~
"""
from datetime import datetime
from json import dumps
from os import environ
from uuid import uuid4
from flask import (
Flask,
redirect,
render_template,
request,
url_for,
)
from flask.ext.stormpath import (
StormpathManager,
User,
login_required,
login_user,
logout_user,
user,
)
from requests import get, post
from sendgrid import SendGridClient, Mail
from stormpath.error import Error as StormpathError
import stripe
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = environ.get('SECRET_KEY')
app.config['STORMPATH_API_KEY_ID'] = environ.get('STORMPATH_API_KEY_ID')
app.config['STORMPATH_API_KEY_SECRET'] = environ.get('STORMPATH_API_KEY_SECRET')
app.config['STORMPATH_APPLICATION'] = environ.get('STORMPATH_APPLICATION')
app.config['STRIPE_SECRET_KEY'] = environ.get('STRIPE_SECRET_KEY')
app.config['STRIPE_PUBLISHABLE_KEY'] = environ.get('STRIPE_PUBLISHABLE_KEY')
app.config['COINBASE_API_KEY'] = environ.get('COINBASE_API_KEY')
sendgrid = SendGridClient(
environ.get('SENDGRID_USERNAME'),
environ.get('SENDGRID_PASSWORD'),
)
stormpath_manager = StormpathManager(app)
stormpath_manager.login_view = '.login'
stripe.api_key = app.config['STRIPE_SECRET_KEY']
##### Website
@app.route('/')
def index():
"""Basic home page."""
return render_template('index.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
"""
This view allows a user to register for the site.
This will create a new User in Stormpath, and then log the user into their
new account immediately (no email verification required).
"""
if request.method == 'GET':
return render_template('register.html')
try:
# Create a new Stormpath User.
_user = stormpath_manager.application.accounts.create({
'email': request.form.get('email'),
'password': request.form.get('password'),
'given_name': 'John',
'surname': 'Doe',
})
_user.__class__ = User
except StormpathError, err:
# If something fails, we'll display a user-friendly error message.
return render_template('register.html', error=err.message)
login_user(_user, remember=True)
message = Mail(
to = _user.email,
subject = 'Welcome to BitRich!',
text = '',
from_email = 'randall@stormpath.com',
)
message.set_html(render_template(
'email/verification_email.html',
user = user,
).encode('utf_8').decode('unicode_escape'))
sendgrid.send(message)
return redirect(url_for('dashboard'))
@app.route('/login', methods=['GET', 'POST'])
def login():
"""
This view logs in a user given an email address and password.
This works by querying Stormpath with the user's credentials, and either
getting back the User object itself, or an exception (in which case well
tell the user their credentials are invalid).
If the user is valid, we'll log them in, and store their session for later.
"""
if request.method == 'GET':
return render_template('login.html')
try:
_user = User.from_login(
request.form.get('email'),
request.form.get('password'),
)
except StormpathError, err:
return render_template('login.html', error=err.message)
login_user(_user, remember=True)
return redirect(request.args.get('next') or url_for('dashboard'))
@app.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
"""
This view renders a simple dashboard page for logged in users.
Users can see their personal information on this page, as well as store
additional data to their account (if they so choose).
"""
resp = get('https://coinbase.com/api/v1/currencies/exchange_rates')
rate = float(resp.json()['usd_to_btc'])
total_btc = total_usd = 0
for i, investment in enumerate(user.custom_data.get('investments', [])):
total_usd += investment['deposit_amount_usd']
total_btc += investment['deposit_amount_bitcoin']
total_btc = float(investment['deposit_amount_bitcoin'])
total_usd_cents = investment['deposit_amount_usd']
btc_adjusted = total_btc * (total_usd_cents / 100.0)
differential = float('%.2f' % (((rate - btc_adjusted) / btc_adjusted) * 100))
current_value = (investment['deposit_amount_usd']/100) + ((investment['deposit_amount_usd']/100) * (differential / 100))
user.custom_data['investments'][i]['current_value'] = current_value
return render_template('dashboard.html', total_usd=total_usd,
total_btc=total_btc)
@app.route('/charge', methods=['POST'])
def charge():
"""
Charge this user, and take their moneys!
"""
# By default, the following is true:
# - All investments are 20$.
# - The default lower limit is 50%.
# - The default upper limit is 50%.
amount = 100
#lower_limit = int(request.form.get('lower-limit')) or 50
#upper_limit = int(request.form.get('upper-limit')) or 50
lower_limit = 50
upper_limit = 50
id = uuid4().hex
# Create a Strip customer.
customer = stripe.Customer.create(
email = user.email,
card = request.form['stripeToken'],
)
# Bill the user.
stripe.Charge.create(
customer = customer.id,
amount = amount,
currency = 'usd',
description = 'BitRich Investment',
)
# Get current exchange rates:
resp = get('https://coinbase.com/api/v1/currencies/exchange_rates')
rate = float(resp.json()['usd_to_btc'])
resp = post(
'https://coinbase.com/api/v1/buys?api_key=%s' % app.config['COINBASE_API_KEY'],
headers = {
'Content-Type': 'application/json',
},
data = dumps({'qty': rate * (amount / 100)}),
)
# Store investment details in Stormpath.
try:
user.custom_data['investments'].append({
'id': id,
'created': datetime.utcnow().isoformat(),
'updated': datetime.utcnow().isoformat(),
'deposit_amount_usd': amount,
'deposit_amount_bitcoin': float(resp.json()['transfer']['btc']['amount']),
'lower_limit': lower_limit,
'upper_limit': upper_limit,
})
except:
user.custom_data['investments'] = []
user.custom_data['investments'].append({
'id': id,
'created': datetime.utcnow().isoformat(),
'updated': datetime.utcnow().isoformat(),
'deposit_amount_usd': amount,
'deposit_amount_bitcoin': float(resp.json()['transfer']['btc']['amount']),
'lower_limit': lower_limit,
'upper_limit': upper_limit,
})
user.save()
message = Mail(
to = user.email,
subject = 'Thanks for your Investment!',
text = '',
from_email = 'randall@stormpath.com',
)
message.set_html(render_template(
'email/deposit_email.html',
user = user,
).encode('utf_8').decode('unicode_escape'))
sendgrid.send(message)
return redirect(url_for('dashboard'))
@app.route('/logout')
@login_required
def logout():
"""
Log out a logged in user. Then redirect them back to the main page of the
site.
"""
logout_user()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run()