def _add_overrides_for_build(self, doc): linked_build = wrap_app(doc) log_prefix = "{}{} app {}, build {}".format( "[DRY RUN] " if self.dry_run else "", linked_build.domain, linked_build.origin_id, linked_build.get_id) if not linked_build.upstream_app_id or not linked_build.upstream_version: return if not linked_build.domain_link: logger.error( "{}: Skipping due to missing domain link".format(log_prefix)) return try: master_build = get_master_app_by_version( linked_build.domain_link, linked_build.upstream_app_id, linked_build.upstream_version) except ActionNotPermitted: logger.error("{}: Skipping due to 403".format(log_prefix)) return if not master_build: logger.info( "{}: Skipping, no master build found".format(log_prefix)) return linked_map = self._get_xmlns_map(linked_build) master_map = self._get_xmlns_map(master_build) override_map = { master_form_unique_id: linked_map[xmlns] for xmlns, master_form_unique_id in master_map.items() if xmlns in linked_map } if not override_map: logger.info( "{}: Skipping, no forms found to map".format(log_prefix)) return current_overrides = { pre_id: override.post_id for pre_id, override in get_xform_resource_overrides( linked_build.domain, linked_build.origin_id).items() } if set(override_map.items()) - set(current_overrides.items()): logger.info("{}: Found {} overrides, updating with {}".format( log_prefix, len(current_overrides), len(override_map))) if not self.dry_run: try: add_xform_resource_overrides(linked_build.domain, linked_build.origin_id, override_map) except ResourceOverrideError as e: logger.error("{}".format(str( e))) # skip log_prefix, error message has same info else: logger.info( "{}: Skipping, all {} overrides already present".format( log_prefix, len(override_map)))
def overwrite_app(app, master_build, report_map=None): excluded_fields = set(Application._meta_fields).union([ 'date_created', 'build_profiles', 'copy_history', 'copy_of', 'name', 'comment', 'doc_type', '_LAZY_ATTACHMENTS', 'practice_mobile_worker_id', 'custom_base_url', 'family_id', ]) master_json = master_build.to_json() app_json = app.to_json() for key, value in master_json.items(): if key not in excluded_fields: app_json[key] = value app_json['version'] = app_json.get('version', 1) app_json['upstream_version'] = master_json['version'] app_json['upstream_app_id'] = master_json['copy_of'] wrapped_app = wrap_app(app_json) for module in wrapped_app.get_report_modules(): if report_map is None: raise AppEditingError('Report map not passed to overwrite_app') for config in module.report_configs: try: config.report_id = report_map[config.report_id] except KeyError: raise AppEditingError(config.report_id) # Legacy linked apps have different form unique ids than their master app(s). These mappings # are stored as ResourceOverride objects. Look up to see if this app has any. from corehq.apps.app_manager.suite_xml.post_process.resources import get_xform_resource_overrides overrides = get_xform_resource_overrides(domain=wrapped_app.domain, app_id=wrapped_app.get_id) ids_map = { pre_id: override.post_id for pre_id, override in overrides.items() } wrapped_app = _update_forms(wrapped_app, master_build, ids_map) # Multimedia versions should be set based on the linked app's versions, not those of the master app. for path in wrapped_app.multimedia_map.keys(): wrapped_app.multimedia_map[path].version = None wrapped_app.set_media_versions() enable_usercase_if_necessary(wrapped_app) return wrapped_app
def test_copy_xform_resource_overrides(self): forms = list(self.factory.app.get_module(0).get_forms()) add_xform_resource_overrides(self.factory.app.domain, self.factory.app.master_id, { forms[0].unique_id: '123', forms[1].unique_id: '456', }) copy_xform_resource_overrides(self.factory.app.domain, self.factory.app.master_id, { forms[0].unique_id: '321', '123': '987', }) overrides = get_xform_resource_overrides(self.factory.app.domain, self.factory.app.master_id) self.assertEqual({o.pre_id: o.post_id for o in overrides.values()}, { forms[0].unique_id: '123', forms[1].unique_id: '456', '321': '123', })