def order_events_for_user_in_market(self, user, weeks=6): market = self.get_primary_market(user) lat_range = [market.latitude - .3, market.latitude + .3] lng_range = [market.longitude - .3, market.longitude + .3] events = SponsoredEvent.objects.filter(start__gte=datetime.now(), start__lte=datetime.now() + timedelta(weeks=weeks), market=market) if len(events) == 0: return None, None user_locations = Location.objects.filter(user=user, latitude__range=lat_range, longitude__range=lng_range).order_by('-date_created')[:50] purchase_events = EventStatus.objects.filter(user=user, event__in=events).filter( Q(status=EVENT_STATUS.REDEEMED) | Q(status=EVENT_STATUS.GOING)) for event in events: event.score = 0 if user_locations: for location in user_locations: for event in events: distance = distance_between_two_points(event.place.latitude, event.place.longitude, location.latitude, location.longitude) event.score = event.score + distance for event in events: event.score = event.score / float(len(user_locations)) if purchase_events: for event in events: total_distance = 0 for event_status in purchase_events: distance = distance_between_two_points(event_status.event.place.latitude, event_status.event.place.longitude, event.place.latitude, event.place.longitude) total_distance = total_distance + distance event.score = event.score + (total_distance/float(len(purchase_events))) events = list(events) events.sort(key=lambda x: x.score) primary_event = events[0] secondary_events = SponsoredEvent.objects.filter(pk__in=[item.pk for item in events]).exclude(pk=primary_event.id).order_by('start') return primary_event, secondary_events
def get_primary_market(self, user): markets = Market.objects.all() purchase_events = EventStatus.objects.filter(user=user).filter( Q(status=EVENT_STATUS.REDEEMED) | Q(status=EVENT_STATUS.GOING)) user_locations = Location.objects.filter(user=user).order_by('-date_created')[:50] if not user_locations and not purchase_events: return None for market in markets: market.score = 0 if user_locations: for location in user_locations: for market in markets: distance = distance_between_two_points(market.latitude, market.longitude, location.latitude, location.longitude) market.score = market.score + distance for market in markets: market.score = market.score / float(len(user_locations)) if purchase_events: for market in markets: total_distance = 0 for event_status in purchase_events: distance = distance_between_two_points(event_status.event.place.latitude, event_status.event.place.longitude, market.latitude, market.longitude) total_distance = total_distance + distance market.score = market.score + (total_distance/float(len(purchase_events))) markets = list(markets) markets.sort(key=lambda x: x.score) return markets[0]
def nearby_happy_hours(user, latitude, longitude, radius): lat_range = [latitude - 0.1, latitude + 0.1] lon_range = [longitude - 0.1, longitude + 0.1] timezone = GeoTimeZone().get_timezone(latitude=latitude, longitude=longitude) now = datetime.datetime.now(tz=timezone) time_in_seconds = (now.hour * 3600) + (now.minute * 60) + now.second bitmask = bitmask_for_day((now.weekday() + 2) % 7) happy_hours = SyndicatedDeal.objects.filter( days_active=bitmask, place__latitude__range=lat_range, place__longitude__range=lon_range).exclude( place__yelp_id=None).select_related('place') # if now and upcoming: happy_hours = happy_hours.filter(end__gte=time_in_seconds) happy_hours = add_favorites_to_deals(user, happy_hours) # elif now: # happy_hours = happy_hours.filter(start__lte=time, end__gte=time) # elif upcoming: # happy_hours = happy_hours.filter(start__gte=time) happy_hours_in_radius = [] for happy_hour in happy_hours: happy_hour.place.distance = distance_between_two_points( latitude, longitude, happy_hour.place.latitude, happy_hour.place.longitude) if happy_hour.place.distance < radius: happy_hours_in_radius.append(happy_hour) happy_hours_in_radius.sort(key=lambda x: x.place.distance) return happy_hours_in_radius
def create_hotspot(user, latitude, longitude, address, description, time, invite_list, user_latitude=None, user_longitude=None): beacon = Beacon(creator=user, description=description, time=time, private=False, longitude=longitude, latitude=latitude, address=address) beacon.save() creatorIsGoing = BeaconFollow(user=user, beacon=beacon, state=BEACON_FOLLOW_STATUS.GOING) if user_latitude: distance = distance_between_two_points(latitude, longitude, user_latitude, user_longitude) dt = abs(datetime.now() - time).total_seconds() if distance < 0.2 and dt < 60 * 15: creatorIsGoing.state = BEACON_FOLLOW_STATUS.HERE creatorIsGoing.save() user_list, contact_list = parse_json_into_users_and_contact_lists( user, invite_list) numberOfFriends = len(user_list) + len(contact_list) if numberOfFriends > 0: send_sms_invites(user, beacon, user_list, contact_list) chat_message1, avatar_url1 = set_hotspot_message(user) chat_message2, avatar_url2 = invite_friends_message( user, numberOfFriends) create_hotspot_message_in_chat(beacon, chat_message1, user, None, avatar_url1) create_hotspot_message_in_chat(beacon, chat_message2, user, None, avatar_url2) else: chat_message, avatar_url = set_hotspot_message(user) create_hotspot_message_in_chat(beacon, chat_message, user, None, avatar_url) hotspot_with_extra_properties = add_extra_hotspot_properties(beacon) scheduler.enqueue_at(beacon.time, send_push_to_wakeup_users, user_list) scheduler.enqueue_at(beacon.time + timedelta(hours=1), send_push_to_wakeup_users, user_list) scheduler.enqueue_at(beacon.time + timedelta(hours=2), send_push_to_wakeup_users, user_list) if (beacon.time - datetime.now()).total_seconds() > 60 * 60 * 2: scheduler.enqueue_at(beacon.time - timedelta(minutes=15), send_reminder, beacon) return hotspot_with_extra_properties
def get_nearest_market(lat, lng, markets): current_distance = 100000 closest_market = None for market in markets: dist = distance_between_two_points(lat, lng, market.latitude, market.longitude) if dist < current_distance: current_distance = dist closest_market = market return closest_market
def nearby_venues(user, latitude, longitude, radius): sponsored_events = get_sponsored_events(user, latitude, longitude) sponsored_events = add_extra_properties_to_sponsored_events(user, sponsored_events) t0 = time.time() places = get_list_of_places_for_user(user, latitude, longitude) t1 = time.time() # today_deals = DealHours.objects.filter(days_active=weekday_bit).values_list('deal', flat=True) place_ids_with_deals = Deal.objects.filter(in_app_payment=True, active=True).values_list('place', flat=True) filtered_places = [] for place in places: place.distance = distance_between_two_points(latitude, longitude, place.latitude, place.longitude) t2 = time.time() places.sort(key=lambda x: x.distance) timezone = GeoTimeZone().get_timezone(latitude=latitude, longitude=longitude) now = datetime.datetime.now(tz=timezone) time_in_seconds = (now.hour * 3600) + (now.minute * 60) + now.second bitmask = bitmask_for_day((now.weekday() + 2) % 7) happy_hours = SyndicatedDeal.objects.filter(days_active=bitmask, place__in=places, end__gte=time_in_seconds, in_review=False).select_related('place') places_with_happy_hours = happy_hours.values_list('place', flat=True) events = SyndicatedEvents.objects.filter(place__in=places, start__gte=datetime.datetime.now()-datetime.timedelta(hours=6), start__lte=datetime.datetime.now()+datetime.timedelta(weeks=1)).order_by('start') places_with_events = events.values_list('place', flat=True) active_place_ids = list(places_with_events) + list(places_with_happy_hours) deal_count = 0 for place in places: place_id = place.id if place_id in place_ids_with_deals and deal_count < 25: filtered_places.append(place) deal_count += 1 elif (place.distance < radius) and (place_id in active_place_ids): filtered_places.append(place) #This is a repeated to improve speed... happy_hours = SyndicatedDeal.objects.filter(days_active=bitmask, place__in=filtered_places, end__gte=time_in_seconds, in_review=False).order_by('start').select_related('place') events = SyndicatedEvents.objects.filter(place__in=filtered_places, start__gte=datetime.datetime.now()-datetime.timedelta(hours=6), start__lte=datetime.datetime.now()+datetime.timedelta(weeks=1)).order_by('start') t3 = time.time() filtered_places = add_extras_to_places(user, filtered_places, latitude, longitude, happy_hours, events) t4 = time.time() deals, non_deals = add_active_deals_to_places(user, filtered_places) t5 = time.time() # places_in_radius = add_reward_info_to_places(user, places_in_radius) deals.sort(key=lambda x: x.distance) non_deals.sort(key=lambda x: x.distance) t6 = time.time() return sponsored_events, deals, non_deals
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 get_deal_places_within_distance(user, lat, lng, radius=1): venues_within_distance = [] lat_range = [lat - 0.1, lat + 0.1] lng_range = [lng - 0.1, lng + 0.1] user_favorites = Favorites.objects.filter( user=user, active=True).values_list('place', flat=True) venues = DealPlace.objects.filter( latitude__range=lat_range, longitude__range=lng_range).exclude(pk__in=user_favorites) for venue in venues: distance = distance_between_two_points(lat, lng, venue.latitude, venue.longitude) if distance < radius: venues_within_distance.append(venue) return venues_within_distance
def get_users_within_distance(lat, lng, distance=10, max_date=datetime.now(), min_date=datetime.now() - timedelta(weeks=52)): lat_range = [lat - 1, lat + 1] lng_range = [lng - 1, lng + 1] locations = Location.objects.filter( latitude__range=lat_range, longitude__range=lng_range, date_created__range=[min_date, max_date]) users_in_location = [] for location in locations: dist = distance_between_two_points(lat, lng, location.latitude, location.longitude) if dist < distance: if location.user_id not in users_in_location: users_in_location.append(location.user_id) user_obj = User.objects.filter(pk__in=users_in_location) return user_obj
def fetch_happy_hours(latitude, longitude, day, time, now=True, upcoming=True): lat_range = [latitude-0.1, latitude+0.1] lon_range = [longitude-0.1, longitude+0.1] bitmask = bitmask_for_day(day) happy_hours = HappyHour.objects.filter(days_active=bitmask, place__latitude__range=lat_range, place__longitude__range=lon_range).exclude(place__yelp_id=None).select_related('place') if now and upcoming: happy_hours = happy_hours.filter(end__gte=time) elif now: happy_hours = happy_hours.filter(start__lte=time, end__gte=time) elif upcoming: happy_hours = happy_hours.filter(start__gte=time) for happy_hour in happy_hours: happy_hour.is_followed = False happy_hour.place.distance = distance_between_two_points(latitude, longitude, happy_hour.place.latitude, happy_hour.place.longitude) happy_hours = list(happy_hours) happy_hours.sort(key=lambda x: x.place.distance) return happy_hours
def check_to_send_notification(user, location): latitude = location.latitude longitude = location.longitude places = get_list_of_places_for_user(user, location.latitude, location.longitude) deals = Deal.objects.filter(in_app_payment=True, active=True, place__in=places) # timezone = GeoTimeZone().get_timezone(latitude=latitude, longitude=longitude) # now = datetime.datetime.now(tz=timezone) now = datetime.datetime.now() weekday = (now.weekday() + 1) % 7 weekday_bit = [ DealHours.days_active.Sunday, DealHours.days_active.Monday, DealHours.days_active.Tuesday, DealHours.days_active.Wednesday, DealHours.days_active.Thursday, DealHours.days_active.Friday, DealHours.days_active.Saturday ][weekday] now_in_seconds = 60 * 60 * now.hour + 60 * now.minute + now.second deal_hours = DealHours.objects.filter(deal__in=deals) if not notification_sent_that_day(user) and is_appropriate_time( now_in_seconds): filtered_deals = [] for deal in deals: deal.distance = distance_between_two_points( latitude, longitude, deal.place.latitude, deal.place.longitude) if deal.distance < .5: filtered_deals.append(deal) filtered_deals.sort(key=lambda x: x.distance) for deal in filtered_deals: if deal_hours.filter(deal=deal, days_active=weekday_bit, end__gte=now_in_seconds).exists(): if check_and_send_notification(user, deal): return True
def nearby_deals(user, latitude, longitude, radius): place_ids = get_list_of_places_for_user(user, latitude, longitude) if len(place_ids) > 0: deals = get_active_deals_at_places(place_ids) # deals = Deal.objects.filter(active=True, deal_type="DT").select_related('place') # events = get_today_events(all_events) user_reward_items = user.profile.number_of_reward_items deals = add_favorites_to_deals(user, deals) for deal in deals: deal.place.distance = distance_between_two_points( latitude, longitude, deal.place.latitude, deal.place.longitude) if deal.reward_eligibility and user_reward_items > 0: deal.is_reward_item = True else: deal.is_reward_item = False deals.sort(key=lambda x: x.place.distance) deals_in_radius = [] for deal in deals: if deal.place.distance < radius: deals_in_radius.append(deal) # if (len(deals) >= 10) and (len(deals_in_radius) < 10): # return deals[0:10] # else: # return deals_in_radius return deals # deals = list(deals) # deals.sort(key=lambda x: place_ids.index(x.place_id)) # events.sort(key=lambda x: place_ids.index(x.place_id)) else: deals_in_radius = [] # events = [] return deals_in_radius