/
views.py
1475 lines (1338 loc) · 45.8 KB
/
views.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
import re
import urllib
import urllib2
import datetime
from time import strptime
from google.appengine.ext import ndb
from google.appengine.api import images, memcache
from google.appengine.api.images import Image
from google.appengine.ext import db
import json
from webob.exc import HTTPUnauthorized
from auth_logic import user_required, api_login_required, CheckAPILogin
from auth_model import User
from dataloader import load_data
import mail_wrapper
from models import Item, DBImage, Vote, Category, getProp, \
Invite, Friends, InviteInternal, Comment, Feedback
from places_db import PlacesDB
from profiler import profile_in, profile_out
from settings import config
import logging
from webapp2_extras.auth import InvalidAuthIdError
from webapp2_extras.auth import InvalidPasswordError
from base_handler import BaseHandler
from logging_ext import logging_ext
import ndb_models
import geo
import settings
__author__ = 'Will'
class GetItemsAjax(BaseHandler):
@user_required
def get(self):
""" get the items for a user
@return:
"""
profile_in("GetItemsAjax")
result = PlacesDB.get_item_list(self.request, False, self.user_id)
if not result:
self.abort(500)
json.dump(result,
self.response.out)
profile_out("GetItemsAjax")
def serialize_user_details(user_id, places_ids, current_user, request, since=None):
""" give the list of votes & places_ids for a user
@param user_id: int: which user
@param places_ids: dict: list of places_ids indexed by key (BY VALUE)
@param current_user: int: current user - if same as user_id then
we exclude untried
@return:
"""
try:
logging.info("serialize_user_details 1")
# get it from the cache
user_id = int(user_id)
votes = Vote.query(Vote.voter == user_id)
user = User.get_by_id(user_id)
user_profile = user.profile()
if getProp(user_profile, 'last_write'):
last_write = user_profile.last_write
else:
last_write = None
result = {"votes": votes,
"id": user_id,
# todo is it first_name?
'name': user_profile.screen_name,
'last_write': last_write}
if votes:
logging.debug("serialize_user_details: %d votes"%len(votes))
for place_key in votes:
if not place_key in places_ids:
place_json = Item.id_to_json(place_key)
# if user_id == current_user:
# place_json['vote'] = votes[place_key]['vote']
if "category" in place_json:
places_ids[place_key] = place_json
for place_id in places_ids:
pl = ndb.Key(Item,place_id).get()
json_data = pl.get_json()
places_ids[place_id] = json_data
logging.debug('serialize_user_details: Added %d places_ids'%len(places_ids))
else:
logging.debug("serialize_user_details: No Votes")
return result
except Exception, e:
logging_ext.error("serialize_user_details Exception", exc_info=True)
class FriendsVotesApi(BaseHandler):
@user_required
def get(self, id):
"""
Get the votes for a friend
:param id: string
:return: json
"""
friend_id = int(id)
votes = Vote.query(Vote.voter ==friend_id)
res = {
'id': friend_id,
'votes': [vote.json for vote in votes]
}
json.dump(res, self.response.out, default=json_serial)
return
class FriendsApiRemove(BaseHandler):
@api_login_required
def post(self):
"""
accept a friend request
:return:
"""
other_id = int(self.request.params['unfriend_id'])
my_id = int(self.user_id)
low = min(other_id, my_id)
high = max(other_id, my_id)
record = Friends.query(Friends.lower == low, Friends.higher == high).get()
if record:
record.key.delete()
# delete invites
inv_from = InviteInternal.query().\
filter(InviteInternal.inviter == other_id).\
filter(InviteInternal.invitee == my_id)
for i in inv_from:
i.key.delete()
inv_to = InviteInternal.query().\
filter(InviteInternal.inviter == my_id).\
filter(InviteInternal.invitee == other_id)
for i in inv_to:
i.key.delete()
self.response.out.write("OK")
return
self.response.out.write("FAIL")
class FriendsApiAccept(BaseHandler):
@api_login_required
def post(self):
"""
accept a friend request
:return:
"""
from_id = self.request.params['from_id']
#find the invite
# inv = InviteInternal.all().get()
# inv = InviteInternal.all().filter("invitee =", self.user_id).get()
inv = InviteInternal.query().\
filter(InviteInternal.invitee == self.user_id).\
filter(InviteInternal.inviter == int(from_id)).\
get()
if not inv:
self.response.out.write("NO INVITE")
return
Friends.addFriends(self.user_id, from_id)
logging.info("FriendsApiAccept %s from %s"%(self.user_id, from_id))
#delete invite
inv.accepted = True
inv.when = datetime.datetime.now()
inv.put()
self.response.out.write("OK")
class FriendsApiReject(BaseHandler):
@api_login_required
def post(self):
"""
accept a friend request
:return:
"""
from_id = self.request.params['from_id']
#find the invite
# inv = InviteInternal.all().get()
# inv = InviteInternal.all().filter("invitee =", self.user_id).get()
inv = InviteInternal.query().\
filter(InviteInternal.invitee == self.user_id).\
filter(InviteInternal.inviter == int(from_id)).\
get()
if inv:
inv.key.delete()
self.response.out.write("OK")
class FriendsApi(BaseHandler):
@api_login_required
def get(self):
"""
get the users friends
:return:
"""
friends_data = []
if config['all_are_friends']:
for user in User.query():
if user.get_id() == self.user_id:
continue # don't add myself again
friends_data.append(self.user_id)
else:
assert False
#TODO: check friends
prof = user.profile()
for friend in prof.friends:
friends_data.append(friend.userId)
json.dump(friends_data, self.response.out, default=json_serial)
return
class itemsAPI(BaseHandler):
@api_login_required
def get(self):
"""
A list of keys is supplied in 'key_list', returns detail list
:return: json: {items: list of places}
"""
if 'key_list' in self.request.params:
res = []
key_list = json.loads(self.request.params['key_list'])
for key in key_list:
res.append(Item.id_to_json(key))
json.dump({'items':res}, self.response.out, default=json_serial)
return
self.abort(403)
class profileAPI(BaseHandler):
@user_required
def get(self):
if not hasattr(self.user,'sex'):
self.user.sex = ""
res = {
'screen_name': self.user.screen_name,
'email': self.user.email_address,
'sex': self.user.sex,
}
json.dump({'profile':res}, self.response.out, default=json_serial)
return
@user_required
def post(self):
sn = self.request.params["screen_name"]
if 'gender' in self.request.params:
gn = self.request.params["gender"]
self.user.sex = gn
self.user.screen_name = sn
self.user.put()
self.response.out.write("OK")
class getUserRecordFast(BaseHandler):
@user_required
def get(self):
""" get the user record, including friends' places """
try:
if self.user.blocked:
raise Exception('Blocked')
my_id = self.user_id
except:
logging_ext.error('getFullUserRecord: User Exception')
json.dump({'result':'FAIL'},
self.response.out,
default=json_serial)
return
if my_id:
user = User.get_by_id(my_id)
if user:
# logged in
result = {
"id": my_id,
"admin": self.user.profile().is_admin }
since = None
if 'since' in self.request.params:
# move since back in time to allow for error
since = datetime.datetime.strptime(
self.request.params['since'],
config['DATETIME_FORMAT']) - \
config['TIMING_DELTA']
user_list = []
user_results = []
# is it for a specific user?
if "forUser" in self.request.params:
user_list.append(user.get(self.request.params['forUser']))
else:
if config['all_are_friends']:
q = User.gql('')
for user in q:
user_list.append(user)
places = {}
my_votes = Vote.query(Vote.voter==my_id)
for u in user_list:
user_id = u.get_id()
if user_id == my_id:
votes = my_votes
else:
votes = Vote.query(Vote.voter==u.get_id())
for v in votes:
#add to the list if it's not there, or overwrite if this is my version
if not v in places or user_id == my_id:
places [v] = Item.id_to_json(v)
user_profile = u.profile()
if getProp(user_profile, 'last_write'):
last_write = user_profile.last_write
else:
last_write = None
user_str = {"votes": votes,
"id": u.get_id(),
# todo is it first_name?
'name': u.screen_name,
'last_write': last_write}
user_results.append(user_str)
result["places"] = places
result["friendsData"] = user_results
json_str = json.dumps(
result,
default=json_serial)
self.response.out.write(json_str)
#profile_out("getFullUserRecord")
return
self.error(401)
class getFullUserRecord(BaseHandler):
@user_required
def get(self):
""" get the entire user record, including friends' places """
try:
if self.user.blocked:
raise Exception('Blocked')
my_id = self.user_id
except:
logging_ext.error('getFullUserRecord: User Exception')
json.dump({'result':'FAIL'},
self.response.out,
default=json_serial)
return
if my_id:
#profile_in("getFullUserRecord")
user = User.get_by_id(my_id)
if user:
# logged in
since = None
if 'since' in self.request.params:
# move since back in time to allow for error
since = datetime.datetime.strptime(
self.request.params['since'],
config['DATETIME_FORMAT']) - \
config['TIMING_DELTA']
# is it for a specific user?
if "forUser" in self.request.params:
for_1_user = long(self.request.get("forUser"))
else:
for_1_user = None
# either the first lookup is for me, plus everyone,
# or it is for a specified user
result = {
"id": my_id,
"admin": self.user.profile().is_admin }
if for_1_user:
logging.info("getFullUserRecord: 1 user")
first_user = for_1_user
result["for_1_user"] = for_1_user
else:
logging.info("getFullUserRecord: 1+ user")
first_user = my_id
dict_id_place = {}
# load the data for the 1 user - me or specified
friends_data = [
serialize_user_details(
first_user,
dict_id_place,
my_id,
self.request,
since)]
# was it for all users? If so we've only done ourselves
if not for_1_user:
# for all users
prof = user['p']
if config['all_are_friends']:
q = User.gql('')
logging.info("getFullUserRecord: %d friends"%q.count())
for user in q:
# for userProf in UserProfile().all():
if user.get_id() == my_id:
continue # don't add myself again
data = serialize_user_details(
user.get_id(), dict_id_place, my_id, self.request, since)
logging.info("getFullUserRecord: record %s"%data)
friends_data.append(data)
else:
for friend in prof.friends:
friends_data.append(serialize_user_details(
friend, dict_id_place, my_id, self.request, since))
result["friendsData"] = friends_data
logging.debug('getFullUserRecord: return %d places'%len(dict_id_place))
result["places"] = dict_id_place
# encode using a custom encoder for datetime
json_str = json.dumps(
result,
default=json_serial)
self.response.out.write(json_str)
#profile_out("getFullUserRecord")
return
self.error(401)
class api_log(BaseHandler):
"""
Level is one of :
'CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG',
"""
@user_required
def post(self):
message = self.request.POST["message"]
level = int(self.request.POST["level"])
logging.log(level, message)
class user_profile(BaseHandler):
@user_required
def get(self):
user_obj = User().get_by_id(self.user_id)
json.dump({'screen_name': user_obj.screen_name}, self.response.out)
@user_required
def post(self):
user_obj = User().get_by_id(self.user_id)
user_obj.screen_name = self.request.get('screen_name')
user_obj.put()
def json_serial(o):
"""
JSON serializer for objects not serializable by default json code
http://stackoverflow.com/questions/11875770/how-to-overcome-
datetime-datetime-not-json-serializable-in-python
"""
if type(o) is datetime.date or type(o) is datetime.datetime:
return o.isoformat()
class getCuisines_ajax(BaseHandler):
@api_login_required
def get(self):
cuisines_list = []
cats = Category.query()
for cat in cats:
cuisines_list.append(cat.title)
results = {'categories': cuisines_list}
json.dump(results,
self.response.out)
class getAddresses_ajax(BaseHandler):
@api_login_required
def get(self):
logging.debug('getAddresses_ajax')
address = self.request.get("addr")
lat = float(self.request.get("lat"))
lng = float(self.request.get("lng"))
names = self.request.get("place_name").split(" ")
near_me = self.request.get("near_me")
if near_me == u'0':
# near the address
url = ("https://maps.googleapis.com/maps/api/geocode/json?address=%s"
"&sensor=false&key=%s&bounds=%f,%f|%f,%f") % \
(urllib2.quote(address),
config['google_api_key'],
lat-0.3,
lng-0.3,
lat+0.3,
lng+0.3)
response = urllib2.urlopen(url)
jsonResult = response.read()
addressResult = json.loads(jsonResult)
if addressResult['status'] == "OK":
lat = addressResult['results'][0]['geometry']['location']['lat']
lng = addressResult['results'][0]['geometry']['location']['lng']
results = PlacesDB.map_and_db_search(
self.request,
-1,
'',
True,
lat,
lng,
geo.LatLng(lat=lat, lng=lng),
self.request.get("place_name").lower(),
self.user_id)
if results:
results['search'] = {'lat': lat,'lng':lng}
# check_for_dirty_data(self, results)
json.dump(results,
self.response.out,
default=json_serial)
else:
# logging.info("get_google_db_places near [%f,%f]: %s" %
# (lat, lng, "none found"))
logging.debug("getAddresses_ajax - none found ")
self.error(401)
def handle_error(request, response, exception):
if request.path.startswith('/json'):
response.headers.add_header('Content-Type', 'application/json')
result = {
'status': 'error',
'status_code': exception.code,
'error_message': exception.explanation,
}
response.write(json.dumps(result))
else:
response.write(exception)
response.set_status(exception.code)
class MainHandler(BaseHandler):
def get(self):
if self.user:
con = {"cats": Category.query()}
logging.info('MainHandler: Logged in')
self.render_template("index.html", con)
else:
logging.info('MainHandler: Not logged in')
self.render_template("login.html")
class InviteUserAPI(BaseHandler):
@api_login_required
def get(self):
# invite a user to the system - get the invite URI
token = Invite.getInviteToken(self.user_id)
uri = self.uri_for('register',type='i', invite_token=token, _full=True)
self.response.out.write(uri)
class register(BaseHandler):
def get(self):
# get the registration form. Embed the invite token in it
params = None
if 'type' in self.request.params:
if self.request.params["type"] == "i":
# it's an invite
params = {'invite_token':self.request.params['invite_token']}
logging.info("register,GET token=%s"%self.request.params['invite_token'])
self.render_template('signup.html', params)
def post(self):
# posted a filled out reg form
# create the user,
# and send an email verification including token to the new user
email = self.request.get('email').lower()
name = self.request.get('name')
password = self.request.get('password')
last_name = self.request.get('lastname')
unique_properties = ['email_address']
logging.info("register,POST email=%s"%email)
user_data = self.user_model.create_user(
email,
unique_properties,
email_address=email,
name=name,
password_raw=password,
last_name=last_name,
verified=False)
if not user_data[0]: # user_data is a tuple
logging.warning("register,POST Duplicate %s"%email)
self.render_template(
'signup.html', {"message": "That userId is already registered", })
return
user = user_data[1]
user_id = user.get_id()
token = self.user_model.create_signup_token(user_id)
invite_token = self.request.params['invite_token'] if \
'invite' in self.request.params \
else "none"
inviter = Invite.checkInviteToken(invite_token)
if inviter:
passwordVerificationHandler.handle_verification(self,user_id,token,"v",invite_token)
else:
verification_url = self.uri_for('verification', type='v', user_id=user_id,
signup_token=token, _full=True)
verification_url += "&invite_token=" + str(invite_token)
logging.info("register,POST emailing")
mail_wrapper.send_mail(
sender=config['system_email'],
to=[email, 'wgilpin+taste5@gmail.com'],
subject="Rayv Registration",
body='Click on this link to verify your address and '
'complete the sign-up process \n'+
verification_url
)
logging.info('Verification email sent to %s [%s] [%s]'%(email,verification_url, invite_token))
params = {
'email':email,
'password':password
}
self.render_template('signup-verify.html', params)
class AddUserAsFriend(BaseHandler):
@api_login_required
def get(self):
user_email = self.request.get('email').lower()
logging.info("AddUserAsFriend "+user_email)
user = User.query(ndb.GenericProperty('email_address') == user_email).get()
if not user:
user = User.query(ndb.GenericProperty('email_address') == user_email).get()
if user.get_id() == self.user_id:
self.response.out.write("EMAIL TO SELF")
return
if user:
InviteInternal.add_invite(self.user_id, user.key.id())
self.response.out.write("FOUND")
else:
self.response.out.write("NOT FOUND")
class emailInviteFriend(BaseHandler):
@api_login_required
def post(self):
user_email = self.request.get('email').lower()
if not re.match('[^@]+@[^@]+\.[^@]+',user_email):
self.response.out.write("BAD EMAIL")
return
logging.info("emailInviteFriend "+user_email.lower())
token = Invite.getInviteToken(self.user_id)
uri = self.uri_for('register',type='i', invite_token=token, _full=True)
msg = "Hi,\n I'd like to share my favourite eateries with you using the Taste5 app, "+\
"Click this link to join for free!\n\n"+uri+"\n\n"+self.user.screen_name
mail_wrapper.send_mail(sender=settings.config['system_email'],
to=user_email,
subject="Share my list of places to eat!",
body=msg)
logging.info("Email invite sent to %s by %s"%(user_email,self.user_id))
self.response.out.write("OK")
class getPlaceDetailsApi(BaseHandler):
@api_login_required
def get(self):
place_id = self.request.params['place_id']
logging.debug('getPlaceDetailsApi '+place_id)
params = {'placeid': place_id,
'key': config['google_api_key']}
url = "https://maps.googleapis.com/maps/api/place/details/json?" + \
urllib.urlencode(params)
res = {}
try:
response = urllib2.urlopen(url)
json_result = response.read()
details_result = json.loads(json_result)
except:
logging_ext.error(
'getPlaceDetailFromGoogle: Exception [%s]',
place_id,
exc_info=True)
return {"photo": None, "telephone": None}
if details_result['status'] == "OK":
if "international_phone_number" in details_result['result']:
res['telephone'] = details_result['result']["international_phone_number"]
if "website" in details_result['result']:
res['website'] = details_result['result']["website"]
json.dump(res, self.response.out)
class updateItem(BaseHandler):
@user_required
def get(self, key):
"""
" get a single item
"""
try:
json.dump(Item.id_to_json(key), self.response.out)
except:
logging_ext.error('updateItem GET Exception '+key,exc_info=True)
def update_photo(it, request_handler):
try:
raw_file = request_handler.request.get('new-photo')
rot = request_handler.request.get("rotation")
if len(raw_file) > 0:
# a new image saved
img = DBImage() # - no: create it
if rot and (rot <> u'0'): # is a rotation requested?
angle = int(rot) * 90
raw_file = images.rotate(raw_file, angle)
# exif = raw_file.get_original_metadata()
img.picture = db.Blob(raw_file)
img.make_thumb()
img.owner = request_handler.user_id
img.put()
logging.debug('update_photo Ins:',img.key.id())
if it.photo: # the item has an image already?
logging.debug( 'update_photo Del:',it.photo.id())
it.photo.delete()
else:
# no new image - rotate an existing image?
img = None # no image supplied
if rot and (rot != u'0'): # is a rotation requested?
old_img = it.photo
if old_img and old_img.picture:
# if so, does the item have a pic already?
angle = int(rot) * 90 # rotate & save in place
rotated_pic = images.rotate(old_img.picture, angle)
old_img.picture = db.Blob(rotated_pic)
old_img.thumb = None
old_img.put()
except Exception:
logging.exception("newOrUpdateItem Image Resize: ", exc_info=True)
img = None
return img
def update_votes(item, request_handler, user_id):
"""
save the vote for an item
:param item: {Item}
:param request_handler: {BaseHandler} for the request
:param user_id: {int}
"""
try:
old_votes = Vote.query(Vote.voter == user_id, Vote.item == item.key)
for v in old_votes:
v.key.delete()
vote = Vote()
vote.item = item.key
vote.voter = user_id
vote.comment = unicode(request_handler.request.get('myComment'))
vote.meal_kind = int(request_handler.request.get('kind'))
vote.place_style= int(request_handler.request.get('style'))
vote.cuisine = Category.get_by_id(request_handler.request.get('cuisine')).key
vote_stars = int(request_handler.request.get("voteScore"))
vote.stars = vote_stars
if vote_stars == 0:
vote_untried= bool(request_handler.request.get("voteUntried"))
else:
vote_untried = False
vote.untried = vote_untried
vote.put()
ndb_models.mark_vote_as_updated(str(vote.key.id()), user_id)
logging.info ('update_votes for %s "%s"=%d'%
(item.place_name,vote.comment,vote.stars))
except Exception, ex:
logging_ext.error("newOrUpdateItem votes exception", exc_info=True)
raise
def update_item_internal(self, user_id, allow_update=True):
def update_field(field_name, value):
# so we can log edits
old_val = getProp(it,field_name)
if old_val != value:
setattr(it,field_name,value)
changed[field_name]="%s->%s"%(old_val,value)
# is it an edit or a new?
it = Item.get_unique_place(self.request, allow_update)
if not it:
# it will be None if it exists and not allow_update
return None
img = update_photo(it, self)
# it.place_name = self.request.get('new-title') set in get_unique_place
changed = {}
update_field ('address', self.request.get('address'))
it.owner = user_id
if img:
it.photo = img
else:
if not it.photo or not it.website:
#TODO: make this async: load one from google
detail = geo.getPlaceDetailFromGoogle(it)
if not it.photo:
img = DBImage()
remoteURL = detail['photo']
if remoteURL:
thumb_url=None
try:
main_url = remoteURL % 250
data = urllib2.urlopen(main_url)
img.picture = db.Blob(data.read())
img.remoteURL = None
thumb_url = remoteURL % 65
thumb_data = urllib2.urlopen(thumb_url)
img.thumb = db.Blob(thumb_data.read())
img.put()
it.photo = img.key
except:
if thumb_url:
logging_ext.error("update_item_internal: remote url ["+str(thumb_url)+"] Exception", exc_info=True)
else:
logging_ext.error("update_item_internal: remote url Exception", exc_info=True)
it.photo = None
if 'telephone' in detail and detail['telephone'] != None:
it.telephone = detail['telephone']
if 'website' in detail and detail['website']:
it.website = detail['website']
if not it.telephone:
it.telephone = self.request.get('telephone')
if not it.website:
it.website = self.request.get('website')
# category
posted_cat = self.request.get("cuisine")
try:
cat_key = ndb.Key(Category,posted_cat)
except:
cat_key = None
update_field('category', cat_key)
if "place_name" in self.request.params:
update_field('place_name', self.request.params['place_name'])
it.put() # so the key is set
# refresh cache
update_votes(it, self, user_id)
# todo: why?
logging.info("update_item_internal for "+it.place_name+": "+str(changed))
return it
class UpdateItemFromAnotherAppAPI(BaseHandler):
def post(self):
#https://cloud.google.com/appengine/docs/python/
# appidentity/#Python_Asserting_identity_to_other_App_Engine_apps
logging.debug("UpdateItemFromAnotherAppAPI")
#TODO: Security
#if app_identity.get_application_id() != settings.API_TARGET_APP_ID:
# logging.debug("UpdateItemFromAnotherAppAPI 403: %s != %s"%\
# (app_identity.get_application_id(),settings.API_TARGET_APP_ID))
# self.abort(403)
#app_id = self.request.headers.get('X-Appengine-Inbound-Appid', None)
#logging.info('UpdateItemFromAnotherAppAPI: from app %s'%app_id)
#if app_id in settings.ALLOWED_APP_IDS:
if True:
seed_user = None
for u in User.query():
if 'pegah' in u.auth_ids:
seed_user = u.key.id()
break
if seed_user:
logging.debug("UpdateItemFromAnotherAppAPI user:"+str(seed_user))
params = ""
for k in self.request.params:
params += '"%s": "%s"'%(k, self.request.params[k])
logging.debug("UpdateItemFromAnotherAppAPI params: "+params)
if update_item_internal(self, seed_user, allow_update=False):
logging.debug("UpdateItemFromAnotherAppAPI Done ")
else:
logging.debug("UpdateItemFromAnotherAppAPI Existed ")
self.response.out.write("OK")
else:
logging_ext.error("UpdateItemFromAnotherAppAPI - couldn't get seed user",
exc_info=True)
self.abort(500)
else:
logging.debug("UpdateItemFromAnotherAppAPI not allowed")
self.abort(403)
def check_good_server_version(request):
good_version = False
if 'version' in request.params:
version = float(request.params['version'])
min_version = ndb_models.Config.min_server_version_allowed()
if version >= float(min_version):
return True
if not good_version:
logging_ext.error("check_good_server_version BAD VERSION")
return False
class newOrUpdateItem(BaseHandler):
@user_required
def post(self):
try:
if not check_good_server_version(self.request):
self.response.out.write("BAD VERSION")
return
it = update_item_internal(self, self.user_id)
logging.info('newOrUpdateItem %s by %s'%(it.place_name, self.user_id))
ndb_models.mark_place_as_updated(str(it.key.id()),str(self.user_id))
vote = Vote.query(Vote.voter == self.user_id, Vote.item == it.key).get()
res = {'place':it.get_json(),
'vote': vote.to_json()}
json.dump(res, self.response.out)
except:
logging_ext.error('newOrUpdateItem', exc_info=True)
class UpdateVote(BaseHandler):
@user_required
def post(self):
if not check_good_server_version(self.request):
self.response.out.write("BAD VERSION")
return
id = self.request.get('key')
it = ndb.Key(Item, int(id)).get()
if it:
update_votes(it, self, self.user_id)
# mark user as dirty
self.response.out.write('OK')
logging.debug("UpdateVote OK")
return
logging_ext.error("UpdateVote 404 for %s"%id)
self.abort(404)
class loadTestData(BaseHandler):
def get(self):
results = None
try:
section = self.request.get("section")
results = load_data(section=section)
self.render_template("dataLoader.html", {"results": results})
except Exception, E:
self.render_template(
"dataLoader.html", {"results": results, "message": E})
class wipeAndLoadTestData(BaseHandler):
def get(self):
results = None
try:
results = load_data(wipe=True)
self.render_template(
"dataLoader.html", {"results": results})
except Exception, E:
self.render_template(
"dataLoader.html", {"results": results, "message": E})
class loadPlace(BaseHandler):
@user_required
def get(self):
self.render_template("item.html")
class geoLookup(BaseHandler):
def get(self):
self.render_template("geoLookup.html", {"mobile": False})
def post(self):
address = self.request.get('address')
pos = geo.geoCodeAddress(address)
if pos:
params = {
"lat": pos['lat'],
"lng": pos['lng'],
"mobile": False,
"categories": ["deprecated"],
}
self.render_template("newOrUpdateItem.html", params)
else:
self.display_message("Unable to lookup address")
class getItem_ajax(BaseHandler):
@user_required
def get(self, id):
"""
get an Item
:param id: int
:return: string json
"""
try:
it = Item.get_by_id(int(id))
res = {"place_name": it.place_name,
"address": it.address,
"cuisineName": it.category.title,
"lat": str(it.lat),
"lng": str(it.lng),
"id": it.key.id()
}
if it.photo:
res["img"] = it.key.id()
if it.owner == self.user_id:
res["mine"] = True
res["descr"], res["stars"], res["untried"] = it.vote_from(it.owner)
else:
res["mine"] = False
res["descr"], res["stars"], res["untried"] = it.vote_from(self.user_id)
json.dump(res, self.response.out)
except Exception:
logging_ext.error("getItem_ajax Exception", exc_info=True)
self.error(500)
class getItemVotes_ajax(BaseHandler):
@user_required
def get(self, id):
"""
votes for an item
:param id: int