def __unicode__(self): if self.user: return smart_format("{0} -> {1}", self.beacon.description, self.user.first_name) else: return smart_format("{0} -> {1}", self.beacon.description, self.contact.name)
def yelp_match_place(place): ll = "{0},{1}".format(place.latitude, place.longitude) term = place.name sort = 0 limit = 2 response = yelp_api.search_query(term=term, ll=ll, sort=sort, limit=limit) found = None idx = 0 businesses = response['businesses'] while not found and idx < len(businesses): business = businesses[idx] name = business['name'] name_0 = place.name.lower().replace("'", "") name_1 = name.lower().replace("'", "") distance = nltk.edit_distance(name_0, name_1) distance_normalized = float(distance) / max(len(name_0), len(name_1)) contains_substring = name_0 in name_1 or name_1 in name_0 match = distance_normalized < 0.5 or contains_substring print smart_format("{0}->{1}, distance:{2} substring:{3} MATCH: {4}", place.name, name, distance, contains_substring, match) if match: found = business idx += 1 if found: place.yelp_id = business['id'] place.save()
def deal_invite_message(deal, beacon, time_string, is_sms=True, custom_message=None, invited_by=None): if invited_by is None: creator = beacon.creator else: creator = invited_by place_name = deal.place.name place_address = deal.place.street_address if is_sms and custom_message: message = smart_format( "{0}: {1}\n\nReply 'Yes' to let {2} know you are coming. {3} is texting you through Hotspot. With the app you'll {4}", creator.get_full_name(), custom_message, creator.first_name, creator.first_name, deal.invite_description) elif not is_sms and custom_message: message = smart_format("{0}: {1}", creator.get_full_name(), custom_message) elif is_sms and not custom_message: message = smart_format( "{0} invited you to redeem a group deal at {1} ({2}). Join {3} {4} and {5}.\n\nReply with 'Yes' to let {6} know you are coming or 'More' to get more info", creator.get_full_name(), place_name, place_address, creator.first_name, time_string, deal.invite_description, creator.first_name) else: message = smart_format( '{0} invited you to redeem a group deal at {1}. Join {3} {4} and {5}.', creator.get_full_name(), place_name, place_address, creator.first_name, time_string, deal.invite_description) return message
def send_recommendation_notification(user): if not user.ios_devices.exists() and not user.android_devices.exists(): return message = smart_format( "Hey {0}! Looking to get friends together tonight? Set a hotspot.", user.first_name) foursquare_id = None bar = get_nearby_bar_for_user(user) if bar: name = bar['name'] foursquare_id = bar['id'] location_data = bar['location'] location = None if 'crossStreet' in location_data.keys(): location = location_data['crossStreet'] elif 'address' in location_data.keys(): location = location_data['address'] if location: message = smart_format( "Hey {0}! Looking to get friends together tonight? Set a hotspot at {1} ({2}). It's heating up!", user.first_name, name, location) else: message = smart_format( "Hey {0}! Looking to get friends together tonight? Set a hotspot at {1}. It's heating up!", user.first_name, name) recommendation = Recommendation.objects.create( user=user, foursquare_venue_id=foursquare_id, message_text=message) send_hotspot_message([user], push_text=message, message_type=MESSAGE_TYPE.HOTSPOT_RECOMMENDATION, recommendation_id=recommendation.pk)
def check_if_here(user, lat, long): hotspots = get_hotspot_list(user) #not here if in future current_hotspots = [] for hotspot in hotspots: if (hotspot.time - datetime.now()).total_seconds() < 60 * 60 * 2: current_hotspots.append(hotspot) at_hotspot = [] for hotspot in current_hotspots: if distance_between_two_points(hotspot.latitude, hotspot.longitude, lat, long) < 0.35: print smart_format("{0} is here", user.first_name) at_hotspot.append(hotspot) return at_hotspot
def sms_deal_invitation_message(deal, beacon, isUser, custom_message=None, invited_by=None): # creator = beacon.creator if isUser: message = smart_format( "{0}: {1}\n\nReply 'Yes' to let {2} know you are coming", invited_by.get_full_name(), custom_message, invited_by.first_name) else: message = smart_format( "{0}: {1}\n\nReply 'Yes' to let {2} know you are coming", invited_by.get_full_name(), custom_message, invited_by.first_name) return message
def send_message_notification(beacon, message, name): message_text = smart_format("{0}: {1}", name, message.message) users = users_to_notify(beacon, message.user) send_hotspot_message.delay(users=users, push_text=message_text, message_type=MESSAGE_TYPE.MESSAGE, beacon_id=beacon.id)
def foursquare_match_place(place): ll = "{0},{1}".format(place.latitude, place.longitude) query = place.name parameters = {"ll": ll, "query": query} response = foursquare_api.venues.search(parameters) found = None idx = 0 venues = response['venues'] while not found and idx < len(venues): venue = venues[idx] name = venue['name'] name_0 = place.name.lower().replace("'", "") name_1 = name.lower().replace("'", "") distance = nltk.edit_distance(name_0, name_1) distance_normalized = float(distance) / max(len(name_0), len(name_1)) contains_substring = name_0 in name_1 or name_1 in name_0 match = distance_normalized < 0.5 or contains_substring print smart_format("{0}->{1}, distance:{2} substring:{3} MATCH: {4}", place.name, name, distance, contains_substring, match) if match: found = venue idx += 1 if found: place.foursquare_id = venue['id'] if 'location' in venue: location_data = venue['location'] if 'address' in location_data: place.street_address = venue['location']['address'] if 'contact' in venue and 'formattedPhone' in venue['contact']: place.phone = venue['contact']['formattedPhone'] place.save() else: #debugging for now so I don't have to make another query for places ive already searched for place.foursquare_id = 'NOT FOUND' place.save()
def check_or_update_saw_invite(beacon_follow): print beacon_follow.saw_invite if beacon_follow.saw_invite is False: print "Was false, will now be made true" beacon_follow.saw_invite = True beacon_follow.save() if beacon_follow.contact: name = beacon_follow.contact.name else: name = beacon_follow.user.first_name message = smart_format("{0} read your invitation", name) if beacon_follow.invited_by is not None: send_hotspot_message.delay( users=[beacon_follow.invited_by], push_text=message, message_type=MESSAGE_TYPE.HOTSPOT_UPDATE, beacon_id=beacon_follow.beacon.id) else: print "Was already true, no change"
def get_guestlist_for_mobileview(beacon_id): beacon = Beacon.objects.get(pk=beacon_id) guests = get_hotspot_guests(beacon) here_list = [] going_list = [] invited_list = [] for g in guests: if g.profile: name = g.profile.user.first_name if g.profile.user.last_name: name = smart_format("{0} {1}", name, g.profile.user.last_name) else: name = g.contact.name if g.status == 'here': here_list.append(name) elif g.status == 'going': going_list.append(name) else: invited_list.append(name) return here_list, going_list, invited_list
def check_in_for_venue(user, request_data): if 'place_id' and "is_public" not in request_data: return False isPublic = bool(int(request_data['is_public'])) place_id = int(request_data['place_id']) place = DealPlace.objects.get(pk=place_id) deal = get_active_deal_for_place(place) description = smart_format("Deal at {0}", place.name) time_cutoff = datetime.datetime.now() - datetime.timedelta(hours=6) if place.has_pos: point_of_sale = place.point_of_sale if user_has_open_tab(user, point_of_sale): tab = Tab.objects.get(user=user, point_of_sale=point_of_sale, closed=False, cancelled=False) tab_items = TabItem.objects.filter(tab=tab) else: tab, tab_items = create_tab(user, request_data) else: tab = None tab_items = None if not DealStatus.objects.filter(deal=deal, user=user, deal_status=DEAL_STATUS.UNLOCKED, date_created__gte=time_cutoff).exists(): timezone = GeoTimeZone().get_timezone(latitude=place.latitude, longitude=place.longitude) beacon_datetime = datetime.datetime.now(tz=timezone) today_time = 60 * 60 * beacon_datetime.hour + 60 * beacon_datetime.minute + beacon_datetime.second beacon = Beacon(creator=user, description=description, place=place, time=beacon_datetime, private=False, longitude=place.longitude, latitude=place.latitude, address=place.street_address) beacon.save() deal_hours = deal_hours_for_datetime(deal, beacon_datetime) start = beacon_datetime if deal_hours is None: deal_end = beacon_datetime + datetime.timedelta(seconds=86400 - today_time) else: deal_end = beacon_datetime + datetime.timedelta( seconds=deal_hours.end - today_time) end = min(deal_end, start + datetime.timedelta(hours=4)) deal_status = DealStatus(deal=deal, beacon=beacon, user=user, start=start, end=end, public=isPublic, deal_status=DEAL_STATUS.UNLOCKED) beacon_follow = BeaconFollow(beacon=beacon, user=user, state=BEACON_FOLLOW_STATUS.GOING) deal_status.save() beacon_follow.save() if isPublic: notify_friends_of_user_for_deal.delay(deal_status, False) else: beacon = Beacon.objects.filter( creator=user, description=description, date_created__gte=time_cutoff).latest('date_created') if deal.reward_eligibility and user.profile.number_of_reward_items > 0: deal.is_reward_item = True else: deal.is_reward_item = False deal.is_followed = False print str(deal) print str(beacon) print str(tab) print str(tab_items) return deal, beacon, tab, tab_items
def __unicode__(self): return smart_format("{0}:{1}", self.place, self.title)
def check_in_for_deal(user, request_data): if 'deal_id' and 'is_present' and "is_public" not in request_data: return False isPresent = bool(int(request_data['is_present'])) isPublic = bool(int(request_data['is_public'])) deal_id = int(request_data['deal_id']) deal = Deal.objects.select_related('place').get(pk=deal_id) place = deal.place description = smart_format("Deal at {0}", place.name) time_cutoff = datetime.datetime.now() - datetime.timedelta(hours=6) if not DealStatus.objects.filter(deal=deal, user=user, deal_status=DEAL_STATUS.UNLOCKED, date_created__gte=time_cutoff).exists(): timezone = GeoTimeZone().get_timezone(latitude=place.latitude, longitude=place.longitude) beacon_datetime = datetime.datetime.now(tz=timezone) today_time = 60 * 60 * beacon_datetime.hour + 60 * beacon_datetime.minute + beacon_datetime.second beacon = Beacon(creator=user, description=description, place=place, time=beacon_datetime, private=False, longitude=place.longitude, latitude=place.latitude, address=place.street_address) beacon.save() deal_hours = deal_hours_for_datetime(deal, beacon_datetime) start = beacon_datetime if deal_hours is None: deal_end = beacon_datetime + datetime.timedelta(seconds=86400 - today_time) else: deal_end = beacon_datetime + datetime.timedelta( seconds=deal_hours.end - today_time) end = min(deal_end, start + datetime.timedelta(hours=4)) deal_status = DealStatus(deal=deal, beacon=beacon, user=user, start=start, end=end, public=isPublic, present=isPresent, deal_status=DEAL_STATUS.UNLOCKED) beacon_follow = BeaconFollow(beacon=beacon, user=user, state=BEACON_FOLLOW_STATUS.GOING) deal_status.save() beacon_follow.save() if isPublic: notify_friends_of_user_for_deal.delay(deal_status, isPresent) else: beacon = Beacon.objects.filter(creator=user, description=description, date_created__gte=time_cutoff)[0] if deal.reward_eligibility and user.profile.number_of_reward_items > 0: deal.is_reward_item = True else: deal.is_reward_item = False deal.is_followed = False return deal, beacon
def send_sms_invites(user, beacon, user_list, contact_list): # get your number so you dont get texts about your own hotspot if Profile.objects.filter(user=user).exists(): profile = Profile.objects.get(user=user) normalized_phone = profile.normalized_phone first_time_body = smart_format( "{0} {1} uses the app Hotspot (GetHotspotApp.com) to simplify meeting up with friends.", user.first_name, user.last_name) if user.last_name: notification_text = smart_format("{0} {1}: {2}", user.first_name, user.last_name, beacon.description) else: notification_text = smart_format("{0}: {1}", user.first_name, beacon.description) #add time and location info try: time_string = DateFormatter().friendly_string_for_timestamp( beacon.beacon_time, beacon.latitude, beacon.longitude) address_string = beacon.address notification_text += smart_format("\n{0} @ {1}", time_string, address_string) except: client.captureException() first_time_contacts = get_first_time_contacts(contact_list) # Add profiles and contacts to Beacon Follow (formerly Beacon Invites) beacon_invites = [] profiles = Profile.objects.filter(user__in=user_list).exclude( normalized_phone=normalized_phone).select_related('user') for profile in profiles: beacon_invites.append( BeaconFollow(beacon=beacon, user=profile.user, invited_by=user, state=BEACON_FOLLOW_STATUS.INVITED)) for contact in contact_list: beacon_invites.append( BeaconFollow(beacon=beacon, contact=contact, invited_by=user, state=BEACON_FOLLOW_STATUS.INVITED)) BeaconFollow.objects.bulk_create(beacon_invites) beacon_invitations = BeaconFollow.objects.filter( beacon=beacon, contact__in=contact_list, invited_by=user, state=BEACON_FOLLOW_STATUS.INVITED).select_related('user') # Create individualized messages for each Hotspot SMS contacts_sms_list = [] sms_response_text = smart_format( 'Reply with "Yes" to let {0} know you can come or with "More" if you want more info', user.first_name) sms_message = smart_format("{0}\n\n{1}", notification_text, sms_response_text) for contact in contact_list: current_dict = { 'contact_phone': contact.normalized_phone, 'message': sms_message } contacts_sms_list.append(current_dict) sms_users = User.objects.filter(pk__in=[o.pk for o in user_list], android_devices=None, ios_devices=None).select_related('profile') sms_profiles = [o.profile for o in sms_users] profile_sms_list = [] for profile in sms_profiles: message = notification_text + "\nEvent details: Hotspot://Go-to-App" current_dict = { 'profile_phone': profile.normalized_phone, 'message': message } profile_sms_list.append(current_dict) send_hotspot_message.delay(contacts=first_time_contacts, sms_text=first_time_body, message_type=MESSAGE_TYPE.HOTSPOT_UPDATE, beacon_id=beacon.id) send_hotspot_message.delay(users=user_list, push_text=notification_text, message_type=MESSAGE_TYPE.HOTSPOT_UPDATE, beacon_id=beacon.id) send_individualized_sms.delay(contacts_sms_list, profile_sms_list) return True
def __unicode__(self): return smart_format("{0}:{1}", self.user, self.message_text)
def has_left(name): chat_message = smart_format("{0} has left", name) avatar_url = HOTBOT_AVATAR.SAD return chat_message, avatar_url
def has_left_checked_out(name, checkedOutByName): chat_message = smart_format("{0} has checked {1} out", checkedOutByName, name) avatar_url = HOTBOT_AVATAR.SAD return chat_message, avatar_url
def is_here(name): push_message = smart_format("{0} is here", name) chat_message = smart_format("{0} is here", name) avatar_url = HOTBOT_AVATAR.EXCITED return push_message, chat_message, avatar_url
def is_here_check_in(name, checkedInByName): push_message = smart_format("{0} checked {1} in", checkedInByName, name) chat_message = smart_format("{0} checked {1} in", checkedInByName, name) avatar_url = HOTBOT_AVATAR.EXCITED return push_message, chat_message, avatar_url
def __unicode__(self): return smart_format("{0}, {1}", self.user.first_name, self.nps_score)
def is_going(name): push_message = smart_format("{0} is coming", name) chat_message = smart_format("{0} is coming", name) avatar_url = HOTBOT_AVATAR.EXCITED return push_message, chat_message, avatar_url
def get_location_id(deal_place, facebook_place_id): if facebook_place_id is not None: query_string = "search?facebook_places_id={0}&access_token={1}".format(facebook_place_id, access_token) url = base_url + query_string try: response = requests.get(url) response = json.loads(response.text) if len(response['data']) > 0: return response['data'][0]['id'] else: error = "CHECK: " + str(deal_place.facebook_id) + " " + str(deal_place.id) + " " + smart_format(deal_place.name) print error facebook_errors.append(error) return None except: error = "Location ID Failure: " + str(url) print error facebook_errors.append(error) return None else: return None
def apply_for_deal(user, deal_id, timestamp, invite_list, custom_message=None, image_url=None): #create deal and beacon try: print "User: "******"deal_id:" + str(deal_id) print "Timestamp" + str(timestamp) except: pass #beacon_datetime = datetime.datetime.fromtimestamp(timestamp) deal = Deal.objects.select_related('place').get(pk=deal_id) place = deal.place description = smart_format("Deal at {0}", place.name) #get deal hours timezone = GeoTimeZone().get_timezone(latitude=place.latitude, longitude=place.longitude) beacon_datetime = datetime.datetime.fromtimestamp(timestamp, tz=timezone) today_time = 60 * 60 * beacon_datetime.hour + 60 * beacon_datetime.minute + beacon_datetime.second beacon = Beacon(creator=user, description=description, time=beacon_datetime, private=False, longitude=place.longitude, latitude=place.latitude, address=place.street_address, custom_deal_message=custom_message) beacon.save() deal_hours = deal_hours_for_datetime(deal, beacon_datetime) try: print "Deal Hours" + str(deal_hours) except: pass #start = beacon_datetime + datetime.timedelta(seconds=deal_hours.start - today_time) start = beacon_datetime if deal_hours is None: deal_end = beacon_datetime + datetime.timedelta(seconds=86400 - today_time) else: deal_end = beacon_datetime + datetime.timedelta( seconds=deal_hours.end - today_time) end = min(deal_end, start + datetime.timedelta(hours=4)) #create deal status and beacon status objects user_list, contact_list = parse_json_into_users_and_contact_lists( user, invite_list) beacon_follows = [] deal_statuses = [] #temp hack: throw error if fewer invited than required num_invites = len(user_list) + len(contact_list) if num_invites < deal.invite_requirement: raise Exception('Too Few Invites') #create deal status and beacon follow for creator deal_statuses.append( DealStatus(deal=deal, beacon=beacon, user=user, start=start, end=end, image_url=image_url)) beacon_follows.append( BeaconFollow(beacon=beacon, user=user, state=BEACON_FOLLOW_STATUS.GOING)) cleaned_contact_list = remove_duplicate_contacts(contact_list) for invited_user in user_list: deal_statuses.append( DealStatus(deal=deal, beacon=beacon, user=invited_user, invited_by=user, start=start, end=end, image_url=image_url)) beacon_follows.append( BeaconFollow(beacon=beacon, user=invited_user, invited_by=user)) for invited_contact in cleaned_contact_list: deal_statuses.append( DealStatus(deal=deal, beacon=beacon, contact=invited_contact, invited_by=user, start=start, end=end, image_url=image_url)) beacon_follows.append( BeaconFollow(beacon=beacon, contact=invited_contact, invited_by=user)) BeaconFollow.objects.bulk_create(beacon_follows) DealStatus.objects.bulk_create(deal_statuses) update_deal_statuses_for_beacon(beacon, deal) create_contact_status_for_deal(beacon) if deal.reward_eligibility and user.profile.number_of_reward_items > 0: deal.is_reward_item = True else: deal.is_reward_item = False deal.is_followed = False #send invitations # send_deal_invites(deal_statuses, custom_message) send_deal_invites.delay(deal_statuses, custom_message) return deal, beacon
def __unicode__(self): return smart_format("{0}:{1}", self.place, self.deal_description)
def __unicode__(self): return smart_format("{0}->{1}", self.deal_status.user.first_name, self.deal_status.deal.place.name)
def __unicode__(self): return smart_format("{0} {1}, {2}-{3}", self.deal.place, self.days, time_string(self.start), time_string(self.end))
def __unicode__(self): return smart_format("{0}, {1}", self.name, self.street)
def set_hotspot_message(user): chat_message = smart_format("{0} set this Hotspot", user.first_name) avatar_url = HOTBOT_AVATAR.NEUTRAL return chat_message, avatar_url
def invite_friends_message(user, numberOfInvites): friend_tense = get_correct_friend_tense(numberOfInvites) chat_message = smart_format("{0} invited {1} {2}", user.first_name, numberOfInvites, friend_tense) avatar_url = HOTBOT_AVATAR.HAPPY return chat_message, avatar_url
def __unicode__(self): return smart_format("{0}->{1}", self.creator, self.description)