def handle(self, *args, **options): group_name = options.get('group') if not group_name: sys.exit('Please provide --group') backend, selected_gateway, sms_receipt_handler = gateway.load_backend(options['gateway']) today = datetime.now().date() reminders.all(selected_gateway, [group_name]) reminders.send_stats(selected_gateway, [group_name], today)
def handle(self, *args, **options): group_name = options.get('group') if not group_name: sys.exit('Please provide --group') backend, selected_gateway, sms_receipt_handler = gateway.load_backend(options['gateway']) date_string = options.get('date') if date_string: today = datetime.strptime(date_string, '%d/%m/%Y').date() else: today = datetime.now().date() clinic_name = options.get('clinic_name') reminders.all(selected_gateway, [group_name], today, clinic_name) reminders.send_stats(selected_gateway, [group_name], today)
def handle(self, *args, **options): group_name = options.get('group') if not group_name: sys.exit('Please provide --group') backend, selected_gateway, sms_receipt_handler = gateway.load_backend( options['gateway']) date_string = options.get('date') if date_string: today = datetime.strptime(date_string, '%d/%m/%Y').date() else: today = datetime.now().date() clinic_name = options.get('clinic_name') reminders.all(selected_gateway, [group_name], today, clinic_name) reminders.send_stats(selected_gateway, [group_name], today)
def test_send_stats(self): today = datetime.now() one_day = timedelta(days=1) one_week = timedelta(weeks=1) yesterday = today - one_day tomorrow = today + one_day two_weeks = today + (one_week * 2) # These settings are needed by the reminder script from txtalert.apps.general.settings.models import Setting Setting.objects.create( name='Stats Emails', type='t', text_value='*****@*****.**', group=self.group, ) Setting.objects.create( name='Stats MSISDNs', type='t', text_value='27123456789', group=self.group, ) def create_visit(status, date): """helper function for creating visits easily""" return Visit.objects.create(patient=self.patient, status=status, clinic=self.clinic, date=date) # setup visits to test visit_yesterday_attended = create_visit('a', yesterday) visit_yesterday_missed = create_visit('m', yesterday) visit_tomorrow_scheduled = create_visit('s', tomorrow) visit_tomorrow_rescheduled = create_visit('r', tomorrow) visit_two_weeks_scheduled = create_visit('s', two_weeks) visit_two_weeks_rescheduled = create_visit('r', two_weeks) # send the SMSs over the dummy gateway # FIXME: rename `all` method to something more explicit group_names = [group.name for group in Group.objects.all()] reminders.all(gateway.gateway, group_names) # send the stats reminders.send_stats(gateway.gateway, group_names, today.date()) # test the emails being sent out from django.core import mail self.assertEquals(len(mail.outbox), 1) # our stats message is the only one in the outbox message = mail.outbox[0] # we're expecting it to have the following body, based on the visits # created earlier expecting_mail_body = reminders.REMINDERS_EMAIL_TEXT % { 'total': 4, # because the scheduleds & rescheduleds are on the same they # we'll only be sending them 1 sms instead of 2, resulting in # a total of 4 instead of 6 messages being sent. 'date': today.date(), 'attended': 1, 'missed': 1, 'missed_percentage': (1.0 / 2.0 * 100), # 1 out of 2 visits was missed 'tomorrow': 2, 'two_weeks': 2, 'group': self.group.name.title(), } # check if this is so self.assertEquals(message.body, expecting_mail_body) self.assertTrue('*****@*****.**' in message.to) # the latest SMS sent should be the stats SMS stat_sms = SendSMS.objects.latest() expecting_sms_text = reminders.REMINDERS_SMS_TEXT % { 'total': 4, 'attended': 1, 'missed': 1, 'missed_percentage': (1.0 / 2.0 * 100), 'tomorrow': 2, 'two_weeks': 2, 'group': self.group.name.title(), } self.assertEquals(stat_sms.smstext, expecting_sms_text) self.assertEquals(stat_sms.msisdn, '27123456789')
def handle(self, *args, **options): backend, selected_gateway, sms_receipt_handler = gateway.load_backend(options['gateway']) today = datetime.now().date() reminders.all(selected_gateway, ['Temba Lethu']) reminders.send_stats(selected_gateway, ['Temba Lethu'], today)
def test_send_stats(self): today = datetime.now() one_day = timedelta(days=1) one_week = timedelta(weeks=1) yesterday = today - one_day tomorrow = today + one_day two_weeks = today + (one_week * 2) # These settings are needed by the reminder script from txtalert.apps.general.settings.models import Setting Setting.objects.create(name="Stats Emails", type="t", text_value="*****@*****.**", group=self.group) Setting.objects.create(name="Stats MSISDNs", type="t", text_value="27123456789", group=self.group) def create_visit(status, date): """helper function for creating visits easily""" return Visit.objects.create(patient=self.patient, status=status, clinic=self.clinic, date=date) # setup visits to test visit_yesterday_attended = create_visit("a", yesterday) visit_yesterday_missed = create_visit("m", yesterday) visit_tomorrow_scheduled = create_visit("s", tomorrow) visit_tomorrow_rescheduled = create_visit("r", tomorrow) visit_two_weeks_scheduled = create_visit("s", two_weeks) visit_two_weeks_rescheduled = create_visit("r", two_weeks) # send the SMSs over the dummy gateway # FIXME: rename `all` method to something more explicit group_names = [group.name for group in Group.objects.all()] reminders.all(gateway.gateway, group_names) # send the stats reminders.send_stats(gateway.gateway, group_names, today.date()) # test the emails being sent out from django.core import mail self.assertEquals(len(mail.outbox), 1) # our stats message is the only one in the outbox message = mail.outbox[0] # we're expecting it to have the following body, based on the visits # created earlier expecting_mail_body = reminders.REMINDERS_EMAIL_TEXT % { "total": 4, # because the scheduleds & rescheduleds are on the same they # we'll only be sending them 1 sms instead of 2, resulting in # a total of 4 instead of 6 messages being sent. "date": today.date(), "attended": 1, "missed": 1, "missed_percentage": (1.0 / 2.0 * 100), # 1 out of 2 visits was missed "tomorrow": 2, "two_weeks": 2, "group": self.group.name.title(), } # check if this is so self.assertEquals(message.body, expecting_mail_body) self.assertTrue("*****@*****.**" in message.to) # the latest SMS sent should be the stats SMS stat_sms = SendSMS.objects.latest() expecting_sms_text = reminders.REMINDERS_SMS_TEXT % { "total": 4, "attended": 1, "missed": 1, "missed_percentage": (1.0 / 2.0 * 100), "tomorrow": 2, "two_weeks": 2, "group": self.group.name.title(), } self.assertEquals(stat_sms.smstext, expecting_sms_text) self.assertEquals(stat_sms.msisdn, "27123456789")