-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserHome.py
1326 lines (1022 loc) · 44.6 KB
/
userHome.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# users need to be logged in (Google users) when arriving methods in this file
import datetime
import re
import os
import webapp2
import logging
#---------------- Import from Google ------------------
#
from google.appengine.api import urlfetch
from google.appengine.api import users, images, memcache
from google.appengine.ext import blobstore, db
from google.appengine.ext.webapp import blobstore_handlers, template
#
#------------------------------------------------------
#----------------- Custom Classes & -------------------
#----------------- Datastore Schema -------------------
#
from commonFunction import createEventLog
from commonFunction import findAccount
from commonFunction import findAccountByNickname
from commonFunction import findItemById
from commonFunction import getCommonUiParams
from commonFunction import getUserInfo
from commonFunction import dateFromNow
from commonFunction import oneDayOrDate
from commonFunction import prepareShopItemData
from commonFunction import returnJsonResult
from commonFunction import returnXmlResult
from commonFunction import retrieveTargetApplicationList
from commonFunction import retrieveApplicationSettings, retrieveRestrictedNicknameList
from decorator import AccessControlForMessage
from decorator import AccessControlForShopItem
from decorator import AccessControlForTransaction
from decorator import UserNicknameRequired
import schema
#
#------------------------------------------------------
_settings = {
'NICKNAME_MIN_LENGTH': retrieveApplicationSettings('env', 'nickname_min_chars'),
'NICKNAME_MAX_LENGTH': retrieveApplicationSettings('env', 'nickname_max_chars'),
'ITEM_MIN_QUANTITY': retrieveApplicationSettings('env', 'item_min_quantity'),
'ITEM_MAX_QUANTITY': retrieveApplicationSettings('env', 'item_max_quantity'),
'ITEM_MIN_PRICE': retrieveApplicationSettings('env', 'item_min_price')*100,
'ITEM_MAX_PRICE': retrieveApplicationSettings('env', 'item_max_price')*100,
'ITEM_MIN_ACTIVEDAYS': retrieveApplicationSettings('env', 'item_min_activeDays'),
'ITEM_MAX_ACTIVEDAYS': retrieveApplicationSettings('env', 'item_max_activeDays')
}
class AddItemHandler(webapp2.RequestHandler):
@UserNicknameRequired
def get(self):
self.response.headers['Content-Type'] = "text/html"
path = os.path.join(os.path.dirname(__file__), "template/itemEdit.html")
self.response.out.write(template.render(path, getCommonUiParams(self, _settings)))
@UserNicknameRequired
def post(self):
self.response.headers['Content-Type'] = "text/xml"
user = users.get_current_user()
account = findAccount()
#TODO run in transaction
itemTitle = itemDescription = itemPrivacy = ''
itemPrice = itemQuantity = itemExpireIn = 0
if self.request.get('title', '') is not '':
itemTitle = self.request.get('title')
if self.request.get('description', '') is not '':
itemDescription = self.request.get('description')
if self.request.get('price', '') is not '':
itemPrice = int(round(float(self.request.get('price'))))
if not _settings['ITEM_MIN_PRICE'] <= itemPrice <= _settings['ITEM_MAX_PRICE']:
returnXmlResult(self, 'false', 'Price out of range')
return
if self.request.get('quantity', '') is not '':
itemQuantity = int(self.request.get('quantity'))
if not _settings['ITEM_MIN_QUANTITY'] <= itemQuantity <= _settings['ITEM_MAX_QUANTITY']:
returnXmlResult(self, 'false', 'Quantity out of range')
return
if self.request.get('privacy', '') is not '':
itemPrivacy = self.request.get('privacy')
if self.request.get('expireIn', '') is not '':
itemExpireIn = int(self.request.get('expireIn'))
if not _settings['ITEM_MIN_ACTIVEDAYS'] <= itemExpireIn <= _settings['ITEM_MAX_ACTIVEDAYS']:
returnXmlResult(self, 'false', 'Number of active days out of range')
return
item = schema.ShopItem(
title = itemTitle,
description = itemDescription,
profilePicBlobKey = '',
picBlobKeys = [],
videoBlobKeys = [],
markedPrice = itemPrice,
discountPrice = None,
quantity = itemQuantity,
status = 'Draft',
privacy = itemPrivacy,
creationDate = None,
expiryDate = None,
expireIn = itemExpireIn,
owner = account.nickname,
viewCount = 0
)
item.put()
createEventLog(account.nickname, 'CREATE_ITEM', 'itemId = '+str(item.key().id()), '')
# breakdown title to each individual KeywordRepo
titleInputList = re.sub(r'[^ a-zA-Z0-9]', '', item.title.lower()).split()
for i in titleInputList:
input = schema.KeywordRepo(
shopItemId = str(item.key().id()),
keyword = i,
weight = 3
)
input.put()
# add description
# breakdown description to each individual KeywordRepo
parsedDescription = item.description.replace(' ',' ').lower()
parsedDescription = re.sub('<[^<]+?>', '', parsedDescription)
parsedDescription = re.sub(r'[^ a-zA-Z0-9]', '', parsedDescription )
descriptionInputList = parsedDescription.split()
for i in descriptionInputList:
input = schema.KeywordRepo(
shopItemId = str(item.key().id()),
keyword = i,
weight = 1
)
input.put()
returnXmlResult(self, 'true', str(item.key().id())+','+blobstore.create_upload_url('/user/uploadItemPhoto') )
#
class UploadItemPhotoHandler(blobstore_handlers.BlobstoreUploadHandler):
@UserNicknameRequired
@AccessControlForShopItem
def post(self):
itemId = self.request.get('itemId', '-1')
account = findAccount()
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
item = schema.ShopItem.get_by_id(int(itemId))
# item is not found
if item is None:
logging.info("item is not found")
self.redirect( "/item/" + itemId )
return
actionType = self.request.get('actionType', '')
if len(upload_files)<1:
logging.info("No files selected")
if actionType == 'addItem':
url = "/user/previewItem?itemId=" + itemId + "&noImageSelected=true"
else:
url = "/item/" + itemId + "?itemUpdated=true&noImageSelected=true"
self.redirect( url )
return
# security issue: only images allowed
# we only allow certain mime types (mainly for .jpg, .png and .gif images) here
for blob_info in upload_files:
content_type = blob_info.content_type
if content_type != 'image/jpeg' and content_type != 'image/png' and content_type != 'image/gif':
logging.info("Incorrect file format")
if actionType == 'addItem':
url = "/user/previewItem?itemId=" + itemId + "&invalidImageFormat=true"
else:
url = "/item/" + itemId + "?itemUpdated=true&invalidImageFormat=true"
self.redirect( url )
return
if item.profilePicBlobKey is not None and item.profilePicBlobKey != '':
#remove the original file
images.delete_serving_url(item.profilePicBlobKey)
blobstore.delete(item.profilePicBlobKey)
logging.info("User "+account.nickname+" deleted profile image for itemId = "+itemId+" KEY = "+item.profilePicBlobKey)
for blob_info in upload_files:
logging.info("***** User '"+ account.nickname +"' uploaded a file: '"+ blob_info.filename +"', type = '" +blob_info.content_type+ "' ")
item.profilePicBlobKey = str(blob_info.key())
logging.info("User "+account.nickname+" uploaded image for itemId = "+itemId+", KEY = "+item.profilePicBlobKey)
item.put()
if actionType == 'addItem':
url = "/user/previewItem?itemId=" + itemId
else:
url = "/item/" + itemId + "?itemUpdated=true"
self.redirect( url )
#
class PreviewItemHandler(webapp2.RequestHandler):
@UserNicknameRequired
@AccessControlForShopItem
def get(self):
self.response.headers['Content-Type'] = "text/html"
itemId = self.request.get('itemId', '-1')
item = schema.ShopItem.get_by_id(int(itemId))
if item.status == "Active":
self.redirect("/item/"+itemId)
path = os.path.join(os.path.dirname(__file__), "template/item.html")
self.response.out.write(template.render(path, getCommonUiParams(self, prepareShopItemData(self, itemId, 'preview') )))
#
class EditItemHandler(webapp2.RequestHandler):
@UserNicknameRequired
@AccessControlForShopItem
def get(self):
self.response.headers['Content-Type'] = "text/html"
itemId = self.request.get('itemId', '-1')
path = os.path.join(os.path.dirname(__file__), "template/itemEdit.html")
self.response.out.write(template.render(path, getCommonUiParams(self, _settings, prepareShopItemData(self, itemId, 'edit') )))
@UserNicknameRequired
@AccessControlForShopItem
def post(self):
self.response.headers['Content-Type'] = "text/xml"
if int(round(float(self.request.get('price', 0)))) <= 0 or int(round(float(self.request.get('quantity', 0)))) <= 0 \
or ( self.request.get('expireIn') is not None and int(self.request.get('expireIn', 1)) <= 0):
returnXmlResult(self, 'false', 'Input value cannot be null or negative')
return
account = findAccount()
#TODO run in transaction
itemId = self.request.get('itemId', '-1')
item = schema.ShopItem.get_by_id(int(itemId))
item.title = self.request.get('title')
item.description = self.request.get('description')
itemPrice = int(round(float(self.request.get('price', 0))))
if not _settings['ITEM_MIN_PRICE'] <= itemPrice <= _settings['ITEM_MAX_PRICE']:
returnXmlResult(self, 'false', 'Price out of range')
return
item.markedPrice = itemPrice
#if self.request.get('expiryDate', '') is not '':
# item.expiryDate = self.request.get('expiryDate', '')
itemExpireIn = int(round(float(self.request.get('expireIn', -1))))
if not itemExpireIn <= _settings['ITEM_MAX_ACTIVEDAYS']:
returnXmlResult(self, 'false', 'Number of active days out of range')
return
item.expireIn = int(self.request.get('expireIn', -1))
itemQuantity = int(round(float(self.request.get('quantity', 0))))
if not _settings['ITEM_MIN_QUANTITY'] <= itemQuantity <= _settings['ITEM_MAX_QUANTITY']:
returnXmlResult(self, 'false', 'Quantity out of range')
return
item.quantity = itemQuantity
item.put()
# KeywordRepo for title (weight = 3)
# DELETE existing records
keywordRepoQuery = schema.KeywordRepo.all().filter("shopItemId = ", itemId).filter("weight = ", 3)
keywordRepoList = keywordRepoQuery.fetch(999999)
for k in keywordRepoList:
k.delete()
# add title
# breakdown title to each individual KeywordRepo
# The str.split() method without an argument splits on whitespace
# http://stackoverflow.com/questions/8113782/split-string-on-whitespace-in-python
titleInputList = re.sub(r'[^ a-zA-Z0-9]', '', item.title.lower()).split()
for i in titleInputList:
input = schema.KeywordRepo(
shopItemId = itemId,
keyword = i,
weight = 3
)
input.put()
# DELETE existing records
keywordRepoQuery = schema.KeywordRepo.all().filter("shopItemId = ", itemId).filter("weight = ", 1)
keywordRepoList = keywordRepoQuery.fetch(999999)
for k in keywordRepoList:
k.delete()
# add description
# breakdown description to each individual KeywordRepo
parsedDescription = item.description.replace(' ',' ').lower()
parsedDescription = re.sub('<[^<]+?>', '', parsedDescription)
parsedDescription = re.sub(r'[^ a-zA-Z0-9]', '', parsedDescription )
descriptionInputList = parsedDescription.split()
for i in descriptionInputList:
input = schema.KeywordRepo(
shopItemId = itemId,
keyword = i,
weight = 1
)
input.put()
createEventLog(account.nickname, 'UPDATE_ITEM', 'itemId = '+itemId, '')
returnXmlResult(self, 'true', itemId+','+blobstore.create_upload_url('/user/uploadItemPhoto'))
return
#
class PublishItemHandler(webapp2.RequestHandler):
@UserNicknameRequired
@AccessControlForShopItem
def get(self):
self.redirect('/')
@UserNicknameRequired
@AccessControlForShopItem
def post(self):
self.response.headers['Content-Type'] = "text/xml"
account = findAccount()
#TODO run in transaction
itemId = self.request.get('itemId', '-1')
item = schema.ShopItem.get_by_id(int(itemId))
item.status = 'Active'
item.creationDate = datetime.datetime.now()
item.expiryDate = datetime.datetime.now()+datetime.timedelta(days=item.expireIn)
item.expireIn = -1
item.viewCount = 0
item.put()
createEventLog(account.nickname, 'PUBLISH_ITEM', 'itemId = '+str(item.key().id()), '')
# HARDCODE FINAL
# notify partner applications
# Only applicable for production environment
# skipped in localhost
if "localhost" not in os.environ.get('HTTP_HOST'):
itemTitle = re.sub('[\']', '\\\'', item.title)
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'
else:
itemImageUrl = ""
itemUrl = "https://" + re.sub('s~', '', os.environ.get('APPLICATION_ID', 'hardcode-appdaptor')) + ".appspot.com/item/" + str(item.key().id())
itemMarkedPrice = "0"
if item.markedPrice is not None:
itemMarkedPrice = str(item.markedPrice/100.0)
parsedDescription = item.description.replace(' ',' ').lower()
parsedDescription = re.sub('<[^<]+?>', '', parsedDescription)
parsedDescription = re.sub(r'[^ a-zA-Z0-9]', '', parsedDescription )
applicationRecipientList = retrieveTargetApplicationList()
for app in applicationRecipientList:
dataToSend = """\"auth_token\": "%s",
"data": [{
"id": "%s",
"title": "%s",
"description": "%s",
"seller": {
"id": "%s",
"username": "%s"
},
"image": "%s",
"price": "%s",
"url": "%s"
}]""" % (app['auth_token'], str(item.key().id()), itemTitle, parsedDescription, item.owner, item.owner, itemImageUrl, "$ "+ itemMarkedPrice, itemUrl)
logging.info(dataToSend)
logging.info(app['domain'])
logging.info(app['auth_token'])
try:
#form_fields = {
# "auth_token": app['auth_token'],
# "last_name": "Johnson",
# "email_address": "[email protected]"
#}
#form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(url=app['domain'] + "webservices/new_item",
payload=dataToSend,
method=urlfetch.POST,
headers={'Content-Type': 'application/x-www-form-urlencoded'},
validate_certificate=False)
logging.info(app['domain'] + "webservices/new_item")
except:
logging.error("Send item create notification error: " + app['domain'] + "webservices/new_item")
#
else:
logging.debug("urlfetch skipped for local environment")
returnXmlResult(self, 'true')
return
#
class UserSettingsHandler(webapp2.RequestHandler):
@UserNicknameRequired
def get(self):
self.response.headers['Content-Type'] = "text/html"
path = os.path.join(os.path.dirname(__file__), "template/accountSettings.html")
self.response.out.write(template.render(path, getCommonUiParams(self)))
#
class DeleteAllMyItemHandler(webapp2.RequestHandler):
@UserNicknameRequired
def post(self):
account = findAccount()
#TODO run in transaction
itemQuery = schema.ShopItem.all().filter("owner = ", account.nickname)
items = itemQuery.fetch(999999)
for item in items:
# Remove associated photos/ videos
if item.profilePicBlobKey != '':
images.delete_serving_url(item.profilePicBlobKey)
blobstore.delete(item.profilePicBlobKey)
# delete record in KeywordRepo
keywordRepoQuery = schema.KeywordRepo.all().filter("shopItemId = ", str(item.key().id()))
keywordRepoList = keywordRepoQuery.fetch(999999)
for k in keywordRepoList:
k.delete()
item.delete()
createEventLog(account.nickname, 'ACCOUNT_ITEM_CLEAR', 'accountId = '+str(account.key().id()), '')
self.response.headers['Content-Type'] = "text/xml"
returnXmlResult(self, 'true')
#
class DeleteMyAccountHandler(webapp2.RequestHandler):
@UserNicknameRequired
def post(self):
account = findAccount()
# delete all ShopItem.profileImg
# delete all ShopItem
itemQuery = schema.ShopItem.all().filter("owner = ", account.nickname)
items = itemQuery.fetch(999999)
for item in items:
# Remove associated photos/ videos
if item.profilePicBlobKey != '':
images.delete_serving_url(item.profilePicBlobKey)
blobstore.delete(item.profilePicBlobKey)
# delete record in KeywordRepo
keywordRepoQuery = schema.KeywordRepo.all().filter("shopItemId = ", str(item.key().id()))
keywordRepoList = keywordRepoQuery.fetch(999999)
for k in keywordRepoList:
k.delete()
item.delete()
#TODO? delete conversationAssignment
#TODO? delete messages
#TODO? delete wishlist
#TODO? delete request items
#TODO? delete user profile image
#TODO? clear user settings
# Finally
account.status = "Deleted"
account.put()
createEventLog(account.nickname, 'ACCOUNT_DELETE', 'accountId = '+str(account.key().id()), '')
self.response.headers['Content-Type'] = "text/xml"
returnXmlResult(self, 'true')
#
class UserNicknameSettingsHandler(webapp2.RequestHandler):
def get(self):
# if username has been set, redirect to user profile
if db.GqlQuery("SELECT __key__ FROM Account WHERE id = :1", users.get_current_user().user_id()).count() == 1:
self.redirect('/user/myProfile')
self.response.headers['Content-Type'] = "text/html"
path = os.path.join(os.path.dirname(__file__), "template/editNicknameFirstTime.html")
self.response.out.write(template.render(path, getCommonUiParams(self)))
#
def post(self):
self.response.headers['Content-Type'] = "text/xml"
if len(self.request.get('nickname', '')) < _settings['NICKNAME_MIN_LENGTH']:
returnXmlResult(self, 'false', 'This nickname is too short.')
return
if len(self.request.get('nickname')) > _settings['NICKNAME_MAX_LENGTH']:
returnXmlResult(self, 'false', 'This nickname is too long.')
return
if db.GqlQuery("SELECT __key__ FROM Account WHERE id = :1", users.get_current_user().user_id()).count() == 1:
returnXmlResult(self, 'false', 'Your account nickname has been set already.')
return
if db.GqlQuery("SELECT __key__ FROM Account WHERE nickname = :1", self.request.get('nickname')).count() == 1:
returnXmlResult(self, 'false', '"'+self.request.get('nickname', '')+'" is not available.')
return
if bool(re.compile(r'[^a-zA-Z0-9]').search(self.request.get('nickname', ''))):
returnXmlResult(self, 'false', 'Only letters and numbers are allowed.')
return
# check for restricted account nickname
matched = 0
# prepare restricted nickname from cache then datastore
_RESTRICTED_ACCOUNT_NICKNAME = memcache.get('restricted-account-nickname-list')
if _RESTRICTED_ACCOUNT_NICKNAME is not None:
logging.debug('cache hit: _RESTRICTED_ACCOUNT_NICKNAME = ' + str(_RESTRICTED_ACCOUNT_NICKNAME))
else:
_RESTRICTED_ACCOUNT_NICKNAME = retrieveRestrictedNicknameList()
nicknameQuery = schema.ApplicationSettings.all().filter('type = ', 'nickname')
nicknameList = nicknameQuery.fetch(999999)
for name in nicknameList:
_RESTRICTED_ACCOUNT_NICKNAME.append(name.value)
memcache.set('restricted-account-nickname-list', value= _RESTRICTED_ACCOUNT_NICKNAME, time=3600)
logging.debug('Put into cache: _RESTRICTED_ACCOUNT_NICKNAME = ' + str(_RESTRICTED_ACCOUNT_NICKNAME))
# compare user input nickname
for name in _RESTRICTED_ACCOUNT_NICKNAME:
if name == self.request.get('nickname').lower():
matched += 1
logging.info('name = '+ name + ' is restricted from registration')
if matched != 0:
returnXmlResult(self, 'false', 'You cannot use "'+self.request.get('nickname', '')+'" as nickname.')
return
# check for matched account names, for case-insensitive purpose
accountQuery = schema.Account.all()
matched = 0
for account in accountQuery.fetch(999999):
if account.nickname.lower() == self.request.get('nickname').lower():
matched += 1
if matched != 0:
returnXmlResult(self, 'false', '"'+self.request.get('nickname', '')+'" is not available.')
return
# save to datastore
user = users.get_current_user()
if self.request.get('nickname', '') is not '' and user is not None:
# TODO run in transaction for saving
account = schema.Account(
id = user.user_id(),
googleNickname = user.nickname(),
nickname = self.request.get('nickname'),
name = '',
description = '',
email = user.email(),
profilePicBlobKey = '',
level = 'user',
status = 'Active',
accountSince = datetime.datetime.now(),
authType = 'goog'
)
account.put()
createEventLog(account.nickname, 'ACCOUNT_CREATE', 'accountId = '+str(account.key().id()), '')
returnXmlResult(self, 'true', 'Account created')
return
returnXmlResult(self, 'false', 'You are not logged in')
#
class UserNicknameAvailabilityChecker(webapp2.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = "text/xml"
if len(self.request.get('nickname', '')) < _settings['NICKNAME_MIN_LENGTH']:
returnXmlResult(self, 'false', 'This nickname is too short.')
return
if len(self.request.get('nickname', '')) > _settings['NICKNAME_MAX_LENGTH']:
returnXmlResult(self, 'false', 'This nickname is too long.')
return
if db.GqlQuery("SELECT __key__ FROM Account WHERE id = :1", users.get_current_user().user_id()).count() == 1:
returnXmlResult(self, 'false', 'Your account nickname has been set already.')
return
logging.info("Checking availability of nickname '"+ self.request.get('nickname') +"'")
if db.GqlQuery("SELECT __key__ FROM Account WHERE nickname = :1", self.request.get('nickname')).count() == 1:
returnXmlResult(self, 'false', '"'+self.request.get('nickname')+'" is not available.')
return
if bool(re.compile(r'[^a-zA-Z0-9]').search(self.request.get('nickname'))):
returnXmlResult(self, 'false', 'Only letters and numbers are allowed.')
return
# check for restricted account nickname
matched = 0
# prepare restricted nickname from cache then datastore
_RESTRICTED_ACCOUNT_NICKNAME = memcache.get('restricted-account-nickname-list')
if _RESTRICTED_ACCOUNT_NICKNAME is not None:
logging.debug('cache hit: _RESTRICTED_ACCOUNT_NICKNAME = ' + str(_RESTRICTED_ACCOUNT_NICKNAME))
else:
_RESTRICTED_ACCOUNT_NICKNAME = retrieveRestrictedNicknameList()
nicknameQuery = schema.ApplicationSettings.all().filter('type = ', 'nickname')
nicknameList = nicknameQuery.fetch(999999)
for name in nicknameList:
_RESTRICTED_ACCOUNT_NICKNAME.append(name.value.encode('utf-8'))
memcache.set('restricted-account-nickname-list', value= _RESTRICTED_ACCOUNT_NICKNAME, time=3600)
logging.debug('Put into cache: _RESTRICTED_ACCOUNT_NICKNAME = ' + str(_RESTRICTED_ACCOUNT_NICKNAME))
# compare user input nickname
for name in _RESTRICTED_ACCOUNT_NICKNAME:
if name == self.request.get('nickname').lower():
matched += 1
logging.info('name = '+ name + ' is restricted from registration')
if matched != 0:
returnXmlResult(self, 'false', 'You cannot use "'+self.request.get('nickname', '')+'" as nickname.')
return
# check for matched account names, for case-insensitive purpose
accountQuery = schema.Account.all()
matched = 0
for account in accountQuery.fetch(999999):
if account.nickname.lower() == self.request.get('nickname').lower():
matched += 1
if matched != 0:
returnXmlResult(self, 'false', '"'+self.request.get('nickname', '')+'" is not available.')
return
returnXmlResult(self, 'true')
#
class MyProfileViewHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user is not None:
self.response.headers['Content-Type'] = "text/html"
path = os.path.join(os.path.dirname(__file__), "template/profile.html")
self.response.out.write(template.render(path, getCommonUiParams(self)))
else:
self.redirect('/')
#
class UserProfileEditHandler(webapp2.RequestHandler):
@UserNicknameRequired
def get(self):
self.response.headers['Content-Type'] = "text/html"
path = os.path.join(os.path.dirname(__file__), "template/profileEdit.html")
self.response.out.write(template.render(path, getCommonUiParams(self, 'edit')))
@UserNicknameRequired
def post(self):
self.response.headers['Content-Type'] = "text/xml"
user = users.get_current_user()
#TODO run in transaction
account = findAccount()
if self.request.get('name', '') is not '':
account.name = self.request.get('name')
if self.request.get('description', '') is not '':
account.description = self.request.get('description')
account.put()
createEventLog(account.nickname, 'ACCOUNT_PROFILE_CHANGE', 'accountId = '+str(account.key().id()), '')
returnXmlResult(self, 'true', blobstore.create_upload_url('/user/uploadUserProfilePhoto'))
#
class UploadUserProfilePhotoHandler(blobstore_handlers.BlobstoreUploadHandler):
@UserNicknameRequired
def post(self):
account = findAccount()
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
if len(upload_files)<1:
self.redirect( "/user/myProfile?noImageSelected=true" )
return
# security issue: only images allowed
# we only allow certain mime types (mainly for .jpg, .png and .gif images) here
for blob_info in upload_files:
content_type = blob_info.content_type
if content_type != 'image/jpeg' and content_type != 'image/png' and content_type != 'image/gif':
self.redirect( "/user/myProfile?invalidImageFormat=true" )
return
if account.profilePicBlobKey is not None and account.profilePicBlobKey != '':
#remove the original file
images.delete_serving_url(account.profilePicBlobKey)
blobstore.delete(account.profilePicBlobKey)
logging.info("User "+account.nickname+" deleted his/her profile image, KEY = "+account.profilePicBlobKey)
for blob_info in upload_files:
logging.info("***** User '"+ account.nickname +"' uploaded a file: '"+ blob_info.filename +"', type = '" +blob_info.content_type+ "' ")
account.profilePicBlobKey = str(blob_info.key())
logging.info("User "+account.nickname+" uploaded his/her profile image, new KEY = "+account.profilePicBlobKey)
account.put()
url = "/user/myProfile?profileUpdated"
self.redirect( url )
#
class MyItemHandler(webapp2.RequestHandler):
@UserNicknameRequired
def get(self):
self.redirect('/search/?type=myItem')
#
class DeleteItemHandler(webapp2.RequestHandler):
@UserNicknameRequired
@AccessControlForShopItem
def post(self):
self.response.headers['Content-Type'] = "text/xml"
account = findAccount()
#TODO run in transaction
itemId = self.request.get('itemId', '-1')
item = schema.ShopItem.get_by_id(int(itemId))
if item is not None:
# Remove associated photos/ videos
if item.profilePicBlobKey != '':
images.delete_serving_url(item.profilePicBlobKey)
blobstore.delete(item.profilePicBlobKey)
#TODO? update associated message conversations
# delete record in KeywordRepo
keywordRepoQuery = schema.KeywordRepo.all().filter("shopItemId = ", itemId)
keywordRepoList = keywordRepoQuery.fetch(999999)
for k in keywordRepoList:
k.delete()
item.delete()
# Event Log
createEventLog(account.nickname, 'DELETE_ITEM', 'itemId = '+str(item.key().id()), '')
returnXmlResult(self, 'true')
return
#
class ConfirmDeleteItemHandler(webapp2.RequestHandler):
@UserNicknameRequired
@AccessControlForShopItem
def get(self):
itemId = self.request.get('itemId', '-1')
item = schema.ShopItem.get_by_id(int(itemId))
if item is None:
self.response.out.write("invalid itemId")
return
self.response.headers['Content-Type'] = "text/html"
path = os.path.join(os.path.dirname(__file__), "template/item.html")
self.response.out.write(template.render(path, getCommonUiParams(self, prepareShopItemData(self, itemId, 'confirmDelete') )))
#
class MyWishlistHandler(webapp2.RequestHandler):
@UserNicknameRequired
def get(self):
self.redirect('/search/?type=myWishlist')
#
class AddToWishlistHandler(webapp2.RequestHandler):
@UserNicknameRequired
def post(self):
self.response.headers['Content-Type'] = "text/xml"
itemId = self.request.get('itemId', '-1')
item = schema.ShopItem.get_by_id(int(itemId))
if item is None:
returnXmlResult(self, 'false', "invalid itemId")
return
account = findAccount()
# check for existence
if db.GqlQuery("SELECT __key__ FROM WishList WHERE nickname = :1 AND shopItemId = :2", account.nickname, itemId).count() == 1:
returnXmlResult(self, 'true', "Item already in wishlist")
return
wishlistItem = schema.WishList(
nickname = account.nickname,
shopItemId = itemId
)
wishlistItem.put()
returnXmlResult(self, 'true')
return
#
class RemoveFromWishlistHandler(webapp2.RequestHandler):
@UserNicknameRequired
def post(self):
self.response.headers['Content-Type'] = "text/xml"
itemId = self.request.get('itemId', '-1')
item = schema.ShopItem.get_by_id(int(itemId))
if item is None:
returnXmlResult(self, 'false', "invalid itemId")
return
account = findAccount()
wishlistItemQuery = schema.WishList.all().filter("nickname = ", account.nickname).filter("shopItemId = ", itemId)
wishlistItem = wishlistItemQuery.fetch(1)
for item in wishlistItem:
item.delete()
returnXmlResult(self, 'true')
return
#
class RequestItemHandler(webapp2.RequestHandler):
@UserNicknameRequired
def post(self):
self.response.headers['Content-Type'] = "application/json;charset=UTF-8"
itemId = self.request.get('itemId', '-1')
item = schema.ShopItem.get_by_id(int(itemId))
if item is None:
returnJsonResult(self, 'false', "invalid itemId")
return
quantity = self.request.get('quantity', '0')
totalPrice = self.request.get('totalPrice', '0')
account = findAccount()
ownerAccount = findAccountByNickname(item.owner)
try:
val = int(quantity)
except ValueError:
logging.info("quantity = " + str(quantity) + " is not an integer")
returnJsonResult(self, 'false', "Invalid quantity value")
return
if item.quantity < int(round(float(quantity))):
logging.info("Error occurred: available amount < requested amount")
returnJsonResult(self, 'false', "There is not enough item available to fulfill your request.")
return
if int(float(totalPrice)*100) != int(quantity)* int(item.markedPrice):
logging.info("Error occurred: total price != counted price")
returnJsonResult(self, 'false', "Item price is updated, please refresh page and try again")
return
transaction = schema.Transaction(
actor = account.nickname,
owner = ownerAccount.nickname,
status = 'Pending',
itemId = itemId,
quantity = int(quantity),
itemTitle = item.title,
itemPrice = item.markedPrice
)
transaction.put()
createEventLog(account.nickname, 'TRANSACTION_CREATE', 'transactionId = '+str(transaction.key().id()), '')
returnJsonResult(self, 'true')
#
class CancelRequestHandler(webapp2.RequestHandler):
@UserNicknameRequired
@AccessControlForTransaction
def post(self):
self.response.headers['Content-Type'] = "application/json;charset=UTF-8"
transactionId = self.request.get('transactionId', '-1')
transaction = schema.Transaction.get_by_id(int(transactionId))
if transaction is None:
returnJsonResult(self, 'false', "invalid transactionId")
return
account = findAccount()
if transaction.actor != account.nickname:
returnJsonResult(self, 'false', "This account cannot access this transaction")
return
transaction.status = "Cancelled"
transaction.endDate = datetime.datetime.now()
transaction.put()
# update conversation as well
# message to owner
recipient = transaction.owner
newConversation = schema.Conversation(
title = 'Item request cancelled by '+transaction.actor,
shopItemId = transaction.itemId,
shopItemPrice = transaction.itemPrice,
shopItemTitle = transaction.itemTitle
)
newConversation.put()
newMessage = schema.Message(
sender = 'System',
recipient = recipient,
content = 'Item request ('+ str(transaction.quantity) +' items) cancelled by '+account.nickname+'.\nClick on "Item Info" for more information.\n\nThis is a system generated message. Please do not reply.',
owner = recipient,
isRead = False,