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


Python Contact.from_urn方法代码示例

本文整理汇总了Python中temba.contacts.models.Contact.from_urn方法的典型用法代码示例。如果您正苦于以下问题:Python Contact.from_urn方法的具体用法?Python Contact.from_urn怎么用?Python Contact.from_urn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在temba.contacts.models.Contact的用法示例。


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

示例1: clean

# 需要导入模块: from temba.contacts.models import Contact [as 别名]
# 或者: from temba.contacts.models.Contact import from_urn [as 别名]
    def clean(self):
        country = self.org.get_country_code()

        # validate URN fields
        for field_key, value in self.cleaned_data.iteritems():
            if field_key.startswith('__urn__') and value:
                scheme = field_key[7:field_key.rfind('__')]

                norm_scheme, norm_path = ContactURN.normalize_urn(scheme, value, country)
                existing = Contact.from_urn(self.org, norm_scheme, norm_path)

                if existing and existing != self.instance:
                    self._errors[field_key] = _("Used by another contact")
                elif not ContactURN.validate_urn(norm_scheme, norm_path):
                    self._errors[field_key] = _("Invalid format")

        return self.cleaned_data
开发者ID:thierhost,项目名称:rapidpro,代码行数:19,代码来源:views.py

示例2: clean

# 需要导入模块: from temba.contacts.models import Contact [as 别名]
# 或者: from temba.contacts.models.Contact import from_urn [as 别名]
    def clean(self):
        channel = self.org.get_receive_channel(TEL_SCHEME)
        country = channel.country if channel else None

        # validate URN fields
        for field_key, value in self.cleaned_data.iteritems():
            if field_key.startswith('__urn__') and value:
                scheme = field_key[7:]

                norm_scheme, norm_path = ContactURN.normalize_urn(scheme, value, country)
                existing = Contact.from_urn(self.org, norm_scheme, norm_path)

                if existing and existing != self.instance:
                    self._errors[field_key] = _("Used by another contact")
                elif not ContactURN.validate_urn(norm_scheme, norm_path):
                    self._errors[field_key] = _("Invalid format")

        return self.cleaned_data
开发者ID:austiine04,项目名称:rapidpro,代码行数:20,代码来源:views.py

示例3: validate

# 需要导入模块: from temba.contacts.models import Contact [as 别名]
# 或者: from temba.contacts.models.Contact import from_urn [as 别名]
    def validate(self, data):
        org = self.context['org']

        # we don't allow updating of contact URNs for anon orgs - tho we do allow creation of contacts with URNs
        if org.is_anon and self.instance and data.get('urns'):
            raise serializers.ValidationError("Updating contact URNs not allowed for anonymous organizations")

        # if creating a contact, urns can't include URNs which are already taken
        if not self.instance and 'urns' in data:
            country_code = org.get_country_code()
            for urn in data['urns']:
                if Contact.from_urn(org, urn, country_code):
                    raise serializers.ValidationError("Contact URN belongs to another contact: %s" % urn)

        # if contact is blocked, they can't be added to groups
        if self.instance and (self.instance.is_blocked or self.instance.is_stopped) and data['groups']:
            raise serializers.ValidationError("Blocked or stopped contacts can't be added to groups")

        return data
开发者ID:eHealthAfrica,项目名称:rapidpro,代码行数:21,代码来源:serializers.py

示例4: test_new_conversation_trigger

# 需要导入模块: from temba.contacts.models import Contact [as 别名]
# 或者: from temba.contacts.models.Contact import from_urn [as 别名]
    def test_new_conversation_trigger(self):
        self.login(self.admin)
        flow = self.create_flow()
        flow2 = self.create_flow()

        # see if we list new conversation triggers on the trigger page
        create_trigger_url = reverse('triggers.trigger_create', args=[])
        response = self.client.get(create_trigger_url)
        self.assertNotContains(response, "conversation is started")

        # create a facebook channel
        fb_channel = Channel.add_facebook_channel(self.org, self.user, 'Temba', 1001, 'fb_token')

        # should now be able to create one
        response = self.client.get(create_trigger_url)
        self.assertContains(response, "conversation is started")

        # go create it
        with patch('requests.post') as mock_post:
            mock_post.return_value = MockResponse(200, '{"message": "Success"}')

            response = self.client.post(reverse('triggers.trigger_new_conversation', args=[]),
                                        data=dict(channel=fb_channel.id, flow=flow.id))
            self.assertEqual(response.status_code, 200)
            self.assertEqual(mock_post.call_count, 1)

        # check that it is right
        trigger = Trigger.objects.get(trigger_type=Trigger.TYPE_NEW_CONVERSATION, is_active=True, is_archived=False)
        self.assertEqual(trigger.channel, fb_channel)
        self.assertEqual(trigger.flow, flow)

        # try to create another one, fails as we already have a trigger for that channel
        response = self.client.post(reverse('triggers.trigger_new_conversation', args=[]), data=dict(channel=fb_channel.id, flow=flow2.id))
        self.assertEqual(response.status_code, 200)
        self.assertFormError(response, 'form', 'channel', 'Trigger with this Channel already exists.')

        # ok, trigger a facebook event
        data = json.loads("""{
        "object": "page",
          "entry": [
            {
              "id": "620308107999975",
              "time": 1467841778778,
              "messaging": [
                {
                  "sender":{
                    "id":"1001"
                  },
                  "recipient":{
                    "id":"%s"
                  },
                  "timestamp":1458692752478,
                  "postback":{
                    "payload":"get_started"
                  }
                }
              ]
            }
          ]
        }
        """ % fb_channel.address)

        with patch('requests.get') as mock_get:
            mock_get.return_value = MockResponse(200, '{"first_name": "Ben","last_name": "Haggerty"}')

            callback_url = reverse('handlers.facebook_handler', args=[fb_channel.uuid])
            response = self.client.post(callback_url, json.dumps(data), content_type="application/json")
            self.assertEqual(response.status_code, 200)

            # should have a new flow run for Ben
            contact = Contact.from_urn(self.org, 'facebook:1001')
            self.assertTrue(contact.name, "Ben Haggerty")

            run = FlowRun.objects.get(contact=contact)
            self.assertEqual(run.flow, flow)

        # archive our trigger, should unregister our callback
        with patch('requests.post') as mock_post:
            mock_post.return_value = MockResponse(200, '{"message": "Success"}')

            Trigger.apply_action_archive(self.admin, Trigger.objects.filter(pk=trigger.pk))
            self.assertEqual(response.status_code, 200)
            self.assertEqual(mock_post.call_count, 1)

            trigger.refresh_from_db()
            self.assertTrue(trigger.is_archived)
开发者ID:ewheeler,项目名称:rapidpro,代码行数:88,代码来源:tests.py

示例5: validate_urn

# 需要导入模块: from temba.contacts.models import Contact [as 别名]
# 或者: from temba.contacts.models.Contact import from_urn [as 别名]
    def validate_urn(self, value):
        if self.context['org'].is_anon:
            raise serializers.ValidationError("Referencing by URN not allowed for anonymous organizations")

        self.instance = Contact.from_urn(self.context['org'], value)
        return value
开发者ID:eHealthAfrica,项目名称:rapidpro,代码行数:8,代码来源:serializers.py


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