def testCouchSyncToSQL(self): self.deleteAllLogs() self.assertEqual(self.getCouchCount(), 0) self.assertEqual(self.getSQLCount(), 0) # Test Create couch_obj = ExpectedCallbackEventLog() self.setRandomCouchObjectValues(couch_obj) couch_obj.save() sleep(1) self.assertEqual(self.getCouchCount(), 1) self.assertEqual(self.getSQLCount(), 1) sql_obj = ExpectedCallback.objects.get(couch_id=couch_obj._id) self.checkFieldValues(couch_obj, sql_obj, ExpectedCallback._migration_get_fields()) self.assertTrue(ExpectedCallbackEventLog.get_db().get_rev(couch_obj._id).startswith('1-')) # Test Update self.setRandomCouchObjectValues(couch_obj) couch_obj.save() sleep(1) self.assertEqual(self.getCouchCount(), 1) self.assertEqual(self.getSQLCount(), 1) sql_obj = ExpectedCallback.objects.get(couch_id=couch_obj._id) self.checkFieldValues(couch_obj, sql_obj, ExpectedCallback._migration_get_fields()) self.assertTrue(ExpectedCallbackEventLog.get_db().get_rev(couch_obj._id).startswith('2-'))
def fire_sms_callback_event(reminder, handler, recipients, verified_numbers, logged_event): current_event = reminder.current_event for recipient in recipients: send_message = False if reminder.callback_try_count > 0: if reminder.event_initiation_timestamp: event = ExpectedCallback.by_domain_recipient_date( reminder.domain, recipient.get_id, reminder.event_initiation_timestamp ) if not event: continue if event.status == CALLBACK_RECEIVED: continue if Call.inbound_entry_exists( recipient.doc_type, recipient.get_id, reminder.event_initiation_timestamp ): event.status = CALLBACK_RECEIVED event.save() continue else: continue if (reminder.callback_try_count >= len(current_event.callback_timeout_intervals)): # On the last callback timeout, instead of sending the SMS # again, log the missed callback if event: event.status = CALLBACK_MISSED event.save() else: send_message = True else: # It's the first time sending the sms, so create an expected # callback event send_message = True event = ExpectedCallback.objects.create( domain=reminder.domain, date=reminder.event_initiation_timestamp, couch_recipient_doc_type=recipient.doc_type, couch_recipient=recipient.get_id, status=CALLBACK_PENDING, ) if send_message: fire_sms_event(reminder, handler, [recipient], verified_numbers, logged_event, workflow=WORKFLOW_CALLBACK)
def fire_sms_callback_event(reminder, handler, recipients, verified_numbers, logged_event): current_event = reminder.current_event for recipient in recipients: send_message = False if reminder.callback_try_count > 0: if reminder.event_initiation_timestamp: event = ExpectedCallback.by_domain_recipient_date( reminder.domain, recipient.get_id, reminder.event_initiation_timestamp) if not event: continue if event.status == CALLBACK_RECEIVED: continue if Call.inbound_entry_exists( recipient.doc_type, recipient.get_id, reminder.event_initiation_timestamp): event.status = CALLBACK_RECEIVED event.save() continue else: continue if (reminder.callback_try_count >= len( current_event.callback_timeout_intervals)): # On the last callback timeout, instead of sending the SMS # again, log the missed callback if event: event.status = CALLBACK_MISSED event.save() else: send_message = True else: # It's the first time sending the sms, so create an expected # callback event send_message = True event = ExpectedCallback.objects.create( domain=reminder.domain, date=reminder.event_initiation_timestamp, couch_recipient_doc_type=recipient.doc_type, couch_recipient=recipient.get_id, status=CALLBACK_PENDING, ) if send_message: fire_sms_event(reminder, handler, [recipient], verified_numbers, logged_event, workflow=WORKFLOW_CALLBACK)
def rows(self): data = ExpectedCallback.by_domain( self.domain, start_date=self.datespan.startdate_utc, end_date=self.datespan.enddate_utc ).order_by('date') result = [] status_descriptions = { CALLBACK_PENDING : _("Pending"), CALLBACK_RECEIVED : _("Received"), CALLBACK_MISSED : _("Missed"), } # Store the results of lookups for faster loading username_map = {} for event in data: recipient_id = event.couch_recipient if recipient_id in [None, ""]: username = "******" elif recipient_id in username_map: username = username_map.get(recipient_id) else: username = "******" try: if event.couch_recipient_doc_type == "CommCareCase": username = CommCareCase.get(recipient_id).name else: username = CouchUser.get_by_user_id(recipient_id).username except Exception: pass username_map[recipient_id] = username timestamp = ServerTime(event.date).user_time(self.timezone).done() row = [ self._fmt_timestamp(timestamp), self._fmt(username), self._fmt(status_descriptions.get(event.status, "-")), ] result.append(row) return result
def rows(self): data = ExpectedCallback.by_domain( self.domain, start_date=self.datespan.startdate_utc, end_date=self.datespan.enddate_utc).order_by('date') result = [] status_descriptions = { CALLBACK_PENDING: _("Pending"), CALLBACK_RECEIVED: _("Received"), CALLBACK_MISSED: _("Missed"), } # Store the results of lookups for faster loading username_map = {} for event in data: recipient_id = event.couch_recipient if recipient_id in [None, ""]: username = "******" elif recipient_id in username_map: username = username_map.get(recipient_id) else: username = "******" try: if event.couch_recipient_doc_type == "CommCareCase": username = CommCareCase.get(recipient_id).name else: username = CouchUser.get_by_user_id( recipient_id).username except Exception: pass username_map[recipient_id] = username timestamp = ServerTime(event.date).user_time(self.timezone).done() row = [ self._fmt_timestamp(timestamp), self._fmt(username), self._fmt(status_descriptions.get(event.status, "-")), ] result.append(row) return result
def rows(self): group_id = None if self.request.couch_user.is_commcare_user(): group_ids = self.request.couch_user.get_group_ids() if len(group_ids) > 0: group_id = group_ids[0] data = {} for case in get_cases_in_domain(self.domain, type='participant'): if case.closed: continue # If a site coordinator is viewing the report, only show participants from that site (group) if group_id is None or group_id == case.owner_id: timezone = pytz.timezone(case.get_case_property("time_zone")) data[case._id] = { "name": case.name, "time_zone": timezone, "dates": [None] * 14, } dates = self.get_past_two_weeks() date_strings = [json_format_date(date) for date in dates] start_date = dates[0] - timedelta(days=1) end_date = dates[-1] + timedelta(days=2) expected_callback_events = ExpectedCallback.by_domain( self.domain, start_date=datetime.combine(start_date, time(0, 0)), end_date=datetime.combine(end_date, time(0, 0))).order_by('date') for event in expected_callback_events: if event.couch_recipient in data: timezone = data[event.couch_recipient]["time_zone"] event_date = (ServerTime( event.date).user_time(timezone).ui_string("%Y-%m-%d")) if event_date in date_strings: data[event.couch_recipient]["dates"][date_strings.index( event_date)] = event.status result = [] for case_id, data_dict in data.items(): row = [ self._fmt(data_dict["name"]), None, None, None, ] total_no_response = 0 total_indicated = 0 total_pending = 0 for date_status in data_dict["dates"]: if date_status == CALLBACK_PENDING: total_indicated += 1 total_pending += 1 row.append(self._fmt(_("pending"))) elif date_status == CALLBACK_RECEIVED: total_indicated += 1 row.append(self._fmt(_("OK"))) elif date_status == CALLBACK_MISSED: total_indicated += 1 total_no_response += 1 row.append(self._fmt_highlight(_("No Response"))) else: row.append(self._fmt(_("not indicated"))) if total_no_response > 0: row[1] = self._fmt_highlight(total_no_response) else: row[1] = self._fmt(total_no_response) row[2] = self._fmt(total_indicated) row[3] = self._fmt(total_pending) result.append(row) return result
def rows(self): group_id = None if self.request.couch_user.is_commcare_user(): group_ids = self.request.couch_user.get_group_ids() if len(group_ids) > 0: group_id = group_ids[0] data = {} for case in get_cases_in_domain(self.domain, type='participant'): if case.closed: continue # If a site coordinator is viewing the report, only show participants from that site (group) if group_id is None or group_id == case.owner_id: timezone = pytz.timezone(case.get_case_property("time_zone")) data[case._id] = { "name": case.name, "time_zone": timezone, "dates": [None] * 14, } dates = self.get_past_two_weeks() date_strings = [json_format_date(date) for date in dates] start_date = dates[0] - timedelta(days=1) end_date = dates[-1] + timedelta(days=2) expected_callback_events = ExpectedCallback.by_domain( self.domain, start_date=datetime.combine(start_date, time(0, 0)), end_date=datetime.combine(end_date, time(0, 0)) ).order_by('date') for event in expected_callback_events: if event.couch_recipient in data: timezone = data[event.couch_recipient]["time_zone"] event_date = (ServerTime(event.date).user_time(timezone) .ui_string("%Y-%m-%d")) if event_date in date_strings: data[event.couch_recipient]["dates"][date_strings.index(event_date)] = event.status result = [] for case_id, data_dict in data.items(): row = [ self._fmt(data_dict["name"]), None, None, None, ] total_no_response = 0 total_indicated = 0 total_pending = 0 for date_status in data_dict["dates"]: if date_status == CALLBACK_PENDING: total_indicated += 1 total_pending += 1 row.append(self._fmt(_("pending"))) elif date_status == CALLBACK_RECEIVED: total_indicated += 1 row.append(self._fmt(_("OK"))) elif date_status == CALLBACK_MISSED: total_indicated += 1 total_no_response += 1 row.append(self._fmt_highlight(_("No Response"))) else: row.append(self._fmt(_("not indicated"))) if total_no_response > 0: row[1] = self._fmt_highlight(total_no_response) else: row[1] = self._fmt(total_no_response) row[2] = self._fmt(total_indicated) row[3] = self._fmt(total_pending) result.append(row) return result
def get_expected_callback(self, date, recipient_id): return ExpectedCallback.by_domain_recipient_date( self.domain, recipient_id, date )