Пример #1
0
    def test_filter(self):
        class User(dico.Document):
            id = dico.IntegerField()

            @classmethod
            def rename_id(cls, filter_dict):
                if 'id' in filter_dict:
                    filter_dict['_id'] = filter_dict['id']
                    del filter_dict['id']
                return filter_dict

            def before_save(self, filter_dict):
                filter_dict['name'] = 'Paule'
                return self.rename_id(filter_dict)

        User.add_view("save", filter="before_save")

        user = User()
        user.id = 53

        self.assertIn('_id', user.to_save())
        self.assertIn('name', user.to_save())

        class User(dico.Document):
            id = dico.IntegerField()

        User.add_view("save", filter=dico.rename_field('id', '_id'))

        user = User()
        user.id = 53
        self.assertIn('_id', user.to_save())
Пример #2
0
 def rename_id(data_dict):
     data_dict = dico.rename_field('_id', 'id')(data_dict)
     data_dict = dico.rename_field('aid', 'id')(data_dict)
     return data_dict