/
code.py
331 lines (319 loc) · 12.9 KB
/
code.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
#!/usr/bin/env python
import web
from web import form
from lucene import *
import urllib2
import os
import time
import img_sch
import shop2_to_shop_recmd
def readin(s):
f=open(s, 'r')
readin=f.read()
f.close()
return readin
def writedwn(s, path):
res=open(path, 'w')
res.write(s)
res.close()
web.config.debug = False
TimeMechine = time.strftime('%Y-%m-%d_%H:%M:%S',time.localtime(time.time()))
urls = (
'/', 'index',
'/index', 'index',
'/s', 'search',
'/indexpic', 'indexpic',
'/pchome', 'pchome'
)
DBStore = eval(readin('data/user_table.dat'))
FavoStore = eval(readin('data/favorite_table.dat'))
HistStore = eval(readin('data/history_table.dat'))
app = web.application(urls, globals())
session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'username':'', 'password':'', 'recommend':[]})
render = web.template.render('templates', globals={'content':session}, cache = False) # your templates
class index:
def GET(self):
if (session.username!=''):
user = session.username
info = 'Weclome!'
session.recommend = shop2_to_shop_recmd.shop_to_cmd(FavoStore[session.username])
else:
user = ''
info = 'Welcome!'
session.recommend = shop2_to_shop_recmd.shop_to_cmd({})
# print 'Hello fuck you!'
# print session.recommend
# print 'Hello fuck you!2'
return render.index(session, info)
def POST(self):
info = 'Welcome!'
user = ''
print '/............Process of LogIn & SignUp............../'
# print DBStore
try:
username1 = web.input()['inputUsername1']
password1 = web.input()['inputPassword1']
except:
username1 = ''
password1 = ''
try:
username2 = web.input()['inputUsername2']
password2 = web.input()['inputPassword2']
password3 = web.input()['inputPassword3']
except:
username2 = ''
password2 = ''
password3 = ''
if ((username1!='' and password1!='')and(username2=='' or password2=='' or password3=='')):
print 'Log In'
if ((username1 in DBStore)and(password1==DBStore[username1])):
user = username1
info = 'Log In Successfully'
session.username = username1
session.password = password1
else:
user = ''
info = 'User Non-existence or Password Error'
elif ((username1=='' or password1=='')and(username2!='' and password2!='' and password3!='')):
print 'Sign Up'
l1 = len(username2)
l2 = len(password2)
l3 = len(password3)
if not(l1>=4 and l1<=20 and l2>=6 and l2<=16 and l3>=6 and l3<=16):#matching rules
user = ''
info = 'Username: 4-20 characters; Password: 6-16 characters'
elif ((username2 not in DBStore)and(password2==password3)):
DBStore[username2] = password2
writedwn(str(DBStore).encode('utf8'), 'data/user_table.dat')
FavoStore[username2] = {}
writedwn(str(FavoStore).encode('utf8'), 'data/favorite_table.dat')
HistStore[username2] = {}
writedwn(str(HistStore).encode('utf8'), 'data/history_table.dat')
user = username2
info = 'Sign Up Successfully'
session.username = username2
session.password = password2
elif (username2 in DBStore):
user = ''
info = 'This Username has been used, please use another Username.'
elif (password2!=password3):
user = ''
info = 'Twice inputs of password are different'
else:
user = ''
info = 'Unknown Errors'
else:
user = ''
if (session.username==''):
info = 'Welcome!'
else:
info = 'Log Out Successfully'
session.username = ''
session.password = ''
if (session.username!=''):
session.recommend = shop2_to_shop_recmd.shop_to_cmd(FavoStore[session.username])
print 'Current User :', session.username
else:
session.recommend = shop2_to_shop_recmd.shop_to_cmd({})
# print HistStore
return render.index(session, info)
# ['place', 'cmd', 'price', 'distance']
class search:
def GET(self):
input_adr = web.input()['adr']
input_cmd = web.input()['cmd']
try:
input_typ = web.input()['typ']
input_bs = web.input()['bs']
except:
input_typ = ''
input_bs = ''
input_adr = input_adr.decode('utf8')
input_cmd = input_cmd.decode('utf8')
#print 'input_adr: ', input_adr
#print 'input_cmd: ', input_cmd
#print 'input_typ: ', input_typ
#print 'input_bs: ', input_bs
list_cmd = ['', '', 0, 0]
list_cmd[0] = input_adr
list_cmd[1] = input_cmd
if (input_typ=='price' and input_bs=='lth'):
list_cmd[2] = 1
elif (input_typ=='price' and input_bs=='htl'):
list_cmd[2] = 2
elif (input_typ=='distance' and input_bs=='lth'):
list_cmd[3] = 1
elif (input_typ=='distance' and input_bs=='htl'):
list_cmd[3] = 2
print str(list_cmd)
if ((len(str(input_cmd))+len(str(input_adr))) >= 0):
writedwn(str(list_cmd), 'data/cmd.dat')
os.system('python SearchFiles.py')
# print "The res: ", readin('data/res.dat')
res = eval(readin('data/res.dat'))
print 'The input_cmd: ', input_cmd
if (session.username!=''):
session.recommend = shop2_to_shop_recmd.shop_to_cmd(FavoStore[session.username])
else:
session.recommend = shop2_to_shop_recmd.shop_to_cmd({})
return render.search(res, input_adr, input_cmd, session)
else:
return render.index(session, '')
def POST(self):
try:
favi = web.input()['favorite']
except:
favi = ''
try:
x = web.input(myfile={})
filedir = './static/pic' # change this to the directory you want to store the file in.
if 'myfile' in x: # to check if the file-object is created
filepath=x.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
filename=filepath.split('/')[-1].decode('utf-8') # splits the and chooses the last part (the filename with extension)
fout = open(filedir +'/'+ filename,'wb') # creates the file where the uploaded file should be stored
fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.
fout.close() # closes the file, upload complete.
infile = filedir +'/'+filename
outfile = infile + ".thumbnail"
img_query = (filedir+'/'+filename).decode('utf8')
print img_query
img_cmd = img_sch.img_to_key(img_query).decode('utf8')
list_cmd = ['', '', 0, 0]
list_cmd[1] = img_cmd
writedwn(str(list_cmd), 'data/cmd.dat')
os.system('python SearchFiles.py')
# print "The res: ", readin('data/res.dat')
res = eval(readin('data/res.dat'))
print 'img_cmd: ', img_cmd
if (session.username!=''):
session.recommend = shop2_to_shop_recmd.shop_to_cmd(FavoStore[session.username])
else:
session.recommend = shop2_to_shop_recmd.shop_to_cmd({})
return render.search(res, '', img_cmd, session)
except:
pass
print 'Adding to Favorite: ', favi
# print type(x)
if (session.username!='' and favi!=''):
username = session.username
print 'Current User :', username
# print FavoStore
if (favi not in FavoStore[username]):
FavoStore[username][favi] = favi######### modify to 'url+id':'url'
writedwn(str(FavoStore).decode('utf8'), 'data/favorite_table.dat')
class indexpic:
def GET(self):
info = 'Welcome!'
if (session.username!=''):
session.recommend = shop2_to_shop_recmd.shop_to_cmd(FavoStore[session.username])
else:
session.recommend = shop2_to_shop_recmd.shop_to_cmd({})
return render.indexpic(session, info)
def POST(self):
info = 'Welcome!'
user = ''
print '/............Process of LogIn & SignUp............../'
# print DBStore
try:
username1 = web.input()['inputUsername1']
password1 = web.input()['inputPassword1']
except:
username1 = ''
password1 = ''
try:
username2 = web.input()['inputUsername2']
password2 = web.input()['inputPassword2']
password3 = web.input()['inputPassword3']
except:
username2 = ''
password2 = ''
password3 = ''
if ((username1!='' and password1!='')and(username2=='' or password2=='' or password3=='')):
print 'Log In'
if ((username1 in DBStore)and(password1==DBStore[username1])):
user = username1
info = 'Log In Successfully'
session.username = username1
session.password = password1
else:
user = ''
info = 'User Non-existence or Password Error'
elif ((username1=='' or password1=='')and(username2!='' and password2!='' and password3!='')):
print 'Sign Up'
l1 = len(username2)
l2 = len(password2)
l3 = len(password3)
if not(l1>=4 and l1<=20 and l2>=6 and l2<=16 and l3>=6 and l3<=16):#matching rules
user = ''
info = 'Username: 4-20 characters; Password: 6-16 characters'
elif ((username2 not in DBStore)and(password2==password3)):
DBStore[username2] = password2
writedwn(str(DBStore).encode('utf8'), 'data/user_table.dat')
FavoStore[username2] = {}
writedwn(str(FavoStore).encode('utf8'), 'data/favorite_table.dat')
HistStore[username2] = {}
writedwn(str(HistStore).encode('utf8'), 'data/history_table.dat')
user = username2
info = 'Sign Up Successfully'
session.username = username2
session.password = password2
elif (username2 in DBStore):
user = ''
info = 'This Username has been used, please use another Username.'
elif (password2!=password3):
user = ''
info = 'Twice inputs of password are different'
else:
user = ''
info = 'Unknown Errors'
else:
user = ''
if (session.username==''):
info = 'Welcome!'
else:
info = 'Log Out Successfully'
session.username = ''
session.password = ''
if (session.username!=''):
session.recommend = shop2_to_shop_recmd.shop_to_cmd(FavoStore[session.username])
print 'Current User :', session.username
else:
session.recommend = shop2_to_shop_recmd.shop_to_cmd({})
# print HistStore
return render.indexpic(session, info)
class pchome:
def GET(self):
try:
delete_item = eval(web.input()['deleteFavo'])
# print delete_item
key_delete = str(delete_item[1]).decode('utf8')+'_'+str(delete_item[-1])
except:
key_delete = 'meiyouooooooo'
pass
print key_delete
if (session.username!=''):
if (key_delete in FavoStore[session.username]):
FavoStore[session.username].pop(key_delete)
writedwn(str(FavoStore).decode('utf8'), 'data/favorite_table.dat')
else:
pass
##try to delete item##
src = []
if (session.username!=''):
session.recommend = shop2_to_shop_recmd.shop_to_cmd(FavoStore[session.username])
else:
session.recommend = shop2_to_shop_recmd.shop_to_cmd({})
##Show recommend(above)##
if (session.username!=''):
username = session.username
print 'Current User :', username
# print type(FavoStore[username])
src = shop2_to_shop_recmd.favishow(FavoStore[username])
#print 'SRC', src
#print '666'
if (session.username!=''):
return render.pchome(src, session)
else:
return render.index(session, 'Welcome!')
if __name__ == "__main__":
app.run()