def obj_create(self, bundle, **kwargs): try: template_id = bundle.data.pop('template_id') name = bundle.data.pop('name') cron = json.loads(bundle.data.pop('cron')) pipeline_tree = json.loads(bundle.data.pop('pipeline_tree')) business_path = bundle.data['business'] except (KeyError, ValueError) as e: raise BadRequest(e.message) # XSS handle name = name_handler(name, PERIOD_TASK_NAME_MAX_LENGTH) creator = bundle.request.user.username # validate pipeline tree try: validate_web_pipeline_tree(pipeline_tree) except PipelineException as e: raise BadRequest(e.message) try: template = TaskTemplate.objects.get(id=template_id) kwargs['template_id'] = template.id except TaskTemplate.DoesNotExist: raise BadRequest('template[id=%s] does not exist' % template_id) try: replace_template_id(TaskTemplate, pipeline_tree) except TaskTemplate.DoesNotExist: raise BadRequest( 'invalid subprocess, check subprocess node please') if not isinstance(cron, dict): raise BadRequest('cron must be a object json string') try: business = Business.objects.get( cc_id=int(business_path.split('/')[-2])) except Exception as e: raise BadRequest(e.message) try: kwargs['task'] = PeriodicTask.objects.create_pipeline_task( business=business, template=template, name=name, cron=cron, pipeline_tree=pipeline_tree, creator=creator) except Exception as e: logger.warning(traceback.format_exc()) raise BadRequest(e.message) response = super(PeriodicTaskResource, self).obj_create(bundle, **kwargs) response.obj.set_enabled(True) return response
def create_pipeline_instance(template, **kwargs): pipeline_tree = kwargs['pipeline_tree'] replace_template_id(template.__class__, pipeline_tree) pipeline_template_data = { 'name': kwargs['name'], 'creator': kwargs['creator'], 'description': kwargs.get('description', ''), } PipelineTemplateWebWrapper.unfold_subprocess(pipeline_tree) pipeline_instance = PipelineInstance.objects.create_instance( template.pipeline_template, pipeline_tree, spread=True, **pipeline_template_data) return pipeline_instance
def create_periodic_task(request, template_id, project_id): project = request.project try: template = TaskTemplate.objects.get(pk=template_id, project_id=project.id, is_deleted=False) except TaskTemplate.DoesNotExist: return JsonResponse({ 'result': False, 'message': 'template(%s) does not exist' % template_id }) try: params = json.loads(request.body) except Exception: return JsonResponse({ 'result': False, 'message': 'invalid json format' }) logger.info( 'apigw create_periodic_task info, ' 'template_id: {template_id}, project_id: {project_id}, params: {params}'.format(template_id=template_id, project_id=project.id, params=params)) try: params.setdefault('constants', {}) params.setdefault('exclude_task_nodes_id', []) jsonschema.validate(params, APIGW_CREATE_PERIODIC_TASK_PARAMS) except jsonschema.ValidationError as e: logger.warning(u"apigw create_periodic_task raise prams error: %s" % e) message = 'task params is invalid: %s' % e return JsonResponse({'result': False, 'message': message}) exclude_task_nodes_id = params['exclude_task_nodes_id'] pipeline_tree = template.pipeline_tree try: TaskFlowInstance.objects.preview_pipeline_tree_exclude_task_nodes(pipeline_tree, exclude_task_nodes_id) except Exception as e: logger.exception(e) return JsonResponse({'result': False, 'message': e.message}) for key, val in params['constants'].items(): if key in pipeline_tree['constants']: pipeline_tree['constants'][key]['value'] = val name = params['name'] cron = params['cron'] try: replace_template_id(TaskTemplate, pipeline_tree) except Exception as e: logger.exception(e) return JsonResponse({'result': False, 'message': e.message}) try: task = PeriodicTask.objects.create( project=project, template=template, name=name, cron=cron, pipeline_tree=pipeline_tree, creator=request.user.username ) except Exception as e: logger.exception(e) return JsonResponse({'result': False, 'message': e.message}) data = info_data_from_period_task(task) return JsonResponse({ 'result': True, 'data': data })