Exemple #1
0
    def test_campaigns(self):
        url = reverse('api.v2.campaigns')

        self.assertEndpointAccess(url)

        reporters = self.create_group("Reporters", [self.joe, self.frank])
        campaign1 = Campaign.create(self.org, self.admin, "Reminders #1", reporters)
        campaign2 = Campaign.create(self.org, self.admin, "Reminders #2", reporters)

        # create campaign for other org
        spammers = ContactGroup.get_or_create(self.org2, self.admin2, "Spammers")
        Campaign.create(self.org2, self.admin2, "Cool stuff", spammers)

        # no filtering
        with self.assertNumQueries(NUM_BASE_REQUEST_QUERIES + 2):
            response = self.fetchJSON(url)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json['next'], None)
        self.assertResultsByUUID(response, [campaign2, campaign1])
        self.assertEqual(response.json['results'][0], {
            'uuid': campaign2.uuid,
            'name': "Reminders #2",
            'group': {'uuid': reporters.uuid, 'name': "Reporters"},
            'created_on': format_datetime(campaign2.created_on)
        })

        # filter by UUID
        response = self.fetchJSON(url, 'uuid=%s' % campaign1.uuid)
        self.assertResultsByUUID(response, [campaign1])
Exemple #2
0
    def test_campaign_events(self):
        url = reverse('api.v2.campaign_events')

        self.assertEndpointAccess(url)

        flow = self.create_flow()
        reporters = self.create_group("Reporters", [self.joe, self.frank])
        registration = ContactField.get_or_create(self.org, self.admin, 'registration', "Registration")

        campaign1 = Campaign.create(self.org, self.admin, "Reminders", reporters)
        event1 = CampaignEvent.create_message_event(self.org, self.admin, campaign1, registration,
                                                    1, CampaignEvent.UNIT_DAYS, "Don't forget to brush your teeth")

        campaign2 = Campaign.create(self.org, self.admin, "Notifications", reporters)
        event2 = CampaignEvent.create_flow_event(self.org, self.admin, campaign2, registration,
                                                 6, CampaignEvent.UNIT_HOURS, flow, delivery_hour=12)

        # create event for another org
        joined = ContactField.get_or_create(self.org2, self.admin2, 'joined', "Joined On")
        spammers = ContactGroup.get_or_create(self.org2, self.admin2, "Spammers")
        spam = Campaign.create(self.org2, self.admin2, "Cool stuff", spammers)
        CampaignEvent.create_flow_event(self.org2, self.admin2, spam, joined,
                                        6, CampaignEvent.UNIT_HOURS, flow, delivery_hour=12)

        # no filtering
        with self.assertNumQueries(NUM_BASE_REQUEST_QUERIES + 4):
            response = self.fetchJSON(url)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json['next'], None)
        self.assertResultsByUUID(response, [event2, event1])
        self.assertEqual(response.json['results'][0], {
            'uuid': event2.uuid,
            'campaign': {'uuid': campaign2.uuid, 'name': "Notifications"},
            'relative_to': {'key': "registration", 'label': "Registration"},
            'offset': 6,
            'unit': 'hours',
            'delivery_hour': 12,
            'flow': {'uuid': flow.uuid, 'name': "Color Flow"},
            'message': None,
            'created_on': format_datetime(event2.created_on)
        })

        # filter by UUID
        response = self.fetchJSON(url, 'uuid=%s' % event1.uuid)
        self.assertResultsByUUID(response, [event1])

        # filter by campaign name
        response = self.fetchJSON(url, 'campaign=Reminders')
        self.assertResultsByUUID(response, [event1])

        # filter by campaign UUID
        response = self.fetchJSON(url, 'campaign=%s' % campaign1.uuid)
        self.assertResultsByUUID(response, [event1])

        # filter by invalid campaign
        response = self.fetchJSON(url, 'campaign=invalid')
        self.assertResultsByUUID(response, [])
Exemple #3
0
def migrate_campaigns_to_production(all_notifications):
    for n in all_notifications:
        campaign = json.loads(n.history)
        definition = BASE_IMPORT.copy()
        definition["campaign"] = [campaign]
        n.org_dest.import_app(definition,
                        n.created_by)
        n.mark_migrated()
        c = Campaign.objects.filter(name = n.item_name,
                                    org = n.org_dest,
                                    group__name =campaign["group"]["name"]).last()
        Campaign.restore_flows(c)
        EventFire.update_campaign_events(c)
Exemple #4
0
    def setUp(self):
        super().setUp()

        self.planting_date = ContactField.get_or_create(
            self.org, self.admin, "planting_date", "Planting Date", value_type=Value.TYPE_DATETIME
        )
        self.contact = self.create_contact("Ben Haggerty", number="+12065552020")
        self.contact.set_field(self.admin, "planting_date", "2018-06-23T13:48:12.123456Z")

        # create a campaign with a message event 1 day after planting date
        self.farmers = self.create_group("Farmers", [self.contact])
        self.campaign = Campaign.create(self.org, self.admin, "Planting Reminders", self.farmers)

        self.event = CampaignEvent.create_message_event(
            self.org,
            self.admin,
            self.campaign,
            relative_to=self.planting_date,
            offset=1,
            unit="D",
            message={
                "eng": "Hi @(upper(contact.name)) don't forget to plant on @(format_date(contact.planting_date))"
            },
            base_language="eng",
        )
Exemple #5
0
    def test_from_event_fire(self):
        flow = self.get_flow("color_v13")
        group = self.create_group("Reporters", contacts=[])
        registered = self.create_field("registered", "Registered", value_type="D")
        campaign = Campaign.create(self.org, self.admin, "Welcomes", group)
        event = CampaignEvent.create_flow_event(
            self.org, self.user, campaign, registered, offset=1, unit="W", flow=flow
        )
        contact = self.create_contact("Jim", phone="0979111111")
        fire = EventFire.objects.create(
            event=event,
            contact=contact,
            scheduled=timezone.now(),
            fired=timezone.now(),
            fired_result=EventFire.RESULT_FIRED,
        )

        self.assertEqual(
            {
                "type": "campaign_fired",
                "created_on": fire.fired.isoformat(),
                "campaign": {"id": campaign.id, "name": "Welcomes", "uuid": campaign.uuid},
                "campaign_event": {
                    "id": event.id,
                    "offset_display": "1 week after",
                    "relative_to": {"key": "registered", "name": "Registered"},
                },
                "fired_result": "F",
            },
            Event.from_event_fire(self.org, self.admin, fire),
        )
Exemple #6
0
    def test_verbose_name_plural(self):
        flow = self.create_flow()
        group = self.create_group("Testers", contacts=[])

        self.assertEqual("Flows", verbose_name_plural(flow))
        self.assertEqual("Groups", verbose_name_plural(group))
        self.assertEqual("Campaigns", verbose_name_plural(Campaign()))
        self.assertEqual("Campaign Events",
                         verbose_name_plural(CampaignEvent()))
Exemple #7
0
def migrate_events_to_production(all_notifications):
    for n in all_notifications:
        #Check if campaign exist
        item = json.loads(n.history)
        c = Campaign.objects.filter(org = n.org_dest,
                                    name = item["name"],
                                    group__name = item["group"]["name"]
                                    ).last()


        if c :
            c.is_archived=False
            c.modified_by=n.created_by
            c.modified_on=timezone.now()
            c.save()
            for event_spec in item["events"]:
                #check if our event is in campaign
                e  = c.events.filter(offset = event_spec ["offset"],
                                    unit = event_spec["unit"],
                                    relative_to__key =event_spec["relative_to"]["key"],
                                    flow__name = event_spec["flow"]["name"],
                                    event_type =event_spec["event_type"])
                if not e:
                    create_event(event_spec, n, c)
                    print("Se creo el evento")
                else:
                    print ("Evento ya existente")
        else:
            definition = BASE_IMPORT.copy()
            definition["campaign"] = item
            definition["flows"] =json.dumps(FlowRevision.objects.filter(flow__name=item.item_name).last().definition)
            n.org_dest.import_app(definition,
                            n.created_by)

            c = Campaign.objects.filter(name = item["name"],
                                        org = n.org_dest,
                                        group__name =item["group"]["name"]).last()
            print(c)
            print (item)
            print (n.org_dest)
            Campaign.restore_flows(c)
            print("Se creo toda la campania")
        EventFire.update_campaign_events(c)
        n.mark_migrated()
Exemple #8
0
    def test_icon(self):
        from temba.campaigns.models import Campaign
        from temba.triggers.models import Trigger
        from temba.flows.models import Flow
        from temba.utils.templatetags.temba import icon

        campaign = Campaign.create(self.org, self.admin, 'Test Campaign', self.create_group('Test group', []))
        flow = Flow.create(self.org, self.admin, 'Test Flow')
        trigger = Trigger.objects.create(org=self.org, keyword='trigger', flow=flow, created_by=self.admin, modified_by=self.admin)

        self.assertEquals('icon-instant', icon(campaign))
        self.assertEquals('icon-feed', icon(trigger))
        self.assertEquals('icon-tree', icon(flow))
        self.assertEquals("", icon(None))
Exemple #9
0
    def test_icon(self):
        from temba.campaigns.models import Campaign
        from temba.triggers.models import Trigger
        from temba.flows.models import Flow
        from temba.utils.templatetags.temba import icon

        campaign = Campaign.create(self.org, self.admin, 'Test Campaign', self.create_group('Test group', []))
        flow = Flow.create(self.org, self.admin, 'Test Flow')
        trigger = Trigger.objects.create(org=self.org, keyword='trigger', flow=flow, created_by=self.admin, modified_by=self.admin)

        self.assertEquals('icon-instant', icon(campaign))
        self.assertEquals('icon-feed', icon(trigger))
        self.assertEquals('icon-tree', icon(flow))
        self.assertEquals("", icon(None))
Exemple #10
0
    def save(self):
        """
        Create or update our campaign
        """
        name = self.validated_data.get('name')
        group = self.validated_data.get('group')

        if self.instance:
            self.instance.name = name
            self.instance.group = group
            self.instance.save(update_fields=('name', 'group'))
        else:
            self.instance = Campaign.create(self.context['org'], self.context['user'], name, group)

        return self.instance
    def save(self):
        """
        Create or update our campaign
        """
        name = self.validated_data.get("name")
        group = self.validated_data.get("group")

        if self.instance:
            self.instance.name = name
            self.instance.group = group
            self.instance.save(update_fields=("name", "group"))
        else:
            self.instance = Campaign.create(self.context["org"], self.context["user"], name, group)

        return self.instance
Exemple #12
0
    def save(self):
        """
        Create or update our campaign
        """
        name = self.validated_data.get('name')
        group = self.validated_data.get('group')

        if self.instance:
            self.instance.name = name
            self.instance.group = group
            self.instance.save(update_fields=('name', 'group'))
        else:
            self.instance = Campaign.create(self.context['org'], self.context['user'], name, group)

        return self.instance
Exemple #13
0
    def save(self):
        """
        Create or update our campaign
        """
        name = self.validated_data.get("name")
        group = self.validated_data.get("group")

        if self.instance:
            self.instance.name = name
            self.instance.group = group
            self.instance.save(update_fields=("name", "group"))
        else:
            self.instance = Campaign.create(self.context["org"], self.context["user"], name, group)

        return self.instance
Exemple #14
0
 def test_object_class_plural(self):
     self.assertEqual("Flow", object_class_name(Flow()))
     self.assertEqual("Campaign", object_class_name(Campaign()))
     self.assertEqual("CampaignEvent", object_class_name(CampaignEvent()))
     self.assertEqual("Trigger", object_class_name(Trigger()))