Beispiel #1
0
def responser_change(request):
    """
    模板编辑中批量修改责任人
    """
    form = ResponserChangeForm(request.POST)
    if not form.is_valid():
        return render_json({'result': False, "msg": u'参数错误!'})
    responser = form.cleaned_data['responser']
    business_name = form.cleaned_data['business_name']
    template_name = form.cleaned_data['template_name']
    if not check_users_from_paas(responser, request.COOKIES['bk_token'])[0]:
        return render_json({'result': False, 'msg': u'参数不合法!'})
    responser = string_to_users(responser)
    try:
        details = BusinessTemplateStepDetail.objects.filter(
            business_step__business__business_name=business_name,
            business_step__business__template_name=template_name,
        )
        for detail in details:
            detail.responser.clear()
            for i in responser:
                detail.responser.add(i)
            detail.save()
    except Exception, e:
        logger.error(
            u'修改责任人失败,ERROR %s, business_name: %s, template_name: %s' %
            (e, business_name, template_name))
        return render_json({'result': False, "msg": u'批量修改责任人失败,请联系管理员!'})
Beispiel #2
0
def application_create(request):
    """
    增加业务
    """
    form = ApplicationInfoForm(request.POST)
    if not form.is_valid():
        return render_json({'result': False, 'msg': u'参数不合法!'})
    cc_id = form.cleaned_data['cc_id']
    cc_name = form.cleaned_data['cc_name']
    cc_name_abbr = form.cleaned_data['cc_name_abbr']
    operator = form.cleaned_data['operator']
    if not check_users_from_paas(operator, request.COOKIES['bk_token'])[0]:
        return render_json({'result': False, 'msg': u'参数不合法!'})
    n = Application.objects.filter(cc_id=cc_id).count()
    if n > 0:
        return render_json({'result': False, 'msg': u"该业务编码已存在,请修改重试!"})
    n = Application.objects.filter(cc_name=cc_name).count()
    if n > 0:
        return render_json({'result': False, 'msg': u"该业务名称已存在,请修改重试!"})
    try:
        app = Application.objects.create(cc_name=cc_name,
                                         cc_name_abbr=cc_name_abbr,
                                         cc_id=cc_id)
        operator = string_to_users(operator)
        for i in operator:
            app.operator.add(i)
        app.save()
    except Exception, e:
        logger.error(u"增加业务失败%s" % e)
        return render_json({'result': False, 'msg': u"添加业务失败!"})
Beispiel #3
0
def application_change(request):
    """
    修改业务信息
    """
    forms = ApplicationInfoForm(request.POST)
    if not forms.is_valid():
        return render_json({'result': False, 'msg': u'参数不合法!'})
    pk = forms.cleaned_data['pk']
    if pk is None:
        return render_json({'result': False, 'msg': u'参数不合法!'})
    cc_name = forms.cleaned_data['cc_name']
    cc_name_abbr = forms.cleaned_data['cc_name_abbr']
    operator = forms.cleaned_data['operator']
    if not check_users_from_paas(operator, request.COOKIES['bk_token'])[0]:
        return render_json({'result': False, 'msg': u'参数不合法!'})
    n = Application.objects.filter(~Q(pk=pk), cc_name=cc_name).count()
    if n > 0:
        return render_json({'result': False, 'msg': u'业务名已存在,请修改重试!'})
    try:
        app = Application.objects.get(pk=pk)
        app.cc_name = cc_name
        app.cc_name_abbr = cc_name_abbr
        operators = string_to_users(operator)
        app.operator.clear()
        for operator in operators:
            app.operator.add(operator)
        app.save()
    except ObjectDoesNotExist:
        return render_json({'result': False, 'msg': u'修改业务失败!'})
    return render_json({'result': True, 'msg': u'修改业务成功!'})
Beispiel #4
0
def operator_change(request):
    """
    更新模板可操作者
    """
    form = OperatorChangeForm(request.POST)
    if not form.is_valid():
        return render_json({'status': False, 'msg': u"数据不合法!"})
    operator = form.cleaned_data['operator']
    if not check_users_from_paas(operator, request.COOKIES['bk_token'])[0]:
        return render_json({'status': False, 'msg': u'部分可操作者不存在!'})

    template_name = form.cleaned_data['template_name']
    business_name = form.cleaned_data['business_name']

    username = request.user.username
    try:
        template = BusinessTemplate.objects.get(business_name=business_name,
                                                template_name=template_name,
                                                operator__username=username)
    except ObjectDoesNotExist:
        return render_json({'status': False, 'msg': u"权限不足!"})
    try:
        cc_name = template.business_name
        operator_list = string_to_users(operator)
        if operator_list.count() != Application.objects.get(
                cc_name=cc_name).operator.filter(
                    username__in=operator.split(';')).count():
            return render_json({'status': False, 'msg': u"部分可操作者无该app权限!"})
        template.updater = request.user
        template.update_time = datetime.datetime.now()
        template.operator.clear()
        for i in operator_list:
            template.operator.add(i)
        template.save()
    except Exception, e:
        logger.error(u'更新纪录失败,%s; updater: %s, operator: %s pk:%s' %
                     (e, username, operator, template.pk))
        return render_json({'status': False, 'msg': u"更新纪录失败!"})
Beispiel #5
0
def business_create(request):
    """
    创建任务实例
    """
    form = BusinessCreateForm(request.POST)
    if not form.is_valid():
        return render_json({'result': False, 'msg': u"参数错误,请修改重试"})
    business_name = form.cleaned_data['business_name']
    template_name = form.cleaned_data['template_name']
    version = form.cleaned_data['version']
    audit_user = form.cleaned_data['audit_user']
    name = '%s_%s' % (business_name, version)
    try:
        template = BusinessTemplate.objects.get(business_name=business_name,
                                                template_name=template_name)
    except ObjectDoesNotExist:
        return render_json({
            'result': False,
            'msg': u"模版不存在",
            'name': name,
            'template_name': template_name
        })

    if BusinessRecord.objects.filter(business_name=name,
                                     business_type=template.business_type,
                                     template_name=template_name,
                                     business_version=version).exists():
        return render_json({
            'result': False,
            'msg': u"启动该任务实例已存在。",
            'name': name,
            'template_name': template_name
        })
    with transaction.atomic():
        record = BusinessRecord.objects.create(
            business_name=name,
            business_name_order=business_name,
            template_name=template_name,
            business_type=template.business_type,
            business_version=version,
            create_time=datetime.datetime.now(),
            status='0',
            creator=request.user,
            submit_status='0',
            app=template.app)
        audit_user_list = string_to_users(audit_user)
        for audit_user in audit_user_list:
            record.audit_user.add(audit_user)
        operator_list = template.operator.all()
        for operator in operator_list:
            record.operator.add(operator)
        record.save()

        steps = BusinessTemplateStep.objects.filter(business=template)
        for step in steps:
            instance_step = BusinessInstanceStep.objects.create(
                name=step.name, business=record)
            details = BusinessTemplateStepDetail.objects.filter(
                business_step=step)
            for detail in details:
                business_detail = BusinessInstanceStepDetail.objects.create(
                    step_xh=detail.step_xh,
                    operate_attention=detail.operate_attention,
                    comment=detail.comment,
                    importance=detail.importance,
                    business_step=instance_step)
                for responser in detail.responser.all():
                    business_detail.responser.add(responser)
                business_detail.save()
        return render_json({
            'result': True,
            'msg': u"恭喜你,启动任务实例信息成功!",
            'name': name,
            'template_name': template_name
        })
Beispiel #6
0
def template_save(request):
    """
    保存模板
    """
    form = TemplateSaveForm(request.POST)
    if not form.is_valid():
        return render_json({'result': False, 'message': u'参数错误, 请修改重试!'})

    business_name = form.cleaned_data['business_name']
    operator = form.cleaned_data['operator']
    business_type = form.cleaned_data['business_type']
    template_data = form.cleaned_data['template_data']
    template_name = form.cleaned_data['template_name']
    user = request.user

    if BusinessTemplate.objects.filter(business_name=business_name,
                                       template_name=template_name).exists():
        return render_json({'result': False, 'message': u'模板名已经存在'})

    # 保存模版
    try:
        app = Application.objects.get(cc_name=business_name,
                                      operator__username=request.user.username)
    except ObjectDoesNotExist:
        return render_json({'result': False, 'message': u'无该业务权限'})
    with transaction.atomic():
        default = {
            'template_name': template_name,
            'business_name': business_name,
            'business_type': business_type,
            'creator': user,
            'updater': user,
            'app': app
        }
        template = BusinessTemplate(**default)
        operator_list = string_to_users(operator)
        template.save()
        for i in operator_list:
            template.operator.add(i)
        template.save()

        # 保存步骤
        param = OrderedDict()
        # 步骤序号
        indexs = 1
        for one_data in template_data:
            step = one_data["step"]
            del one_data["step"], one_data["id"]
            one_data[u"step_xh"] = indexs
            if step not in param:
                param[step] = list()
            param[step].append(one_data)
            indexs += 1
        for keys in param:
            k = param[keys]
            tempstep = BusinessTemplateStep.objects.create(name=keys,
                                                           business=template)

            # 保存详情
            for details in k:
                responser_list = string_to_users(details['responser'])
                del details['responser']
                detail = BusinessTemplateStepDetail(business_step=tempstep,
                                                    **details)
                detail.save()
                for responser in responser_list:
                    detail.responser.add(responser)
                detail.save()
    return render_json({'result': True, 'message': u'恭喜你,导入数据成功!'})