当前位置: 首页>>代码示例>>Python>>正文


Python models.PhoneNumber类代码示例

本文整理汇总了Python中corehq.apps.sms.models.PhoneNumber的典型用法代码示例。如果您正苦于以下问题:Python PhoneNumber类的具体用法?Python PhoneNumber怎么用?Python PhoneNumber使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了PhoneNumber类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testSync

    def testSync(self):
        extra_number = PhoneNumber.objects.create(
            domain=self.domain,
            owner_doc_type='X',
            owner_id='owner1',
            phone_number='999123',
            verified=True,
            pending_verification=False,
            is_two_way=True
        )

        user = self.mobile_worker1
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 1)

        user.phone_numbers = ['9990001']
        user.save()
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 2)
        self.assertPhoneEntries(user, ['9990001'])

        before = user.get_phone_entries()['9990001']

        user.phone_numbers = ['9990001', '9990002']
        user.save()
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 3)
        self.assertPhoneEntries(user, ['9990001', '9990002'])

        after = user.get_phone_entries()['9990001']
        self.assertEqual(before.pk, after.pk)

        user.phone_numbers = ['9990002']
        user.save()
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 2)
        self.assertPhoneEntries(user, ['9990002'])

        self.assertEqual(PhoneNumber.get_two_way_number('999123'), extra_number)
开发者ID:dimagi,项目名称:commcare-hq,代码行数:35,代码来源:test_phone_numbers.py

示例2: test_by_owner_id

    def test_by_owner_id(self):
        number = PhoneNumber.objects.create(
            domain=self.domain,
            owner_doc_type='X',
            owner_id='owner1',
            phone_number='999123',
            verified=True,
            pending_verification=False,
            is_two_way=True
        )

        [lookup] = PhoneNumber.by_owner_id('owner1')
        self.assertEqual(lookup, number)

        # test cache clear
        number.owner_id = 'owner2'
        number.save()
        self.assertEqual(PhoneNumber.by_owner_id('owner1'), [])
        [lookup] = PhoneNumber.by_owner_id('owner2')
        self.assertEqual(lookup, number)

        number.verified = False
        number.is_two_way = False
        number.save()
        [lookup] = PhoneNumber.by_owner_id('owner2')
        self.assertFalse(lookup.verified)
        self.assertFalse(lookup.is_two_way)
开发者ID:dimagi,项目名称:commcare-hq,代码行数:27,代码来源:test_phone_numbers.py

示例3: test_sms

    def test_sms(self):
        from corehq.apps.sms.models import PhoneNumber, MessagingEvent, MessagingSubEvent
        expected_object_counts = Counter({PhoneNumber: 1, MessagingEvent: 1, MessagingSubEvent: 1})

        phone_number = PhoneNumber(
            domain=self.domain_name,
            owner_doc_type='CommCareCase',
            owner_id='fake-owner-id1',
            phone_number='99912341234',
            backend_id=None,
            ivr_backend_id=None,
            verified=True,
            is_two_way=True,
            pending_verification=False,
            contact_last_modified=datetime.utcnow()
        )
        phone_number.save()
        event = MessagingEvent.objects.create(
            domain=self.domain_name,
            date=datetime.utcnow(),
            source=MessagingEvent.SOURCE_REMINDER,
            content_type=MessagingEvent.CONTENT_SMS,
            status=MessagingEvent.STATUS_COMPLETED
        )
        MessagingSubEvent.objects.create(
            parent=event,
            date=datetime.utcnow(),
            recipient_type=MessagingEvent.RECIPIENT_CASE,
            content_type=MessagingEvent.CONTENT_SMS,
            status=MessagingEvent.STATUS_COMPLETED
        )

        self._dump_and_load(expected_object_counts)
开发者ID:dimagi,项目名称:commcare-hq,代码行数:33,代码来源:test_sql_dump_load.py

示例4: test_backend

    def test_backend(self):
        backend1 = SQLTestSMSBackend.objects.create(
            hq_api_id=SQLTestSMSBackend.get_api_id(),
            is_global=False,
            domain=self.domain,
            name='BACKEND1'
        )

        backend2 = SQLTestSMSBackend.objects.create(
            hq_api_id=SQLTestSMSBackend.get_api_id(),
            is_global=False,
            domain=self.domain,
            name='BACKEND2'
        )

        SQLMobileBackendMapping.set_default_domain_backend(self.domain, backend1)

        number = PhoneNumber(domain=self.domain, phone_number='+999123')
        self.assertEqual(number.backend, backend1)

        number.backend_id = backend2.name
        self.assertEqual(number.backend, backend2)

        number.backend_id = '  '
        self.assertEqual(number.backend, backend1)
开发者ID:dimagi,项目名称:commcare-hq,代码行数:25,代码来源:test_phone_numbers.py

示例5: testCouchSyncToSQL

    def testCouchSyncToSQL(self):
        self.assertEqual(self.getCouchCount(), 0)
        self.assertEqual(self.getSQLCount(), 0)

        # Test Create
        couch_obj = VerifiedNumber()
        self.setRandomCouchObjectValues(couch_obj)
        couch_obj.save()

        sleep(1)
        self.assertEqual(self.getCouchCount(), 1)
        self.assertEqual(self.getSQLCount(), 1)

        sql_obj = PhoneNumber.objects.get(couch_id=couch_obj._id)
        self.checkFieldValues(couch_obj, sql_obj, PhoneNumber._migration_get_fields())
        self.assertTrue(VerifiedNumber.get_db().get_rev(couch_obj._id).startswith('1-'))

        # Test Update
        self.setRandomCouchObjectValues(couch_obj)
        couch_obj.save()

        sleep(1)
        self.assertEqual(self.getCouchCount(), 1)
        self.assertEqual(self.getSQLCount(), 1)
        sql_obj = PhoneNumber.objects.get(couch_id=couch_obj._id)
        self.checkFieldValues(couch_obj, sql_obj, PhoneNumber._migration_get_fields())
        self.assertTrue(VerifiedNumber.get_db().get_rev(couch_obj._id).startswith('2-'))

        # Test Delete
        couch_id = couch_obj._id
        couch_obj.delete()
        with self.assertRaises(ResourceNotFound):
            VerifiedNumber.get(couch_id)
        self.assertEqual(self.getCouchCount(), 0)
        self.assertEqual(self.getSQLCount(), 0)
开发者ID:tlwakwella,项目名称:commcare-hq,代码行数:35,代码来源:migration.py

示例6: assertMatch

    def assertMatch(self, match, phone_search, suffix_search, owner_id_search):
        lookedup = PhoneNumber.get_two_way_number(phone_search)
        self.assertPhoneNumbersEqual(match, lookedup)

        lookedup = PhoneNumber.get_two_way_number_by_suffix(suffix_search)
        self.assertPhoneNumbersEqual(match, lookedup)

        [lookedup] = PhoneNumber.by_owner_id(owner_id_search)
        self.assertPhoneNumbersEqual(match, lookedup)
开发者ID:dimagi,项目名称:commcare-hq,代码行数:9,代码来源:test_phone_numbers.py

示例7: process_incoming

def process_incoming(msg, delay=True):
    v = VerifiedNumber.by_phone(msg.phone_number, include_pending=True)

    if v is not None and v.verified:
        msg.couch_recipient_doc_type = v.owner_doc_type
        msg.couch_recipient = v.owner_id
        msg.domain = v.domain
        contact = v.owner
        if isinstance(contact, CommCareUser) and hasattr(contact, 'location_id'):
            msg.location_id = contact.location_id
        msg.save()

    if msg.domain_scope:
        # only process messages for phones known to be associated with this domain
        if v is None or v.domain != msg.domain_scope:
            raise DomainScopeValidationError(
                'Attempted to simulate incoming sms from phone number not ' \
                'verified with this domain'
            )

    can_receive_sms = PhoneNumber.can_receive_sms(msg.phone_number)
    opt_in_keywords, opt_out_keywords = get_opt_keywords(msg)
    if is_opt_message(msg.text, opt_out_keywords) and can_receive_sms:
        if PhoneNumber.opt_out_sms(msg.phone_number):
            metadata = MessageMetadata(ignore_opt_out=True)
            text = get_message(MSG_OPTED_OUT, v, context=(opt_in_keywords[0],))
            if v:
                send_sms_to_verified_number(v, text, metadata=metadata)
            else:
                send_sms(msg.domain, None, msg.phone_number, text, metadata=metadata)
    elif is_opt_message(msg.text, opt_in_keywords) and not can_receive_sms:
        if PhoneNumber.opt_in_sms(msg.phone_number):
            text = get_message(MSG_OPTED_IN, v, context=(opt_out_keywords[0],))
            if v:
                send_sms_to_verified_number(v, text)
            else:
                send_sms(msg.domain, None, msg.phone_number, text)
    elif v is not None and v.verified:
        if domain_has_privilege(msg.domain, privileges.INBOUND_SMS):
            for h in settings.SMS_HANDLERS:
                try:
                    handler = to_function(h)
                except:
                    notify_exception(None, message=('error loading sms handler: %s' % h))
                    continue

                try:
                    was_handled = handler(v, msg.text, msg=msg)
                except Exception, e:
                    log_sms_exception(msg)
                    was_handled = False

                if was_handled:
                    break
开发者ID:sheelio,项目名称:commcare-hq,代码行数:54,代码来源:api.py

示例8: testGetOrCreate

    def testGetOrCreate(self):
        before = self.mobile_worker1.get_or_create_phone_entry('999123')
        self.assertEqual(before.owner_doc_type, 'CommCareUser')
        self.assertEqual(before.owner_id, self.mobile_worker1.get_id)
        self.assertEqual(before.phone_number, '999123')
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 1)

        after = self.mobile_worker1.get_or_create_phone_entry('999123')
        self.assertEqual(before.pk, after.pk)
        self.assertEqual(after.owner_doc_type, 'CommCareUser')
        self.assertEqual(after.owner_id, self.mobile_worker1.get_id)
        self.assertEqual(after.phone_number, '999123')
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 1)
开发者ID:dimagi,项目名称:commcare-hq,代码行数:13,代码来源:test_phone_numbers.py

示例9: get_inbound_phone_entry

def get_inbound_phone_entry(msg):
    if msg.backend_id:
        backend = SQLMobileBackend.load(msg.backend_id, is_couch_id=True)
        if not backend.is_global and toggles.INBOUND_SMS_LENIENCY.enabled(backend.domain):
            p = PhoneNumber.get_two_way_number_with_domain_scope(msg.phone_number, backend.domains_with_access)
            return (
                p,
                p is not None
            )

    return (
        PhoneNumber.get_reserved_number(msg.phone_number),
        False
    )
开发者ID:kkrampa,项目名称:commcare-hq,代码行数:14,代码来源:api.py

示例10: save_verified_number

    def save_verified_number(self, domain, phone_number, verified, backend_id=None, ivr_backend_id=None, only_one_number_allowed=False):
        """
        Saves the given phone number as this contact's verified phone number.

        backend_id - the name of an SMSBackend to use when sending SMS to
            this number; if specified, this will override any project or
            global settings for which backend will be used to send sms to
            this number

        return  The PhoneNumber
        raises  InvalidFormatException if the phone number format is invalid
        raises  PhoneNumberInUseException if the phone number is already in use by another contact
        """
        from corehq.apps.sms.models import PhoneNumber

        phone_number = apply_leniency(phone_number)
        self.verify_unique_number(phone_number)
        if only_one_number_allowed:
            v = self.get_verified_number()
        else:
            v = self.get_verified_number(phone_number)
        if v is None:
            v = PhoneNumber(
                owner_doc_type=self.doc_type,
                owner_id=self.get_id
            )
        v.domain = domain
        v.phone_number = phone_number
        v.verified = verified
        v.backend_id = backend_id
        v.ivr_backend_id = ivr_backend_id
        v.save()
开发者ID:saketkanth,项目名称:commcare-hq,代码行数:32,代码来源:mixin.py

示例11: testRetire

    def testRetire(self):
        self.mobile_worker1.phone_numbers = ['9990001']
        self.mobile_worker1.save()
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 1)
        self.assertPhoneEntries(self.mobile_worker1, ['9990001'])

        self.mobile_worker2.phone_numbers = ['9990002']
        self.mobile_worker2.save()
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 2)
        self.assertPhoneEntries(self.mobile_worker2, ['9990002'])

        self.mobile_worker1.retire()
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 1)
        self.assertPhoneEntries(self.mobile_worker2, ['9990002'])
开发者ID:dimagi,项目名称:commcare-hq,代码行数:14,代码来源:test_phone_numbers.py

示例12: testDelete

    def testDelete(self):
        self.mobile_worker1.get_or_create_phone_entry('999123')
        self.mobile_worker1.get_or_create_phone_entry('999124')
        self.mobile_worker1.get_or_create_phone_entry('999125')
        self.mobile_worker2.get_or_create_phone_entry('999126')
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 4)

        self.mobile_worker1.delete_phone_entry('999124')
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 3)

        entries = self.mobile_worker1.get_phone_entries()
        self.assertEqual(set(entries.keys()), set(['999123', '999125']))

        entries = self.mobile_worker2.get_phone_entries()
        self.assertEqual(set(entries.keys()), set(['999126']))
开发者ID:dimagi,项目名称:commcare-hq,代码行数:15,代码来源:test_phone_numbers.py

示例13: testUserSyncNoChange

    def testUserSyncNoChange(self):
        before = self.mobile_worker1.get_or_create_phone_entry('999123')
        before.set_two_way()
        before.set_verified()
        before.save()
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 1)

        self.mobile_worker1.phone_numbers = ['999123']
        self.mobile_worker1.save()
        self.assertEqual(PhoneNumber.by_domain(self.domain).count(), 1)

        after = self.mobile_worker1.get_phone_entries()['999123']
        self.assertEqual(before.pk, after.pk)
        self.assertTrue(after.is_two_way)
        self.assertTrue(after.verified)
        self.assertFalse(after.pending_verification)
开发者ID:dimagi,项目名称:commcare-hq,代码行数:16,代码来源:test_phone_numbers.py

示例14: initiate_sms_verification_workflow

def initiate_sms_verification_workflow(contact, phone_number):
    # For now this is only applicable to mobile workers
    assert isinstance(contact, CommCareUser)

    logged_event = MessagingEvent.get_current_verification_event(
        contact.domain, contact.get_id, phone_number)

    with CriticalSection(['verifying-phone-number-%s' % phone_number]):
        vn = PhoneNumber.by_phone(phone_number, include_pending=True)
        if vn:
            if vn.owner_id != contact.get_id:
                return VERIFICATION__ALREADY_IN_USE
            if vn.verified:
                return VERIFICATION__ALREADY_VERIFIED
            else:
                result = VERIFICATION__RESENT_PENDING
        else:
            contact.save_verified_number(contact.domain, phone_number, False)
            result = VERIFICATION__WORKFLOW_STARTED
            # Always create a new event when the workflow starts
            if logged_event:
                logged_event.status = MessagingEvent.STATUS_NOT_COMPLETED
                logged_event.save()
            logged_event = MessagingEvent.create_verification_event(contact.domain, contact)

        if not logged_event:
            logged_event = MessagingEvent.create_verification_event(contact.domain, contact)

        send_verification(contact.domain, contact, phone_number, logged_event)
        return result
开发者ID:saketkanth,项目名称:commcare-hq,代码行数:30,代码来源:verify.py

示例15: test_sending_to_opted_out_number

    def test_sending_to_opted_out_number(self):
        self.assertEqual(PhoneBlacklist.objects.count(), 0)

        incoming('99912345678', 'join opt-test', 'GVI')
        v = PhoneNumber.get_two_way_number('99912345678')
        self.assertIsNotNone(v)

        send_sms_to_verified_number(v, 'hello')
        sms = self.get_last_sms('+99912345678')
        self.assertEqual(sms.direction, 'O')
        self.assertEqual(sms.text, 'hello')

        incoming('99912345678', 'stop', 'GVI')
        self.assertEqual(PhoneBlacklist.objects.count(), 1)
        phone_number = PhoneBlacklist.objects.get(phone_number='99912345678')
        self.assertFalse(phone_number.send_sms)

        send_sms_to_verified_number(v, 'hello')
        sms = self.get_last_sms('+99912345678')
        self.assertEqual(sms.direction, 'O')
        self.assertEqual(sms.text, 'hello')
        self.assertTrue(sms.error)
        self.assertEqual(sms.system_error_message, SMS.ERROR_PHONE_NUMBER_OPTED_OUT)

        incoming('99912345678', 'start', 'GVI')
        self.assertEqual(PhoneBlacklist.objects.count(), 1)
        phone_number = PhoneBlacklist.objects.get(phone_number='99912345678')
        self.assertTrue(phone_number.send_sms)

        send_sms_to_verified_number(v, 'hello')
        sms = self.get_last_sms('+99912345678')
        self.assertEqual(sms.direction, 'O')
        self.assertEqual(sms.text, 'hello')
        self.assertFalse(sms.error)
        self.assertIsNone(sms.system_error_message)
开发者ID:dimagi,项目名称:commcare-hq,代码行数:35,代码来源:opt_tests.py


注:本文中的corehq.apps.sms.models.PhoneNumber类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。