def _perform_import(self, template_data, check_info, override, defaults_getter, resource): template = template_data['template'] tid_to_reuse = {} # find old template_id for override using # import_id -> reuse_id for template_to_be_replaced in check_info['override_template']: task_template_id = template_to_be_replaced['id'] template_id = template_data['template'][str( task_template_id)]['pipeline_template_str_id'] tid_to_reuse[template_id] = template_to_be_replaced['template_id'] # import pipeline template first id_map = PipelineTemplateWebWrapper.import_templates( template_data['pipeline_template_data'], override=override, tid_to_reuse=tid_to_reuse) old_id_to_new_id = id_map[PipelineTemplateWebWrapper.ID_MAP_KEY] new_objects = [] new_objects_template_ids = set() # find templates which had been deleted if override: new_objects_template_ids = set( self.model.objects.filter(id__in=template.keys(), is_deleted=True).values_list( 'pipeline_template_id', flat=True)) for tid, template_dict in template.items(): template_dict['pipeline_template_id'] = old_id_to_new_id[ template_dict['pipeline_template_str_id']] defaults = defaults_getter(template_dict) # use update or create to avoid id conflict if override: obj, created = self.update_or_create(id=tid, defaults=defaults) if created: new_objects_template_ids.add( template_dict['pipeline_template_id']) else: new_objects.append(self.model(**defaults)) new_objects_template_ids.add( template_dict['pipeline_template_id']) self.model.objects.bulk_create(new_objects) create_templates = list( self.model.objects.filter( pipeline_template_id__in=new_objects_template_ids)) if create_templates: resource.batch_register_instance(create_templates) return { 'result': True, 'data': len(template), 'message': 'Successfully imported %s flows' % len(template) }
def import_templates(self, template_data, override): template = template_data['template'] check_info = self.import_operation_check(template_data) tid_to_reuse = {} # operation validation check if override and (not check_info['can_override']): return { 'result': False, 'message': 'Unable to override common flows or keep ID when importing business flows data', 'data': 0 } # find old template_id for override using # import_id -> reuse_id for template_to_be_replaced in check_info['override_template']: task_template_id = template_to_be_replaced['id'] template_id = template_data['template'][str( task_template_id)]['pipeline_template_str_id'] tid_to_reuse[template_id] = template_to_be_replaced['template_id'] # import pipeline template first id_map = PipelineTemplateWebWrapper.import_templates( template_data['pipeline_template_data'], override=override, tid_to_reuse=tid_to_reuse) old_id_to_new_id = id_map[PipelineTemplateWebWrapper.ID_MAP_KEY] new_objects = [] for tid, tmpl_dict in template.items(): tmpl_dict['pipeline_template_id'] = old_id_to_new_id[ tmpl_dict['pipeline_template_str_id']] defaults = { 'category': tmpl_dict['category'], 'notify_type': tmpl_dict['notify_type'], 'notify_receivers': tmpl_dict['notify_receivers'], 'time_out': tmpl_dict['time_out'], 'pipeline_template_id': tmpl_dict['pipeline_template_id'], 'is_deleted': False } # use update or create to avoid id conflict if override: self.update_or_create(id=tid, defaults=defaults) else: new_objects.append(self.model(**defaults)) self.model.objects.bulk_create(new_objects) return { 'result': True, 'data': { 'count': len(template) }, 'message': 'Successfully imported %s common flows' % len(template) }
def _perform_import(self, template_data, check_info, override, defaults_getter, operator): template = template_data["template"] tid_to_reuse = {} # find old template_id for override using # import_id -> reuse_id for template_to_be_replaced in check_info["override_template"]: task_template_id = template_to_be_replaced["id"] template_id = template_data["template"][str(task_template_id)]["pipeline_template_str_id"] tid_to_reuse[template_id] = template_to_be_replaced["template_id"] # import pipeline template first id_map = PipelineTemplateWebWrapper.import_templates( template_data["pipeline_template_data"], override=override, tid_to_reuse=tid_to_reuse ) old_id_to_new_id = id_map[PipelineTemplateWebWrapper.ID_MAP_KEY] new_objects = [] new_objects_template_ids = set() # find templates which had been deleted if override: new_objects_template_ids = set( self.model.objects.filter(id__in=list(template.keys()), is_deleted=True).values_list( "pipeline_template_id", flat=True ) ) for tid, template_dict in list(template.items()): template_dict["pipeline_template_id"] = old_id_to_new_id[template_dict["pipeline_template_str_id"]] defaults = defaults_getter(template_dict) # use update or create to avoid id conflict if override: obj, created = self.update_or_create(id=tid, defaults=defaults) if created: new_objects_template_ids.add(template_dict["pipeline_template_id"]) else: new_objects.append(self.model(**defaults)) new_objects_template_ids.add(template_dict["pipeline_template_id"]) # update creator when templates are created PipelineTemplate.objects.filter(template_id__in=new_objects_template_ids).update(creator=operator) # update flows map flows = {info["id"]: info["name"] for info in check_info["new_template"]} if not override: self.model.objects.bulk_create(new_objects) create_templates = list(self.model.objects.filter(pipeline_template_id__in=new_objects_template_ids)) # create flows map flows = {tmp.id: tmp.name for tmp in create_templates} # send_signal if create_templates: batch_create.send(self.model, instance=create_templates, creator=operator) return { "result": True, "data": {"count": len(template), "flows": flows}, "message": "Successfully imported %s flows" % len(template), "code": err_code.SUCCESS.code, }