Exemple #1
0
 def case_block_ok(case_updates):
     case_ids = set()
     for case_update in case_updates:
         case_ids.add(case_update.id)
         create_action = case_update.get_create_action()
         update_action = case_update.get_update_action()
         index_action = case_update.get_index_action()
         if create_action:
             if create_action.user_id not in ('demo_user', None):
                 return False
             if create_action.owner_id not in ('demo_user', None):
                 return False
         if update_action:
             if update_action.owner_id not in ('demo_user', None):
                 return False
         if index_action:
             for index in index_action.indices:
                 case_ids.add(index.referenced_id)
     cases = CommCareCase.bulk_get_lite(list(case_ids))
     for case in cases:
         if case.domain != domain:
             return False
         if case.owner_id or case.user_id != 'demo_user':
             return False
     return True
Exemple #2
0
    def case_block_ok(case_updates):
        """
        Check for all cases that we are submitting as demo_user and that the domain we
        are submitting against for any previously existing cases matches the submission
        domain.
        """
        allowed_ids = ('demo_user', 'demo_user_group_id', None)
        case_ids = set()
        for case_update in case_updates:
            case_ids.add(case_update.id)
            create_action = case_update.get_create_action()
            update_action = case_update.get_update_action()
            index_action = case_update.get_index_action()
            if create_action:
                if create_action.user_id not in allowed_ids:
                    return False
                if create_action.owner_id not in allowed_ids:
                    return False
            if update_action:
                if update_action.owner_id not in allowed_ids:
                    return False
            if index_action:
                for index in index_action.indices:
                    case_ids.add(index.referenced_id)

        # todo: consider whether we want to remove this call, and/or pass the result
        # through to the next function so we don't have to get the cases again later
        cases = CommCareCase.bulk_get_lite(list(case_ids))
        for case in cases:
            if case.domain != domain:
                return False
            if case.owner_id or case.user_id not in allowed_ids:
                return False
        return True
Exemple #3
0
    def case_updates_to_sync(self):
        other_case_ids_on_phone = set([
            case_id
            for case_id in self.last_sync.get_footprint_of_cases_on_phone()
            if case_id not in self.global_state.actual_relevant_cases_dict
        ])

        logger.debug("%s other cases on phone", len(other_case_ids_on_phone))
        if not other_case_ids_on_phone:
            return []

        if self.use_minimal_cases:
            other_cases_on_phone = [
                self.global_state.minimal_cases[case_id] for case_id in other_case_ids_on_phone
            ]
        else:
            other_cases_on_phone = CommCareCase.bulk_get_lite(
                other_case_ids_on_phone,
                wrap=False,
                chunksize=len(other_case_ids_on_phone)
            )

        potential_to_sync = self._get_potential_cases(other_cases_on_phone)
        cases_to_sync = self._fetch_missing_cases_and_wrap(potential_to_sync)
        case_sync_updates = self._case_sync_updates(cases_to_sync)

        self.global_state.update_synced_cases(case_sync_updates)
        return case_sync_updates
Exemple #4
0
    def case_block_ok(case_updates):
        """
        Check for all cases that we are submitting as demo_user and that the domain we
        are submitting against for any previously existing cases matches the submission
        domain.
        """
        allowed_ids = ('demo_user', 'demo_user_group_id', None)
        case_ids = set()
        for case_update in case_updates:
            case_ids.add(case_update.id)
            create_action = case_update.get_create_action()
            update_action = case_update.get_update_action()
            index_action = case_update.get_index_action()
            if create_action:
                if create_action.user_id not in allowed_ids:
                    return False
                if create_action.owner_id not in allowed_ids:
                    return False
            if update_action:
                if update_action.owner_id not in allowed_ids:
                    return False
            if index_action:
                for index in index_action.indices:
                    case_ids.add(index.referenced_id)

        # todo: consider whether we want to remove this call, and/or pass the result
        # through to the next function so we don't have to get the cases again later
        cases = CommCareCase.bulk_get_lite(list(case_ids))
        for case in cases:
            if case.domain != domain:
                return False
            if case.owner_id or case.user_id not in allowed_ids:
                return False
Exemple #5
0
def iter_cases(case_ids, strip_history=False, wrap=True):
    from casexml.apps.case.models import CommCareCase
    if not strip_history:
        for doc in iter_docs(CommCareCase.get_db(), case_ids):
            yield CommCareCase.wrap(doc) if wrap else doc
    else:
        for case in CommCareCase.bulk_get_lite(case_ids, wrap=wrap):
            yield case
Exemple #6
0
def iter_cases(case_ids, strip_history=False, wrap=True):
    from casexml.apps.case.models import CommCareCase
    if not strip_history:
        for doc in iter_docs(CommCareCase.get_db(), case_ids):
            yield CommCareCase.wrap(doc) if wrap else doc
    else:
        for case in CommCareCase.bulk_get_lite(case_ids, wrap=wrap):
            yield case
Exemple #7
0
 def iter_cases(self, ids):
     database = CommCareCase.get_db()
     if not self.strip_history:
         for doc in iter_docs(database, ids):
             yield CommCareCase.wrap(doc)
     else:
         for doc_ids in chunked(ids, 100):
             for case in CommCareCase.bulk_get_lite(doc_ids):
                 yield case
Exemple #8
0
 def iter_cases(self, ids):
     database = CommCareCase.get_db()
     if not self.strip_history:
         for doc in iter_docs(database, ids):
             yield CommCareCase.wrap(doc)
     else:
         for doc_ids in chunked(ids, 100):
             for case in CommCareCase.bulk_get_lite(doc_ids):
                 yield case
Exemple #9
0
    def _fetch_missing_cases_and_wrap(self, casedoc_list):
        cases = []
        to_fetch = []
        for doc in casedoc_list:
            if doc['doc_type'] == 'CommCareCase':
                cases.append(CommCareCase.wrap(doc))
            else:
                to_fetch.append(doc['_id'])

        cases.extend(CommCareCase.bulk_get_lite(to_fetch, wrap=True, chunksize=self.chunksize))
        return cases
Exemple #10
0
    def _actual_owned_cases(self):
        """
        This returns a list of case dicts. Each dict will either be an actual case dict or else
        a dict containing only these keys: '_id', 'type', 'indices'. These 'minimal cases' are
        created from CaseState objects from the previous SyncLog.
        """
        def _case_domain_match(case):
            return not self.domain or self.domain == case.get('domain')

        case_ids = self._get_case_ids()
        if self.use_minimal_cases:
            # First we check to see if there is a case state available that we can use
            # rather than fetching the whole case.
            minimal_cases = []
            cases_to_fetch = []
            for case_id in case_ids:
                minimal_case = self.global_state.minimal_cases.get(case_id)
                if minimal_case:
                    minimal_cases.append(minimal_case)
                else:
                    cases_to_fetch.append(case_id)

            logger.debug(
                "%s cases found in previous SyncLog. %s still to fetch",
                len(minimal_cases), len(cases_to_fetch)
            )

            if cases_to_fetch:
                cases = CommCareCase.bulk_get_lite(cases_to_fetch, wrap=False, chunksize=self.chunksize)
                minimal_cases.extend(
                    case_doc for case_doc in cases
                    if _case_domain_match(case_doc)
                )
            return minimal_cases
        else:
            lite_cases = list(iter_lite_cases_json(case_ids, self.chunksize))
            logger.debug("No previous SyncLog. Fetched %s cases", len(lite_cases))
            return lite_cases
Exemple #11
0
 def case_block_ok(case_updates):
     case_ids = set()
     for case_update in case_updates:
         case_ids.add(case_update.id)
         create_action = case_update.get_create_action()
         update_action = case_update.get_update_action()
         index_action = case_update.get_index_action()
         if create_action:
             if create_action.user_id not in ('demo_user', None):
                 return False
             if create_action.owner_id not in ('demo_user', None):
                 return False
         if update_action:
             if update_action.owner_id not in ('demo_user', None):
                 return False
         if index_action:
             for index in index_action.indices:
                 case_ids.add(index.referenced_id)
     cases = CommCareCase.bulk_get_lite(list(case_ids))
     for case in cases:
         if case.domain != domain:
             return False
         if case.owner_id or case.user_id != 'demo_user':
             return False