def recommend_travel_list_by_travel(user, travel_id, amount=5, max_dist=1200): # 遍历好友的旅行记录 # 根据城市和该travel的距离、时间间隔排序进行推荐 # 总数不超过amount user_id = user.get_user_id() travel = mod_travel.Travel(user_id, travel_id) # travel 是Travel类实例对象 travel_info = travel.get_travel_info() my_city = travel_info.get_city_id() friend_list = user.get_friend_list() # friend id list other_travel_group_list = [] for fr in friend_list: for tg_id in user.get_others_travel_group_list(fr): travel_group = mod_travel.TravelGroup(user_id=user_id, travel_group_id=tg_id) # rep_time = mod_travel.Travel(user_id=user_id, travel_id=travel_list[0]).get_travel_info().get_date_start() for travel_id in travel_group.get_travel_list(): travel = mod_travel.Travel(user_id=user_id, travel_id=travel_id) company_list = travel.get_company_list() if user_id not in company_list: rep_time = travel.get_travel_info().get_date_start() city = travel.get_travel_info().get_city_id() dist = get_city_distance(my_city, city) if (city != my_city) and (dist < max_dist): other_travel_group_list.append({ "travel_id": travel_id, "time_delta_days": get_time_delta_days(rep_time), "city_distance": dist }) other_travel_group_list.sort(key=lambda x: ((int(x['city_distance'] / 50)) ** 2 + (int(x['time_delta_days'] / 1)) ** 2)) return generate_rec_list(other_travel_group_list, amount, key="travel_id")
def recommend_city_list_by_travel(user, travel_id, amount=3, max_dist=1200): # 遍历好友的旅行记录 # 根据城市和该travel的距离排序进行推荐 # 不包括本城市 # 总数不超过amount user_id = user.get_user_id() travel = mod_travel.Travel(user_id, travel_id) # travel 是Travel类实例对象 travel_info = travel.get_travel_info() my_city = travel_info.get_city_id() friend_list = user.get_friend_list() # friend id list other_travel_group_list = [] for fr in friend_list: for tg_id in user.get_others_travel_group_list(fr): travel_group = mod_travel.TravelGroup(user_id=user_id, travel_group_id=tg_id) for travel_id in travel_group.get_travel_list(): travel = mod_travel.Travel(user_id=user_id, travel_id=travel_id) company_list = travel.get_company_list() if user_id not in company_list: city = travel.get_travel_info().get_city_id() dist = get_city_distance(my_city, city) if (city != my_city) and (dist < max_dist): other_travel_group_list.append({ 'city_id': city, 'city_distance': dist }) other_travel_group_list.sort(key=lambda x: x['city_distance']) return generate_rec_list(other_travel_group_list, amount, key="city_id")
def recommend_travel_group_list(user, amount=10): # 遍历好友的旅行记录 # 根据时间和今天的距离排序进行推荐 # 总数不超过amount user_id = user.get_user_id() friend_list = user.get_friend_list() other_travel_group_list = [] for fr in friend_list: for tg_id in user.get_others_travel_group_list(fr): travel_group = mod_travel.TravelGroup(user_id=user_id, travel_group_id=tg_id) travel_list = travel_group.get_travel_list() if len(travel_list) == 0: continue travel = mod_travel.Travel(user_id=user_id, travel_id=travel_list[0]) company_list = travel.get_company_list() # 得到朋友travel的company,自己不允许出现在里面 if user_id not in company_list: rep_time = travel.get_travel_info().get_date_start() # represent time in isoformat # e.g. "1989-06-04" other_travel_group_list.append({ 'travel_group_id': tg_id, 'time_delta_days': get_time_delta_days(rep_time) }) other_travel_group_list.sort(key=lambda x: x['time_delta_days']) return generate_rec_list(other_travel_group_list, amount, key="travel_group_id")
def join_friends_travel(request_data): travel = mod_travel.Travel(user_id=request_data['friend_user_id'], travel_id=request_data['friend_travel_id']) travel.add_company(company_user_id=request_data['user_id']) response = {} return response
def invite_travel_company(request_data): travel = mod_travel.Travel(user_id=request_data['user_id'], travel_id=request_data['travel_id']) travel.invite_company(company_user_id=request_data['friend_user_id']) response = {} return response
def leave_friends_travel(request_data): travel = mod_travel.Travel(user_id=request_data['friend_user_id'], travel_id=request_data['friend_travel_id']) travel.remove_company(company_user_id=request_data['user_id'], actively_leave=True) response = {} return response
def get_travel_company_list(request_data): travel = mod_travel.Travel(user_id=request_data['user_id'], travel_id=request_data['travel_id']) company_list = travel.get_company_list() response = { 'count': len(company_list), 'company_list': company_list } return response
def copy_travel(request_data): user_id = request_data['user_id'] src_travel = mod_travel.Travel(user_id=user_id, travel_id=request_data['source_travel_id']) src_travel_info = src_travel.get_travel_info() src_travel_group_dbobj = mod_travel.get_travel_group_instance_by_travel_id(travel_id=src_travel.get_travel_id()) src_travel_owner_user_dbobj = mod_travel.get_travel_group_owner_user_instance( travel_group_id=src_travel_group_dbobj.travel_group_id) travel_group = mod_travel.TravelGroup(user_id=user_id, travel_group_id=request_data['travel_group_id']) new_travel = travel_group.add_travel(city_id=src_travel_info.get_city_id(), date_start=isoformat_to_date(src_travel_info.get_date_start()), date_end=isoformat_to_date(src_travel_info.get_date_end()), travel_note=src_travel_info.get_travel_note(), visibility=src_travel_info.get_visibility()) if user_id in src_travel.get_company_list() and request_data['add_association']: new_travel.add_company(company_user_id=src_travel_owner_user_dbobj.user_id) response = { 'new_travel_id': new_travel.get_travel_id() } return response