Esempio n. 1
0
    def get_payload(self, repeat_record, payload_doc):

        case_ids_to_forward = payload_doc.get_case_property('cases_to_forward')
        if not case_ids_to_forward:
            raise ReferralError(
                f'No cases included in transfer. Please add case ids to "cases_to_forward" property'
            )
        else:
            case_ids_to_forward = case_ids_to_forward.split(' ')
        new_owner = payload_doc.get_case_property('new_owner')
        cases_to_forward = CaseAccessors(
            payload_doc.domain).get_cases(case_ids_to_forward)
        case_ids_to_forward = set(case_ids_to_forward)
        included_case_types = payload_doc.get_case_property(
            'case_types').split(' ')
        case_type_configs = {}
        for case_type in included_case_types:
            constant_properties = []
            for key, value in payload_doc.case_json.items():
                constant_prefix = f'{case_type}_setter_'
                if key.startswith(constant_prefix):
                    property_name = key[len(constant_prefix):]
                    constant_properties.append((property_name, value))
            whitelist = payload_doc.case_json.get(f'{case_type}_whitelist')
            blacklist = payload_doc.case_json.get(f'{case_type}_blacklist')
            if blacklist and whitelist:
                raise ReferralError(
                    f'both blacklist and whitelist included for {case_type}')
            if not blacklist and not whitelist:
                raise ReferralError(
                    f'blacklist or whitelist not included for {case_type}')
            if blacklist:
                listed_properties = blacklist.split(' ')
                use_blacklist = True
            else:
                listed_properties = whitelist.split(' ')
                use_blacklist = False
            case_type_configs[case_type] = CaseTypeReferralConfig(
                use_blacklist, listed_properties, constant_properties)

        case_blocks = self._get_case_blocks(cases_to_forward,
                                            case_ids_to_forward,
                                            case_type_configs, new_owner)
        return render_to_string(
            'hqcase/xml/case_block.xml', {
                'xmlns': SYSTEM_FORM_XMLNS,
                'case_block': case_blocks,
                'time': datetime.utcnow(),
                'uid': uuid4().hex,
                'username': self.repeater.username,
                'user_id': CouchUser.get_by_username(
                    self.repeater.username).user_id,
                'device_id': "ReferCaseRepeater",
            })
 def _get_case_blocks(self, cases_to_forward, case_ids_to_forward,
                      case_type_configs, new_owner):
     case_blocks = []
     case_id_map = {}
     for case in cases_to_forward:
         original_id = case.case_id
         indices = case.indices
         case.case_id = self._get_updated_case_id(original_id, case_id_map)
         case.owner_id = new_owner
         for index in indices:
             if index.referenced_id in case_ids_to_forward:
                 index.referenced_id = self._get_updated_case_id(
                     index.referenced_id, case_id_map)
             else:
                 raise ReferralError(
                     f'case {original_id} included without referenced case {index.referenced_id}'
                 )
         config = case_type_configs[case.type]
         if config.use_blacklist:
             self._update_case_properties_with_blacklist(case, config)
         else:
             self._update_case_properties_with_whitelist(case, config)
         self._set_constant_properties(case, config)
         self._set_referral_properties(case, original_id)
         case_blocks.append(case.to_xml(V2).decode('utf-8'))
     case_blocks = ''.join(case_blocks)
     return case_blocks