def rows(self): startdate = json_format_datetime(self.datespan.startdate_utc) enddate = json_format_datetime(self.datespan.enddate_utc) data = FRISMSLog.view("sms/by_domain", startkey=[self.domain, "SMSLog", startdate], endkey=[self.domain, "SMSLog", enddate], include_docs=True, reduce=False).all() result = [] direction_map = { INCOMING: _("Incoming"), OUTGOING: _("Outgoing"), } message_bank_messages = get_message_bank(self.domain, for_comparing=True) case_cache = CaseDbCache(domain=self.domain, strip_history=False, deleted_ok=True) user_cache = UserCache() show_only_survey_traffic = self.show_only_survey_traffic() for message in data: if message.direction == OUTGOING and not message.processed: continue if show_only_survey_traffic and message.xforms_session_couch_id is None: continue # Add metadata from the message bank if it has not been added already if (message.direction == OUTGOING) and ( not message.fri_message_bank_lookup_completed): add_metadata(message, message_bank_messages) if message.couch_recipient_doc_type == "CommCareCase": recipient = case_cache.get(message.couch_recipient) else: recipient = user_cache.get(message.couch_recipient) if message.chat_user_id: sender = user_cache.get(message.chat_user_id) else: sender = None study_arm = None if message.couch_recipient_doc_type == "CommCareCase": study_arm = case_cache.get( message.couch_recipient).get_case_property("study_arm") timestamp = ServerTime(message.date).user_time( self.timezone).done() result.append([ self._fmt(self._participant_id(recipient)), self._fmt(study_arm or "-"), self._fmt(self._originator(message, recipient, sender)), self._fmt_timestamp(timestamp), self._fmt(message.text), self._fmt(message.fri_id or "-"), self._fmt(direction_map.get(message.direction, "-")), ]) return result
def testStripHistory(self): case_ids = _make_some_cases(3) history_cache = CaseDbCache() for i, id in enumerate(case_ids): self.assertFalse(history_cache.in_cache(id)) case = history_cache.get(id) self.assertEqual(str(i), case.my_index) self.assertTrue(len(case.actions) > 0) nohistory_cache = CaseDbCache(strip_history=True) for i, id in enumerate(case_ids): self.assertFalse(nohistory_cache.in_cache(id)) case = nohistory_cache.get(id) self.assertEqual(str(i), case.my_index) self.assertTrue(len(case.actions) == 0) more_case_ids = _make_some_cases(3) history_cache.populate(more_case_ids) nohistory_cache.populate(more_case_ids) for i, id in enumerate(more_case_ids): self.assertTrue(history_cache.in_cache(id)) case = history_cache.get(id) self.assertEqual(str(i), case.my_index) self.assertTrue(len(case.actions) > 0) for i, id in enumerate(more_case_ids): self.assertTrue(nohistory_cache.in_cache(id)) case = nohistory_cache.get(id) self.assertEqual(str(i), case.my_index) self.assertTrue(len(case.actions) == 0)
def testGetPopulatesCache(self): case_ids = _make_some_cases(3) cache = CaseDbCache() for id in case_ids: self.assertFalse(cache.in_cache(id)) for i, id in enumerate(case_ids): case = cache.get(id) self.assertEqual(str(i), case.my_index)
def rows(self): startdate = json_format_datetime(self.datespan.startdate_utc) enddate = json_format_datetime(self.datespan.enddate_utc) data = FRISMSLog.view("sms/by_domain", startkey=[self.domain, "SMSLog", startdate], endkey=[self.domain, "SMSLog", enddate], include_docs=True, reduce=False).all() result = [] direction_map = { INCOMING: _("Incoming"), OUTGOING: _("Outgoing"), } message_bank_messages = get_message_bank(self.domain, for_comparing=True) case_cache = CaseDbCache(domain=self.domain, strip_history=False, deleted_ok=True) user_cache = UserCache() show_only_survey_traffic = self.show_only_survey_traffic() for message in data: if message.direction == OUTGOING and not message.processed: continue if show_only_survey_traffic and message.xforms_session_couch_id is None: continue # Add metadata from the message bank if it has not been added already if (message.direction == OUTGOING) and (not message.fri_message_bank_lookup_completed): add_metadata(message, message_bank_messages) if message.couch_recipient_doc_type == "CommCareCase": recipient = case_cache.get(message.couch_recipient) else: recipient = user_cache.get(message.couch_recipient) if message.chat_user_id: sender = user_cache.get(message.chat_user_id) else: sender = None study_arm = None if message.couch_recipient_doc_type == "CommCareCase": study_arm = case_cache.get(message.couch_recipient).get_case_property("study_arm") timestamp = ServerTime(message.date).user_time(self.timezone).done() result.append([ self._fmt(self._participant_id(recipient)), self._fmt(study_arm or "-"), self._fmt(self._originator(message, recipient, sender)), self._fmt_timestamp(timestamp), self._fmt(message.text), self._fmt(message.fri_id or "-"), self._fmt(direction_map.get(message.direction,"-")), ]) return result
def testDocTypeCheck(self): id = uuid.uuid4().hex CommCareCase.get_db().save_doc({ "_id": id, "doc_type": "AintNoCasesHere" }) cache = CaseDbCache() try: cache.get(id) self.fail('doc type security check failed to raise exception') except IllegalCaseId: pass doc_back = CommCareCase.get_db().get(id) self.assertEqual("AintNoCasesHere", doc_back['doc_type'])
def testDomainCheck(self): id = uuid.uuid4().hex post_case_blocks([ CaseBlock(create=True, case_id=id, user_id='some-user', version=V2).as_xml() ], {'domain': 'good-domain'}) bad_cache = CaseDbCache(domain='bad-domain') try: bad_cache.get(id) self.fail('domain security check failed to raise exception') except IllegalCaseId: pass good_cache = CaseDbCache(domain='good-domain') case = good_cache.get(id) self.assertEqual( 'some-user', case.user_id) # just sanity check it's the right thing
def testDomainCheck(self): id = uuid.uuid4().hex post_case_blocks([ CaseBlock( create=True, case_id=id, user_id='some-user', version=V2 ).as_xml() ], {'domain': 'good-domain'} ) bad_cache = CaseDbCache(domain='bad-domain') try: bad_cache.get(id) self.fail('domain security check failed to raise exception') except IllegalCaseId: pass good_cache = CaseDbCache(domain='good-domain') case = good_cache.get(id) self.assertEqual('some-user', case.user_id) # just sanity check it's the right thing
def testGetPopulatesCache(self): case_ids = _make_some_cases(3) cache = CaseDbCache() for id in case_ids: self.assertFalse(cache.in_cache(id)) for i, id in enumerate(case_ids): case = cache.get(id) self.assertEqual(str(i), case.my_index) for id in case_ids: self.assertTrue(cache.in_cache(id))
def test_nowrap(self): case_ids = _make_some_cases(1) cache = CaseDbCache(wrap=False) case = cache.get(case_ids[0]) self.assertTrue(isinstance(case, dict)) self.assertFalse(isinstance(case, CommCareCase))