Пример #1
0
def add_app(request):
    if request.method == 'POST':
        form = AppForm(request.POST)
        if form.is_valid():
            form.save(commit=True)
            return app(request)
        else:
            print form.errors
    else:
        form = AppForm()
    return render(request, 'cmdb/add_app.html', {'form': form})
Пример #2
0
def edit_app(request):
    if request.method == 'POST':
        r_id = request.POST["id"]
        app_info = CM_APP.objects.get(id=r_id)  # 获取要更改行
        form = AppForm(request.POST, instance=app_info)
        if form.is_valid():
            form.save()
            return app(request)
        else:
            print form.errors
    else:
        form = AppForm()
    return render(request, 'cmdb/add_app.html', {'form': form})
Пример #3
0
    def put(self, request, *args, **kwargs):
        pk = kwargs.get('pk')
        p = get_object_or_404(App, pk=pk)
        if p.status is True:
            # 当status为False时,表示该应用还在申请阶段,允许用户修改部分属性
            if not request.user.has_perm('auth.perm_cmdb_business_edit'):
                return self.render_json_response({
                    'code': 1,
                    'errmsg': '权限不足,无法修改!'
                })
        post_data = QueryDict(request.body).dict()
        # 应用下线
        if "action" in post_data and post_data.pop("action") == "off":
            app = App.objects.get(pk=pk)
            if Server.objects.filter(app=app).exists():
                res = {"code": 1, "errmsg": "请先去服务器移除该应用!"}
            else:
                # for s in Server.objects.filter(app=app):
                #     s.app.remove(app)
                for s in Server.objects.filter(pre_app=app):
                    s.pre_app.remove(app)
                App.objects.filter(pk=pk).update(status=3)
                res = {"code": 0, "result": "下线成功"}
            return JsonResponse(res, safe=True)

        dept_id = post_data.pop('dept_id')
        # 校验 git地址是否合法
        git_pattern = re.compile(
            r'^git@git\.\w+\.\S+:[a-zA-Z0-9\-]+/[a-zA-Z0-9\-]+.git')
        if not git_pattern.match(post_data['scm_url']):
            return self.render_json_response({
                'code': 1,
                'errmsg': 'Git地址填写错误!'
            })
        # 校验第一、第二负责人 是否真实存在
        email_pattern = re.compile(
            r'^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$'
        )
        email_primary = post_data['primary']
        email_secondary = post_data['secondary']
        for email in email_primary.split(",") + email_secondary.split(","):
            if email_pattern.match(email):
                if get_user_detail_from_mb(email) is None:
                    return self.render_json_response({
                        'code':
                        1,
                        'errmsg':
                        '第一、第二负责人未找到,请再次确认!'
                    })
            else:
                return self.render_json_response({
                    'code': 1,
                    'errmsg': '请正确填写第一、第二负责人的邮箱!'
                })
        post_data['status'] = p.status
        form = AppForm(post_data, instance=p)
        if form.is_valid():
            app = form.save(commit=False)
            app.biz_mgt_dept = BizMgtDept.objects.get(id=dept_id)
            app.save()
            application_update(post_data)
            res = {"code": 0, "result": "更新成功"}
        else:
            res = {"code": 1, "errmsg": form.errors}
        return JsonResponse(res, safe=True)
Пример #4
0
    def post(self, request, **kwargs):
        post_data = request.POST.copy()
        dept_id = post_data.pop('dept_id')
        status = int(post_data.get('status'))
        if status == 0:
            # 申请新建应用,需分配一个新端口
            app_code, scm_url = post_data.get('app_code'), post_data.get(
                'scm_url')
            if '/' + app_code + '.git' not in scm_url:
                return self.render_json_response({
                    'code':
                    1,
                    'errmsg':
                    '应用编码(xxx)与仓库名(xxx.git)必须相同!'
                })
            scm_owner = re.findall(r'\S*:(\S+)/\S+', scm_url, re.I)[0]
            if scm_owner not in get_gitlib_group_list():
                return self.render_json_response({
                    'code': 1,
                    'errmsg': '请将仓库移动到公共组!'
                })
            maximum_port = App.objects.get_queryset().aggregate(
                Max('tomcat_port'))
            post_data['tomcat_port'] = maximum_port[
                'tomcat_port__max'] + 1 if maximum_port else 8080
        else:
            # 补录应用
            if not request.user.has_perm('auth.perm_cmdb_business_edit'):
                return self.render_json_response({
                    'code': 1,
                    'errmsg': '权限不足,无法补录应用!'
                })
        # 校验 git地址是否合法
        git_pattern = re.compile(
            r'^git@git\.\w+\.\S+:[a-zA-Z0-9\-]+/[a-zA-Z0-9\-]+.git')
        if not git_pattern.match(post_data['scm_url']):
            return self.render_json_response({
                'code': 1,
                'errmsg': 'Git地址填写错误!'
            })
        # 校验第一、第二负责人 是否真实存在
        email_pattern = re.compile(
            r'^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$'
        )
        email_primary = post_data['primary']
        email_secondary = ",".join(request.POST.getlist('secondary', []))
        for email in email_primary.split(",") + email_secondary.split(","):
            if email_pattern.match(email):
                if get_user_detail_from_mb(email) is None:
                    return self.render_json_response({
                        'code':
                        1,
                        'errmsg':
                        '第一、第二负责人未找到,请再次确认!'
                    })
            else:
                return self.render_json_response({
                    'code': 1,
                    'errmsg': '请正确填写第一、第二负责人的邮箱!'
                })

        form = AppForm(post_data)
        if form.is_valid():
            app = form.save(commit=False)
            bgd = BizMgtDept.objects.get(id=dept_id[0])
            if bgd.id < 3:
                return self.render_json_response({
                    'code': 1,
                    'errmsg': '请勿选择根部门!'
                })
            app.biz_mgt_dept = bgd
            if Process.objects.filter(name='java').exists():
                app.process = Process.objects.get(name='java')
            app.applicant = request.user.username
            app.save()
            if status == 0:
                res = {'code': 0, 'id': app.id, 'result': '新应用申请已提交,请等待管理员审核!'}
            else:
                res = {'code': 0, 'result': '添加成功!'}
        else:
            # form.errors 会把验证不通过的信息以对象的形式传到前端,前端直接渲染即可
            res = {'code': 1, 'errmsg': form.errors}
        return self.render_json_response(res)