def get(self): cur_usr = self.request.get('client_ID') target_usr = self.request.get('target_person') match_result = [] # retrieve cur_usr preference cur_usr_info = Person.Person.query( ancestor=management.person_key(str(cur_usr))).fetch(1)[0] # retrieve target usr preference target_usr_info = Person.Person.query( ancestor=management.person_key(str(target_usr))).fetch(1)[0] if cur_usr_info.roommate_choice == target_usr_info.roommate_choice: match_result.append(1) if cur_usr_info.smoking_choice == target_usr_info.smoking_choice: match_result.append(2) if cur_usr_info.overnight_guest_choice == target_usr_info.overnight_guest_choice: match_result.append(3) if cur_usr_info.study_habits_choice == target_usr_info.study_habits_choice: match_result.append(4) if cur_usr_info.sleep_habits_choice == target_usr_info.sleep_habits_choice: match_result.append(5) if cur_usr_info.cleanliness_choice == target_usr_info.cleanliness_choice: match_result.append(6) self.response.write(match_result)
def get(self): # get the current usr curr_usr = users.get_current_user() logout_url = users.create_logout_url('/') # get the current match list usr_ndb_info = Person.Person.query(ancestor= management.person_key(str(curr_usr))).fetch(1)[0] current_matches_names = usr_ndb_info.potential_roommate current_matches = [] for match_usr in current_matches_names: # retrive the usr info match_usr_info = Person.Person.query(ancestor= management.person_key(str(match_usr))).fetch(1)[0] # construct a dict info_dict = dict() info_dict['ID'] = match_usr_info.person_account info_dict['Name'] = match_usr_info.person_name info_dict['Gender'] = match_usr_info.person_gender info_dict['Age'] = match_usr_info.person_age info_dict['School'] = match_usr_info.person_school current_matches.append(info_dict) first_p_setting = None first_person_id = None if current_matches_names: # get the 1st match person's information, that is used as the default to show on match page first_person = current_matches_names[0] first_person_info = Person.Person.query(ancestor= management.person_key(str(first_person))).fetch(1)[0] # save first person info in a dict first_p_setting = dict() first_person_id = current_matches_names[0] first_p_setting['name'] = first_person_info.person_name first_p_setting['roommategender'] = Person.preferenceMatchEngine("pref1", str(first_person_info.roommate_choice)) first_p_setting['smoking_choice'] = Person.preferenceMatchEngine("pref2", str(first_person_info.smoking_choice)) first_p_setting['overnight_guest_choice'] = Person.preferenceMatchEngine("pref3", str(first_person_info.overnight_guest_choice)) first_p_setting['study_habits_choice'] = Person.preferenceMatchEngine("pref4", str(first_person_info.study_habits_choice)) first_p_setting['sleep_habits_choice'] = Person.preferenceMatchEngine("pref5", str(first_person_info.sleep_habits_choice)) first_p_setting['cleanliness_choice'] = Person.preferenceMatchEngine("pref6", str(first_person_info.cleanliness_choice)) template_values = { 'usr': curr_usr, 'logout_url': logout_url, 'current_matches': current_matches, 'first_person_setting': first_p_setting, 'first_person_id': first_person_id, } template = JINJA_ENVIRONMENT.get_template('/myhtml/potential_roommate.html') self.response.write(template.render(template_values))
def notifiyclient(clientID, caller, chatcontent, messageType): # set caller's new message unread to True usr_i_info = Person.Person.query( ancestor=management.person_key(clientID)).fetch(1)[0] if messageType == "chatroom": j_messagefrom = { "message_type": "chatroom", "new_message_from": caller, "chatcontent": chatcontent } message = json.dumps(j_messagefrom) possible_client1 = clientID + "management_page" possible_client2 = clientID + "chatroom" print("start to send messages from chat server to " + possible_client1) channel.send_message(possible_client1, message) channel.send_message(possible_client2, message) if messageType == "newmatchnotification": num_of_matches = usr_i_info.usr_notification j_num_matches = { "message_type": "newmatchnotification", "num_of_matches": num_of_matches } message = json.dumps(j_num_matches) possible_client1 = clientID + "management_page" possible_client2 = clientID + "preferencepage" print("start to send messages to " + possible_client1 + " and " + possible_client2) channel.send_message(possible_client1, message) channel.send_message(possible_client2, message)
def updateMutualMatches(curr_usr, potential_target): # retrive the current chat history list usr_ndb_info = Person.Person.query( ancestor=management.person_key(curr_usr)).fetch(1)[0] myChatHistory = usr_ndb_info.myChatHistory newTarget = True for addedtarget in myChatHistory: addedtarget_account = addedtarget.person_account if addedtarget_account == potential_target: newTarget = False break if newTarget: # create a new PersonIChat new_chat_person = Person.PersonIChat() new_chat_person.person_account = potential_target new_chat_person.person_name = chat_server.getAccountName( potential_target) new_chat_person.new_message_unread = False new_chat_person.chat_history = [] # add this PersonIChat to the usr myChatHistory.append(new_chat_person) usr_ndb_info.myChatHistory = myChatHistory usr_ndb_info.put()
def doWeMatch(usr_i_account, usr_login): usr_i_info = Person.Person.query( ancestor=management.person_key(usr_i_account)).fetch(1)[0] usr_info = Person.Person.query( ancestor=management.person_key(usr_login)).fetch(1)[0] usr_i_match_list = usr_i_info.current_matches for in_list in usr_i_match_list: if in_list == usr_login: return False ''' if usr_i_info.person_school == usr_info.person_school: return True ''' # if usr_i_info.person_age == usr_info.person_age: # return True matchScore = 0 if usr_i_info.person_school == usr_info.person_school: if usr_i_info.roommate_choice == usr_info.roommate_choice: matchScore += 1 if usr_i_info.smoking_choice == usr_info.smoking_choice: matchScore += 1 if usr_i_info.overnight_guest_choice == usr_info.overnight_guest_choice: matchScore += 1 if usr_i_info.study_habits_choice == usr_info.study_habits_choice: matchScore += 1 if usr_i_info.sleep_habits_choice == usr_info.sleep_habits_choice: matchScore += 1 if usr_i_info.musictv_choice == usr_info.musictv_choice: matchScore += 1 if usr_i_info.cleanliness_choice == usr_info.cleanliness_choice: matchScore += 1 print "match score = ", matchScore if matchScore >= matchThreshold: print('meet matchThreshold') return True # if usr_i_info.person_school == usr_info.person_school: # return True return False
def notifiyclient(clientID): # get num of notifications usr_i_info = Person.Person.query( ancestor=management.person_key(clientID)).fetch(1)[0] num_of_matches = usr_i_info.usr_notification j_num_matches = {"num_of_matches": num_of_matches} message = json.dumps(j_num_matches) possible_client1 = clientID+"management_page" possible_client2 = clientID+"preferencepage" print("start to send messages to "+possible_client1+" and "+possible_client2) channel.send_message(possible_client1, message) channel.send_message(possible_client2, message)
def post(self): # get the usr email info usr_login = users.get_current_user() body = self.request.body # parse the json j = json.loads(body) roommate_choice = str(j["roommate_choice"]) smoking_choice = str(j["smoking_choice"]) overnight_guest_choice = str(j["overnight_guest_choice"]) study_habits_choice = str(j["study_habits_choice"]) sleep_habits_choice = str(j["sleep_habits_choice"]) musictv_choice = str(j["musictv_choice"]) cleanliness_choice = str(j["cleanliness_choice"]) # Option string to integer map optionMap = { "Option1": 1, "Option2": 2, "Option3": 3, } # print out for testing print(roommate_choice) print(smoking_choice) print(overnight_guest_choice) print(study_habits_choice) print(sleep_habits_choice) print(musictv_choice) print(cleanliness_choice) # update the datebase usr_personal_info = Person.Person.query( ancestor=management.person_key(usr_login)).fetch(1)[0] usr_personal_info.roommate_choice = optionMap[roommate_choice] usr_personal_info.smoking_choice = optionMap[smoking_choice] usr_personal_info.overnight_guest_choice = optionMap[ overnight_guest_choice] usr_personal_info.study_habits_choice = optionMap[study_habits_choice] usr_personal_info.sleep_habits_choice = optionMap[sleep_habits_choice] usr_personal_info.musictv_choice = optionMap[musictv_choice] usr_personal_info.cleanliness_choice = optionMap[cleanliness_choice] usr_personal_info.put() # call match engine to compute number of match update_match_engine.update_match(str(usr_login))
def deleteYou(usr_i_account, usr_login): usr_i_info = Person.Person.query( ancestor=management.person_key(usr_i_account)).fetch(1)[0] usr_info = Person.Person.query( ancestor=management.person_key(usr_login)).fetch(1)[0] usr_i_match_list = usr_i_info.current_matches matchScore = 0 for in_list in usr_i_match_list: if in_list == usr_login: # I am in his old list and now no longer qualified if usr_i_info.person_school != usr_info.person_school: print('not the same shcool anymore') return True else: if usr_i_info.roommate_choice == usr_info.roommate_choice: matchScore += 1 if usr_i_info.smoking_choice == usr_info.smoking_choice: matchScore += 1 if usr_i_info.overnight_guest_choice == usr_info.overnight_guest_choice: matchScore += 1 if usr_i_info.study_habits_choice == usr_info.study_habits_choice: matchScore += 1 if usr_i_info.sleep_habits_choice == usr_info.sleep_habits_choice: matchScore += 1 if usr_i_info.musictv_choice == usr_info.musictv_choice: matchScore += 1 if usr_i_info.cleanliness_choice == usr_info.cleanliness_choice: matchScore += 1 print "match score = ", matchScore if matchScore < matchThreshold: print('remove from matchlist') return True return False
def get(self): curr_usr = users.get_current_user() curr_usr = str(curr_usr) target_usr = self.request.get('whoichat') # retrive his/her chat history usr_personal_info = Person.Person.query( ancestor=management.person_key(str(curr_usr))).fetch(1)[0] myChatHistory = usr_personal_info.myChatHistory for personIchat in myChatHistory: if personIchat.person_account == target_usr: personIchat.new_message_unread = False break usr_personal_info.put()
def get(self): curr_usr = users.get_current_user() curr_usr = str(curr_usr) # retrive his/her chat history usr_personal_info = Person.Person.query( ancestor=management.person_key(str(curr_usr))).fetch(1)[0] myChatHistory = usr_personal_info.myChatHistory unreadList = [] for personIchat in myChatHistory: if personIchat.new_message_unread is True: unreadList.append(str(personIchat.person_account)) self.response.write(unreadList)
def update_match(usr_login): print("Updating " + usr_login + " match list") num_NewMatch = updateMutualMatches(usr_login) usr_personal_info = Person.Person.query( ancestor=management.person_key(usr_login)).fetch(1)[0] if num_NewMatch > 0: usr_personal_info.usr_notification = num_NewMatch usr_personal_info.usr_viewed_updates = False usr_personal_info.put() # send my self notification chat_server.notifiyclient(usr_login, "", "", "newmatchnotification")
def post(self): usr_name = self.request.get('client_ID') source = self.request.get('source') # fetch usr info usr_personal_info = Person.Person.query( ancestor=management.person_key(usr_name)).fetch(1) if usr_personal_info: usr_personal_info_data = usr_personal_info[0] num_notification = usr_personal_info_data.usr_notification usr_checked_notification = usr_personal_info_data.usr_viewed_updates if num_notification > 0 and (not usr_checked_notification): print("Send "+usr_name + " "+ str(num_notification) + "notifications\n") notifiyclient(usr_name) else: print("No notification unread")
def get(self): current_usr = users.get_current_user() logout_url = users.create_logout_url('/') print(str(current_usr) + "is in chat room now") # get the chat list usr_ndb_info = Person.Person.query(ancestor= management.person_key(str(current_usr))).fetch(1)[0] chat_list = usr_ndb_info.myChatHistory template_values = { 'logout_url': logout_url, 'usr': current_usr, 'chat_list': chat_list, } template = JINJA_ENVIRONMENT.get_template('/myhtml/chatroom.html') self.response.write(template.render(template_values))
def post(self): body = self.request.body cur_usr = users.get_current_user(); cur_usr = str(cur_usr) # parse the json j = json.loads(body) potentialpersons = j["potentialPerson"] # get the current match list usr_ndb_info = Person.Person.query(ancestor= management.person_key(str(cur_usr))).fetch(1)[0] potential_roommates = usr_ndb_info.potential_roommate for person in potentialpersons: potential_roommates.remove(person) usr_ndb_info.potential_roommate = potential_roommates usr_ndb_info.put()
def get(self): curr_usr = users.get_current_user() curr_usr = str(curr_usr) # retrive his/her chat history usr_personal_info = Person.Person.query( ancestor=management.person_key(str(curr_usr))).fetch(1) hasUnread = False if usr_personal_info: person_info = usr_personal_info[0] myChatHistory = person_info.myChatHistory for personIchat in myChatHistory: if personIchat.new_message_unread is True: hasUnread = True break self.response.write(hasUnread)
def get(self): curr_usr = users.get_current_user() target_usr = self.request.get('client_ID') print("Retriving " + str(curr_usr) + "'s dialog history with " + target_usr) usr_personal_info = Person.Person.query( ancestor=management.person_key(str(curr_usr))).fetch(1)[0] myChatHistory = usr_personal_info.myChatHistory chatHistory = [] for personIchat in myChatHistory: if personIchat.person_account == target_usr: # retrive the chat dialog history a JSONproperty Array for chatEntry in personIchat.chat_history: chatHistory.append(chatEntry) # set the new_message_unread to False personIchat.new_message_unread = False break usr_personal_info.put() self.response.write(chatHistory)
def get(self): target_usr = self.request.get('target_person') # retrieve target usr preference target_usr_info = Person.Person.query( ancestor=management.person_key(str(target_usr))).fetch(1)[0] target_result = [] pref1_op = str(target_usr_info.roommate_choice) pref2_op = str(target_usr_info.smoking_choice) pref3_op = str(target_usr_info.overnight_guest_choice) pref4_op = str(target_usr_info.study_habits_choice) pref5_op = str(target_usr_info.sleep_habits_choice) pref6_op = str(target_usr_info.cleanliness_choice) target_result.append(Person.preferenceMatchEngine("pref1", pref1_op)) target_result.append(Person.preferenceMatchEngine("pref2", pref2_op)) target_result.append(Person.preferenceMatchEngine("pref3", pref3_op)) target_result.append(Person.preferenceMatchEngine("pref4", pref4_op)) target_result.append(Person.preferenceMatchEngine("pref5", pref5_op)) target_result.append(Person.preferenceMatchEngine("pref6", pref6_op)) self.response.write(target_result)
def post(self): caller = self.request.get('client_ID') chatcontent = self.request.get('chatcontent') receiver = self.request.get('receiverID') print(caller + " is sending following message to " + receiver) print(chatcontent) # ========================================================================================= # update the backend server # PART 1 update caller database usr_personal_info = Person.Person.query( ancestor=management.person_key(caller)).fetch(1)[0] chat_history = usr_personal_info.myChatHistory # go over the chat history to find the receiver if any new_receiver = True j_new_message = {"myMessage": True, "content": chatcontent} message = json.dumps(j_new_message) for chat_record in chat_history: chat_person = chat_record.person_account # not a new receiver if chat_person == receiver: new_receiver = False chat_record.new_message_unread = False # append the message print("add to a existing chat record") if chat_record.chat_history: chat_record.chat_history.append(message) else: chat_record.chat_history = [message] break if new_receiver: # create a new chat record print("add a new chat record") new_chat_record = Person.PersonIChat() new_chat_record.person_name = getAccountName(receiver) new_chat_record.person_account = receiver new_chat_record.new_message_unread = False new_chat_record.chat_history = [message] # add this record if usr_personal_info.myChatHistory: usr_personal_info.myChatHistory.append(new_chat_record) else: usr_personal_info.myChatHistory = [new_chat_record] usr_personal_info.put() # PART 2 update receiver database usr_personal_info = Person.Person.query( ancestor=management.person_key(receiver)).fetch(1)[0] chat_history = usr_personal_info.myChatHistory # go over the chat history to find the caller if any new_caller = True j_new_message = {"myMessage": False, "content": chatcontent} message = json.dumps(j_new_message) for chat_record in chat_history: chat_person = chat_record.person_account # not a new caller if chat_person == caller: new_caller = False chat_record.new_message_unread = True # append the message print("add to a existing chat record") chat_record.chat_history.append(message) break # a new caller if new_caller: # create a new chat record print("add a new chat record") new_chat_record = Person.PersonIChat() new_chat_record.person_name = getAccountName(caller) new_chat_record.person_account = caller new_chat_record.new_message_unread = True new_chat_record.chat_history = [message] # add this record if usr_personal_info.myChatHistory: usr_personal_info.myChatHistory.append(new_chat_record) else: usr_personal_info.myChatHistory = [new_chat_record] usr_personal_info.put() # end of backend server update # ======================================================================================================= # send notification to the receiver # Note: for now the receiver should in management page to get this notice notifiyclient(receiver, caller, chatcontent, "chatroom")
def getAccountName(usr_account): usr_personal_info = Person.Person.query( ancestor=management.person_key(usr_account)).fetch(1)[0] print("return person name " + usr_personal_info.person_name) return usr_personal_info.person_name
def updateMutualMatches(usr_login): new_match = 0 # Step1 get current usr information usr_personal_info = Person.Person.query( ancestor=management.person_key(usr_login)).fetch(1)[0] usr_current_matches = usr_personal_info.current_matches #usr_school = usr_personal_info.person_school #print("Usr_school is "+usr_school) # Step2 go over current match list, remove unmatched items according to some threshold setting for usr_i in usr_current_matches: usr_i_info = Person.Person.query( ancestor=management.person_key(usr_i)).fetch(1)[0] #usr_i_school = usr_i_info.person_school #print("Usr_i_school= "+usr_i_school+" Usr_school = "+usr_school) if deleteYou(usr_login, usr_i): usr_current_matches.remove(usr_i) ''' if usr_i_school != usr_school: usr_current_matches.remove(usr_i) ''' # Step3 go over the database to add any new matches to the list all_usrs = Person.Person.query().fetch() for usr_i in all_usrs: usr_i_account = usr_i.person_account if usr_i_account != str(usr_login): # see if it is not in the current match list in_list_already = find_in_list(usr_current_matches, usr_i_account) # a POTENTIAL candidate if not in_list_already: usr_i_info = Person.Person.query( ancestor=management.person_key(usr_i_account)).fetch(1)[0] #usr_i_school = usr_i_info.person_school #if usr_i_school == usr_school: if doWeMatch(usr_i_account, str(usr_login)): print("Going to append " + usr_i_account + " to " + (str(usr_login))) usr_current_matches.append(usr_i_account) # try to test and add this potential new match to chat list update_chattarget_engine.updateMutualMatches( usr_login, usr_i_account) new_match = new_match + 1 usr_i_info.put() # Step4 go over other accounts to see if current usr can be a candidate or should be removed, can be combined into Step3 for concise all_usrs = Person.Person.query().fetch() for usr_i in all_usrs: usr_i_account = usr_i.person_account #print(str(usr_i_account) == str(usr_login)) if str(usr_i_account) != str(usr_login): usr_i_has_new_noti = False we_match_and_i_dont_have_you = doWeMatch(usr_i_account, str(usr_login)) # add this usr, update usr_i info, send usr_i notification if we_match_and_i_dont_have_you: usr_i_info = Person.Person.query( ancestor=management.person_key(usr_i_account)).fetch(1)[0] usr_i_match_list = usr_i_info.current_matches usr_i_notification = usr_i_info.usr_notification print("Going to append " + (str(usr_login)) + " to " + usr_i_account) usr_i_match_list.append(str(usr_login)) # try to test and add this potential new match to chat list update_chattarget_engine.updateMutualMatches( usr_i_account, usr_login) usr_i_info.usr_notification = usr_i_notification + 1 usr_i_info.usr_viewed_updates = False usr_i_has_new_noti = True should_i_delete_you = deleteYou(usr_i_account, str(usr_login)) # delete this usr if should_i_delete_you: usr_i_info = Person.Person.query( ancestor=management.person_key(usr_i_account)).fetch(1)[0] usr_i_match_list = usr_i_info.current_matches print("Going to remove " + (str(usr_login)) + " from " + usr_i_account) usr_i_match_list.remove(str(usr_login)) usr_i_info.put() if usr_i_has_new_noti: print("I will notify him") chat_server.notifiyclient(usr_i_account, "", "", "newmatchnotification") # Step5 return number of new matches usr_personal_info.put() return new_match
def post(self): # get the usr email info usr_login = users.get_current_user() print("Update "+str(usr_login)+" 's personal information\n") # The validility of these input fields are checked by JavaScript in the front end usr_account = str(usr_login) usr_name = self.request.get('name_input') usr_gender = self.request.get('gender_input') usr_age = int(self.request.get('age_input')) usr_school = self.request.get('school_input') usr_major = self.request.get('major_input') usr_email = self.request.get('email_input') usr_phone_area = self.request.get('phone_area_input') usr_phone_notarea = self.request.get('phone_notarea_input') # following print are used for testing parameter passing print(usr_account) print(usr_name) print(usr_gender) print(usr_age) print(usr_school) print(usr_major) print(usr_email) print(usr_phone_area + usr_phone_notarea) # store what is submitted into NDB store # step 1: get the handle usr_ndb_info = Person.Person.query(ancestor= management.person_key(str(usr_login))).fetch(1) # set the fields if usr_ndb_info: # update current usr infp usr_ndb_info[0].person_name = usr_name usr_ndb_info[0].person_gender = usr_gender usr_ndb_info[0].person_age = usr_age usr_ndb_info[0].person_school = usr_school usr_ndb_info[0].person_major = usr_major usr_ndb_info[0].person_email = usr_email usr_ndb_info[0].person_phone_number = usr_phone_area + usr_phone_notarea usr_ndb_info[0].personInfoSet = True usr_ndb_info[0].usr_viewed_updates = False usr_ndb_info[0].put() else: #create a ndb entry new_usr_info = Person.Person(parent= management.person_key(str(usr_login))) new_usr_info.person_account = usr_account new_usr_info.person_name = usr_name new_usr_info.person_gender = usr_gender new_usr_info.person_age = usr_age new_usr_info.person_school = usr_school new_usr_info.person_major = usr_major new_usr_info.person_email = usr_email new_usr_info.person_phone_number = usr_phone_area + usr_phone_notarea new_usr_info.personInfoSet = True new_usr_info.usr_viewed_updates = False new_usr_info.usr_notification = 0 new_usr_info.myChatHistory = [] # init with empty list new_usr_info.current_matches = [] new_usr_info.potential_roommate = [] # ================================================================================ # hard code the chat history for testing only ''' if usr_account == "*****@*****.**": # creat a PersonIChat new_chat_person = Person.PersonIChat() new_chat_person.person_account = "*****@*****.**" new_chat_person.new_message_unread = False new_chat_person.chat_history = [] # create another one new_chat_person2 = Person.PersonIChat() new_chat_person2.person_account = "*****@*****.**" new_chat_person2.new_message_unread = False new_chat_person2.chat_history = [] # add this PersonIChat to the usr chatPeopleList = new_usr_info.myChatHistory chatPeopleList.append(new_chat_person) chatPeopleList.append(new_chat_person2) new_usr_info.myChatHistory = chatPeopleList # retrieve to check print(new_usr_info.myChatHistory[0].person_account) ''' # ================================================================================= new_usr_info.put() # call match engine to compute number of match update_match_engine.update_match(str(usr_login))