示例#1
0
 def test_should_add_country_code_when_broadcasting_sms_to_other_people(
         self):
     message_tracker = Mock()
     ONG_TEL_NUMBER = "12354"
     sms_content = "test message"
     with patch.object(SMSClient, "send_sms") as mock_send_sms:
         broadcast_message([], sms_content, ONG_TEL_NUMBER, ["03312345678"],
                           message_tracker, "261")
         mock_send_sms.assert_called_with(ONG_TEL_NUMBER, "2613312345678",
                                          sms_content, MSG_TYPE_USER_MSG)
         broadcast_message([], sms_content, ONG_TEL_NUMBER, ["03312345678"],
                           message_tracker)
         mock_send_sms.assert_called_with(ONG_TEL_NUMBER, "03312345678",
                                          sms_content, MSG_TYPE_USER_MSG)
示例#2
0
def broadcast_message(request, project_id):
    dbm = get_database_manager(request.user)
    project = Project.load(dbm.database, project_id)
    number_associated_ds = len(project.data_senders)
    number_of_ds = len(import_module.load_all_entities_of_type(dbm, type=REPORTER)[0]) - 1
    questionnaire = FormModel.get(dbm, project.qid)
    organization = utils.get_organization(request)
    if request.method == 'GET':
        form = BroadcastMessageForm(associated_ds=number_associated_ds, number_of_ds=number_of_ds)
        html = 'project/broadcast_message_trial.html' if organization.in_trial_mode else 'project/broadcast_message.html'
        return render_to_response(html, {'project': project,
                                         "project_links": make_project_links(project, questionnaire.form_code),
                                         'is_quota_reached': is_quota_reached(request, organization=organization),
                                         "form": form, "ong_country": organization.country,
                                         "success": None},
                                  context_instance=RequestContext(request))
    if request.method == 'POST':
        form = BroadcastMessageForm(associated_ds=number_associated_ds, number_of_ds=number_of_ds, data=request.POST)
        if form.is_valid():
            no_smsc = False
            data_senders = _get_data_senders(dbm, form, project)
            organization_setting = OrganizationSetting.objects.get(organization=organization)
            current_month = datetime.date(datetime.datetime.now().year, datetime.datetime.now().month, 1)
            message_tracker = organization._get_message_tracker(current_month)
            other_numbers = form.cleaned_data['others']
            failed_numbers = []
            try:
                failed_numbers = helper.broadcast_message(data_senders, form.cleaned_data['text'],
                                                          organization_setting.get_organisation_sms_number()[0],
                                                          other_numbers,
                                                          message_tracker,
                                                          country_code=organization.get_phone_country_code())
            except NoSMSCException as e:
                no_smsc = True
            success = not no_smsc and len(failed_numbers) == 0

            if success:
                form = BroadcastMessageForm(associated_ds=number_associated_ds, number_of_ds=number_of_ds)
            else:
                form = BroadcastMessageForm(associated_ds=number_associated_ds, number_of_ds=number_of_ds,
                                            data=request.POST)
            return render_to_response('project/broadcast_message.html',
                                      {'project': project,
                                       "project_links": make_project_links(project, questionnaire.form_code),
                                       'is_quota_reached': is_quota_reached(request, organization=organization),
                                       "form": form,
                                       "ong_country": organization.country, "no_smsc": no_smsc,
                                       'failed_numbers': ",".join(failed_numbers), "success": success},
                                      context_instance=RequestContext(request))

        return render_to_response('project/broadcast_message.html',
                                  {'project': project,
                                   "project_links": make_project_links(project, questionnaire.form_code), "form": form,
                                   'is_quota_reached': is_quota_reached(request, organization=organization),
                                   'success': None, "ong_country": organization.country},
                                  context_instance=RequestContext(request))
示例#3
0
    def post(self, request, *args, **kwargs):
        dbm = get_database_manager(request.user)
        sms_text = request.POST['sms-text']
        other_numbers = self._other_numbers(request)
        organization = utils.get_organization(request)
        organization_setting = OrganizationSetting.objects.get(
            organization=organization)
        current_month = datetime.date(datetime.datetime.now().year,
                                      datetime.datetime.now().month, 1)
        message_tracker = organization._get_message_tracker(current_month)
        no_smsc = False
        mobile_numbers_for_ds_linked_to_questionnaire = self._get_mobile_numbers_for_registered_data_senders(
            dbm, request)
        mobile_numbers_for_specific_contacts = self._get_mobile_numbers_for_specific_contacts(
            dbm, request)
        mobile_numbers_for_ds_linked_to_group = self._get_mobile_numbers_for_groups(
            dbm, request)
        mobile_numbers_for_ds = self._get_mobile_number_for_contacts(
            dbm, request.POST['my_poll_recipients'], request.POST['recipient'])
        mobile_numbers = mobile_numbers_for_ds_linked_to_questionnaire + mobile_numbers_for_ds_linked_to_group + \
                         mobile_numbers_for_specific_contacts + mobile_numbers_for_ds

        failed_numbers = []
        try:
            failed_numbers = broadcast_message(
                mobile_numbers,
                sms_text,
                organization_setting.get_organisation_sms_number()[0],
                other_numbers,
                message_tracker,
                country_code=organization.get_phone_country_code())
            # log sent messages only for poll questionnaire
            current_project_id = json.loads(request.POST['project_id'])
            if current_project_id != "":
                self._log_poll_questionnaire_sent_messages(
                    dbm, organization, organization_setting, request, sms_text,
                    current_project_id, failed_numbers)

        except NoSMSCException:
            no_smsc = True

        successful = len(failed_numbers) == 0 and not no_smsc

        return HttpResponse(
            json.dumps({
                'successful': successful,
                'nosmsc': no_smsc,
                'failednumbers': failed_numbers
            }))