def cosine_similarity(city_id_1, city_id_2): sin = lambda theta: math.sin(theta * math.pi / 180.0) cos = lambda theta: math.cos(theta * math.pi / 180.0) city_1 = mod_city.get_city_instance_by_id(city_id_1) city_2 = mod_city.get_city_instance_by_id(city_id_2) return sin(city_1.latitude) * sin(city_2.latitude) \ + cos(city_1.latitude) * cos(city_2.latitude) \ * cos(city_2.longitude - city_1.longitude)
def __getitem__(self, item): try: return getattr(self, f'get_{item}')() except AttributeError: if item == 'resident_city': return dict(mod_city.get_city_instance_by_id(city_id=self.get_resident_city_id())) else: raise
def recommend_city_list_by_travel_group(request_data): user = LOGGED_IN_USERS[request_data['user_id']] city_list = mod_rcmd.recommend_city_list_by_travel_group(user=user, travel_group_id=request_data['travel_group_id']) response = { 'count': len(city_list), 'city_list': [dict(mod_city.get_city_instance_by_id(city_id=city_id)) for city_id in city_list] } return response
def new_user(cls, email, pswd_hash, user_name, gender, resident_city_id): if db_user.User.objects.filter(email=email).exists(): raise UserAlreadyExistsException(f'User (Email={email}) already exists, try to login.') resident_city = mod_city.get_city_instance_by_id(city_id=resident_city_id) user = db_user.User.objects.create(email=email, pswd_hash=pswd_hash) user_info = db_user.UserInfo.objects.create(user_id=user, user_name=user_name, gender=gender, resident_city_id=resident_city, comment='') return cls(email=email, pswd_hash=pswd_hash)
def new_travel_info(cls, user_id, travel_group_id, city_id, date_start, date_end, visibility, travel_note): visibility = check_visibility(visibility=visibility) if date_start > date_end: raise DateStartLaterThanDateEndError( 'The date_start should be earlier than date_end.') city = mod_city.get_city_instance_by_id(city_id=city_id) travel = db_travel.Travel.objects.create(city_id=city, date_start=date_start, date_end=date_end, visibility=visibility, travel_note=travel_note) travel_group = get_travel_group_instance_by_id( travel_group_id=travel_group_id) db_travel.TravelGrouping.objects.create(travel_id=travel, travel_group_id=travel_group) return cls(user_id=user_id, travel_id=travel.travel_id)
def set_resident_city_id(self, city_id): city = mod_city.get_city_instance_by_id(city_id=city_id) self.user_info_dbobj.resident_city_id = city self.user_info_dbobj.save()
def _send_msg_to_company(self, msg_type, content='', company_list=(), modify_term=''): # 仅通知用户将来的关联行程 travel_info = self.get_travel_info() if datetime.date.today().isoformat() > travel_info.get_date_end(): if msg_type != db_msg.TravelAssociation.INVITE: return if len(company_list) > 0: target_list = company_list else: target_list = self.get_company_list() if len(target_list) == 0: return self_user_dbobj = get_user_instance_by_id(user_id=self.watcher_user_id) self_user_info_dbobj = get_user_info_instance_by_id( user_id=self.watcher_user_id) self_user_name = self_user_info_dbobj.user_name travel_dbobj = get_travel_instance_by_id( travel_id=self.get_travel_id()) city = mod_city.get_city_instance_by_id( city_id=travel_info.get_city_id()) city_name = city.city_name if msg_type == db_msg.TravelAssociation.LEAVE: for company_user_id in target_list: other_user_dbobj = get_user_instance_by_id( user_id=company_user_id) other_user_info_dbobj = get_user_info_instance_by_id( user_id=company_user_id) other_user_name = other_user_info_dbobj.user_name msg_content = f'您的好友 {other_user_name} 离开了前往 {city_name} 的行迹。' db_msg.TravelAssociation.objects.create( user_id=self_user_dbobj, friend_user_id=other_user_dbobj, travel_id=travel_dbobj, msg_type=msg_type, msg_content=msg_content) return elif msg_type == db_msg.TravelAssociation.DELETE: msg_content = f'您的好友 {self_user_name} 删除了前往 {city_name} 的行迹。' travel_dbobj = None elif msg_type == db_msg.TravelAssociation.ADD: msg_content = f'您的好友 {self_user_name} 添加了 {content} 一并前往 {city_name}。' elif msg_type == db_msg.TravelAssociation.REMOVE: msg_content = f'您的好友 {self_user_name} 将你从前往 {city_name} 的行迹中删除。' elif msg_type == db_msg.TravelAssociation.INVITE: msg_content = f'您的好友 {self_user_name} 邀请你一并前往 {city_name}。' elif msg_type == db_msg.TravelAssociation.MODIFY: if modify_term == 'city': msg_content = f'您的好友 {self_user_name} 将行迹目的地由 {content} 改为 {city_name}。' elif modify_term == 'date_start': msg_content = f'您的好友 {self_user_name} 将前往 {city_name} 的行迹起始日期改为 {content}。' elif modify_term == 'date_end': msg_content = f'您的好友 {self_user_name} 将前往 {city_name} 的行迹结束日期改为 {content}。' elif modify_term == 'general': msg_content = f'您的好友 {self_user_name} 更新了前往 {city_name} 的行迹信息。' else: raise MsgTypeError else: raise MsgTypeError for company_user_id in target_list: other_user_dbobj = get_user_instance_by_id(user_id=company_user_id) db_msg.TravelAssociation.objects.create( user_id=other_user_dbobj, friend_user_id=self_user_dbobj, travel_id=travel_dbobj, msg_type=msg_type, msg_content=msg_content)
def set_city_id(self, city_id): self.check_permission() city = mod_city.get_city_instance_by_id(city_id=city_id) self.travel_info_dbobj.city_id = city self.travel_info_dbobj.save()
def city_id_to_city(request_data): city = mod_city.get_city_instance_by_id(city_id=request_data['city_id']) response = dict(city) return response