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")
def post(self): # should run at most 1/s due to entity group limit """ Task Worker to mark place as updated Params: placeId: string place Id userId: string """ try: logging_ext.log_to_console("AddPlaceChangesWorker") place_id_str = self.request.get('placeId') user_id_str = self.request.get('userId') place_entries = Change.\ query(Change.kind==Change.CHANGE_PLACE, Change.recordId == place_id_str) now = datetime.now() for p in place_entries: if p.when < now: p.when = now p.put() user = User.get_by_id(int(user_id_str)) friends_key_list = user.get_friends_key_list() for user_key in friends_key_list: p = Change.\ query( Change.kind == Change.CHANGE_PLACE, Change.subscriberId == str(user_key.id()), Change.recordId == place_id_str).get() if not p: p = Change() p.kind = Change.CHANGE_PLACE p.subscriberId = str(user_key.id()) p.placeId = place_id_str p.when = now p.put() except: logging_ext.error('** AddPlaceChangesWorker', exc_info=True)
def load_one_user(user_number): usr = Users[user_number] user_name = usr[0] this_user = User.get_by_auth_id(user_name) if not this_user: email = usr[1] name = usr[2] last_name = usr[3] password = usr[4] unique_properties = ['email_address'] this_user = User.create_user(user_name, unique_properties, email_address=email, name=name, password_raw=password, last_name=last_name, verified=False) else: this_user.set_password(usr[4]) this_user.profile().is_admin = True this_user.profile().put() return this_user
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
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)
def post(self): if is_administrator(): try: logging.info("SyncToProd") seed_user = None for u in User.query(): if 'pegah' in u.auth_ids: seed_user = u.key break if seed_user: logging.info("SyncToProd seed user") url = 'https://rayv-app.appspot.com/admin/put_place_api' place_list = json.loads(self.request.params['list']) for place in place_list: it = Item.get_by_id(place) logging.info("SyncToProd sending " + it.place_name) form_fields = place.id_to_json() vote = Vote.query(Vote.voter == seed_user, Vote.item == it.key).get() if vote: form_fields['myComment'] = vote.comment form_fields['voteScore'] = vote.vote else: form_fields['voteScore'] = VoteValue.VOTE_LIKED form_fields['myComment'] = "" form_data = urllib.urlencode(form_fields) result = urlfetch.fetch( url=url, payload=form_data, method=urlfetch.POST, headers={ 'Content-Type': 'application/x-www-form-urlencoded' }) else: self.response.out.write('No Seed User') except Exception: logging.error('admin.SyncToProd', exc_info=True) logging.info("Sync Done to Prod") self.response.out.write("OK")
def post(self): username = self.request.get('username').lower() user = User.query( google.appengine.ext.ndb.GenericProperty('email_address') == username).get() # #user = self.user_model.get_by_auth_id(username) if not user: logging.info('Could not find any userId entry for username %s', username) else: try: if user.blocked: logging.info('ForgotPasswordHandler: Blocked user ' + username) self.abort(403) except: pass user_id = user.get_id() token = self.user_model.create_signup_token(user_id) verification_url = self.uri_for('verification', type='p', user_id=user_id, signup_token=token, _full=True) msg = 'Please visit this link to reset your password\n%s' % verification_url mail_wrapper.send_mail(sender=settings.config['system_email'], to=user.email_address, subject="Password Reset", body=msg) logging.info("Reset email sent to %s" % user.email_address) params = { 'message2': "If that account exists, an email was sent. " "Please read it for instructions" } self.render_template('login.html', params)
def getFullUserRecord(self, user, now=None): try: places_id2json = {} vote_list = [] if settings.config['all_are_friends']: q = User.gql('') else: # start with me q = [user] # then get my friends for f in user.get_friends_key_list(): q.append(f.get()) place_json = None place_id = None for u in q: if u: user_votes = models.Vote.query( models.Vote.voter == u.key.integer_id()).fetch() for vote in user_votes: try: place_id = vote.item.id() vote_list.append(vote.get_json()) if not place_id in places_id2json: place_json = models.Item.id_to_json(place_id) if "cuisineName" in place_json: places_id2json[place_id] = place_json except Exception, e: if place_json: logging_ext.error( "** getFullUserRecord Exception 1 %s" % place_json['place_name'], exc_info=True) else: logging_ext.error( "** getFullUserRecord Exception %s" % place_id, exc_info=True) return vote_list, places_id2json
def post(self): # should run at most 1/s due to entity group limit """ Task Worker to mark votes as updated - deletes all entries for the vote - adds a new one for all friends of voter Params: voteId: string userId: string """ try: logging_ext.log_to_console('AddVoteChangesWorker IN') vote_id_str = self.request.get('voteId') if not vote_id_str: logging_ext.error("** AddVoteChangesWorker: no vote id") return user_id_str = self.request.get('userId') if not user_id_str: logging_ext.error("** AddVoteChangesWorker: no user_id") return user_id = int(user_id_str) me = User.get_by_id(user_id) time = self.request.get('time') old_votes = Change.\ query(Change.kind == Change.CHANGE_VOTE, Change.recordId == vote_id_str).\ fetch(keys_only=True) ndb.delete_multi(old_votes) friends_list = me.get_friends_key_list() for u in friends_list: change = Change() change.kind = Change.CHANGE_VOTE change.recordId = vote_id_str change.subscriberId = str(u.id()) change.when = datetime.strptime( time, views.config['DATETIME_FORMAT']) change.put() except Exception: logging_ext.error("** AddVoteChangesWorker", exc_info=True)
def post(self): if not is_administrator(): self.abort(404) pwd = self.request.get("pwd") pwd2 = self.request.get("pwd2") if pwd != pwd2: self.render_template("admin-password.html", {'message': 'Passwords don\'t match'}) return try: email = self.request.get('email') user = User.get_by_email(email) if not user: self.render_template("admin-password.html", {'message': 'User not found'}) return user.set_password(pwd) user.put() if config['log_passwords']: self.response.out.write("Password has been reset to %s" % pwd) else: self.response.out.write("Password has been reset ") except Exception, e: self.response.out.write(e)
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:"******"" 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 add_blocked_to_user(self): users = User.query() for u in users: if not u.blocked: u.blocked = False u.put()
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=views.json_serial) return if my_id: try: # logged in #check if the client version is allowed good_version = False if 'version' in self.request.params: version = float(self.request.params['version']) min_version = Config.min_server_version_allowed() if version >= float(min_version): good_version = True if not good_version: logging_ext.error( "getUserRecordFastViaWorkers BAD VERSION") self.response.out.write("BAD_VERSION") return #good client version if we got here result = { "id": my_id, "admin": self.user.profile().is_admin, "version": settings.config["version"], "min_version": settings.config['min_version'] } since = None now = datetime.now() if 'since' in self.request.params: try: # move since back in time to allow for error since = datetime.strptime( self.request.params['since'], views.config['DATETIME_FORMAT']) - \ views.config['TIMING_DELTA'] vote_list, place_id2json = self.getIncrement( my_id, since) except OverflowError, ex: logging_ext.error( "** getFullUserRecord Time error with %s" % since, exc_info=True) #full update vote_list, place_id2json = self.getFullUserRecord( self.user) else: #full update vote_list, place_id2json = self.getFullUserRecord( self.user) friends_list = [] if views.config['all_are_friends']: q = User.gql('') for u in q: user_str = { "id": u.get_id(), # todo is it first_name? 'name': u.screen_name } friends_list.append(user_str) else: friends = self.user.get_friends_key_list() for f in friends: friend = f.get() try: user_str = { "id": f.id(), # todo is it first_name? 'name': friend.screen_name } except: logging.error("getFullUserRecord Friends error") friends_list.append(user_str) sentInvites = models.InviteInternal.query( models.InviteInternal.inviter == my_id) recdInvites = models.InviteInternal.query( models.InviteInternal.invitee == my_id) sent = [] for i in sentInvites: sent.append(i.to_json()) recd = [] for i in recdInvites: recd.append(i.to_json()) result["sentInvites"] = sent result["receivedInvites"] = recd result['votes'] = vote_list result["places"] = place_id2json result["friendsData"] = friends_list json_str = json.dumps(result, default=views.json_serial) try: since_str = str(since) if since else "" logging.debug( "GetFullUserRecord for %s %s P:%d, V:%d, F:%d" % (self.user.screen_name, since_str, len(place_id2json), len(vote_list), len(friends_list))) except: pass try: #logging logging.debug("getUserRecordFastViaWorkers done ") except: pass self.response.out.write(json_str) #profile_out("getFullUserRecord") return
def load_data(wipe=False, section=None, Max=None): if not settings.running_on_test_server(): return "Forbidden" result_strings = [] if geo.geoCodeAddress("1 Crouch Hill, London"): # if we can geocode, we will - no fake fakeGeoCoder = None else: # if we cant geocode, use the fake one fakeGeoCoder = fakeGeoCode if section == "addresses": return add_addresses_to_db() else: if wipe: # wipe_table("User") wipe_table("Category") wipe_table("Item") wipe_table("Vote") print "wiped" if not section or section == 'user': for idx, usr in enumerate(Users): if Max: if idx >= Max: break user_name = usr[0] this_user = User.get_by_auth_id(user_name) if not this_user: email = usr[1] name = usr[2] last_name = usr[3] password = usr[4] unique_properties = ['email_address'] this_user = User.create_user(user_name, unique_properties, email_address=email, name=name, password_raw=password, last_name=last_name, verified=False) if not this_user[0]: # user_data is a tuple result_strings.append("ERROR - User: "******"User: "******"User exists: " + usr[0]) a_sample_user = User.get_by_auth_id( Users[0][0]) # used for the owner of the records print "users ok" if not section or section == "category": for idx, cat in enumerate(Categories): if Max: if idx >= Max: break if models.Category.get_by_id(cat): result_strings.append("Category exists: " + cat) else: new_cat = models.Category(id=cat) new_cat.title = cat new_cat.put() result_strings.append("Created: " + cat) print "category ok" if not section or section == "item": home = geo.LatLng(lat=51.57, lng=-0.13) for idx, item in enumerate(items_data_list): if Max: if idx >= Max: break it = models.Item.query(models.Item.place_name == item[0]).get() if it: result_strings.append("Item exists: " + item[0]) it.category = models.Category.get_by_id(item[2]).key it.save() else: new_it = models.Item() new_it.category = models.Category.get_by_id(item[2]).key new_it.place_name = item[0] lat_long = fakeGeoCode( ) if fakeGeoCoder else geo.geoCodeAddress(item[1]) new_it.lat = lat_long['lat'] new_it.lng = lat_long['lng'] new_it.address = item[1] new_it.owner = a_sample_user.key.id() # new_it.descr = "blah" new_it.geo_hash = geohash.encode(new_it.lat, new_it.lng) img = models.DBImage() detail = geo.getPlaceDetailFromGoogle(new_it) remoteURL = detail['photo'] if remoteURL: 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() new_it.photo = img.key new_it.telephone = detail['telephone'] new_it.put() result_strings.append('Item: ' + item[0]) print "items" # votes items = models.Item.query() i = 0 for idx, vote_item in enumerate(items): if Max: if idx >= Max: break vote = models.Vote() vote_score = randint(0, 5) if vote_score == 0: vote.stars = 0 vote.untried = True else: vote.stars = vote_score vote.untried = False vote.comment = "blah v" + str(i) vote.voter = a_sample_user.key.integer_id() vote.item = vote_item.key vote.cuisine = vote_item.category vote.put() i += 1 result_strings.append("Votes") print "votes" if not section or section == 'friends': for idx, pair in enumerate(FriendsList): if Max: if idx >= Max: break left = User.get_by_auth_id(pair[0]) right = User.get_by_auth_id(pair[1]) left_prof = left.profile() models.Friends.addFriends(left.get_id(), right.get_id()) result_strings.append("Friends %s - %s" % (pair[0], pair[1])) print "friends" return result_strings
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)
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 get(self): user_obj = User().get_by_id(self.user_id) json.dump({'screen_name': user_obj.screen_name}, self.response.out)
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)