def received_twilio_sms(twilio_sms): from_phone = normalize_phone_number(twilio_sms.from_number) print str(from_phone) chat_line_phone = normalize_phone_number(twilio_sms.to_number) print str(chat_line_phone) body = twilio_sms.body track_sms_received(from_phone, twilio_sms.body) if ChatLine.objects.filter(phone=chat_line_phone).exists(): chat_line = ChatLine.objects.get(phone=chat_line_phone) from_user = User.objects.get(username=from_phone) if SmsConversation.objects.filter( chat_line=chat_line, active=True).filter( Q(user_one=from_user) | Q(user_two=from_user)).exists(): print "Found SMS Conversation" if SmsConversation.objects.filter(user_one=from_user, chat_line=chat_line, active=True).exists(): sms_conversation = SmsConversation.objects.get( user_one=from_user, chat_line=chat_line, active=True) to_phone = sms_conversation.user_two.username print "User One" send_chat_sms(body, to_phone, chat_line_phone) elif SmsConversation.objects.filter(user_two=from_user, chat_line=chat_line, active=True).exists(): sms_conversation = SmsConversation.objects.get( user_two=from_user, chat_line=chat_line, active=True) to_phone = sms_conversation.user_one.username print "User Two" send_chat_sms(body, to_phone, chat_line_phone)
def add_deal_place_from_raw_venue(venue): if not RawPlace.objects.filter(yelp_id=venue['id']).exists(): name = venue['name'] yelp_id = venue['id'] if 'coordinate' in venue['location']: longitude = venue['location']['coordinate']['longitude'] latitude = venue['location']['coordinate']['latitude'] else: longitude = 0.0 latitude = 0.0 if 'neighborhoods' in venue['location']: neighborhood = venue['location']['neighborhoods'][0] else: neighborhood = "" if len(venue['location']['address']) > 0: street_address = venue['location']['address'][0] else: street_address = "" if 'phone' in venue: phone = normalize_phone_number(venue['phone']) else: phone = "" place_type = "" place_description = "" raw_place = RawPlace(longitude=longitude, latitude=latitude, name=name, street_address=street_address, phone=phone, yelp_id=yelp_id, place_description=place_description, place_type=place_type, neighborhood=neighborhood) raw_place.save()
def check_to_send_final_reminder_staffer_notifications(): try: min_dt = datetime.datetime.now() max_dt = datetime.datetime.now() + datetime.timedelta(hours=1) if SponsoredEvent.objects.filter(start__gte=min_dt, start__lte=max_dt).exists(): events = SponsoredEvent.objects.filter(start__range=(min_dt, max_dt)) count = 0 for event in events: staffers = EventStaffer.objects.filter( event=event, status=STAFFER_STATUS.PRIMARY) for staffer in staffers: text = "You should be on your way to the event at {0}. The address is {1}. Please confirm when you arrive by responding 'HERE'".format( staffer.event.place.name, staffer.event.place.street_address) send_chat_sms( text, normalize_phone_number(staffer.manager.phone_number), event_manager_number) notification = StafferNotification( event=staffer.event, manager=staffer.manager, notification_type=STAFFER_NOTIFICATION.FIRST_REMINDER) notification.save() count = count + 1 gm_sms = "{0} final reminders were sent for events today".format( count) send_chat_sms(gm_sms, "5413359388", event_manager_number) except: html = "Sending final staffer notifications failed" traceback.print_exc() html = html + str(traceback.format_exc()) send_error_email(html)
def contacts_from_data(user, data): phone_numbers = [] for c_json in data: try: cobj = json.loads(c_json) phone = normalize_phone_number(cobj['phone']) phone_numbers.append(phone) except Exception: client.captureException() return Contact.objects.filter(user=user, normalized_phone__in=phone_numbers)
def create_user(user_data): if not ('first_name' and 'last_name' and 'email' and 'phone_number' in user_data): return False user = User.objects.create_user(username=user_data['phone_number'], password="******", first_name=user_data['first_name'], last_name=user_data['last_name'], email=user_data['email']) user.save() print "User created" normalizedPhone = normalize_phone_number(user_data['phone_number']) print "phone number normalized" if 'facebook_token' and 'facebook_id' in user_data: profile = Profile.objects.create( user=user, phone_number=user_data['phone_number'], normalized_phone=normalizedPhone, fb_token=user_data['facebook_token'], facebook_id=user_data['facebook_id']) update_facebook_friendships(user) else: profile = Profile.objects.create( user=user, phone_number=user_data['phone_number'], normalized_phone=normalizedPhone) print "Profile created" send_activation_code(profile) send_push_that_friend_joined.delay(profile) #if the person registering was a contact before, need to update invites to point to user instead contacts = get_contact_with_phone(user_data['phone_number']) print "Got contacts" # check_if_user_was_invited(contacts) for contact in contacts: bf_invites = BeaconFollow.objects.filter(contact=contact) for i in bf_invites: i.contact = None i.user = user i.save() ds_invites = DealStatus.objects.filter(contact=contact) for d in ds_invites: d.contact = None d.user = user d.save() check_if_user_was_referred(user) create_promo_code(user) send_welcome_email(user) if 'promo_code' in user_data: print "Checking Promo Code" check_promo_code(user, user_data['promo_code']) print "For loop completed" return user
def parse_json_into_users_and_contact_lists(user, list): new_list = [] for c_json in list: c = json.loads(json.dumps(c_json)) new_list.append(c) user_list = [] contact_list = [] for c in new_list: cobj = json.loads(c) if Profile.objects.filter(normalized_phone=normalize_phone_number( cobj['phone'])).exists(): p = Profile.objects.get( normalized_phone=normalize_phone_number(cobj['phone'])) user_list.append(p.user) else: if not Contact.objects.filter( user=user, name=cobj['name'], phone_number=cobj['phone'], normalized_phone=normalize_phone_number( cobj['phone'])).exists(): contact = Contact(user=user, name=cobj['name'], phone_number=cobj['phone'], normalized_phone=normalize_phone_number( cobj['phone'])) try: contact.save() except DatabaseError: client.captureException() else: contact = Contact.objects.filter( user=user, name=cobj['name'], phone_number=cobj['phone'], normalized_phone=normalize_phone_number( cobj['phone'])).latest('date_created') contact_list.append(contact) return user_list, contact_list
def text_payment_link(request_data): if not 'phone_number' and 'event_id' in request_data: return False phone_number = normalize_phone_number(request_data['phone_number']) event_id = int(request_data['event_id']) if SponsoredEvent.objects.filter(pk=event_id).exists(): event = SponsoredEvent.objects.get(pk=event_id) payment_url = get_shortened_payment_url(event) text_body = "Payment link for the Hotspot event at {0}:\n\n{1}".format( event.place.name, payment_url) send_sms(text_body, [phone_number]) else: return False
def send_day_before_reminder(): tomorrow = datetime.datetime.now() + datetime.timedelta(days=1) min_dt = datetime.datetime.combine(tomorrow, datetime.time.min) max_dt = datetime.datetime.combine(tomorrow, datetime.time.max) if SponsoredEvent.objects.filter(start__range=(min_dt, max_dt)).exists(): events = SponsoredEvent.objects.filter(start__range=(min_dt, max_dt)) for event in events: staffers = EventStaffer.objects.filter(event=event) for staffer in staffers: url = get_shortened_staffer_url(staffer) time = get_event_time(staffer.event) text = "You're scheduled to staff the event tomorrow at {0}. Make sure you arrive at {1} by {2}.\n " \ "Event Dashboard Link: {3}".format(staffer.event.place.name, staffer.event.place.street_address, time, url) send_chat_sms( text, [normalize_phone_number(staffer.manager.phone_number)])
def post(self, request, format=None): response = {} print str(request.DATA) normalizedPhoneNumber = normalize_phone_number( request.DATA['phone_number']) if User.objects.filter(username=request.DATA['phone_number']).exists( ) or Profile.objects.filter( normalized_phone=normalizedPhoneNumber).exists(): response['message'] = "User with phone number already exists." return Response(response, status=status.HTTP_400_BAD_REQUEST) else: user = create_user(request.DATA) if user: if Profile.objects.filter(user=user).exists(): p = Profile.objects.get(user=user) serializer = ProfileSerializer(p) return Response(serializer.data) else: response['message'] = "Failed to create a user." return Response(response, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def check_to_send_first_reminder_staffer_notifications(): try: day_after_tomorrow = datetime.datetime.now() + datetime.timedelta( days=2) min_dt = datetime.datetime.combine(day_after_tomorrow, datetime.time.min) max_dt = datetime.datetime.combine(day_after_tomorrow, datetime.time.max) if SponsoredEvent.objects.filter(start__range=(min_dt, max_dt)).exists(): events = SponsoredEvent.objects.filter(start__range=(min_dt, max_dt)) count = 0 for event in events: staffers = EventStaffer.objects.filter( event=event, status=STAFFER_STATUS.PRIMARY) for staffer in staffers: url = get_shortened_staffer_url(staffer) time = get_event_time(staffer.event) text = "You're scheduled to staff the event day after tomorrow at {0}. Make sure you arrive at {1} by {2}. Please confirm this by responding 'Yes'".format( staffer.event.place.name, staffer.event.place.street_address, time) send_chat_sms( text, normalize_phone_number(staffer.manager.phone_number), event_manager_number) notification = StafferNotification( event=staffer.event, manager=staffer.manager, notification_type=STAFFER_NOTIFICATION.FIRST_REMINDER) notification.save() count = count + 1 gm_sms = "{0} first reminders were sent for events in two days".format( count) send_chat_sms(gm_sms, "5413359388", event_manager_number) except: html = "Sending first reminder staffer notifications failed" traceback.print_exc() html = html + str(traceback.format_exc()) send_error_email(html)
def get_contact_with_phone(phone): normalizedPhone = normalize_phone_number(phone) if Contact.objects.filter(normalized_phone=normalizedPhone).exists(): return list(Contact.objects.filter(normalized_phone=normalizedPhone)) else: return []
def store_contact_list(user, phone_list): new_contact_list = [] for c_json in phone_list: try: cobj = json.loads(c_json) phone = cobj['phone'] name = cobj['name'] contact = Contact(user=user, name=name, phone_number=phone, normalized_phone=normalize_phone_number(phone)) new_contact_list.append(contact) except Exception: client.captureException() stored_contact_numbers = Contact.objects.filter(user=user, normalized_phone__in=[contact.normalized_phone for contact in new_contact_list]).values_list('normalized_phone', flat=True) stored_contact_numbers = set(stored_contact_numbers) contacts_to_add = [] for contact in new_contact_list: if not contact.normalized_phone in stored_contact_numbers: if validate_contact(contact): contacts_to_add.append(contact) if len(contacts_to_add): safe_bulk_create(contacts_to_add) update_contact_friendships(user)
def received_twilio_sms_from_staffer(twilio_sms): from_phone = normalize_phone_number(twilio_sms.from_number) body = twilio_sms.body manager = EventManager.objects.get(phone_number=from_phone) if StafferNotification.objects.filter(manager=manager).exists(): last_notification = StafferNotification.objects.filter( manager=manager).latest('date_created') print from_phone print body if last_notification.notification_type == STAFFER_NOTIFICATION.INITIAL: if ("yes" in body.lower()) and (last_notification.response is None): event = last_notification.event last_notification.response = STAFFER_RESPONSE.YES last_notification.save() if not EventStaffer.objects.filter(event=event).filter( status=STAFFER_STATUS.PRIMARY).exists(): event_staffer = EventStaffer(event=event, manager=manager, status=STAFFER_STATUS.PRIMARY) event_staffer.save() else: event_staffer = EventStaffer(event=event, manager=manager, status=STAFFER_STATUS.BACKUP) event_staffer.save() elif ("no" in body.lower()) and (last_notification.response is None): last_notification.response = STAFFER_RESPONSE.NO last_notification.save() else: text_for_gm = "{0} in {1} ({2}) sent this: {3}".format( manager.name, manager.market, manager.phone_number, body) send_chat_sms(text_for_gm, "5413359388", event_manager_phone) elif last_notification.notification_type == STAFFER_NOTIFICATION.FIRST_REMINDER: if ("yes" in body.lower()) and (last_notification.response is None): last_notification.response = STAFFER_RESPONSE.YES last_notification.save() staffer = EventStaffer.objects.get( event=last_notification.event, manager=last_notification.manager) url = get_shortened_staffer_url(staffer) text_to_staffer = "Awesome!\n\nEvent Link: {0}".format(url) send_chat_sms(text_to_staffer, last_notification.manager.phone_number, event_manager_phone) text_to_gm = "{0} just checked in for event at {1}".format( last_notification.manager.name, last_notification.event.place.name) send_chat_sms(text_to_gm, "5413359388", event_manager_phone) else: text_for_gm = "{0} in {1} ({2}) sent this: {3}".format( manager.name, manager.market, manager.phone_number, body) send_chat_sms(text_for_gm, "5413359388", event_manager_phone) elif last_notification.notification_type == STAFFER_NOTIFICATION.SECOND_REMINDER: if ("yes" in body.lower()) and (last_notification.response is None): last_notification.response = STAFFER_RESPONSE.YES last_notification.save() staffer = EventStaffer.objects.get( event=last_notification.event, manager=last_notification.manager) url = get_shortened_staffer_url(staffer) text_to_staffer = "Awesome!\n\nEvent Link: {0}".format(url) send_chat_sms(text_to_staffer, last_notification.manager.phone_number, event_manager_phone) text_to_gm = "{0} just checked in for event at {1}".format( last_notification.manager.name, last_notification.event.place.name) send_chat_sms(text_to_gm, "5413359388", event_manager_phone) else: text_for_gm = "{0} in {1} ({2}) sent this: {3}".format( manager.name, manager.market, manager.phone_number, body) send_chat_sms(text_for_gm, "5413359388", event_manager_phone) elif last_notification.notification_type == STAFFER_NOTIFICATION.THIRD_REMINDER: if ("here" in body.lower()) and (last_notification.response is None): last_notification.response = STAFFER_RESPONSE.YES last_notification.save() staffer = EventStaffer.objects.get( event=last_notification.event, manager=last_notification.manager) url = get_shortened_staffer_url(staffer) text_to_staffer = "Awesome!\n\nEvent Link: {0}".format(url) send_chat_sms(text_to_staffer, last_notification.manager.phone_number, event_manager_phone) text_to_gm = "{0} just checked in for event at {1}".format( last_notification.manager.name, last_notification.event.place.name) send_chat_sms(text_to_gm, "5413359388", event_manager_phone) else: text_for_gm = "{0} in {1} ({2}) sent this: {3}".format( manager.name, manager.market, manager.phone_number, body) send_chat_sms(text_for_gm, "5413359388", event_manager_phone) else: text_for_gm = "Error: {0}".format(manager.name) send_chat_sms(text_for_gm, "5413359388", event_manager_phone) else: text_for_gm = "Error: {0}".format(manager.name) send_chat_sms(text_for_gm, "5413359388", event_manager_phone)