Beispiel #1
0
    def process_sql(self, doc_dict, delete=False):
        if delete or doc_dict['doc_type'] != 'CommCareCase':
            try:
                CaseData.objects.get(case_id=doc_dict['_id']).delete()
            except CaseData.DoesNotExist:
                pass
            return

        doc = self.document_class.wrap(doc_dict)
        doc = ReadOnlyObject(doc)
        try:
            CaseData.create_or_update_from_instance(doc)
        except InvalidDataException, e:
            # this is a less severe class of errors
            logger.info("CaseDataPillow: bad update in listener for line: %s\n%s" % (doc_dict, e))
Beispiel #2
0
    def setUp(self):
        delete_all_xforms()
        delete_all_cases()

        FormProcessorInterface.post_case_blocks(
            [CaseBlock(create=True, case_id="mother_case_id", case_type="mother-case").as_xml()],
            {"domain": TEST_DOMAIN},
        )

        self.case_id = "test_case_1"
        self.date_modified = datetime.utcnow() - timedelta(hours=1)
        self.date_modified = self.date_modified.replace(microsecond=0)
        FormProcessorInterface.post_case_blocks(
            [
                CaseBlock(
                    create=True,
                    case_id=self.case_id,
                    owner_id="owner",
                    user_id="user",
                    case_type="c_type",
                    case_name=("a" * TEST_NAME_LEN) + "123456789",
                    external_id="external_id",
                    date_modified=self.date_modified,
                    update={"foo": "bar"},
                    index={"mom": ("mother-case", "mother_case_id")},
                ).as_xml()
            ],
            {"domain": TEST_DOMAIN},
        )

        instance = CommCareCase.get(self.case_id)
        self.casedata = CaseData.create_or_update_from_instance(instance)
Beispiel #3
0
    def setUp(self):
        delete_all_xforms()
        delete_all_cases()

        post_case_blocks([
            CaseBlock(
                create=True,
                case_id='mother_case_id',
                case_type='mother-case',
            ).as_xml()
        ], {'domain': TEST_DOMAIN})

        self.case_id = 'test_case_1'
        self.date_modified = datetime.utcnow() - timedelta(hours=1)
        self.date_modified = self.date_modified.replace(microsecond=0)
        post_case_blocks([
            CaseBlock(
                create=True,
                case_id=self.case_id,
                owner_id="owner",
                user_id='user',
                case_type='c_type',
                case_name=('a' * TEST_NAME_LEN) + '123456789',
                external_id='external_id',
                date_modified=self.date_modified,
                update={'foo': 'bar'},
                index={'mom': ('mother-case', 'mother_case_id')}
            ).as_xml()
        ], {'domain': TEST_DOMAIN})

        instance = CommCareCase.get(self.case_id)
        self.casedata = CaseData.create_or_update_from_instance(instance)
Beispiel #4
0
    def setUp(self):
        delete_all_xforms()
        delete_all_cases()

        post_case_blocks([
            CaseBlock(
                create=True,
                case_id='mother_case_id',
                case_type='mother-case',
                version=V2,
            ).as_xml()
        ], {'domain': TEST_DOMAIN})

        self.case_id = 'test_case_1'
        self.date_modified = datetime.utcnow() - timedelta(hours=1)
        self.date_modified = self.date_modified.replace(microsecond=0)
        post_case_blocks([
            CaseBlock(
                create=True,
                case_id=self.case_id,
                owner_id="owner",
                user_id='user',
                case_type='c_type',
                case_name=('a' * TEST_NAME_LEN) + '123456789',
                external_id='external_id',
                date_modified=self.date_modified,
                version=V2,
                update={'foo': 'bar'},
                index={'mom': ('mother-case', 'mother_case_id')}
            ).as_xml()
        ], {'domain': TEST_DOMAIN})

        instance = CommCareCase.get(self.case_id)
        self.casedata = CaseData.create_or_update_from_instance(instance)
Beispiel #5
0
    def process_sql(self, doc_dict, delete=False):
        if delete or doc_dict['doc_type'] != 'CommCareCase':
            try:
                CaseData.objects.get(case_id=doc_dict['_id']).delete()
            except CaseData.DoesNotExist:
                pass
            return

        doc = self.document_class.wrap(doc_dict)
        doc = ReadOnlyObject(doc)
        try:
            CaseData.create_or_update_from_instance(doc)
        except InvalidDataException, e:
            # this is a less severe class of errors
            logger.info(
                "CaseDataPillow: bad update in listener for line: %s\n%s" %
                (doc_dict, e))
Beispiel #6
0
    def test_empty_name(self):
        case_id = "case_with_no_name"
        FormProcessorInterface.post_case_blocks(
            [CaseBlock(create=True, case_id=case_id, case_type="nameless").as_xml()], {"domain": TEST_DOMAIN}
        )

        instance = CommCareCase.get(case_id)
        casedata = CaseData.create_or_update_from_instance(instance)
        self.assertIsNotNone(casedata)
        self.assertEqual("", casedata.name)
Beispiel #7
0
    def test_empty_name(self):
        case_id = 'case_with_no_name'
        post_case_blocks([
            CaseBlock(
                create=True,
                case_id=case_id,
                case_type='nameless',
            ).as_xml()
        ], {'domain': TEST_DOMAIN})

        instance = CommCareCase.get(case_id)
        casedata = CaseData.create_or_update_from_instance(instance)
        self.assertIsNotNone(casedata)
        self.assertEqual('', casedata.name)
Beispiel #8
0
    def test_update(self):
        FormProcessorInterface.post_case_blocks(
            [
                CaseBlock(
                    create=True, case_id="grand_mother_case_id", case_type="mother-case", owner_id="owner"
                ).as_xml()
            ],
            {"domain": TEST_DOMAIN},
        )

        date_modified = datetime.utcnow()
        FormProcessorInterface.post_case_blocks(
            [
                CaseBlock(
                    close=True,
                    case_id=self.case_id,
                    user_id="user2",
                    date_modified=date_modified,
                    index={"gmom": ("mother-case", "grand_mother_case_id")},
                ).as_xml()
            ],
            {"domain": TEST_DOMAIN},
        )

        instance = CommCareCase.get(self.case_id)
        updateddata = CaseData.create_or_update_from_instance(instance)
        self.assertEqual(date_modified, updateddata.modified_on)
        self.assertEqual("user2", updateddata.modified_by)
        self.assertEqual(date_modified, updateddata.closed_on)
        self.assertEqual(True, updateddata.closed)

        actions = updateddata.actions.all()
        self.assertEqual(5, len(actions))
        for action in actions:
            if action.index == 4:
                self.assertEqual("close", action.action_type)
                self.assertEqual(date.today(), action.date.date())
                self.assertEqual(date.today(), action.server_date.date())
                self.assertEqual("user2", action.user_id)
                self.assertEqual("owner", action.case_owner)
                self.assertEqual("c_type", action.case_type)

        indices = self.casedata.indices.all()
        self.assertEqual(2, len(indices))
        self.assertEqual("gmom", indices[0].identifier)
        self.assertEqual("mother-case", indices[0].referenced_type)
        self.assertEqual("grand_mother_case_id", indices[0].referenced_id)
        self.assertEqual("mom", indices[1].identifier)
        self.assertEqual("mother-case", indices[1].referenced_type)
        self.assertEqual("mother_case_id", indices[1].referenced_id)
Beispiel #9
0
    def test_update(self):
        post_case_blocks([
            CaseBlock(
                create=True,
                case_id='grand_mother_case_id',
                case_type='mother-case',
                owner_id='owner',
                version=V2,
            ).as_xml()
        ], {'domain': TEST_DOMAIN})

        date_modified = datetime.utcnow()
        post_case_blocks([
            CaseBlock(
                close=True,
                case_id=self.case_id,
                user_id='user2',
                date_modified=date_modified,
                version=V2,
                index={'gmom': ('mother-case', 'grand_mother_case_id')}
            ).as_xml()
        ], {'domain': TEST_DOMAIN})

        instance = CommCareCase.get(self.case_id)
        updateddata = CaseData.create_or_update_from_instance(instance)
        self.assertEqual(date_modified, updateddata.modified_on)
        self.assertEqual('user2', updateddata.modified_by)
        self.assertEqual(date_modified, updateddata.closed_on)
        self.assertEqual(True, updateddata.closed)

        actions = updateddata.actions.all()
        self.assertEqual(5, len(actions))
        for action in actions:
            if action.index == 4:
                self.assertEqual('close', action.action_type)
                self.assertEqual(date.today(), action.date.date())
                self.assertEqual(date.today(), action.server_date.date())
                self.assertEqual('user2', action.user_id)
                self.assertEqual('owner', action.case_owner)
                self.assertEqual('c_type', action.case_type)

        indices = self.casedata.indices.all()
        self.assertEqual(2, len(indices))
        self.assertEqual('gmom', indices[0].identifier)
        self.assertEqual('mother-case', indices[0].referenced_type)
        self.assertEqual('grand_mother_case_id', indices[0].referenced_id)
        self.assertEqual('mom', indices[1].identifier)
        self.assertEqual('mother-case', indices[1].referenced_type)
        self.assertEqual('mother_case_id', indices[1].referenced_id)
Beispiel #10
0
    def test_empty_name(self):
        case_id = 'case_with_no_name'
        post_case_blocks([
            CaseBlock(
                create=True,
                case_id=case_id,
                case_type='nameless',
                version=V2,
            ).as_xml(format_datetime=None)
        ], {'domain': TEST_DOMAIN})

        instance = CommCareCase.get(case_id)
        casedata = CaseData.create_or_update_from_instance(instance)
        self.assertIsNotNone(casedata)
        self.assertIsNone(casedata.name)
Beispiel #11
0
def get_casedata(case_info, domain, user_id, owner_id, opened_by, closed_by):
    now = datetime.utcnow()
    date_ago = now - timedelta(days=case_info.days_ago)
    return CaseData(
        case_id=case_info.id,
        type=case_info.case_type,
        domain=domain,
        owner_id=owner_id,
        user_id=user_id,
        opened_on=date_ago,
        opened_by=opened_by or user_id,
        modified_on=now,
        closed=case_info.is_closed,
        closed_on=(date_ago if case_info.is_closed else None),
        closed_by=(closed_by or user_id) if case_info.is_closed else None,
        case_owner=(owner_id or user_id))
    return case
Beispiel #12
0
    def test_empty_owner_id(self):
        case_id = 'case_with_no_owner'
        post_case_blocks([
            CaseBlock(
                create=True,
                case_id=case_id,
                user_id='user',
                case_type='c_type',
                case_name='bob',
                date_modified=self.date_modified,
                update={'foo': 'bar'},
            ).as_xml()
        ], {'domain': TEST_DOMAIN})

        instance = CommCareCase.get(case_id)
        casedata = CaseData.create_or_update_from_instance(instance)
        self.assertIsNotNone(casedata)
        self.assertEqual('user', casedata.case_owner)
Beispiel #13
0
    def test_empty_owner_id(self):
        case_id = 'case_with_no_owner'
        post_case_blocks([
            CaseBlock(
                create=True,
                case_id=case_id,
                user_id='user',
                case_type='c_type',
                case_name='bob',
                date_modified=self.date_modified,
                version=V2,
                update={'foo': 'bar'},
            ).as_xml()
        ], {'domain': TEST_DOMAIN})

        instance = CommCareCase.get(case_id)
        casedata = CaseData.create_or_update_from_instance(instance)
        self.assertIsNotNone(casedata)
        self.assertEqual('user', casedata.case_owner)
Beispiel #14
0
    def test_empty_owner_id(self):
        case_id = "case_with_no_owner"
        FormProcessorInterface.post_case_blocks(
            [
                CaseBlock(
                    create=True,
                    case_id=case_id,
                    user_id="user",
                    case_type="c_type",
                    case_name="bob",
                    date_modified=self.date_modified,
                    update={"foo": "bar"},
                ).as_xml()
            ],
            {"domain": TEST_DOMAIN},
        )

        instance = CommCareCase.get(case_id)
        casedata = CaseData.create_or_update_from_instance(instance)
        self.assertIsNotNone(casedata)
        self.assertEqual("user", casedata.case_owner)