Ejemplo n.º 1
0
    def test_ReadOnlyObject(self):

        from couchdbkit import Document, StringListProperty
        log = []

        def read_log():
            x = log[:]
            del log[:]
            return x

        class Thing(Document):
            words = StringListProperty()
            @property
            def calc(self):
                for i, word in enumerate(self.words):
                    log.append(i)
                    yield word + '!'
        thing = Thing(words=['danny', 'is', 'so', 'clever'])
        thing = ReadOnlyObject(thing)
        self.assertEqual(thing.words, ['danny', 'is', 'so', 'clever'])
        self.assertEqual(thing.words, ['danny', 'is', 'so', 'clever'])
        self.assertIs(thing.words, thing.words)
        self.assertEqual(thing.calc, ['danny!', 'is!', 'so!', 'clever!'])
        self.assertEqual(read_log(), [0, 1, 2, 3])
        self.assertEqual(thing.calc, ['danny!', 'is!', 'so!', 'clever!'])
        self.assertEqual(read_log(), [])
        self.assertIs(thing.calc, thing.calc)
Ejemplo n.º 2
0
    def process_sql(self, doc_dict):
        doc = self.document_class.wrap(doc_dict)
        doc = ReadOnlyObject(doc)

        try:
            FormData.create_or_update_from_xforminstance(doc)
        except InvalidDataException, e:
            # this is a less severe class of errors
            logger.info(
                "FormDataPillow: bad update in form listener for line: %s\n%s"
                % (doc_dict, e))
Ejemplo n.º 3
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))
Ejemplo n.º 4
0
    def change_transform(self, doc_dict):
        doc = self.wrapper.wrap(doc_dict)
        doc = ReadOnlyObject(doc)

        if self.document_filter and not self.document_filter.filter(doc):
            return None

        indicator_id = '%s-%s' % (self.indicator_class.__name__, doc.get_id)

        try:
            current_indicator = self.indicator_class.get(indicator_id)
        except ResourceNotFound:
            current_indicator = None

        if not current_indicator:
            indicator = self.indicator_class(_id=indicator_id)
        else:
            indicator = current_indicator
            current_indicator = indicator.to_json()

        indicator.calculate(doc)
        return current_indicator, indicator
Ejemplo n.º 5
0
    def change_transform(self, doc_dict):
        delete = False
        doc = self.wrapper.wrap(doc_dict)
        doc = ReadOnlyObject(doc)

        if self.document_filter and not self.document_filter.filter(doc):
            if self.delete_filtered:
                delete = True
            else:
                return None

        indicator = _get_indicator_doc_from_class_and_id(self.indicator_class, doc.get_id)
        if delete or self._is_doc_type_deleted_match(doc.doc_type):
            indicator['id'] = doc.get_id
            delete = True
        else:
            indicator.calculate(doc)

        return {
            'doc_dict': doc_dict,
            'indicators': indicator,
            'delete': delete
        }