-
Notifications
You must be signed in to change notification settings - Fork 0
/
postpushr.py
298 lines (248 loc) · 9.85 KB
/
postpushr.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python
from flask import Flask, Response, session, redirect, url_for, escape, request, render_template, g, flash, make_response
from functions import *
import tasks
from User import User
from bson.objectid import ObjectId
from dateutil import parser
import arrow, urllib
from email_reply_parser import EmailReplyParser
app = Flask(__name__)
app.secret_key = os.environ['sk']
launch_celery()
@app.route('/api/login', methods=['POST'])
def api_login():
username = request.form.get('email')
password = request.form.get('password')
if None in [username,password]:
return Response(response=jfail("missing required parameters"), status=200)
user = User(username)
if user.is_valid():
if user.check_pass_hash(password):
return Response(response=jsuccess(), status=200)
else:
return Response(response=jfail("incorrect password"), status=200)
else:
return Response(response=jfail("user does not exist"), status=200)
@app.route('/api/token', methods=['POST'])
def api_token_request():
username = request.form.get('email')
password = request.form.get('password')
token = request.form.get('token')
if len([x for x in [username,password,token] if x == None]) > 1:
return Response(response=jfail("missing required parameters"), status=200)
user = User(username)
if user.is_valid():
if password:
if user.check_pass_hash(password):
return Response(response=jsuccess_with_token(user.get_token()), status=200)
else:
return Response(response=jfail("incorrect password"), status=200)
else:
checked = user.check_token(token)
if checked == 1:
return Response(response=jsuccess(), status=200)
elif checked == 0:
return Response(response=jfail("expired token"), status=200)
else:
return Response(response=jfail("invalid token"), status=200)
else:
return Response(response=jfail("user does not exist"), status=200)
@app.route('/api/user', methods=['POST'])
def api_user():
username = request.form.get('email')
token = request.form.get('token')
if None in [username,token]:
return Response(response=jfail("missing required parameters"), status=200)
user = User(username)
if user.is_valid():
checked = user.check_token(token)
if checked == 1:
return Response(response=api_user_json(user), status=200)
elif checked == 0:
return Response(response=jfail("expired token"), status=200)
else:
return Response(response=jfail("invalid token"), status=200)
else:
return Response(response=jfail("user does not exist"), status=200)
@app.route('/api/register', methods=['POST'])
def api_register():
username = request.form.get('email').strip()
password = request.form.get('password')
name = request.form.get('name').strip()
address = request.form.get('address').lower().strip()
token = request.form.get('token')
if None in [username,password,name,address,token]:
return Response(response=jfail("missing required parameters"), status=200)
if User(username).is_valid():
return Response(response=jfail('username taken'), status=200)
cust = create_stripe_cust(token,username)
if cust == None:
return Response(response=jfail('card was declined'), status=200)
userid = create_user(username,password,name=name,token=cust,address=address)
return Response(response=jsuccess(), status=200)
@app.route('/api/create/callback', methods=['POST'])
def api_postcard_callback():
txid = request.form.get('txid')
if None in [txid]:
return Response(response=jfail("missing required parameters"), status=200)
job = postcards.find_one({'jobid': txid})
if job == None:
return Response(response=json.dumps({"status": "pending"}), status=200)
cost = int(float(job["price"])*1.75*100)
return Response(response=json.dumps({"status": "success", "results": {"price": '$%0.2f' % (float(cost)/100.0), "date": arrow.get(parser.parse(job["date_created"])).format("h:m A MMMM D, YYYY")}}), status=200)
@app.route('/api/create/postcard', methods=['POST'])
def api_postcard_create():
username = request.form.get('email')
token = request.form.get('token')
name = request.form.get('name')
message = request.form.get('message')
address = request.form.get('address')
picture = request.files['picture']
if None in [username,password,name,message,address,picture]:
return Response(response=jfail("missing required parameters"), status=200)
user = User(username)
if user.is_valid():
checked = user.check_token(token)
if checked == 1:
return Response(response=send_postcard(user,name,address,message,picture), status=200)
elif checked == 0:
return Response(response=jfail("expired token"), status=200)
else:
return Response(response=jfail("invalid token"), status=200)
else:
return Response(response=jfail("user does not exist"), status=200)
@app.route('/', methods=['POST', 'GET'])
def index():
if 'userid' in session:
return redirect(url_for('documents'))
if request.method == "POST":
user = User(request.form.get('email'))
if user.is_valid():
if user.check_pass(request.form.get('password')):
session["userid"] = str(user.get("_id"))
return redirect(url_for('documents'))
else:
flash("Your password was incorrect.")
else:
session["username"] = request.form.get('email').lower()
session["password"] = request.form.get('password')
return redirect(url_for('signup'))
return render_template('index.html')
@app.route('/signup', methods=['POST', 'GET'])
def signup():
if 'userid' in session:
return redirect(url_for('documents'))
if request.method == "POST":
username = session.pop("username").strip()
password = session.pop("password")
name = request.form.get("name").strip()
snapchat = request.form.get("snapchat").strip()
token = request.form.get("stripeToken")
address = request.form.get("address").lower().strip()
if User(username).is_valid():
flash("That email has already been registered.")
return redirect(url_for('index'))
cust = create_stripe_cust(token,username)
if cust == None:
flash("Your card was declined.")
return redirect(url_for('signup'))
userid = create_user(username,hash_password(password),name=name,snapchat=snapchat,token=cust,address=address)
session["userid"] = userid
return redirect(url_for('documents'))
return render_template('signup.html',email=session["username"],smarty_key=os.environ['smarty_key'])
@app.route('/documents', methods=['POST', 'GET'])
def documents():
if session.get('userid') == None:
return redirect(url_for('index'))
user = User(None,userid=session["userid"])
if user.is_valid():
return render_template('documents.html',user=user)
else:
return redirect(url_for('logout'))
@app.route('/letter/<_hash>')
def get_letter(_hash):
l = letters.find_one({"jobid": _hash})
if session.get('userid') == None or l == None:
return redirect(url_for('index'))
user = User(None,userid=session["userid"])
if user.is_valid():
return render_template('document.html',l=l)
else:
return redirect(url_for('logout'))
@app.route('/logout')
def logout():
session["userid"] = None
session.clear()
return redirect(url_for('index'))
@app.route('/incoming/letter/email', methods=['POST', 'GET'])
def incoming_letter_email():
body = EmailReplyParser.parse_reply(unicode(request.form.get('text')).encode('ascii','xmlcharrefreplace'))
body = '\n'.join(body.split('\n')).replace("\n","<br />")
regexp = re.findall(r'[\w\.-]+@[\w\.-]+',request.form.get('from'))
try:
attachments = int(request.form.get('attachments'))
except Exception:
attachments = 0
if len(regexp) > 0 and len(regexp[-1]) > 0:
username = regexp[-1].lower()
else:
return_bad_params(username)
return Response(response=jfail("missing parameters"), status=200)
to_name = request.form.get('to')
to_address = unicode(request.form.get('subject')).encode('ascii','xmlcharrefreplace').lower().replace("fw:","").replace("re:","").strip()
if None in [body,username,to_name,to_address]:
return_bad_params(username)
return Response(response=jfail("missing parameters"), status=200)
user = User(username)
if user.is_valid():
send_letter(user,to_name,to_address,body,attachments)
else:
return_unknown_sender(username)
return Response(response=jfail("unknown sender"), status=200)
return Response(response=jsuccess(), status=200)
@app.route('/incoming/email/add', methods=['POST', 'GET'])
def add_new_email():
regexp = re.findall(r'[\w\.-]+@[\w\.-]+',request.form.get('from'))
if len(regexp) > 0:
new_email = regexp[len(regexp)-1].lower()
else:
return Response(response=jfail("missing parameters"), status=200)
userid = unicode(request.form.get('subject')).encode('ascii','xmlcharrefreplace')
user = User(None,userid=userid)
if user.is_valid():
user.add_email(new_email)
confirm_email_addition(user, new_email)
else:
return Response(response=jfail("unknown sender"), status=200)
return Response(response=jsuccess(), status=200)
@app.route('/incoming/email', methods=['POST', 'GET'])
def incoming_email():
text = unicode(request.form.get('text')).encode('ascii','xmlcharrefreplace')
html = unicode(request.form.get('html')).encode('ascii','xmlcharrefreplace')
regexp = re.findall(r'[\w\.-]+@[\w\.-]+',request.form.get('from'))
if len(regexp) > 0:
_from = regexp[len(regexp)-1]
else:
_from = request.form.get('from')
to = request.form.get('to')
subject = unicode(request.form.get('subject')).encode('ascii','xmlcharrefreplace')
forward_email(_from,subject,text,html)
return Response(response=jsuccess(), status=200)
@app.template_filter('ucfirst')
def ucfirst_filter(txt):
return ucfirst(txt)
@app.template_filter('dt')
def dt_filer(dt):
return arrow.get(parser.parse(dt)).format("MMMM D, YYYY")
@app.template_filter('s3URL')
def s3URL(_hash):
return bitly.shorten(tasks.s3_upload(_hash))["url"]
@app.template_filter('escURL')
def escURL(url):
return urllib.quote_plus(url)
if __name__ == '__main__':
if os.environ.get('PORT'):
app.run(host='0.0.0.0',port=int(os.environ.get('PORT')),debug=(os.environ.get('dev') == "True"))
else:
app.run(host='0.0.0.0',port=5000,debug=(os.environ.get('dev') == "True"))