forked from georgesung/tennis-match
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1212 lines (967 loc) · 37 KB
/
main.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
'''
The API backend
'''
from datetime import datetime
from datetime import timedelta
from eastern_tzinfo import Eastern_tzinfo
import json
import os
from django.utils.http import urlquote
import Crypto.Random
from Crypto.Protocol import KDF
import jwt
import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote
from google.appengine.api import urlfetch
from google.appengine.ext import ndb
from models import Profile
from models import ProfileMsg
from models import AccountAuthMsg
from models import ChangePasswordMsg
from models import Match
from models import MatchMsg
from models import MatchesMsg
from models import AccessTokenMsg
from models import StringMsg
from models import BooleanMsg
from models import StringArrayMsg
# Custom accounts
from settings import CA_SECRET
from settings import EMAIL_VERIF_SECRET
# Facebook
from settings import FB_APP_ID
from settings import FB_APP_SECRET
from settings import FB_API_VERSION
# Google
from settings import GRECAPTCHA_SECRET
#from settings import WEB_CLIENT_ID
# SparkPost
from settings import SPARKPOST_SECRET
EMAIL_SCOPE = endpoints.EMAIL_SCOPE
API_EXPLORER_CLIENT_ID = endpoints.API_EXPLORER_CLIENT_ID
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@endpoints.api( name='tennis',
version='v1',
allowed_client_ids=[API_EXPLORER_CLIENT_ID],
scopes=[EMAIL_SCOPE])
class TennisApi(remote.Service):
"""Tennis API"""
###################################################################
# Email Management
###################################################################
def _postToSparkpost(self, payload):
""" Post to Sparkpost API. Return True/False status """
payload_json = json.dumps(payload)
headers = {
'Authorization': SPARKPOST_SECRET,
'Content-Type': 'application/json',
}
url = 'https://api.sparkpost.com/api/v1/transmissions?num_rcpt_errors=3'
try:
result = urlfetch.Fetch(url, headers=headers, payload=payload_json, method=2)
except:
raise endpoints.BadRequestException('urlfetch error: Unable to POST to SparkPost')
return False
data = json.loads(result.content)
# Determine status from SparkPost, return True/False
if 'errors' in data:
return False
if data['results']['total_accepted_recipients'] != 1:
return False
return True
def _emailVerif(self, profile):
""" Send verification email, given reference to Profile object. Return success True/False. """
# Generate JWT w/ payload of userId and email, secret is EMAIL_VERIF_SECRET
token = jwt.encode(
{'userId': profile.userId, 'contactEmail': profile.contactEmail},
EMAIL_VERIF_SECRET,
algorithm='HS256'
)
# Create SparkPost request to send verification email
payload = {
'recipients': [{
'address': {
'email': profile.contactEmail,
'name': profile.firstName + ' ' + profile.lastName,
},
'substitution_data': {
'first_name': profile.firstName,
'token': token,
},
}],
'content': {
'template_id': 'email-verif',
},
}
return self._postToSparkpost(payload)
def _emailPwChange(self, profile):
""" Send password change notification email. Return success True/False. """
# If user email is unverified, return
if not profile.emailVerified:
return False
# Create SparkPost request to send pw change notification email
payload = {
'recipients': [{
'address': {
'email': profile.contactEmail,
'name': profile.firstName + ' ' + profile.lastName,
},
'substitution_data': {
'first_name': profile.firstName,
},
}],
'content': {
'template_id': 'password-change-notification',
},
}
return self._postToSparkpost(payload)
def _emailPwReset(self, profile):
""" Send password reset link to user's email. Return success True/False. """
# If user email is unverified, return
if not profile.emailVerified:
return False
# Generate JWT to reset password, expires 30 minutes from now
token = jwt.encode({'userId': profile.userId, 'exp': datetime.now() + timedelta(minutes=30)}, CA_SECRET, algorithm='HS256')
# Create SparkPost request to send pw reset email
payload = {
'recipients': [{
'address': {
'email': profile.contactEmail,
'name': profile.firstName + ' ' + profile.lastName,
},
'substitution_data': {
'first_name': profile.firstName,
'token': token
},
}],
'content': {
'template_id': 'password-reset',
},
}
return self._postToSparkpost(payload)
def _emailMatchUpdate(self, user_id, message, person, action):
"""
Send match update email to user, via match-update SparkPost template
Given user, message content, person-of-interest, action (e.g. joined/left)
"""
# Get profile of user_id
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# If user disabled email notifications or email is unverified, return
if not profile.notifications[1] or not profile.emailVerified:
return False
# Create SparkPost request to send notification email
payload = {
'recipients': [{
'address': {
'email': profile.contactEmail,
'name': profile.firstName + ' ' + profile.lastName,
},
'substitution_data': {
'first_name': profile.firstName,
'message': message,
'person': person,
'action': action,
},
}],
'content': {
'template_id': 'match-update',
},
}
return self._postToSparkpost(payload)
def _emailAvailMatch(self, partner, message, player_name):
"""
Send notification to potential parter of a newly created match
'partner' is the person to send the email to, a Profile object
'player_name' is the name of the person who created the match, a string
"""
profile = partner
# If user disabled email notifications or email is unverified, return
if not profile.notifications[1] or not profile.emailVerified:
return False
# Create SparkPost request to send notification email
payload = {
'recipients': [{
'address': {
'email': profile.contactEmail,
'name': profile.firstName + ' ' + profile.lastName,
},
'substitution_data': {
'first_name': profile.firstName,
'message': message,
'person': player_name,
},
}],
'content': {
'template_id': 'available-match-notification',
},
}
return self._postToSparkpost(payload)
@endpoints.method(AccessTokenMsg, StringMsg, path='',
http_method='POST', name='verifyEmailToken')
def verifyEmailToken(self, request):
""" Verify email token, to verify email address. Return email address string or 'error' """
status = StringMsg() # return status
status.data = 'error' # default to error
# Decode the JWT token
try:
payload = jwt.decode(request.accessToken, EMAIL_VERIF_SECRET, algorithm='HS256')
except:
return status
# If valid JWT token, extract the info and update DB if applicable
user_id = payload['userId']
email = payload['contactEmail']
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# If user changed email and clicked on old email verif link, this request is invalid
if profile.contactEmail != email:
return status
# If we get here then email is verified. Update DB and return successful status
profile.emailVerified = True
profile.put()
status.data = email
return status
###################################################################
# Custom Accounts
###################################################################
def _genToken(self, payload):
""" Generate custom auth JWT token given payload dict """
secret = CA_SECRET
return jwt.encode(payload, secret, algorithm='HS256')
def _decodeToken(self, token):
""" Decode custom token. If successful, return payload. Else, return None. """
secret = CA_SECRET
try:
return jwt.decode(token, secret, algorithm='HS256')
except:
return None
def _getUserId(self, token):
""" Get userId: First check if local account, then check if FB account """
# See if token belongs to custom account user
ca_payload = self._decodeToken(token)
if ca_payload is not None:
return ca_payload['userId']
# If above failed, try FB token
return self._getFbUserId(token)
@endpoints.method(AccessTokenMsg, BooleanMsg, path='',
http_method='POST', name='verifyToken')
def verifyToken(self, request):
""" Verify validity of custom account token, check if user is logged in. Return True/False. """
status = BooleanMsg() # return status
status.data = False # default to invalid (False)
ca_payload = self._decodeToken(request.accessToken)
if ca_payload is not None:
if 'userId' in ca_payload and 'session_id' in ca_payload:
# Check if user is logged into valid session
user_id = ca_payload['userId']
session_id = ca_payload['session_id']
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
if profile is not None:
status.data = profile.loggedIn and (profile.session_id == session_id)
return status
@endpoints.method(AccountAuthMsg, StringMsg, path='',
http_method='POST', name='createAccount')
def createAccount(self, request):
""" Create new custom account """
status = StringMsg() # return status
status.data = 'error' # default to error
# Verify if user passed reCAPTCHA
# POST request to Google reCAPTCHA API
url = 'https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s' % (GRECAPTCHA_SECRET, request.recaptcha)
try:
result = urlfetch.Fetch(url, method=2)
except:
raise endpoints.BadRequestException('urlfetch error: Unable to POST to Google reCAPTCHA')
return status
data = json.loads(result.content)
if not data['success']:
status.data = 'recaptcha_fail'
return status
user_id = 'ca_' + request.email
# Get profile from datastore -- if profile not found, then profile=None
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# If profile exists, return status
if profile:
status.data = 'user_exists'
return status
# Salt and hash the password
salt = Crypto.Random.new().read(16)
passkey = KDF.PBKDF2(request.password, salt).encode('hex')
salt_passkey = salt.encode('hex') + '|' + passkey
# Generate new session ID
session_id = Crypto.Random.new().read(16).encode('hex')
# Create new profile for user
Profile(
key = profile_key,
userId = user_id,
contactEmail = request.email,
salt_passkey = salt_passkey,
session_id = session_id,
loggedIn = True,
emailVerified = False,
notifications = [False, True]
).put()
# Generate user access token
token = self._genToken({'userId': user_id, 'session_id': session_id})
# If we get here, means we suceeded
status.data = 'success'
status.accessToken = token
return status
@endpoints.method(AccountAuthMsg, StringMsg, path='',
http_method='POST', name='login')
def login(self, request):
""" Check username/password to login """
status = StringMsg() # return status
status.data = 'error' # default to error
# Verify if user passed reCAPTCHA
# POST request to Google reCAPTCHA API
url = 'https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s' % (GRECAPTCHA_SECRET, request.recaptcha)
try:
result = urlfetch.Fetch(url, method=2)
except:
raise endpoints.BadRequestException('urlfetch error: Unable to POST to Google reCAPTCHA')
return status
data = json.loads(result.content)
if not data['success']:
status.data = 'recaptcha_fail'
return status
user_id = 'ca_' + request.email
# Get profile from datastore -- if profile not found, then profile=None
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# If profile does not exist, return False
if not profile:
return status
# Parse salt and passkey from DB, compare it to provided version
db_salt, db_passkey = profile.salt_passkey.split('|')
passkey = KDF.PBKDF2(request.password, db_salt.decode('hex')).encode('hex')
# Passwords don't match, return False
if passkey != db_passkey:
return status
# Generate new session ID
session_id = Crypto.Random.new().read(16).encode('hex')
profile.session_id = session_id
# Update user's status to logged-in
profile.loggedIn = True
profile.put()
# Generate user access token
token = self._genToken({'userId': user_id, 'session_id': session_id})
# If we get here, means we suceeded
status.data = 'success'
status.accessToken = token
return status
@endpoints.method(AccessTokenMsg, BooleanMsg, path='',
http_method='POST', name='logout')
def logout(self, request):
""" Logout """
status = BooleanMsg() # return status
status.data = False # default to error (False)
user_id = self._getUserId(request.accessToken)
# Get Profile from NDB, update login status
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
profile.session_id = 'invalid'
profile.loggedIn = False
profile.put()
status.data = True
return status
@endpoints.method(ChangePasswordMsg, StringMsg, path='',
http_method='POST', name='changePassword')
def changePassword(self, request):
""" Change password """
status = StringMsg()
status.data = 'error'
# Get user profile
user_id = self._getUserId(request.accessToken)
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# Not sure how this would happen, but it would be an error
if not profile:
return status
# Check if provided old password matches user's current password
db_salt, db_passkey = profile.salt_passkey.split('|')
passkey = KDF.PBKDF2(request.oldPw, db_salt.decode('hex')).encode('hex')
# Passwords don't match, return
if passkey != db_passkey:
status.data = 'old_pw_wrong'
return status
# If passwords match, salt & hash new password
new_salt = Crypto.Random.new().read(16)
new_passkey = KDF.PBKDF2(request.newPw, new_salt).encode('hex')
new_salt_passkey = new_salt.encode('hex') + '|' + new_passkey
profile.salt_passkey = new_salt_passkey
# Also generate new session ID
session_id = Crypto.Random.new().read(16).encode('hex')
profile.session_id = session_id
# Update DB
profile.put()
# Send user an email to notify password change
self._emailPwChange(profile)
# Return success status
status.data = 'success'
return status
@endpoints.method(AccountAuthMsg, StringMsg, path='',
http_method='POST', name='forgotPassword')
def forgotPassword(self, request):
""" Forgot password, send user password reset link via email """
status = StringMsg()
status.data = 'error'
# Verify if user passed reCAPTCHA
# POST request to Google reCAPTCHA API
url = 'https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s' % (GRECAPTCHA_SECRET, request.recaptcha)
try:
result = urlfetch.Fetch(url, method=2)
except:
raise endpoints.BadRequestException('urlfetch error: Unable to POST to Google reCAPTCHA')
return status
data = json.loads(result.content)
if not data['success']:
status.data = 'recaptcha_fail'
return status
user_id = 'ca_' + request.email
# Get profile from datastore -- if profile not found, then profile=None
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# Check if profile exists. If not, return status
if not profile:
status.data = 'invalid_email'
return status
# If email unverified, return status
if not profile.emailVerified:
status.data = 'unverified_email'
return status
# Send password reset link to user's email
if self._emailPwReset(profile):
status.data = 'success'
return status
@endpoints.method(StringMsg, StringMsg, path='',
http_method='POST', name='resetPassword')
def resetPassword(self, request):
""" Reset password, verify token. Return status. """
status = StringMsg()
status.data = 'error'
# Validate and decode token
try:
payload = jwt.decode(request.accessToken, CA_SECRET, algorithm='HS256')
except:
status.data = 'invalid_token'
return status
# Get user profile
user_id = payload['userId']
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# Not sure how this would happen, but it would be an error
if not profile:
return status
# Salt & hash new password
new_salt = Crypto.Random.new().read(16)
new_passkey = KDF.PBKDF2(request.data, new_salt).encode('hex')
new_salt_passkey = new_salt.encode('hex') + '|' + new_passkey
profile.salt_passkey = new_salt_passkey
# Also generate new session ID
session_id = Crypto.Random.new().read(16).encode('hex')
profile.session_id = session_id
# Update DB
profile.put()
# Send user an email to notify password change
self._emailPwChange(profile)
# Return success status
status.data = 'success'
return status
###################################################################
# Facebook Authentication & Graph API
###################################################################
def _getFbUserId(self, token):
""" Given token, find FB user ID from FB, and return it """
url = 'https://graph.facebook.com/v%s/me?access_token=%s&fields=id' % (FB_API_VERSION, token)
try:
result = urlfetch.Fetch(url, method=1)
except:
raise endpoints.BadRequestException('urlfetch error: Get FB user ID')
return None
data = json.loads(result.content)
if 'error' in data:
raise endpoints.BadRequestException('FB OAuth token error')
return None
user_id = 'fb_' + data['id']
return user_id
def _postFbNotif(self, user_id, message, href):
"""
Post FB notification with message to user
"""
# Get profile of user_id
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# Only post FB notif if FB user and user enabled FB notifs
if not (user_id[:3] == 'fb_' and profile.notifications[0]):
return False
fb_user_id = user_id[3:]
# Get App Access Token, different than User Token
# https://developers.facebook.com/docs/facebook-login/access-tokens/#apptokens
url = 'https://graph.facebook.com/v%s/oauth/access_token?grant_type=client_credentials&client_id=%s&client_secret=%s' % (FB_API_VERSION, FB_APP_ID, FB_APP_SECRET)
try:
result = urlfetch.Fetch(url, method=1)
except:
raise endpoints.BadRequestException('urlfetch error: FB app access token')
return False
token = json.loads(result.content)['access_token']
url = 'https://graph.facebook.com/v%s/%s/notifications?access_token=%s&template=%s&href=%s' % (FB_API_VERSION, fb_user_id, token, message, href)
try:
result = urlfetch.Fetch(url, method=2)
except:
raise endpoints.BadRequestException('urlfetch error: Unable to POST FB notification')
return False
data = json.loads(result.content)
if 'error' in data:
raise endpoints.BadRequestException('FB notification error')
return False
return True
@endpoints.method(AccessTokenMsg, StringMsg, path='',
http_method='POST', name='fbLogin')
def fbLogin(self, request):
""" Handle Facebook login """
status = StringMsg() # return status message
status.data = 'error' # default to error message, unless specified otherwise
'''
# Swap short-lived token for long-lived token
short_token = request.data
url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=%s&client_secret=%s&fb_exchange_token=%s' % (
FB_APP_ID, FB_APP_SECRET, short_token)
try:
result = urlfetch.Fetch(url, method=1)
except:
print('urlfetch error1')
return status
token = result.content.split('&')[0] # 'access_token=blahblahblah'
'''
token = request.accessToken
# Use token to get user info from API
url = 'https://graph.facebook.com/v%s/me?access_token=%s&fields=name,id,email' % (FB_API_VERSION, token)
try:
result = urlfetch.Fetch(url, method=1)
except:
raise endpoints.BadRequestException('urlfetch error')
return status
data = json.loads(result.content)
if 'error' in data:
raise endpoints.BadRequestException('FB OAuth token error')
return status
user_id = 'fb_' + data['id']
first_name = data['name'].split()[0]
last_name = data['name'].split()[-1]
email = data['email']
# Get existing profile from datastore, or create new one
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# If profile already exists, return 'existing_user'
# Unless, they have empty firstName (maybe they got d/c'ed on profile page)
if profile:
# If empty first name, return new_user
if profile.firstName == '':
status.data = 'new_user'
return status
# If user previously logged-out, update login status in NDB
if not profile.loggedIn:
profile.loggedIn = True
profile.put()
status.data = 'existing_user'
return status
# Else, create new profile and return 'new_user'
profile = Profile(
key = profile_key,
userId = user_id,
contactEmail = email,
firstName = first_name,
lastName = last_name,
loggedIn = True,
emailVerified = False,
notifications = [True, False]
).put()
status.data = 'new_user'
return status
###################################################################
# Profile Objects
###################################################################
@ndb.transactional(xg=True)
def _updateProfile(self, request):
"""Update user profile."""
status = StringMsg()
status.data = 'normal'
token = request.accessToken
user_id = self._getUserId(token)
# Make sure the incoming message is initialized, raise exception if not
request.check_initialized()
# Get existing profile from datastore
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# Check if email changed. Note custom account users cannot change email.
email_change = (profile.contactEmail != request.contactEmail) and (user_id[:3] != 'ca_')
# Update profile object from the user's form
for field in request.all_fields():
if field.name == 'userId':
continue # userId is fixed
elif user_id[:3] == 'ca_' and field.name == 'contactEmail':
continue # custom account users cannot change email address
elif field.name != 'accessToken':
setattr(profile, field.name, getattr(request, field.name))
# If this is user's first time updating profile, or changing email address
# then send email verification
if profile.pristine or email_change:
profile.pristine = False
self._emailVerif(profile)
status.data = 'email_verif'
# Save updated profile to datastore
profile.put()
return status
@endpoints.method(AccessTokenMsg, ProfileMsg,
path='', http_method='POST', name='getProfile')
def getProfile(self, request):
"""Return user profile."""
token = request.accessToken
user_id = self._getUserId(token)
# Create new ndb key based on unique user ID (email)
# Get profile from datastore -- if profile not found, then profile=None
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# If profile does not exist, return empty ProfileMsg
if not profile:
return ProfileMsg()
# Else, copy profile to ProfileForm, and return it
pf = ProfileMsg()
for field in pf.all_fields():
if hasattr(profile, field.name):
setattr(pf, field.name, getattr(profile, field.name))
pf.check_initialized()
return pf
@endpoints.method(ProfileMsg, StringMsg,
path='', http_method='POST', name='updateProfile')
def updateProfile(self, request):
"""Update user profile."""
return self._updateProfile(request) # transactional
###################################################################
# Match Objects
###################################################################
@ndb.transactional(xg=True)
def _createMatch(self, request):
"""Create new Match, update user Profile to add new Match to Profile.
Also notify all applicable users this new match is available to them.
Returns MatchMsg/request."""
status = BooleanMsg()
status.data = False
token = request.accessToken
user_id = self._getUserId(token)
# If any field in request is None, then raise exception
if any([getattr(request, field.name) is None for field in request.all_fields()]):
raise endpoints.BadRequestException('All input fields required to create a match')
# Copy MatchMsg/ProtoRPC Message into dict
data = {field.name: getattr(request, field.name) for field in request.all_fields()}
del data['accessToken'] # don't need this for match object
# Get user profile from NDB
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
# Normalize NTRP if needed
ntrp = profile.ntrp
if profile.gender == 'f':
ntrp = profile.ntrp - 0.5
# Add default values for those missing
data['players'] = [user_id]
data['confirmed'] = False
data['ntrp'] = ntrp
# Convert date/time from string to datetime object
dt_string = data['date'] + '|' + data['time']
dt_string2 = 'on ' + data['date'] + ' at ' + data['time'] # used for notifications
data['dateTime'] = datetime.strptime(dt_string, '%m/%d/%Y|%H:%M')
del data['date']
del data['time']
# Create new match based on data, and put in datastore
match_key = Match(**data).put().urlsafe()
# Update user profile, adding the new match to Profile.matches
profile.matches.append(match_key)
profile.put()
status.data = True
return status, profile, match_key, dt_string2
def _notifyAvailMatch(self, profile, match_key, dt_string):
""" Notify all potential partners of newly created match """
# FIXME: Put this functionality in a task queue
# Get name and NTRP of currently player
player_name = profile.firstName + ' ' + profile.lastName
my_ntrp = profile.ntrp
if profile.gender == 'f':
my_ntrp -= 0.5
# Query the DB to find partners of similar skill
query = Profile.query(ndb.OR(Profile.ntrp == my_ntrp, Profile.ntrp == my_ntrp + 0.5, Profile.ntrp == my_ntrp - 0.5))
for partner in query:
# Current user does not get notified
if profile.userId == partner.userId:
continue
# Notify the potential partner
match_url = '?match_type=avail&match_id=' + match_key
email_message = 'You have a new available match with %s %s.' % (player_name, dt_string)
email_message += '<br>To view the match, <a href="http://www.georgesungtennis.com/%s">click here</a>.' % match_url
# Try FB and email notifications
# The functions themselves will test if FB user and/or if they enabled the notification
self._postFbNotif(partner.userId, urlquote('New available match with ' + player_name + ' ' + dt_string), match_url)
self._emailAvailMatch(partner, email_message, player_name)
@endpoints.method(MatchMsg, BooleanMsg, path='',
http_method='POST', name='createMatch')
def createMatch(self, request):
"""Create new Match"""
#return self._createMatch(request)
status, profile, match_key, dt_string = self._createMatch(request)
self._notifyAvailMatch(profile, match_key, dt_string)
return status
@ndb.transactional(xg=True)
def _joinMatch(self, request):
"""Join an available Match, given Match's key.
If there is mid-air collision, return false. If successful, return true."""
token = request.accessToken
user_id = self._getUserId(token)
# If any field in request is None, then raise exception
if request.data is None:
raise endpoints.BadRequestException('Need match ID from request.data')
# Get match key, then get the Match entity from db
match_key = request.data
match = ndb.Key(urlsafe=match_key).get()
# Make sure match is not full. If full, return false.
match_full = False
if match.singles and len(match.players) >= 2:
match_full = True
elif not match.singles and len(match.players) >= 4:
match_full = True
if match_full:
status = BooleanMsg()
status.data = False
return status
# Update 'players' and 'confirmed' fields (if needed)
match.players.append(user_id)
if match.singles or len(match.players) >= 4:
match.confirmed = True
# Update Match db
match.put()
# Add current match key to current user's matches list
profile_key = ndb.Key(Profile, user_id)
profile = profile_key.get()
profile.matches.append(match_key)
profile.put()
# Notify all other players that current user/player has joined the match
player_name = profile.firstName + ' ' + profile.lastName
for other_player in match.players:
if other_player == user_id:
continue
match_url = '?match_type=conf_pend&match_id=' + match_key
email_message = '%s has <b>joined</b> your match. To view your match, <a href="http://www.georgesungtennis.com/%s">click here</a>.' % (player_name, match_url)
# Try FB and email notifications
# The functions themselves will test if FB user and/or if they enabled the notification
self._postFbNotif(other_player, urlquote(player_name + ' has joined your match'), match_url)
self._emailMatchUpdate(other_player, email_message, player_name, 'joined')
# Return true, for success
status = BooleanMsg()
status.data = True
return status
@endpoints.method(StringMsg, BooleanMsg, path='',
http_method='POST', name='joinMatch')
def joinMatch(self, request):
"""Join an available Match, given Match's key"""
return self._joinMatch(request)
@ndb.transactional(xg=True)
def _cancelMatch(self, request):
"""Cancel an existing Match, given Match's key.
If successful, return true."""
status = BooleanMsg()
status.data = False
token = request.accessToken
user_id = self._getUserId(token)
# If any field in request is None, then raise exception
if request.data is None:
raise endpoints.BadRequestException('Need match ID from request.data')
return status
# Get match key, then get the Match entity from db
match_key = request.data
match = ndb.Key(urlsafe=match_key).get()
# Determine if cancelling player is the owner of the match
owner_leaving = match.players[0] == user_id
# Update 'players' and 'confirmed' fields
match.players.remove(user_id)
match.confirmed = False
# Remove current match key from current user's matches list
profile = ndb.Key(Profile, user_id).get()
profile.matches.remove(match_key)
profile.put()
# Notify all other players that current user/player has left the match
player_name = profile.firstName + ' ' + profile.lastName
match_url = '?match_type=conf_pend&match_id=' + match_key
for other_player in match.players:
# Try FB and email notifications
# The functions themselves will test if FB user and/or if they enabled the notification
if owner_leaving:
# FB
self._postFbNotif(other_player, urlquote(player_name + ' has cancelled your match'), '')