-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonFunction.py
704 lines (556 loc) · 20.8 KB
/
commonFunction.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
import datetime
import re
import os
import webapp2
import logging
#---------------- Import from Google ------------------
from google.appengine.api import users, images, memcache
from google.appengine.ext import blobstore, db
from google.appengine.ext.webapp import template
#
#------------------------------------------------------
#----------------- Custom Classes & -------------------
#----------------- Datastore Schema -------------------
#
import schema
#
#------------------------------------------------------
def returnXmlResult(self, *args):
path = os.path.join(os.path.dirname(__file__), "template/result.xml")
success='false'
message='no parameter given'
if len(args)>0:
success = args[0]
message=''
if len(args)>1:
message = args[1]
self.response.out.write(template.render(path, {
'success': success,
'message': message,
}))
#
def returnJsonResult(self, *args):
path = os.path.join(os.path.dirname(__file__), "template/statusResult.json")
success='false'
message='no parameter given'
if len(args)>0:
success = args[0]
message=''
if len(args)>1:
message = args[1]
self.response.out.write(template.render(path, {
'success': success,
'message': message,
}))
#
def retrieveTargetApplicationInfo(token):
tokenList = []
tokenListQuery = schema.ApplicationApiSettings.all().filter('apiType = ', 'partner').filter('key = ', token)
tokenListResult = tokenListQuery.fetch(1000)
for api in tokenListResult:
logging.debug("Retrieved from datastore: token = " + api.token)
tokenList.append({
'domain': api.domain,
'auth_token': api.token
})
return tokenList
def retrieveTargetApplicationList():
tokenList = []
tokenListQuery = schema.ApplicationApiSettings.all().filter('apiType = ', 'partner')
tokenListResult = tokenListQuery.fetch(1000)
for api in tokenListResult:
logging.debug("Retrieved from datastore: token = " + api.token)
tokenList.append({
'domain': api.domain,
'auth_token': api.token
})
return tokenList
# get token list.
# Since it is frequently used one, we have to put it in memcache to improve performance.
def getTokenList():
# prepare restricted nickname from cache then datastore
tokenList = memcache.get('api-own-token-list')
if tokenList is not None:
logging.debug('cache hit: tokenList = ' + str(tokenList))
else:
tokenList = []
tokenListQuery = schema.ApplicationApiSettings.all().filter('apiType = ', 'own')
tokenListResult = tokenListQuery.fetch(1000)
for api in tokenListResult:
logging.debug("Retrieved from datastore: token = " + api.token)
tokenList.append(api.token)
memcache.set('api-own-token-list', value= tokenList, time=3600)
logging.debug('Put into cache: tokenList = ' + str(tokenList))
return tokenList
def invalidateTokenList():
memcache.delete('api-own-token-list')
return
def retrieveEventLogFullSet():
list = ['',
'CREATE_ITEM',
'UPDATE_ITEM',
'DELETE_ITEM',
'PUBLISH_ITEM',
'EXPIRE_ITEM_REMINDER',
'ACCOUNT_ACCESS_DENIED',
'SHOPITEM_ACCESS_DENIED',
'MESSAGE_ACCESS_DENIED',
'TRNASACTION_ACCESS_DENIED',
'ACCOUNT_CREATE',
'ACCOUNT_PROFILE_CHANGE',
'ACCOUNT_ACTIVATE',
'ACCOUNT_DEACTIVATE',
'ACCOUNT_DELETE',
'ACCOUNT_ITEM_CLEAR',
'STUDENT_ACCOUNT_VERIFY',
'STUDENT_ACCOUNT_EXPIRE',
'STUDENT_ACC_VERIFY_FAILED',
'ADMIN_CREATE',
'ADMIN_DELETE',
'ADMIN_DELETE_ITEM',
'ADMIN_API_UPDATE',
'APP_SETTINGS_UPDATE',
'TRANSACTION_CREATE',
'TRANSACTION_CANCEL',
'TRANSACTION_APPROVED',
'TRANSACTION_REJECTED',
'TRANSACTION_RESOLVED',
'MESSAGE_CREATE',
'MESSAGE_REPLY'
]
return sorted(list)
#
def createEventLog(nickname, type, target, content):
log = schema.EventLog(
actor = nickname,
actionType = type,
target = target,
content = content
)
log.put()
#
def retrieveRestrictedNicknameList():
list = ['robot', 'system', 'admin', 'master', 'visitor', 'noreply']
return sorted(list)
def updateApplicationSettings(type, name, remarks, value):
if type == 'delete-restricted':
record = schema.ApplicationSettings.all().filter('type = ', 'nickname').filter('value = ', value).get()
logging.debug(record)
if record is not None:
record.delete()
return
# save into datastore then memcache
# check existence of record before creating one, update if exist
record = schema.ApplicationSettings.all().filter('type = ', type).filter('name = ', name).get()
if record is not None and type != 'nickname':
record.value = value
record.put()
else:
record = schema.ApplicationSettings(
type = type,
name = name,
remarks = remarks,
value = value
)
record.put()
if type != 'nickname':
memcache.set(generateMemcacheKey(type, name), value= value, time=3600)
logging.debug("Updated application settings. " + name + " = " + str(value))
return
def retrieveApplicationSettings(type, name):
if name == 'item_max_activeDays':
return retrieveApplicationSettingsWithDefault(type, name, 180)
elif name == 'item_min_activeDays':
return retrieveApplicationSettingsWithDefault(type, name, 1)
elif name == 'item_max_price':
return retrieveApplicationSettingsWithDefault(type, name, 5000)
elif name == 'item_min_price':
return retrieveApplicationSettingsWithDefault(type, name, 1)
elif name == 'item_max_quantity':
return retrieveApplicationSettingsWithDefault(type, name, 1000)
elif name == 'item_min_quantity':
return retrieveApplicationSettingsWithDefault(type, name, 1)
elif name == 'nickname_max_chars':
return retrieveApplicationSettingsWithDefault(type, name, 20)
elif name == 'nickname_min_chars':
return retrieveApplicationSettingsWithDefault(type, name, 5)
elif name == 'item_expire_alert_daysBefore':
return retrieveApplicationSettingsWithDefault(type, name, 3)
elif name == 'search_result_length':
return retrieveApplicationSettingsWithDefault(type, name, 10)
elif name == 'conversation_group_size':
return retrieveApplicationSettingsWithDefault(type, name, 5)
elif name == 'cron_item_expiryDateChecker':
return retrieveApplicationSettingsWithDefault(type, name, '1') #true
elif name == 'cron_account_expiryDateChecker':
return retrieveApplicationSettingsWithDefault(type, name, '1') #true
else:
logging.error("missing application settings: type = " +type+ ",name = "+ name)
return 0
def retrieveApplicationSettingsWithDefault(type, name, default):
# get from memcache then datastore
value = memcache.get(generateMemcacheKey(type, name))
# check memcache
if value is not None:
logging.debug("hit from memcache for name = " + name + ". value = " + str(value))
return value
# check datastore
else:
record = schema.ApplicationSettings.all().filter('type =', type).filter('name =', name).get()
if record is not None:
value = record.value
logging.debug("record found from datastore for name = " + name + ". value = " + str(value))
# update memcache
memcache.set(generateMemcacheKey(type, name), value= value, time=3600)
logging.debug("saved to memcache for name = " + name + ", type = "+ type +" with value = " + str(value))
else:
value = default
logging.debug("record not found from datastore for name = " +name + ", type = "+ type )
memcache.set(generateMemcacheKey(type, name), value= value, time=3600)
logging.debug("saved to memcache for name = " + name + ", type = "+ type +" with default value = " + str(value))
return value
#private
def generateMemcacheKey(type, name):
return type + '_' + name
#private
class UtcTzinfo(datetime.tzinfo):
def utcoffset(self, dt): return datetime.timedelta(0)
def dst(self, dt): return datetime.timedelta(0)
def tzname(self, dt): return 'UTC'
def olsen_name(self): return 'UTC'
TZINFOS = {
'utc': UtcTzinfo()
}
#private
def adds(num):
try:
float(num)
if num >= 2 or num == 0:
return "s "
else:
return " "
except:
return " "
def oneDayOrDate(the_date):
if the_date is None:
return ''
if the_date == 0:
return ''
try:
utc = TZINFOS['utc']
fileDate = the_date.replace(tzinfo = utc)
today = datetime.datetime.today().replace(tzinfo = utc)
# see if 16 hours is a better way to display the list
one_day = datetime.timedelta(hours=16)
timezone = datetime.timedelta(0)
# adjust timezone timedelta here
#timezone = datetime.timedelta(hours=8)
dayBorder = today - one_day + timezone
today = today + timezone
if fileDate > dayBorder:
return the_date.strftime("%H:%M")
elif fileDate.year < today.year:
return the_date.strftime("%b %d, %Y")
else:
return the_date.strftime("%b %d")
except:
return the_date
def dateFromNow(the_date):
if the_date is None:
return ''
try:
if the_date == 0:
return 'Unknown time '
utc = TZINFOS['utc']
loginTime = the_date.replace(tzinfo = utc)
now = datetime.datetime.today().replace(tzinfo = utc)
diff = now - loginTime
# to compare:
diff_sec = (diff.days)*86400 - diff.seconds - diff.microseconds/1000000
diff_days = ((diff.days)*86400 - diff.seconds - diff.microseconds/1000000)/60
if diff.days == 0:
if 0 <= diff.seconds < 60:
return str(diff.seconds) + " second" + adds(diff.seconds)
elif 60 <= diff.seconds < 3600:
if diff.seconds%60 == 0:
return str(diff.seconds/60) + " minute" + adds(diff.seconds/60)
return str(diff.seconds/60) + " minute" + adds(diff.seconds/60) + str(diff.seconds%60) + " sec" + adds(diff.seconds%60)
elif 3600 <= diff.seconds < 86400:
if diff.seconds/60%60 == 0:
return str(diff.seconds/3600) + " hour" + adds(diff.seconds/3600)
return str(diff.seconds/3600) + " hour" + adds(diff.seconds/3600) + str(diff.seconds/60%60) + " minute" + adds(diff.seconds/60%60)
else:
logging.error("Error: Unexpected time value --> "+str(diff))
return the_date
elif diff.days<366:
if diff.seconds/3600 == 0:
return str(abs(diff.days)) + " day" + adds(diff.days)
return str(abs(diff.days)) + " day" + adds(diff.days) + str(diff.seconds/3600) + " hour" + adds(diff.seconds/3600)
else:
return str(diff.days/365) + " year" + adds(diff.days/365)
except:
return the_date
#
def getCommonUiParams(self, *args):
#
user = users.get_current_user()
# params to be returned
username = ''
loggedIn = ''
message = ''
userInfo = ''
userType = ''
if user is not None:
account = findAccount()
loggedIn = 'true'
if account is not None:
if account.name is not None:
accountName = account.name
else:
accountName = ''
if account.description is not None:
accountDescription = account.description
else:
accountDescription = ''
if len(args) > 0 and args[0] is 'edit':
accountDescription = re.sub('[\']', '\\\'', accountDescription)
accountDescription = re.sub('[\"]', '\\\"', accountDescription)
accountDescription = re.sub('[\n\r]', '', accountDescription)
if account.profilePicBlobKey is not None and account.profilePicBlobKey != '':
accountImageLinkUrl = images.get_serving_url(account.profilePicBlobKey, size=None, crop=False, secure_url=True)
accountImageUrl = accountImageLinkUrl + '=s108-c'
else:
accountImageLinkUrl = ''
accountImageUrl = '/image/user.jpg'
if account.isStudent == True:
accountIsStudent = 'true'
else:
accountIsStudent = ''
userInfo = {
'nickname': account.nickname,
'email':account.email,
'id': account.id,
'name': accountName,
'description': accountDescription,
'imageLinkUrl': accountImageLinkUrl,
'imageUrl': accountImageUrl,
'isStudent': accountIsStudent,
'isStudentUntil': account.isStudentUntil,
'itemCount': db.GqlQuery("SELECT __key__ FROM ShopItem WHERE owner = :1 ", account.nickname).count(),
'wishCount': db.GqlQuery("SELECT __key__ FROM WishList WHERE nickname = :1 ", account.nickname).count(),
'unreadCount': db.GqlQuery("SELECT __key__ FROM Message WHERE owner = :1 AND isRead = :2", account.nickname, False).count(),
'requestCount': db.GqlQuery("SELECT __key__ FROM Transaction WHERE owner = :1 AND status = :2", account.nickname, 'Pending').count(),
}
userType = 'user'
else:
accountImageLinkUrl = ''
accountImageUrl = '/image/user.jpg'
userInfo = {
'nickname': user.nickname(),
'email':user.email(),
'id': user.user_id(),
'name': '',
'description': '',
'imageLinkUrl': accountImageLinkUrl,
'imageUrl': accountImageUrl,
'isStudent': '',
'isStudentUntil': '',
'itemCount': 0,
'wishCount': 0,
'unreadCount': 0,
'requestCount': 0,
}
userType = 'visitor'
if db.GqlQuery("SELECT nickname FROM Account WHERE level = 'admin' and id = :1", user.user_id()).count() == 1 or users.is_current_user_admin():
userType = 'admin'
param = {
#
'userType': userType,
'loggedIn': loggedIn,
'message': message,
'osInfo': {
'application_id': re.sub('s~', '', os.environ.get('APPLICATION_ID', 'hardcode-appdaptor')),
'version': os.environ.get('CURRENT_VERSION_ID', 'unknown'),
'user_agent': os.environ.get('HTTP_USER_AGENT', 'unknown'),
'http_host': os.environ.get('HTTP_HOST', 'unknown'),
'query_string': os.environ.get('QUERY_STRING', ''),
},
'userInfo': userInfo,
}
param['url'] = {
'signin': users.create_login_url(self.request.uri.replace('?logout', '')),
'signout': users.create_logout_url('http://'+ param['osInfo']['http_host'] +'/?logout'),
'current': self.request.uri,
}
if len(args) > 0:
param['optionalInfo'] = args[0]
if len(args) > 1:
param['optionalInfo2'] = args[1]
if param['osInfo']['query_string'] == "logout" :
message = "<div class='notice'><div>You have successfully signed out. <a href='" + \
users.create_login_url(self.request.uri.replace('?logout', '')+'') + \
"'>Sign in again</a> </div></div>\n\n"
param['message'] = message
elif param['osInfo']['query_string'] == "profileUpdated" :
message = "<div class='notice'><div>Your profile is successfully updated.</div></div>\n\n"
param['message'] = message
elif param['osInfo']['query_string'] == "profileSetup" :
message = "<div class='notice'><div>Welcome, "+ userInfo['nickname'] +"! Your account is created.</div></div>\n\n"
param['message'] = message
elif self.request.get('itemUpdated', '') == "true":
message = "<div class='notice'><div>The item is successfully updated.</div></div>\n\n"
param['message'] = message
elif self.request.get('itemPublished', '') == "true" :
message = "<div class='notice'><div>The item is published. <span style='display:inline-block;margin-left:20px;'><a href='/user/addItem'>Add another item</a></span></div></div>\n\n"
param['message'] = message
elif self.request.get('itemDeleted', '') == "true" :
message = "<div class='notice'><div>Item was deleted.</div></div>\n\n"
param['message'] = message
elif self.request.get('accountDeleted', '') == "true" :
message = "<div class='notice'><div>We are sorry to see you going. Your account has been removed.</div></div>\n\n"
param['message'] = message
elif self.request.get('allItemsDeleted', '') == "true" :
message = "<div class='notice'><div>All of your items have been removed successfully.</div></div>\n\n"
param['message'] = message
if self.request.get('invalidImageFormat', '') == "true" :
message = "<div class='notice'><div>Error occur when saving image. Please select a valid image file to upload.</div></div><div class='notice'><div>Other changes have been saved successfully. </div></div>\n\n"
param['message'] = message
return param
#
def getSearchQuery(self):
type = self.request.get("type", 'allItem')
keyword = self.request.get("query", '')
sortBy = self.request.get("sortBy", '')
display = self.request.get("display", '')
priceMin = self.request.get("priceMin", 0)
priceMax = self.request.get("priceMax", 0)
searchQuery = {
'type': type,
'keyword': keyword,
'sortBy': sortBy,
'display': display,
'priceMin': priceMin, #TODO
'priceMax': priceMax, #TODO
}
return searchQuery
#
def getUserInfo(nickname):
account = findAccountByNickname(nickname)
if account is None:
return None
if account.profilePicBlobKey is not None and account.profilePicBlobKey != '':
accountImageLinkUrl = images.get_serving_url(account.profilePicBlobKey, size=None, crop=False, secure_url=True)
accountImageUrl = accountImageLinkUrl + '=s108-c'
else:
accountImageLinkUrl = ''
accountImageUrl = '/image/user.jpg'
if account.isStudent == True:
accountIsStudent = 'true'
else:
accountIsStudent = ''
userInfo = {
'nickname': account.nickname,
'description': account.description,
#'name': account.name,
#'email':account.email,
'status': account.status,
'imageLinkUrl': accountImageLinkUrl,
'imageUrl': accountImageUrl,
'isStudent': accountIsStudent,
'isStudentUntil': account.isStudentUntil,
'itemCount': db.GqlQuery("SELECT __key__ FROM ShopItem WHERE owner = :1", nickname).count(),
'avgRating': 0,
'totalRatingCount': 0,
'action': 'view',
}
return userInfo
#
def findAccount():
user = users.get_current_user()
if user is None:
return None
accountQuery = schema.Account.all()
accountQuery.filter("id = ", user.user_id()) # for google account login only
account = accountQuery.get()
return account
#
def findAccountByNickname(nickname):
accountQuery = schema.Account.all()
accountQuery.filter("nickname = ", nickname)
account = accountQuery.get()
return account
#
def prepareShopItemData(self, *args):
itemId = ''
action = ''
if len(args) > 0:
itemId = args[0]
if len(args) > 1:
action = args[1]
item = schema.ShopItem.get_by_id(int(itemId))
itemTitle = itemDescription = itemStatus = itemPrivacy = \
itemCreationDate = itemExpiryDate = itemOwner = itemImageLinkUrl = ''
itemImageUrl = '/image/noimage.png'
itemPicBlobKeys = itemVideoBlobKeys = []
itemMarkedPrice = itemDiscountPrice = itemQuantity = itemExpireIn = itemViewCount = 0
if item.title is not None:
itemTitleReadOnly = item.title
itemTitle = re.sub('[\']', '\\\'', item.title)
if item.description is not None:
itemDescription = re.sub('[\n\r]', '', item.description)
if item.profilePicBlobKey is not None and item.profilePicBlobKey != '':
itemImageLinkUrl = images.get_serving_url(item.profilePicBlobKey, size=None, crop=False, secure_url=True)
itemImageUrl = itemImageLinkUrl + '=s108-c'
if item.picBlobKeys is not None:
itemPicBlobKeys = item.picBlobKeys
if item.videoBlobKeys is not None:
itemVideoBlobKeys = item.videoBlobKeys
if item.markedPrice is not None:
itemMarkedPrice = item.markedPrice/100.0
if item.discountPrice is not None:
itemDiscountPrice = item.discountPrice/100.0
if item.quantity is not None:
itemQuantity = item.quantity
if item.status is not None:
itemStatus = item.status
if item.privacy is not None:
itemPrivacy = item.privacy
if item.creationDate is not None:
itemCreationDate = item.creationDate
if item.expiryDate is not None:
itemExpiryDate = item.expiryDate
if item.expireIn is not None:
itemExpireIn = item.expireIn
if item.owner is not None:
itemOwner = item.owner
if item.viewCount is not None:
itemViewCount = item.viewCount
if action is 'edit':
itemDescription = re.sub('[\']', '\\\'', itemDescription)
itemDescription = re.sub('[\"]', '\\\"', itemDescription)
itemInfo = {
'id': itemId,
'title': itemTitle,
'titleReadOnly': itemTitleReadOnly,
'description': itemDescription,
'imageUrl': itemImageUrl,
'imageLinkUrl': itemImageLinkUrl,
'picBlobKeys': itemPicBlobKeys,
'videoBlobKeys': itemVideoBlobKeys,
'markedPrice': itemMarkedPrice,
'discountPrice': itemDiscountPrice,
'quantity': itemQuantity,
'status': itemStatus,
'privacy': itemPrivacy,
'creationDate': itemCreationDate,
'expiryDate': itemExpiryDate,
'expireIn': itemExpireIn,
'owner': itemOwner,
'viewCount': itemViewCount,
'action': action
}
return itemInfo
def findItemById(itemId):
if not itemId.isdigit() or int(itemId) <1:
return None
return schema.ShopItem.get_by_id(int(itemId))