def callback_campaign_sent(sends_result, user_id, campaign_type, blast_id, oauth_header): """ Once SMS campaign has been sent to all candidates, this function is hit. This is a Celery task. Here we 1) Update number of sends in campaign blast 2) Add activity e.g. (SMS Campaign "abc" was sent to "1000" candidates") This uses post_campaign_sent_processing() function defined in common/campaign_services/campaign_utils.py :param sends_result: Result of executed task :param user_id: id of user (owner of campaign) :param campaign_type: type of campaign. i.e. sms_campaign or push_campaign :param blast_id: id of blast object :param oauth_header: auth header of current user to make HTTP request to other services :type sends_result: list :type user_id: int :type campaign_type: str :type blast_id: int :type oauth_header: dict **See Also** .. see also:: send_campaign_to_candidates() method in CampaignBase class inside common/utils/campaign_base.py """ with app.app_context(): CampaignUtils.post_campaign_sent_processing( CampaignBase, sends_result, user_id, campaign_type, blast_id, oauth_header)
def test_with_valid_param_type(self): """ This tests url_conversion() with valid parameter type. i.e. str """ with app.app_context(): response, error = url_conversion(TEST_DATA['valid_url']) assert not error assert response
def test_with_invalid_url(self): """ This tests url_conversion() with invalid URL. It should get None response and a error message. """ with app.app_context(): response, error = url_conversion(LOCAL_HOST) assert not response assert error
def send_callback(celery_result, campaign_obj): """ When all celery tasks to retrieve smartlist candidates are finished, celery chord calls this function with an array or data (candidates) from all tasks. This function will further call super class method to process this data and send campaigns to all candidates. :param list[list[Candidate]] celery_result: list of lists of candidate ids :param SmsCampaignBase campaign_obj: PushCampaignBase object """ with app.app_context(): campaign_obj.process_campaign_send(celery_result)
def validate_url_by_http_request(url): """ This function makes HTTP GET call to given URL, and return True if we get OK response, It returns False otherwise :param url: :return: True or False :rtype: bool """ raise_if_not_instance_of(url, basestring) try: with app.app_context(): http_request('GET', url) except Exception: return False return True
def delete_test_scheduled_task(task_id, headers): """ This deletes the scheduled task from scheduler_service """ with app.app_context(): CampaignUtils.delete_scheduled_task(task_id, headers)
def _call_pre_process_url_redirect(request_args, url): """ This directly calls the pre_process_url_redirect() class method of CampaignBase """ with app.app_context(): SmsCampaignBase.pre_process_url_redirect(request_args, url)
def _call_process_url_redirect(url_conversion_obj): """ This directly calls the url_redirect() class method of CampaignBase """ with app.app_context(): CampaignBase.url_redirect(url_conversion_obj.id, CampaignUtils.SMS)
def send_campaign_to_candidate(self, candidate_and_phone): """ This is a task of celery. We need to make sure that if any exception is raised, we handle it here gracefully. Otherwise, exception will be raised to chord and callback function will not be called as we expect. In callback function we create an activity that 'SMS campaign 'Job opening at plan 9' has been sent to 400 candidates' For each candidate with associated phone, we do the following: 1- Replace URLs in SMS body text with short URLs(to redirect candidate to our app) 2- Send SMS 3- Create SMS campaign send 4- Update sms_campaign_send_url_conversion 5- Update SMS campaign blast 6- Add activity e.g.('Vincent' received SMS of campaign 'Hiring senior SE'") 7- Once campaign is sent to all candidates, we create activity in callback function of Celery task. e.g. (SMS Campaign "abc" was sent to "1000" candidates") - This method is called from send_sms_campaign_to_candidates() method of class SmsCampaignBase inside sms_campaign_service/sms_campaign_base.py. :param candidate_and_phone: candidate obj at index 0 and candidate phone value at index 1 :type candidate_and_phone: tuple :exception: ErrorUpdatingBodyText :exception: TwilioApiError :exception: GoogleShortenUrlAPIError :return: True if SMS is sent otherwise False. :rtype: bool **See Also** .. see also:: send_sms_campaign_to_candidates() method in SmsCampaignBase class. """ # Celery app is not configured with flask app, so need to use app.app_context() here # so that Celery tasks know the config of flask app. with app.app_context(): candidate, candidate_phone_value = candidate_and_phone try: modified_body_text, url_conversion_ids = \ self.process_urls_in_sms_body_text(candidate.id) except Exception: logger.exception( 'send_campaign_to_candidate: Error processing URLs in SMS body' ) return False # send SMS try: message_sent_datetime = self.send_sms(candidate_phone_value, modified_body_text) except TwilioApiError or InvalidUsage: logger.exception( 'send_campaign_to_candidate: Cannot send SMS.') return False # Create sms_campaign_send i.e. it will record that an SMS has been sent # to the candidate try: sms_campaign_send_obj = self.create_or_update_campaign_send( self.campaign_blast_id, candidate.id, message_sent_datetime, SmsCampaignSend) except Exception: logger.exception('send_campaign_to_candidate: Error saving ' 'record in sms_campaign_send') return False # We keep track of all URLs sent, in sms_send_url_conversion table, # so we can later retrieve that to perform some tasks try: for url_conversion_id in url_conversion_ids: self.create_or_update_send_url_conversion( sms_campaign_send_obj, url_conversion_id) except Exception: logger.exception( 'send_campaign_to_candidate: Error adding entry in ' 'sms_campaign_send_url_conversion.') return False # Create SMS sent activity try: self.create_sms_send_activity(candidate, sms_campaign_send_obj) except Exception: logger.exception( 'send_campaign_to_candidate: Error creating SMS send activity.' ) return False logger.info( 'send_sms_campaign_to_candidate: SMS has been sent to candidate(id:%s).' ' Campaign(id:%s). (User(id:%s))' % (candidate.id, self.campaign.id, self.user.id)) return True