/
app_clone.py
392 lines (308 loc) · 12.4 KB
/
app_clone.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import os
from flask import Flask, request, redirect, render_template, url_for, jsonify, send_from_directory, session
from werkzeug.utils import secure_filename
from pymongo import MongoClient
from threading import Thread
import json
from bson import Binary, Code
from bson.json_util import dumps
from charge import *
from constants import CONSUMER_ID, CONSUMER_SECRET, APP_SECRET
import requests
# import scan.okraparser
# import scan.okraparser.OkraParseException
app = Flask(__name__)
app.secret_key = APP_SECRET
################################ DB ####################################
def get_db_conection(db):
client = MongoClient()
return client[db]
def get_db_collection(collection):
return get_db_conection('okra').okra[collection]
########################################################################
############################# TAB #########################
# CREATE TAB
@app.route('/create_tab', methods=['POST', 'GET'])
def create_tab():
db = get_db_conection("okra") #get conncection
# tabs = db.tabs #get tabs collection
tabs = get_db_collection('tabs')
# invites = db.invites #get invites collection
if request.method == 'POST':
#MUST VALIDATE
tab_id = request.form['id']
tab_group = request.form['group']
tab_items = dumps(request.form['items']) #convert to json
tab_subtotal = request.form['subtotal']
tab_total = request.form['total']
tab_tip = request.form['tip']
tab_tax = request.form['tax']
#prepare for db entry
tab = {
'id' : request.form['id'],
'title' : request.form['title'],
'group' : request.form['group'], #array of user ids
'items' : tab_items,
'subtotal' : request.form['subtotal'],
'total' : request.form['total'],
'tip' : request.form['tip'],
'tax' : request.form['tax']
}
tabs_id = tabs.insert(tab)
# create invites from group.
create_invites(tab_group, tab_id)
print 'items = ' + tabs.find_one({"_id":tabs_id})['items']
return 'inserted tab with tab_id: ' + tabs.find_one({"_id":tabs_id})['id']
else:
return "not a post request"
# GET TAB
@app.route('/get_tab', methods=['GET'])
def get_tab():
'''Returns tab object in json for requested tab_id'''
db = get_db_conection("okra") #get conncection
tabs = db.tabs #get tabs collection
tab_id = request.args.get('tab_id', '')
le_tab = tabs.find_one({"id" : tab_id})
json1 = dumps(le_tab)
return json1
# UPDATE TAB ITEMS
@app.route('/update_tab_items', methods=['POST'])
def update_tab_items():
''' updates items in a tab '''
tabs = get_db_collection('tabs')
tab_id = request.form['tab_id']
le_tab = tabs.find_one({"id" : tab_id})
if (le_tab == None):
return 'Tab not found'
else:
print le_tab['items'][1]
return 'jello'
# UPDATE TAB BILL
@app.route('/update_tab_bill', methods=['POST'])
def update_tab_bill(bill_json):
'''Updates tab to add each bill items description and value'''
db = get_db_conection("okra") #get conncection
tabs = get_db_collection('tabs')#get tabs collection
tab_id = request.args.get('tab_id', '')
le_tab = tabs.find_one({"id" : tab_id})
#whatever stevens json collection is called
bill_json = get_db_collection('bill_json')
#Insert bill items to tab
le_tab['items_prices'] = bill_json['tab_items']
le_tab['total'] = bill_json['tab_meta']
tabs.insert(le_tab)
########################################################################
############################## USERS ###########################
@app.route('/add_user' , methods=['POST', 'GET'])
def add_user():
users = get_db_collection('users')
if request.method == 'POST':
user_id = request.form['id']
user_phone = request.form['phone']
user_name = request.form['name']
user_friends = request.form['friends'] #list
user = {
'id' : user_id,
'phone' : user_phone,
'name' : user_name,
'friends' : user_friends
}
users.insert(user)
return "registerd user: " + users.find_one({"id":user_id})['id']
#GET USER
@app.route('/get_user')
def get_user():
''' gets a user_id and returns json info of user '''
users = get_db_collection('users')
user_id = request.args.get('user_id')
json = dumps(users.find_one({"id":user_id}))
print 'hello'
return json
#GET FRIENDS
@app.route('/get_friends')
def get_friends():
''' gets the list of friends with their ids and names for a given user id '''
users = get_db_collection('users')
user_id = request.args.get('user_id')
user = users.find_one({'id':user_id})
friend_ids = user['friends']
friends = {}
for friend_id in friend_ids:
name = users.find_one({'id':friend_id})['name']
friends[friend_id] = {name:'name'}
return friends
########################################################################
############################## ITEMS #############################
#ASSIGN ITEM
@app.route('/assign_item')
def assign_item(tab_id, item_id, user_id):
''' gets the list of friends with their ids and names for a given user id '''
tabs = get_db_collection('tabs')
tab_id = request.form['tab_id']
le_tab = tabs.find_one({"id" : tab_id})
le_tab['items'][2].append(user_id)
tabs.insert(le_tab)
#UNASSIGN ITEM
@app.route('/unassign_item')
def unassign_item(tab_id, item_id, user_id):
''' gets the list of friends with their ids and names for a given user id '''
tabs = get_db_collection('tabs')
tab_id = request.form['tab_id']
le_tab = tabs.find_one({"id" : tab_id})
le_tab['items'][2].append(user_id)
number_of_users = len(le_tab['items'][2])
remove_location = -1
i=0
for i in range(0, number_of_users):
if (le_tab['items'][2][i] == user_id):
remove_location = i
break
if (remove_location > 0):
le_tab['items'][2].pop(remove_location)
tabs.insert(le_tab)
############################## INVITE shit ############################
def create_invites(group, tab_id): #used by create tab to invite users that are added.
invites = get_db_collection('invites')
print "Creating invites for " + group
users = eval(group)
for user_id in users:
print str(user_id) + " " + tab_id
invite = { 'user_id': user_id, 'tab_id' : tab_id }
invite_id = invites.insert(invite)
#poll invite
@app.route('/poll_for_invite')
def poll_for_invite():
'''Returns tab_id if the passed user_id has an invite '''
#get db collection
invites = get_db_collection('invites')
#get param
req_user_id = request.args.get('user_id', '')
print "requested user id : " + req_user_id
inv = invites.find_one({'user_id':req_user_id})
print inv
if ( inv == None ):
return 'null'
else:
return inv['tab_id']
########################################################################
############################# UPLAOD IMAGE #############################
# This is the path to the upload directory
app.config['UPLOAD_FOLDER'] = 'images/'
# These are the extension that we are accepting to be uploaded
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
# For a given file, return whether it's an allowed type or not
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
# This route will show a form to perform an AJAX request
# jQuery is loaded to execute the request and update the
# value of the operation
@app.route('/capture-ocr')
def img_upload():
return render_template('img-upload.html')
# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
# Get the name of the uploaded file
file = request.files['file']
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
# Move the file form the temporal folder to
# the upload folder we setup
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Redirect the user to the uploaded_file route, which
# will basicaly show on the browser the uploaded file
thr = Thread(target = async_parse, args = [filename])
thr.start()
# return 'uploaded - async analyzing'
return redirect(url_for('uploaded_file', filename=filename))
def async_parse(filename):
try:
tabs = scan.okraparser.full_scan()
db = get_db_conection("okra") #get conncection
# tabs = get_db_collection('tabs')#get tabs collection
tab_id = request.args.get('tab_id', '')
le_tab = tabs.find_one({"id" : tab_id})
#whatever stevens json collection is called
bill_json = get_db_collection('bill_json')
#Insert bill items to tab
le_tab['items_prices'] = bill_json['tab_items']
le_tab['total'] = bill_json['tab_meta']
except OkraParseException:
print 'the scan was bad'
def send_email(subject, sender, recipients, text_body, html_body):
msg = Message(subject, sender = sender, recipients = recipients)
msg.body = text_body
msg.html = html_body
thr = Thread(target = send_async_email, args = [msg])
thr.start()
# This route is expecting a parameter containing the name
# of a file. Then it will locate that file on the upload
# directory and show it on the browser, so if the user uploads
# an image, that image is going to be show after the upload
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
########################################################################
############################## VENMO ###################################
### init
@app.route('/')
def index():
if session.get('venmo_token'):
return 'fuck'
# return 'Your Venmo token is %s' % session.get('venmo_token')
# venmo_token = session.get('venmo_token')
# charge_or_pay('charge',venmo_token,8576009129, 0.01,'')
# return 'Jaime charged and my token is %s' % venmo_token
else:
return redirect('https://api.venmo.com/v1/oauth/authorize?client_id=%s&scope=make_payments,access_profile&response_type=code' % CONSUMER_ID)
# return redirect('http://app.grasscat.org/index.html')
#### Charge
@app.route('/master_charge')
def master_charge(master):
if session.get('venmo_token'):
db = get_db_conection("okra") #get conncection
users = db.users #get tabs collection
for user in users:
user_id = request.args.get('user_id', '')
le_user = tabs.find_one({"id" : user_id})
if (user_id != ""):
charge_or_pay('pay', user.token, master.phone, master.amt, user.note)
# def add_venmo_user(phone_number,display_name, token):
###### OAuth
@app.route('/oauth-authorized')
def oauth_authorized():
db = get_db_conection("okra") #get conncection
users = get_db_collection('users')
AUTHORIZATION_CODE = request.args.get('code')
data = {
"client_id":CONSUMER_ID,
"client_secret":CONSUMER_SECRET,
"code":AUTHORIZATION_CODE
}
url = "https://api.venmo.com/v1/oauth/access_token"
response = requests.post(url, data)
response_dict = response.json()
access_token = response_dict.get('access_token')
user = response_dict.get('user')
session['venmo_token'] = access_token
session['venmo_username'] = user['username']
session['first_name'] = user['first_name']
session['last_name'] = user['last_name']
session['profile_picture_url'] = user['profile_picture_url']
users.insert( {
"first_name" : session['first_name'],
"second_name": session['last_name'],
"friends" : ["user_id","user_id"],
"phone": "",
"token": session['venmo_token'],
"pic_url": session['profile_picture_url']
}
)
#return 'fuck you %s' % session['venmo_token']
return 'You were signed in as %s' % session['venmo_token']
#########################################################################
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)