当前位置: 首页>>代码示例>>Python>>正文


Python CaseFactory.post_case_blocks方法代码示例

本文整理汇总了Python中casexml.apps.case.mock.CaseFactory.post_case_blocks方法的典型用法代码示例。如果您正苦于以下问题:Python CaseFactory.post_case_blocks方法的具体用法?Python CaseFactory.post_case_blocks怎么用?Python CaseFactory.post_case_blocks使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在casexml.apps.case.mock.CaseFactory的用法示例。


在下文中一共展示了CaseFactory.post_case_blocks方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MockDevice

# 需要导入模块: from casexml.apps.case.mock import CaseFactory [as 别名]
# 或者: from casexml.apps.case.mock.CaseFactory import post_case_blocks [as 别名]
class MockDevice(object):

    def __init__(self, project, user, restore_options=None,
                 sync=False, default_case_type="case", default_owner_id=None):
        self.id = uuid4().hex
        self.project = project
        self.user = user
        self.user_id = user.user_id
        self.restore_options = restore_options or {}
        self.case_blocks = []
        self.case_factory = CaseFactory(
            self.project.name,
            case_defaults={
                'user_id': self.user_id,
                'owner_id': default_owner_id or self.user_id,
                'case_type': default_case_type,
            },
        )
        self.last_sync = None
        if sync:
            self.sync()

    def change_cases(self, cases=None, **case_kwargs):
        """Enqueue case changes to be synced

        Does not post changes to HQ. Use `post_changes()` for that,
        possibly after enqueuing changes with this method.

        :param cases: A `CaseBlock` or `CaseStructure` or a list of the
        same (all must have same type). `CaseStructure` objects will be
        converted to case XML with defaults from this device's case
        factory. `CaseBlock` objects will be posted as is (no defaults).
        :param **case_kwargs: Arguments to be passed to
        `CaseFactory.get_case_block(...)`, which implies using defaults
        from this device's case factory.
        """
        factory = self.case_factory
        if case_kwargs:
            if cases is not None:
                raise ValueError("pass one: cases or kwargs")
            if "case_id" not in case_kwargs:
                if not case_kwargs.get('create'):
                    raise ValueError("case_id is required for update")
                case_kwargs["case_id"] = uuid4().hex
            self.case_blocks.append(factory.get_case_block(**case_kwargs))
            return
        if isinstance(cases, (CaseStructure, CaseBlock)):
            cases = [cases]
        elif not isinstance(cases, list):
            raise ValueError(repr(cases))
        if all(isinstance(s, CaseStructure) for s in cases):
            self.case_blocks.extend(factory.get_case_blocks(cases))
        else:
            self.case_blocks.extend(b.as_xml() for b in cases)

    def post_changes(self, *args, **kw):
        """Post enqueued changes from device to HQ

        Calls `change_cases(*args, **kw)` with any arguments (if given)
        for convenience.

        This is the first half of a full sync. It does not affect the
        latest sync log on the device and can be used when the result of
        a restore on this device is not important.
        """
        if args or kw:
            self.change_cases(*args, **kw)
        if self.case_blocks:
            # post device case changes
            token = self.last_sync.restore_id if self.last_sync else None
            form = self.case_factory.post_case_blocks(
                self.case_blocks,
                device_id=self.id,
                form_extras={"last_sync_token": token},
                user_id=self.user_id,
            )[0]
            self.case_blocks = []
            return form

    def get_restore_config(self, **options):
        for name, value in self.restore_options.items():
            options.setdefault(name, value)
        if "device_id" in options:
            raise ValueError("illegal parameter: device_id")
        options["device_id"] = self.id
        options.setdefault('version', V2)
        if self.last_sync is not None and 'restore_id' not in options:
            options['restore_id'] = self.last_sync.restore_id
        return get_restore_config(self.project, self.user, **options)

    def sync(self, **config):
        """Synchronize device with HQ"""
        form = self.post_changes()
        # restore
        restore_config = self.get_restore_config(**config)
        payload = restore_config.get_payload().as_string()
        self.last_sync = SyncResult(self, restore_config, payload, form)
        return self.last_sync

    def restore(self, **config):
#.........这里部分代码省略.........
开发者ID:dimagi,项目名称:commcare-hq,代码行数:103,代码来源:utils.py


注:本文中的casexml.apps.case.mock.CaseFactory.post_case_blocks方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。