def test_list(self): pollrun1 = factories.UniversalPollRun( poll=self.poll1, conducted_on=datetime.datetime(2014, 12, 1, tzinfo=pytz.UTC)) Response.create_empty( self.unicef, pollrun1, Run.create(id=123, contact='C-001', created_on=timezone.now())) self.login(self.admin) response = self.url_get('unicef', reverse('contacts.contact_list')) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context['object_list']), 5) # no poll pollruns shown in "All Regions" view self.assertNotContains(response, "Farm Poll") url = '{}?search=an'.format(reverse('contacts.contact_list')) response = self.url_get('unicef', url) self.assertEqual(len(response.context['object_list']), 2) self.assertContains(response, "Ann") self.assertContains(response, "Dan") self.login(self.user1) response = self.url_get('unicef', reverse('contacts.contact_list')) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context['object_list']), 3) self.assertContains(response, "Farm Poll")
def pollrun_start(pollrun_id): """ Starts a newly created pollrun by creating runs in RapidPro and creating empty responses for them. """ from tracpro.polls.models import PollRun, Response pollrun = PollRun.objects.select_related('poll', 'region').get(pk=pollrun_id) if pollrun.pollrun_type not in (PollRun.TYPE_PROPAGATED, PollRun.TYPE_REGIONAL): raise ValueError("Can't start non-regional poll") org = pollrun.poll.org client = org.get_temba_client() contacts = Contact.objects.filter(is_active=True) if pollrun.pollrun_type == PollRun.TYPE_PROPAGATED: descendants = pollrun.region.get_descendants(include_self=True) contacts = contacts.filter(region__in=descendants) elif pollrun.pollrun_type == PollRun.TYPE_REGIONAL: contacts = contacts.filter(region=pollrun.region) contact_uuids = contacts.values_list('uuid', flat=True) runs = client.create_runs(pollrun.poll.flow_uuid, contact_uuids, restart_participants=True) for run in runs: Response.create_empty(org, pollrun, run) logger.info("Created %d new runs for new poll pollrun #%d" % (len(runs), pollrun.pk))
def pollrun_start(pollrun_id): """ Starts a newly created pollrun by creating runs in RapidPro and creating empty responses for them. """ from tracpro.polls.models import PollRun, Response pollrun = PollRun.objects.select_related('poll', 'region').get(pk=pollrun_id) if pollrun.pollrun_type not in (PollRun.TYPE_PROPAGATED, PollRun.TYPE_REGIONAL): raise ValueError("Can't start non-regional poll") org = pollrun.poll.org client = org.get_temba_client() contacts = Contact.objects.active() if pollrun.pollrun_type == PollRun.TYPE_PROPAGATED: descendants = pollrun.region.get_descendants(include_self=True) contacts = contacts.filter(region__in=descendants) elif pollrun.pollrun_type == PollRun.TYPE_REGIONAL: contacts = contacts.filter(region=pollrun.region) contact_uuids = list(contacts.values_list('uuid', flat=True)) runs = client.create_runs(pollrun.poll.flow_uuid, contact_uuids, restart_participants=True) for run in runs: Response.create_empty(org, pollrun, run) logger.info("Created %d new runs for new poll pollrun #%d" % (len(runs), pollrun.pk))
def pollrun_restart_participants(pollrun_id, contact_uuids): """ Restarts the given contacts in the given poll pollrun by replacing any existing response they have with an empty one. """ from tracpro.polls.models import PollRun, Response pollrun = PollRun.objects.select_related('poll', 'region').get(pk=pollrun_id) if pollrun.pollrun_type not in (PollRun.TYPE_REGIONAL, PollRun.TYPE_PROPAGATED): raise ValueError("Can't restart participants of a non-regional poll") if not pollrun.is_last_for_region(pollrun.region): raise ValueError("Can only restart last pollrun of poll for a region") org = pollrun.poll.org client = org.get_temba_client() runs = client.create_runs(pollrun.poll.flow_uuid, contact_uuids, restart_participants=True) for run in runs: Response.create_empty(org, pollrun, run) logger.info("Created %d restart runs for poll pollrun #%d" % (len(runs), pollrun.pk))
def test_list(self): issue1 = Issue.objects.create(poll=self.poll1, region=None, conducted_on=self.datetime(2014, 12, 1)) Response.create_empty(self.unicef, issue1, Run.create(id=123, contact='C-001', created_on=timezone.now())) self.login(self.admin) response = self.url_get('unicef', reverse('contacts.contact_list')) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context['object_list']), 5) self.assertNotContains(response, "Farm Poll") # no poll issues shown in "All Regions" view response = self.url_get('unicef', '%s?search=an' % reverse('contacts.contact_list')) self.assertEqual(len(response.context['object_list']), 2) self.assertContains(response, "Ann") self.assertContains(response, "Dan") self.login(self.user1) response = self.url_get('unicef', reverse('contacts.contact_list')) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context['object_list']), 3) self.assertContains(response, "Farm Poll")
def pollrun_restart_participants(pollrun_id, contact_uuids): """ Restarts the given contacts in the given poll pollrun by replacing any existing response they have with an empty one. """ from tracpro.polls.models import PollRun, Response pollrun = PollRun.objects.select_related('poll', 'region').get(pk=pollrun_id) if pollrun.pollrun_type not in (PollRun.TYPE_REGIONAL, PollRun.TYPE_PROPAGATED): raise ValueError("Can't restart participants of a non-regional poll") if not pollrun.is_last_for_region(pollrun.region): raise ValueError("Can only restart last pollrun of poll for a region") org = pollrun.poll.org client = org.get_temba_client() runs = client.create_runs(pollrun.poll.flow_uuid, contact_uuids, restart_participants=True) for run in runs: Response.create_empty(org, pollrun, run) logger.info("Created %d restart runs for poll pollrun #%d" % (len(runs), pollrun.pk))