Ejemplo n.º 1
0
    def rows(self):
        risk_profile = self.risk_profile
        result = []
        message_bank_messages = get_message_bank(self.domain,
                                                 for_comparing=True)
        data = {}
        for case in self.interactive_participants:
            data[case._id] = self.get_participant_message_counts(
                message_bank_messages, case)

        for entry in message_bank_messages:
            msg_risk_profile = entry["message"].risk_profile
            if risk_profile and risk_profile != msg_risk_profile:
                continue
            msg_risk_profile_desc = None
            if msg_risk_profile:
                msg_risk_profile_desc = PROFILE_DESC.get(msg_risk_profile)
            msg_risk_profile_desc = msg_risk_profile_desc or "-"
            row = [
                self._fmt(entry["message"].message),
                self._fmt2(entry["message"].fri_id, msg_risk_profile_desc),
            ]
            for case in self.interactive_participants:
                row.append(self._fmt(data[case._id][entry["message"]._id]))
            result.append(row)
        return result
Ejemplo n.º 2
0
def upload_message_bank(request, domain):
    if request.method == "POST":
        form = MessageBankForm(request.POST, request.FILES)
        if form.is_valid():
            current_bank = get_message_bank(domain)
            message_id_map = {}
            for message in current_bank:
                message_id_map[message.fri_id.upper()] = message

            # The message bank is supposed to be static, and is intended
            # to be a one-time upload. So to prevent any issues with
            # overwriting messages or deleting messages by accident, this
            # api will only add new messages to the message bank.
            # If more specialized functionality is needed later on, a new
            # UI should be built.
            for message in form.cleaned_data["message_bank_file"]:
                msg_id = message["msg_id"]
                text = message["text"]
                if msg_id.upper() not in message_id_map:
                    msg = FRIMessageBankMessage(
                        domain=domain,
                        risk_profile=msg_id[0].upper(),
                        message=text,
                        fri_id=msg_id,
                    )
                    msg.save()
            messages.success(request, _("Message Bank Uploaded."))
        else:
            messages.error(request,
                           form._errors["message_bank_file"].as_text())
    else:
        messages.error(request, _("ERROR: POST Expected."))
    return HttpResponseRedirect(
        reverse(CustomProjectReportDispatcher.name(),
                args=[domain, MessageBankReport.slug]))
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
def upload_message_bank(request, domain):
    if request.method == "POST":
        form = MessageBankForm(request.POST, request.FILES)
        if form.is_valid():
            current_bank = get_message_bank(domain)
            message_id_map = {}
            for message in current_bank:
                message_id_map[message.fri_id.upper()] = message

            # The message bank is supposed to be static, and is intended
            # to be a one-time upload. So to prevent any issues with
            # overwriting messages or deleting messages by accident, this
            # api will only add new messages to the message bank.
            # If more specialized functionality is needed later on, a new
            # UI should be built.
            for message in form.cleaned_data["message_bank_file"]:
                msg_id = message["msg_id"]
                text = message["text"]
                if msg_id.upper() not in message_id_map:
                    msg = FRIMessageBankMessage(
                        domain = domain,
                        risk_profile = msg_id[0].upper(),
                        message = text,
                        fri_id = msg_id,
                    )
                    msg.save()
            messages.success(request, _("Message Bank Uploaded."))
        else:
            messages.error(request, form._errors["message_bank_file"].as_text())
    else:
        messages.error(request, _("ERROR: POST Expected."))
    return HttpResponseRedirect(reverse(CustomProjectReportDispatcher.name(), args=[domain, MessageBankReport.slug]))
Ejemplo n.º 5
0
    def rows(self):
        data = SMS.by_domain(
            self.domain,
            start_date=self.datespan.startdate_utc,
            end_date=self.datespan.enddate_utc
        ).exclude(
            direction=OUTGOING,
            processed=False
        ).order_by('date')

        if self.show_only_survey_traffic():
            data = data.filter(
                xforms_session_couch_id__isnull=False
            )

        result = []
        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }
        message_bank_messages = get_message_bank(self.domain, for_comparing=True)

        FormProcessorInterface(self.domain).casedb_cache(
            domain=self.domain, strip_history=False, deleted_ok=True
        )
        user_cache = UserCache()

        for message in data:
            # 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
Ejemplo n.º 6
0
    def rows(self):
        data = SMS.by_domain(self.domain,
                             start_date=self.datespan.startdate_utc,
                             end_date=self.datespan.enddate_utc).exclude(
                                 direction=OUTGOING,
                                 processed=False).order_by('date')

        if self.show_only_survey_traffic():
            data = data.filter(xforms_session_couch_id__isnull=False)

        result = []
        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }
        message_bank_messages = get_message_bank(self.domain,
                                                 for_comparing=True)

        FormProcessorInterface(self.domain).casedb_cache(domain=self.domain,
                                                         strip_history=False,
                                                         deleted_ok=True)
        user_cache = UserCache()

        for message in data:
            # 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
Ejemplo n.º 7
0
    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)

        FormProcessorInterface(self.domain).casedb_cache(
            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
Ejemplo n.º 8
0
    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()

        for message in data:
            # 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 = tz_utils.adjust_datetime_to_timezone(message.date, pytz.utc.zone, self.timezone.zone)
            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
Ejemplo n.º 9
0
    def rows(self):
        risk_profile = self.risk_profile
        result = []
        message_bank_messages = get_message_bank(self.domain, for_comparing=True)
        data = {}
        for case in self.interactive_participants:
            data[case._id] = self.get_participant_message_counts(message_bank_messages, case)

        for entry in message_bank_messages:
            if risk_profile and risk_profile != entry["message"].risk_profile:
                continue
            row = [
                self._fmt(entry["message"].message),
                self._fmt(entry["message"].fri_id or "-"),
            ]
            for case in self.interactive_participants:
                row.append(self._fmt(data[case._id][entry["message"]._id]))
            result.append(row)
        return result
Ejemplo n.º 10
0
    def rows(self):
        risk_profile = self.risk_profile
        result = []
        message_bank_messages = get_message_bank(self.domain, for_comparing=True)
        data = {}
        for case in self.interactive_participants:
            data[case._id] = self.get_participant_message_counts(message_bank_messages, case)

        for entry in message_bank_messages:
            msg_risk_profile = entry["message"].risk_profile
            if risk_profile and risk_profile != msg_risk_profile:
                continue
            msg_risk_profile_desc = None
            if msg_risk_profile:
                msg_risk_profile_desc = PROFILE_DESC.get(msg_risk_profile)
            msg_risk_profile_desc = msg_risk_profile_desc or "-"
            row = [
                self._fmt(entry["message"].message),
                self._fmt2(entry["message"].fri_id, msg_risk_profile_desc),
            ]
            for case in self.interactive_participants:
                row.append(self._fmt(data[case._id][entry["message"]._id]))
            result.append(row)
        return result