Пример #1
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)

        case_cache = 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
Пример #2
0
def process_stock(xforms, case_db=None):
    """
    process the commtrack xml constructs in an incoming submission
    """
    if not case_db:
        case_db = FormProcessorInterface(xforms[0].domain).casedb_cache()
    else:
        assert isinstance(case_db, AbstractCaseDbCache)

    sorted_forms = sorted(xforms, key=lambda f: 0 if f.is_deprecated else 1)
    stock_report_helpers = []
    case_action_intents = []
    for xform in sorted_forms:
        actions_for_form = get_stock_actions(xform)
        stock_report_helpers += actions_for_form.stock_report_helpers
        case_action_intents += actions_for_form.case_action_intents

    # omitted: normalize_transactions (used for bulk requisitions?)

    # validate the parsed transactions
    for stock_report_helper in stock_report_helpers:
        stock_report_helper.validate()

    relevant_cases = []
    # touch every case for proper ota restore logic syncing to be preserved
    for action_intent in case_action_intents:
        case_id = action_intent.case_id
        case = case_db.get(action_intent.case_id)
        relevant_cases.append(case)
        if case is None:
            raise IllegalCaseId(
                _('Ledger transaction references invalid Case ID "{}"')
                .format(case_id))

        if action_intent.is_deprecation:
            # just remove the old stock actions for the form from the case
            case.actions = [
                a for a in case.actions if not
                (a.xform_id == action_intent.form_id and a.action_type == CASE_ACTION_COMMTRACK)
            ]
        else:
            case_action = action_intent.action
            # hack: clear the sync log id so this modification always counts
            # since consumption data could change server-side
            case_action.sync_log_id = ''
            case.actions.append(case_action)
            if action_intent.form_id not in case.xform_ids:
                case.xform_ids.append(action_intent.form_id)

        case_db.mark_changed(case)

    return StockProcessingResult(
        xform=sorted_forms[-1],
        relevant_cases=relevant_cases,
        stock_report_helpers=stock_report_helpers,
    )