-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.py
executable file
·300 lines (255 loc) · 8.47 KB
/
verify.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
#!/usr/bin/python
from collections import OrderedDict
try:
import cPickle as pickle
except:
import pickle
import json,os
import random
import string
from collections import defaultdict
from blinker import signal
from flask import Flask
from flask import make_response,Response
from flask import render_template,flash
from flask import request,session,g
import httplib2
import oauth2client.client
from oauth2client.crypt import AppIdentityError
from oauth2client.client import verify_id_token
import MySQLdb
from gcm import *
from orderclass import Order,OrderWrapper,jdefault,JsonLoad
from google.appengine.api import mail
import jinja2
import requests
import urllib3
urllib3.disable_warnings()
APPLICATION_NAME = 'Grocshare'
app = Flask(__name__)
app.secret_key = ''.join(random.choice(string.ascii_uppercase + string.digits)
for x in range(32))
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader('templates'),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
# Update client_secrets.json with your Google API project information.
# Do not change this assignment.
CLIENT_ID = json.loads(
open('client_secrets.json', 'r').read())['web']['client_id']
INSTANCE_NAME="grocshare-0408:grocshare-db"
API_KEY = json.loads(open('gcm_id.json', 'r').read())['api_key']
# Start the scheduler
send_data = signal('addorder')
check = signal('checkorder')
def sendmergedorder(mergelist=None,total=0):
for items in mergelist:
send_user_email(items)
userid=items['userid']
send_gcm(userid=userid,message="Your order has been sent to the vendor")
send_vendor_email(mergelist)
def totalcost(o):
total=0
print o['items']
for i in o['items']:
total=total+i["cost"]
print total
return total
def send_user_email(order):
db=get_request_connection()
cur=db.cursor()
userid=order['userid']
items=order['items']
d1={"items":items}
query="""select username,email from userlist where userid={};""".format(userid)
cur.execute(query);
d = cur.fetchone()
username=d[0]
email=d[1]
d1['username']=username
d1['total']=totalcost(order)
template = JINJA_ENVIRONMENT.get_template('userorder.html')
subject="Grocshare Order Details"
message = mail.EmailMessage(sender="Grocshare New Order Confirmation <[email protected]>",subject=subject)
message.to = email
message.html=template.render(d1)
message.send()
def send_vendor_email(mergelist=None):
db=get_request_connection()
cur=db.cursor()
order=defaultdict(list)
email="[email protected]"
subject="Grocshare Order Details"
for m in mergelist:
g={}
query="""select username,email from userlist where userid={};""".format(m['userid'])
cur.execute(query);
d = cur.fetchone()
g["username"]=d[0]
g["email"]=d[1]
g["items"]=m["items"]
g["total"]=totalcost(m)
order["orders"].append(g)
# print json.dumps(order)
message = mail.EmailMessage(sender="Grocshare Orders <[email protected]>",subject=subject)
template = JINJA_ENVIRONMENT.get_template('vendor.html')
message.to = email
message.html=template.render(order)
message.send()
def request_has_connection():
return hasattr(g, 'db')
def get_request_connection():
if not request_has_connection():
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
g.db = MySQLdb.connect(unix_socket='/cloudsql/' + INSTANCE_NAME, db='orderlist', user='root', charset='utf8',passwd='root')
else:
g.db = MySQLdb.connect(host='127.0.0.1', port=3306, db='orderlist', user='root',charset='utf8',passwd='toor')
return g.db
@app.teardown_request
def close_db_connection(ex):
if request_has_connection():
conn = get_request_connection()
conn.close()
def index():
"""Render index.html."""
response=Response("grocshare application",status=200)
return response
@app.route('/auth',methods=['GET'])
def get():
if 'valid' in session.keys():
valid=session['valid']
resp=Response(str(valid),status=200,mimetype='text/plain')
else:
resp=Response(status=404)
return resp
@app.route('/auth', methods=['POST'])
def verify():
"""Verify an ID Token or an Access Token."""
id_token = request.form.get('id_token')
access_token = request.form.get('access_token', None)
token_status = {}
id_status = {}
valid=""
v={}
print id_token
if id_token is not None:
# Check that the ID Token is valid.
try:
# Client library can verify the ID token.
jwt = verify_id_token(id_token, CLIENT_ID)
valid=True
db=get_request_connection()
cursor=db.cursor()
name=jwt['given_name']
email=jwt['email']
userid=jwt['sub']
try:
cursor.execute("insert into userlist(username,userid,email) values(%s,%s,%s);",(name,userid,email))
except:
Response("Could not save data",status=200)
db.commit()
# db.close()
except AppIdentityError:
valid=False
token_status['id_token_status'] = id_status
session['valid']=valid
v['valid']=valid
resp = Response(json.dumps(v), status=200, mimetype='application/json')
else:
resp=Response("Empty token",status=200)
return resp
@app.route('/addorder',methods=['POST'])
def addorder():
orderslist=json.loads(request.data)
userid=orderslist['userID']
thisorder=OrderWrapper(userid)
totalcost=orderslist['total']
for o in orderslist['items']:
orderitem=Order(o['item'],o['qty'],o['cost'])
orderitemjson=json.dumps(orderitem,default=jdefault)
thisorder.addorder(orderitem)
db=get_request_connection()
cursor=db.cursor()
cursor.execute("insert into orders(userid,status,orderObject,totalcost) values(%s,%s,%s,%s);",(userid,"pending",json.dumps(thisorder,default=jdefault),totalcost))
db.commit()
send_gcm(userid=userid,message="Your order has been submitted")
result = send_data.send('add',abc=123)
return Response(status=200)
@app.route('/history/<userid>',methods=['GET'])
def history(userid):
db=get_request_connection()
cursor=db.cursor()
status=""
query='select * from orders where userid={}'.format(userid)
cursor.execute(query)
orderlist=defaultdict(list)
rows=cursor.fetchall()
if len(rows)<1:
return Response(json.dumps({"status":"invalid user id"}),status=200)
for r in rows:
g=OrderedDict()
g["orderid"]=int(r[0])
g["items"]=eval(r[4])['items']
g["status"]=r[3]
g["total"]=float(r[5])
orderlist["orders"].append(g)
orderlist["userid"]=userid
return Response(json.dumps(orderlist,indent=4),status=200,mimetype='application/json')
@app.route('/checkemail',methods=['GET'])
def checkemail():
return Response(status=200)
@app.route('/checkorder',methods=['GET'])
def checkorder():
res=check.send('add',abc=123)
return Response(json.dumps({"status":res[0][1]}),status=200)
@check.connect
@send_data.connect
def receive_data(sender, **kw):
db=get_request_connection()
cur=db.cursor()
cur.execute("select orderid,userid,time,totalcost,orderObject from orders where status='pending';")
l=cur.fetchall()
print len(l)
if len(l)<1:
return "No pending orders"
total=0
k=[]
orders=[]
for i,r in enumerate(l):
total=total+float(r[3])
orders.append(int(r[0]))
k.append(json.loads(r[4]))
if total>=25:
sendmergedorder(k,total)
changestatus(orders)
break
def changestatus(orders):
for o in orders:
db=get_request_connection()
cur=db.cursor()
query="""update orders set status='completed' where orderid={} and status='pending';""".format(o)
cur.execute(query)
db.commit()
@app.route('/gcmregister',methods=['POST'])
def gcmregister():
data=json.loads(request.data)
print data
reg_id = data['regID']
userid = data['userID']
db=get_request_connection()
cur=db.cursor()
query="""update userlist set regID='{}' where userid={};""".format(reg_id,userid)
cur.execute(query)
db.commit()
return "registration successful",200
def send_gcm(userid,message):
gcm = GCM(API_KEY)
db=get_request_connection()
cursor=db.cursor()
l=cursor.execute("""select regID from userlist where userid={};""".format(userid))
reg_id=cursor.fetchone()
data = {'message':message}
gcm.plaintext_request(registration_id=reg_id, data=data)
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=80)