コード例 #1
0
 def get_sms_couch_ids(self):
     result = SMSLog.view(
         'sms/by_domain',
         include_docs=False,
         reduce=False,
     ).all()
     return [row['id'] for row in result if row['key'][1] == 'SMSLog']
コード例 #2
0
 def get_sms_couch_ids(self):
     result = SMSLog.view(
         'sms/by_domain',
         include_docs=False,
         reduce=False,
     ).all()
     return [row['id'] for row in result if row['key'][1] == 'SMSLog']
コード例 #3
0
 def get_sms_and_call_couch_count(self):
     result = SMSLog.view(
         'sms/by_domain',
         include_docs=False,
         reduce=True,
     ).all()
     if result:
         return result[0]['value']
     return 0
コード例 #4
0
 def get_last_outbound_sms(self, doc_type, contact_id):
     sms = SMSLog.view("sms/by_recipient",
                       startkey=[doc_type, contact_id, "SMSLog", "O", {}],
                       endkey=[doc_type, contact_id, "SMSLog", "O"],
                       descending=True,
                       include_docs=True,
                       reduce=False,
                       ).first()
     return sms
コード例 #5
0
 def get_items_to_be_processed(self, utcnow):
     # We're just querying for ids here, so no need to limit
     entries = SMSLog.view(
         "sms/queued_sms",
         startkey="1970-01-01T00:00:00Z",
         endkey=json_format_datetime(utcnow),
         include_docs=False
     ).all()
     return entries
コード例 #6
0
ファイル: migration.py プロジェクト: ekush/commcare-hq
 def getSMSLogCount(self):
     result = SMSLog.view(
         'sms/by_domain',
         startkey=[self.domain, 'SMSLog'],
         endkey=[self.domain, 'SMSLog', {}],
         include_docs=False,
         reduce=True,
     ).all()
     if result:
         return result[0]['value']
     return 0
コード例 #7
0
ファイル: migration.py プロジェクト: ansarbek/commcare-hq
 def getSMSLogCount(self):
     result = SMSLog.view(
         'sms/by_domain',
         startkey=[self.domain, 'SMSLog'],
         endkey=[self.domain, 'SMSLog', {}],
         include_docs=False,
         reduce=True,
     ).all()
     if result:
         return result[0]['value']
     return 0
コード例 #8
0
ファイル: migration.py プロジェクト: bazuzi/commcare-hq
    def tearDown(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()

        SMS.objects.filter(domain=self.domain).delete()
コード例 #9
0
 def handle(self, *labels, **options):
     messages = SMSLog.view("sms/migrate_smslog_2012_04", include_docs=True)
     print "Migrating MessageLog to SMSLog"
     for message in messages:            
         try:
             message.doc_type = "SMSLog"
             message.base_doc = "MessageLog"
             message.couch_recipient_doc_type = "CouchUser"
             message.save()
         except Exception as e:
             print "There was an error migrating message %s." % (message._id)
コード例 #10
0
ファイル: migration.py プロジェクト: ekush/commcare-hq
    def tearDown(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()

        SMS.objects.filter(domain=self.domain).delete()
コード例 #11
0
 def handle(self, *labels, **options):
     messages = SMSLog.view("sms/migrate_smslog_2012_04", include_docs=True)
     print "Migrating MessageLog to SMSLog"
     for message in messages:
         try:
             message.doc_type = "SMSLog"
             message.base_doc = "MessageLog"
             message.couch_recipient_doc_type = "CouchUser"
             message.save()
         except Exception as e:
             print "There was an error migrating message %s." % (
                 message._id)
コード例 #12
0
ファイル: util.py プロジェクト: sheelio/commcare-hq
 def get_last_outbound_sms(self, contact):
     # Not clear why this should be necessary, but without it the latest
     # sms may not be returned
     sleep(0.25)
     sms = SMSLog.view("sms/by_recipient",
         startkey=[contact.doc_type, contact._id, "SMSLog", "O", {}],
         endkey=[contact.doc_type, contact._id, "SMSLog", "O"],
         descending=True,
         include_docs=True,
         reduce=False,
     ).first()
     return sms
コード例 #13
0
ファイル: migration.py プロジェクト: ansarbek/commcare-hq
    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()
コード例 #14
0
 def get_sms_couch_ids(self):
     result = SMSLog.view("sms/by_domain", include_docs=False, reduce=False).all()
     return [row["id"] for row in result if row["key"][1] == "SMSLog"]
コード例 #15
0
ファイル: views.py プロジェクト: dszafranek/commcare-hq
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))
コード例 #16
0
 def get_items_to_be_processed(self, utcnow):
     # We're just querying for ids here, so no need to limit
     entries = SMSLog.view(
         "sms/queued_sms", startkey="1970-01-01T00:00:00Z", endkey=json_format_datetime(utcnow), include_docs=False
     ).all()
     return entries
コード例 #17
0
ファイル: views.py プロジェクト: rigambhir/commcare-hq
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 = {}
    for sms in data:
        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")
        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),
            }
        )
    return HttpResponse(json.dumps(result))