def testSQLSyncToCouch(self): self.deleteAllLogs() self.assertEqual(self.getCouchCount(), 0) self.assertEqual(self.getSQLCount(), 0) # Test Create sql_obj = SQLLastReadMessage() self.setRandomSQLObjectValues(sql_obj) sql_obj.save() sleep(1) self.assertEqual(self.getCouchCount(), 1) self.assertEqual(self.getSQLCount(), 1) couch_obj = LastReadMessage.get(sql_obj.couch_id) self.checkFieldValues(couch_obj, sql_obj, SQLLastReadMessage._migration_get_fields()) self.assertTrue(LastReadMessage.get_db().get_rev(couch_obj._id).startswith('2-')) # Test Update self.setRandomSQLObjectValues(sql_obj) sql_obj.save() sleep(1) self.assertEqual(self.getCouchCount(), 1) self.assertEqual(self.getSQLCount(), 1) couch_obj = LastReadMessage.get(sql_obj.couch_id) self.checkFieldValues(couch_obj, sql_obj, SQLLastReadMessage._migration_get_fields()) self.assertTrue(LastReadMessage.get_db().get_rev(couch_obj._id).startswith('3-'))
def api_last_read_message(request, domain): contact_id = request.GET.get("contact_id", None) domain_obj = Domain.get_by_name(domain, strict=True) if domain_obj.count_messages_as_read_by_anyone: lrm = LastReadMessage.by_anyone(domain, contact_id) else: lrm = LastReadMessage.by_user(domain, request.couch_user._id, contact_id) result = { "message_timestamp" : None, } if lrm: result["message_timestamp"] = json_format_datetime(lrm.message_timestamp) return HttpResponse(json.dumps(result))
def get_lastreadmessage_couch_ids(self): result = LastReadMessage.view( 'sms/last_read_message', startkey=['by_anyone'], endkey=['by_anyone', {}], include_docs=False, ).all() return [row['id'] for row in result]
def getCouchCount(self): result = LastReadMessage.view( 'sms/last_read_message', startkey=['by_anyone', self.domain], endkey=['by_anyone', self.domain, {}], include_docs=False, ).all() return len(result)
def deleteAllLogs(self): for smslog in SMSLog.view( 'sms/by_domain', startkey=[self.domain, 'SMSLog'], endkey=[self.domain, 'SMSLog', {}], include_docs=True, reduce=False, ).all(): smslog.delete() for callog in CallLog.view( 'sms/by_domain', startkey=[self.domain, 'CallLog'], endkey=[self.domain, 'CallLog', {}], include_docs=True, reduce=False, ).all(): callog.delete() for obj in LastReadMessage.view( 'sms/last_read_message', startkey=['by_anyone', self.domain], endkey=['by_anyone', self.domain, {}], include_docs=True, ).all(): obj.delete() for obj in ExpectedCallbackEventLog.view( 'sms/expected_callback_event', startkey=[self.domain], endkey=[self.domain, {}], include_docs=True, ).all(): obj.delete() SMS.objects.filter(domain=self.domain).delete() Call.objects.filter(domain=self.domain).delete() MessagingSubEvent.objects.filter(parent__domain=self.domain).delete() MessagingEvent.objects.filter(domain=self.domain).delete() SQLLastReadMessage.objects.filter(domain=self.domain).delete() ExpectedCallback.objects.filter(domain=self.domain).delete()
def delete_models(self, delete_interval): print 'Deleting SMSLogs...' count = iter_bulk_delete_with_doc_type_verification(SMSLog.get_db(), self.get_sms_couch_ids(), 'SMSLog', wait_time=delete_interval, max_fetch_attempts=5) print 'Deleted %s documents' % count print 'Deleting CallLogs...' count = iter_bulk_delete_with_doc_type_verification(CallLog.get_db(), self.get_call_couch_ids(), 'CallLog', wait_time=delete_interval, max_fetch_attempts=5) print 'Deleted %s documents' % count print 'Deleting ExpectedCallbackEventLogs...' count = iter_bulk_delete_with_doc_type_verification(ExpectedCallbackEventLog.get_db(), self.get_callback_couch_ids(), 'ExpectedCallbackEventLog', wait_time=delete_interval, max_fetch_attempts=5) print 'Deleted %s documents' % count print 'Deleting LastReadMessages...' count = iter_bulk_delete_with_doc_type_verification(LastReadMessage.get_db(), self.get_lastreadmessage_couch_ids(), 'LastReadMessage', wait_time=delete_interval, max_fetch_attempts=5) print 'Deleted %s documents' % count
def delete_models(self, delete_interval): print('Deleting SMSLogs...') count = iter_bulk_delete_with_doc_type_verification(SMSLog.get_db(), self.get_sms_couch_ids(), 'SMSLog', wait_time=delete_interval, max_fetch_attempts=5) print('Deleted %s documents' % count) print('Deleting CallLogs...') count = iter_bulk_delete_with_doc_type_verification(CallLog.get_db(), self.get_call_couch_ids(), 'CallLog', wait_time=delete_interval, max_fetch_attempts=5) print('Deleted %s documents' % count) print('Deleting ExpectedCallbackEventLogs...') count = iter_bulk_delete_with_doc_type_verification(ExpectedCallbackEventLog.get_db(), self.get_callback_couch_ids(), 'ExpectedCallbackEventLog', wait_time=delete_interval, max_fetch_attempts=5) print('Deleted %s documents' % count) print('Deleting LastReadMessages...') count = iter_bulk_delete_with_doc_type_verification(LastReadMessage.get_db(), self.get_lastreadmessage_couch_ids(), 'LastReadMessage', wait_time=delete_interval, max_fetch_attempts=5) print('Deleted %s documents' % count)
def api_history(request, domain): result = [] contact_id = request.GET.get("contact_id", None) start_date = request.GET.get("start_date", None) timezone = report_utils.get_timezone(None, domain) domain_obj = Domain.get_by_name(domain, strict=True) try: assert contact_id is not None doc = get_contact(contact_id) assert doc is not None assert doc.domain == domain except Exception: return HttpResponse("[]") query_start_date_str = None if start_date is not None: try: query_start_date = parse(start_date) query_start_date += timedelta(seconds=1) query_start_date_str = json_format_datetime(query_start_date) except Exception: pass if query_start_date_str is not None: data = SMSLog.view("sms/by_recipient", startkey=[doc.doc_type, contact_id, "SMSLog", INCOMING, query_start_date_str], endkey=[doc.doc_type, contact_id, "SMSLog", INCOMING, {}], include_docs=True, reduce=False).all() data += SMSLog.view("sms/by_recipient", startkey=[doc.doc_type, contact_id, "SMSLog", OUTGOING, query_start_date_str], endkey=[doc.doc_type, contact_id, "SMSLog", OUTGOING, {}], include_docs=True, reduce=False).all() else: data = SMSLog.view("sms/by_recipient", startkey=[doc.doc_type, contact_id, "SMSLog"], endkey=[doc.doc_type, contact_id, "SMSLog", {}], include_docs=True, reduce=False).all() data.sort(key=lambda x : x.date) username_map = {} last_sms = None for sms in data: # Don't show outgoing SMS that haven't been processed yet if sms.direction == OUTGOING and not sms.processed: continue # Filter SMS that are tied to surveys if necessary if ((domain_obj.filter_surveys_from_chat and sms.xforms_session_couch_id) and not (domain_obj.show_invalid_survey_responses_in_chat and sms.direction == INCOMING and sms.invalid_survey_response)): continue if sms.direction == INCOMING: if doc.doc_type == "CommCareCase" and domain_obj.custom_case_username: sender = doc.get_case_property(domain_obj.custom_case_username) elif doc.doc_type == "CommCareCase": sender = doc.name else: sender = doc.first_name or doc.raw_username elif sms.chat_user_id is not None: if sms.chat_user_id in username_map: sender = username_map[sms.chat_user_id] else: try: user = CouchUser.get_by_user_id(sms.chat_user_id) sender = user.first_name or user.raw_username except Exception: sender = _("Unknown") username_map[sms.chat_user_id] = sender else: sender = _("System") last_sms = sms result.append({ "sender" : sender, "text" : sms.text, "timestamp" : tz_utils.adjust_datetime_to_timezone(sms.date, pytz.utc.zone, timezone.zone).strftime("%I:%M%p %m/%d/%y").lower(), "utc_timestamp" : json_format_datetime(sms.date), }) if last_sms: try: entry, lock = LastReadMessage.get_locked_obj( sms.domain, request.couch_user._id, sms.couch_recipient, create=True ) if (not entry.message_timestamp or entry.message_timestamp < last_sms.date): entry.message_id = last_sms._id entry.message_timestamp = last_sms.date entry.save() lock.release() except: logging.exception("Could not create/save LastReadMessage for message %s" % last_sms._id) # Don't let this block returning of the data pass return HttpResponse(json.dumps(result))