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


Python Contact.import_csv方法代码示例

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


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

示例1: test_deleting_reimport_contact_groups

# 需要导入模块: from temba.contacts.models import Contact [as 别名]
# 或者: from temba.contacts.models.Contact import import_csv [as 别名]
    def test_deleting_reimport_contact_groups(self):
        campaign = Campaign.create(self.org, self.admin, "Planting Reminders", self.farmers)

        # create a reminder for our first planting event
        planting_reminder = CampaignEvent.create_flow_event(self.org, self.admin, campaign, relative_to=self.planting_date,
                                                            offset=3, unit='D', flow=self.reminder_flow)

        self.assertEquals(0, EventFire.objects.all().count())
        self.farmer1.set_field(self.user, 'planting_date', "10-05-2020 12:30:10")
        self.farmer2.set_field(self.user, 'planting_date', "15-05-2020 12:30:10")

        # now we have event fires accordingly
        self.assertEquals(2, EventFire.objects.all().count())

        # farmer one fire
        scheduled = EventFire.objects.get(contact=self.farmer1, event=planting_reminder).scheduled
        self.assertEquals("13-5-2020", "%s-%s-%s" % (scheduled.day, scheduled.month, scheduled.year))

        # farmer two fire
        scheduled = EventFire.objects.get(contact=self.farmer2, event=planting_reminder).scheduled
        self.assertEquals("18-5-2020", "%s-%s-%s" % (scheduled.day, scheduled.month, scheduled.year))

        # delete our farmers group
        self.farmers.release()

        # this should have removed all the event fires for that group
        self.assertEquals(0, EventFire.objects.filter(event=planting_reminder).count())

        # and our group is no longer active
        self.assertFalse(campaign.group.is_active)

        # now import the group again
        filename = 'farmers.csv'
        extra_fields = [dict(key='planting_date', header='planting_date', label='Planting Date', type='D')]
        import_params = dict(org_id=self.org.id, timezone=self.org.timezone, extra_fields=extra_fields, original_filename=filename)

        from temba.contacts.models import ImportTask, Contact
        import json
        task = ImportTask.objects.create(
            created_by=self.admin, modified_by=self.admin,
            csv_file='test_imports/' + filename,
            model_class="Contact", import_params=json.dumps(import_params), import_log="", task_id="A")
        Contact.import_csv(task, log=None)

        # check that we have new planting dates
        self.farmer1 = Contact.objects.get(pk=self.farmer1.pk)
        self.farmer2 = Contact.objects.get(pk=self.farmer2.pk)

        planting = self.farmer1.get_field('planting_date').datetime_value
        self.assertEquals("10-8-2020", "%s-%s-%s" % (planting.day, planting.month, planting.year))

        planting = self.farmer2.get_field('planting_date').datetime_value
        self.assertEquals("15-8-2020", "%s-%s-%s" % (planting.day, planting.month, planting.year))

        # now update the campaign
        from temba.contacts.models import ContactGroup
        self.farmers = ContactGroup.user_groups.get(name='Farmers')
        self.login(self.admin)
        post_data = dict(name="Planting Reminders", group=self.farmers.pk)
        self.client.post(reverse('campaigns.campaign_update', args=[campaign.pk]), post_data)

        # should have two fresh new fires
        self.assertEquals(2, EventFire.objects.all().count())

        # check their new planting dates
        scheduled = EventFire.objects.get(contact=self.farmer1, event=planting_reminder).scheduled
        self.assertEquals("13-8-2020", "%s-%s-%s" % (scheduled.day, scheduled.month, scheduled.year))

        # farmer two fire
        scheduled = EventFire.objects.get(contact=self.farmer2, event=planting_reminder).scheduled
        self.assertEquals("18-8-2020", "%s-%s-%s" % (scheduled.day, scheduled.month, scheduled.year))

        # give our non farmer a planting date
        self.nonfarmer.set_field(self.user, 'planting_date', "20-05-2020 12:30:10")

        # now update to the non-farmer group
        self.nonfarmers = self.create_group("Not Farmers", [self.nonfarmer])
        post_data = dict(name="Planting Reminders", group=self.nonfarmers.pk)
        self.client.post(reverse('campaigns.campaign_update', args=[campaign.pk]), post_data)

        # only one fire for the non-farmer the previous two should be deleted by the group change
        self.assertEquals(1, EventFire.objects.all().count())
        self.assertEquals(1, EventFire.objects.filter(contact=self.nonfarmer).count())
开发者ID:Ebaneck,项目名称:rapidpro,代码行数:85,代码来源:tests.py


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