Beispiel #1
0
def user_save_password(request):
    old_password = request.POST.get("old_password", "")
    new_password = request.POST.get("new_password", "")
    re_new_password = request.POST.get("re_new_password", "")
    result = {}
    head = {}
    if old_password == "" or new_password == "" or re_new_password == "":
        error_code = 100021
    elif new_password != re_new_password:
        error_code = 100019
    elif len(new_password) < 6:
        error_code = 100020
    else:
        uid = request.session["uid"]
        user = User.objects.get(id=uid)
        if user:
            if user.check_password(old_password):
                try:
                    user.set_password(new_password)
                    user.save()
                    error_code = 0
                except Exception as ex:
                    head["exceptions"] = ex
                    error_code = 110000
            else:
                error_code = 100023
        else:
            error_code = 100018
    head["code"] = error_code
    head["message"] = errorinfo.change_to_message(error_code)
    result["heads"] = head
    return HttpResponse(json.dumps(result), content_type='application/json')
Beispiel #2
0
def user_save_password(request):
    old_password = request.POST.get("old_password", "")
    new_password = request.POST.get("new_password", "")
    re_new_password = request.POST.get("re_new_password", "")
    result = {}
    head = {}
    if old_password == "" or new_password == "" or re_new_password == "":
        error_code = 100021
    elif new_password != re_new_password:
        error_code = 100019
    elif len(new_password) < 6:
        error_code = 100020
    else:
        uid = request.session["uid"]
        user = User.objects.get(id=uid)
        if user:
            if user.check_password(old_password):
                try:
                    user.set_password(new_password)
                    user.save()
                    error_code = 0
                except Exception as ex:
                    head["exceptions"] = ex
                    error_code = 110000
            else:
                error_code = 100023
        else:
            error_code = 100018
    head["code"] = error_code
    head["message"] = errorinfo.change_to_message(error_code)
    result["heads"] = head
    return HttpResponse(json.dumps(result), content_type='application/json')
Beispiel #3
0
def user_login(request):
    if request.method == "POST":
        username = request.POST.get("username", "")
        password = request.POST.get("password", "")
        user = authenticate(username=username, password=password)
        if user is not None and user.is_active:
            login(request, user)
            uid = user.id
            request.session["uid"] = uid
            redirect_url = request.GET.get('next')
            if redirect_url:
                return HttpResponseRedirect(redirect_url)
            elif user.is_superuser:
                return HttpResponseRedirect(reverse("api_user:user_list"))
            else:
                return HttpResponseRedirect(reverse("api_api:api_list"))
        else:
            error_code = 100000
            message = errorinfo.change_to_message(error_code)
            return render_to_response("user_login.html",
                                      context_instance=RequestContext(
                                          request, {
                                              "error_user": True,
                                              "error_message": message
                                          }))
    return render_to_response("user_login.html")
Beispiel #4
0
def api_batch_move(request):
    try:
        all_api_list = request.POST.get("api_list", "")
        heads = {}
        error_code = 0
        project_id = request.POST.get("project_id", "")
        if all_api_list != "" and project_id != "":
            all_api_list = all_api_list.split(",")
            api_info = ApiApi.objects.filter(id__in=all_api_list)
            for api in api_info:
                try:
                    api.project_id = project_id
                    api.save()
                except Exception as ex:
                    heads["exceptions"] = str(ex)
                    error_code = 110000
        heads = {
            "code": error_code,
            "message": errorinfo.change_to_message(error_code)
        }
        result = {"heads": heads}
        return HttpResponse(json.dumps(result),
                            content_type='application/json')
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #5
0
def public_save(request):
    try:
        create_time = time.strftime("%Y-%m-%d %H:%M:%S",
                                    time.localtime(time.time()))
        project_id = request.POST.get("project_id", "")
        parent_project_id = request.POST.get("parent_project_id", "")
        api_document = request.POST.get("api_document", "")
        project_name = request.POST.get("project_name", "")
        project_level = request.POST.get("project_level", 0)
        if project_name == "":
            # 无论添加还是编辑都名称都不能为空
            error_code = 100012
        else:
            if parent_project_id == "":
                project_level = 1
            else:
                project_level = int(project_level) + 1
            # 有project_id表示是编辑
            if project_id != "":
                if ApiProject.objects.filter(name=project_name,
                                             status=1).exclude(id=project_id):
                    error_code = 100013
                else:
                    edit_project = get_object_or_404(ApiProject,
                                                     id=project_id,
                                                     status=1)
                    edit_project.name = project_name
                    edit_project.api_document = api_document
                    if parent_project_id != "":
                        edit_project.parent_id = int(parent_project_id)
                        edit_project.level = project_level
                    edit_project.save()
                    error_code = 0
                # 无project_id表示是添加
            else:
                if ApiProject.objects.filter(name=project_name, status=1):
                    error_code = 100013
                else:
                    if parent_project_id != "":
                        parent_project_id = int(parent_project_id)
                    else:
                        parent_project_id = None
                    new_project = ApiProject(name=project_name,
                                             parent_id=parent_project_id,
                                             level=project_level,
                                             api_document=api_document,
                                             create_time=create_time)
                    new_project.save()
                    error_code = 0
        heads = {
            "code": error_code,
            "message": errorinfo.change_to_message(error_code)
        }
        result = {"heads": heads}
        return result
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #6
0
def hosts_delete(request):
    try:
        hosts_id = request.GET.get("hosts_id", "")
        delete_hosts = get_object_or_404(ApiHosts, id=hosts_id, status=1)
        if delete_hosts.is_current:
            error_code = 100013
        else:
            error_code = 0
            delete_hosts.status = -1
            delete_hosts.save()
        heads = {"code": error_code, "message": errorinfo.change_to_message(error_code)}
        result = {"heads": heads}
        return HttpResponse(json.dumps(result), content_type='application/json')
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #7
0
def public_save(request):
    try:
        create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
        project_id = request.POST.get("project_id", "")
        parent_project_id = request.POST.get("parent_project_id", "")
        api_document = request.POST.get("api_document","")
        project_name = request.POST.get("project_name", "")
        project_level = request.POST.get("project_level", 0)
        if project_name == "":
            # 无论添加还是编辑都名称都不能为空
            error_code = 100012
        else:
            if parent_project_id == "":
                project_level = 1
            else:
                project_level = int(project_level)+1
            # 有project_id表示是编辑
            if project_id != "":
                if ApiProject.objects.filter(name=project_name, status=1).exclude(id=project_id):
                    error_code = 100013
                else:
                    edit_project = get_object_or_404(ApiProject, id=project_id, status=1)
                    edit_project.name = project_name
                    edit_project.api_document = api_document
                    if parent_project_id != "":
                        edit_project.parent_id = int(parent_project_id)
                        edit_project.level = project_level
                    edit_project.save()
                    error_code = 0
                # 无project_id表示是添加
            else:
                if ApiProject.objects.filter(name=project_name, status=1):
                    error_code = 100013
                else:
                    if parent_project_id != "":
                        parent_project_id = int(parent_project_id)
                    else:
                        parent_project_id = None
                    new_project = ApiProject(name=project_name, parent_id=parent_project_id,
                                             level=project_level, api_document=api_document,
                                             create_time=create_time)
                    new_project.save()
                    error_code = 0
        heads = {"code": error_code, "message": errorinfo.change_to_message(error_code)}
        result = {"heads": heads}
        return result
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #8
0
def project_delete(request):
    try:
        project_id = request.GET.get("project_id", "")
        if ApiApi.objects.filter(project_id=project_id, status=1):
            error_code = 100024
        elif ApiProject.objects.filter(parent_id=project_id, status=1):
            error_code = 100028
        else:
            del_project = get_object_or_404(ApiProject, id=project_id, status=1)
            del_project.status = -1
            del_project.save()
            error_code = 0
        heads = {"code": error_code, "message": errorinfo.change_to_message(error_code)}
        result = {"heads": heads}
        return HttpResponse(json.dumps(result), content_type='application/json')
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #9
0
def api_delete(request):
    try:
        uid = request.session["uid"]
        all_api_list = request.POST.get("api_list", "")
        error_code = 0
        if all_api_list != "":
            all_api_list = all_api_list.split(",")
            api_info = ApiApi.objects.filter(id__in=all_api_list)
            for api in api_info:
                if int(api.creater) == int(uid):
                    api.status = -1
                    api.save()
                    error_code = 0
                else:
                    error_code = 100027
        heads = {"code": error_code, "message": errorinfo.change_to_message(error_code)}
        result = {"heads": heads}
        return HttpResponse(json.dumps(result), content_type='application/json')
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #10
0
def hosts_add_save(request):
    try:
        hosts_name = request.POST.get("hosts_name", "")
        hosts_content = request.POST.get("hosts_content", "")
        create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
        new_hosts_id = 0
        if ApiHosts.objects.filter(name=hosts_name, status=1):
            error_code = 100001
        elif hosts_name == "":
            error_code = 100002
        else:
            error_code = 0
            add_hosts = ApiHosts(name=hosts_name, content=hosts_content, create_time=create_time, is_current=False, status=1)
            add_hosts.save()
            new_hosts_id = add_hosts.id
        heads = {"code": error_code, "message": errorinfo.change_to_message(error_code)}
        result = {"heads": heads, "hosts_id": new_hosts_id}
        return HttpResponse(json.dumps(result), content_type='application/json')
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #11
0
def api_batch_move(request):
    try:
        all_api_list = request.POST.get("api_list", "")
        heads = {}
        error_code = 0
        project_id = request.POST.get("project_id", "")
        if all_api_list != "" and project_id != "":
            all_api_list = all_api_list.split(",")
            api_info = ApiApi.objects.filter(id__in=all_api_list)
            for api in api_info:
                try:
                    api.project_id = project_id
                    api.save()
                except Exception as ex:
                    heads["exceptions"] = str(ex)
                    error_code = 110000
        heads = {"code": error_code, "message": errorinfo.change_to_message(error_code)}
        result = {"heads": heads}
        return HttpResponse(json.dumps(result), content_type='application/json')
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #12
0
def user_delete(request):
    head = {}
    uid = request.session["uid"]
    try:
        user_id = request.GET.get("user_id", "")
        if int(user_id) == int(uid):
            error_code = 100026
        else:
            user_info = get_object_or_404(User, id=user_id, is_active=1)
            try:
                user_info.is_active = 0
                user_info.save()
                Permission.objects.filter(user_id=user_id).delete()
                error_code = 0
            except Exception as ex:
                error_code = 110000
                head["exception"] = ex
        head["code"] = error_code
        head["message"] = errorinfo.change_to_message(error_code)
        result = {"heads": head}
        return HttpResponse(json.dumps(result), content_type='application/json')
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #13
0
def user_login(request):
    if request.method == "POST":
        username = request.POST.get("username", "")
        password = request.POST.get("password", "")
        user = authenticate(username=username, password=password)
        if user is not None and user.is_active:
            login(request, user)
            uid = user.id
            request.session["uid"] = uid
            redirect_url = request.GET.get('next')
            if redirect_url:
                return HttpResponseRedirect(redirect_url)
            elif user.is_superuser:
                return HttpResponseRedirect(reverse("api_user:user_list"))
            else:
                return HttpResponseRedirect(reverse("api_api:api_list"))
        else:
            error_code = 100000
            message = errorinfo.change_to_message(error_code)
            return render_to_response("user_login.html",
                                      context_instance=RequestContext(request, {"error_user": True,
                                                                                "error_message": message}))
    return render_to_response("user_login.html")
Beispiel #14
0
def project_delete(request):
    try:
        project_id = request.GET.get("project_id", "")
        if ApiApi.objects.filter(project_id=project_id, status=1):
            error_code = 100024
        elif ApiProject.objects.filter(parent_id=project_id, status=1):
            error_code = 100028
        else:
            del_project = get_object_or_404(ApiProject,
                                            id=project_id,
                                            status=1)
            del_project.status = -1
            del_project.save()
            error_code = 0
        heads = {
            "code": error_code,
            "message": errorinfo.change_to_message(error_code)
        }
        result = {"heads": heads}
        return HttpResponse(json.dumps(result),
                            content_type='application/json')
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #15
0
def hosts_edit_save(request):
    try:
        hosts_id = request.POST.get("hosts_id", 0)
        hosts_id = int(hosts_id)
        hosts_name = request.POST.get("hosts_name", "")
        hosts_content = request.POST.get("hosts_content", "")
        if ApiHosts.objects.filter(name=hosts_name, status=1).exclude(id=hosts_id):
            error_code = 100001
        elif hosts_name == "":
            error_code = 100002
        else:
            edit_hosts = get_object_or_404(ApiHosts, id=hosts_id, status=1)
            edit_hosts.name = hosts_name
            edit_hosts.content = hosts_content
            edit_hosts.save()
            error_code = 0
            if edit_hosts.is_current:
                set_server_hosts(edit_hosts.content)
        heads = {"code": error_code, "message": errorinfo.change_to_message(error_code)}
        result = {"heads": heads}
        return HttpResponse(json.dumps(result), content_type='application/json')
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #16
0
def user_delete(request):
    head = {}
    uid = request.session["uid"]
    try:
        user_id = request.GET.get("user_id", "")
        if int(user_id) == int(uid):
            error_code = 100026
        else:
            user_info = get_object_or_404(User, id=user_id, is_active=1)
            try:
                user_info.is_active = 0
                user_info.save()
                Permission.objects.filter(user_id=user_id).delete()
                error_code = 0
            except Exception as ex:
                error_code = 110000
                head["exception"] = ex
        head["code"] = error_code
        head["message"] = errorinfo.change_to_message(error_code)
        result = {"heads": head}
        return HttpResponse(json.dumps(result),
                            content_type='application/json')
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #17
0
def api_delete(request):
    try:
        uid = request.session["uid"]
        all_api_list = request.POST.get("api_list", "")
        error_code = 0
        if all_api_list != "":
            all_api_list = all_api_list.split(",")
            api_info = ApiApi.objects.filter(id__in=all_api_list)
            for api in api_info:
                if int(api.creater) == int(uid):
                    api.status = -1
                    api.save()
                    error_code = 0
                else:
                    error_code = 100027
        heads = {
            "code": error_code,
            "message": errorinfo.change_to_message(error_code)
        }
        result = {"heads": heads}
        return HttpResponse(json.dumps(result),
                            content_type='application/json')
    except Exception as ex:
        raise Http404(str(ex))
Beispiel #18
0
def public_save(request):
    user_id = request.POST.get("user_id", "")
    user_name = request.POST.get("user_name", "")
    user_real_name = request.POST.get("user_real_name", "")
    user_email = request.POST.get("user_email", "")
    user_password = request.POST.get("user_password", "")
    user_re_password = request.POST.get("user_re_password", "")
    user_is_admin = request.POST.get("user_is_admin", 0)
    permission_list = request.POST.get("permission_list", "")
    if user_is_admin == 1 or user_is_admin == "1":
        user_is_admin = True
    else:
        user_is_admin = False
    result = {}
    head = {}
    if user_name == "":
        error_code = 100015
    elif user_password == "" or user_re_password == "":
        error_code = 100021
    elif user_password != user_re_password:
        error_code = 100019
    elif len(user_password) < 6:
        error_code = 100020
    else:
        new_user_id = ""
        if user_id == "":
            if User.objects.filter(username=user_name, is_active=1):
                error_code = 100017
            else:
                add_user = User(username=user_name,
                                first_name=user_real_name,
                                is_superuser=user_is_admin,
                                email=user_email)
                try:
                    add_user.set_password(user_password)
                    add_user.save()
                    new_user_id = add_user.id
                    error_code = 0
                except Exception as ex:
                    head["exceptions"] = ex
                    error_code = 110000
        else:
            if User.objects.filter(username=user_name,
                                   is_active=1).exclude(id=user_id):
                error_code = 100017
            else:
                edit_user = get_object_or_404(User, id=user_id)
                try:
                    edit_user.username = user_name
                    edit_user.first_name = user_real_name
                    edit_user.email = user_email
                    if user_password != edit_user.password:
                        edit_user.set_password(user_password)
                    edit_user.is_superuser = user_is_admin
                    edit_user.save()
                    new_user_id = user_id
                    error_code = 0
                except Exception as ex:
                    head["exceptions"] = ex
                    error_code = 110000
        # 保存权限
        if new_user_id != "":
            permissions = Permission.objects.filter(user_id=new_user_id)
            if permissions:
                for pers in permissions:
                    pers.delete()
            if permission_list != "":
                permission_list = permission_list.encode("utf8")
                permission_list = permission_list.split(",")
                for permission_id in permission_list:
                    permission_info = Permission.objects.filter(
                        user_id=new_user_id, project_id=permission_id)
                    if permission_info:
                        pass
                    else:
                        new_permission = Permission(user_id=new_user_id,
                                                    project_id=permission_id)
                        new_permission.save()
    head["code"] = error_code
    head["message"] = errorinfo.change_to_message(error_code)
    result["heads"] = head
    return result
Beispiel #19
0
def execute(api_url, url_list, api_domain, api_http_type, api_method,
            api_headers, api_body_type, api_body_value, api_id):
    parameters = get_request_parameters(api_url, url_list, api_domain,
                                        api_http_type, api_method, api_headers,
                                        api_body_type, api_body_value)
    domain = parameters["api_domain"]
    if domain.split("//")[0] == "http:" or domain.split("//")[0] == "https:":
        domain = domain.split("//")[1]
    url = parameters["api_url"]
    method = parameters["api_method"]
    headers = parameters["api_headers"]
    body_type = parameters["api_body_type"]
    body_value = parameters["api_body_value"]
    result = {}
    heads = {"is_success": ""}

    # 必填项没有时,则显示错误
    if url == "":
        error_code = 100007
    elif domain == "":
        error_code = 100008
    else:
        try:
            if body_type == "1" or body_type == 1:
                register_openers()
                datagen, re_headers = multipart_encode(body_value)
                api_request = urllib2.Request(url, datagen, re_headers)
                if headers != "":
                    for keys in headers:
                        api_request.add_header(keys, headers[keys])
                response = urllib2.urlopen(api_request)
                result_data = response.read()
                http_code = response.code
            else:
                if api_http_type == 2 or api_http_type == "2":
                    connection = httplib.HTTPSConnection(domain)
                else:
                    connection = httplib.HTTPConnection(domain)
                if headers == "":
                    connection.request(method=method, url=url, body=body_value)
                else:
                    connection.request(method=method,
                                       url=url,
                                       headers=headers,
                                       body=body_value)
                response = connection.getresponse()
                http_code = response.status
                result_data = response.read()
            error_code = 0
            result["datas"] = json.dumps(result_data)
            if api_id != "":
                execute_time = time.strftime("%Y-%m-%d %H:%M:%S",
                                             time.localtime(time.time()))
                update_api = ApiApi.objects.get(id=api_id)
                update_api.last_execute_time = execute_time
                update_api.api_real_result = result_data
                if int(http_code) == 200:
                    # 如果实际结果和数据库中存的期望结果一致,显示成功
                    if str(result_data).strip().lstrip() == str(
                            update_api.api_expect_result).strip().lstrip():
                        is_success = 1
                        update_api.api_is_success = 1
                    else:
                        is_success = 0
                        update_api.api_is_success = 0
                else:
                    is_success = -1
                    update_api.api_is_success = -1
                update_api.save()
                heads["is_success"] = is_success
                heads["http_code"] = int(http_code)
        except Exception as ex:
            error_code = 110000
            heads["exception"] = str(ex)

    heads["code"] = error_code
    heads["message"] = errorinfo.change_to_message(error_code)
    result["heads"] = heads
    return result
Beispiel #20
0
def public_save(request):
    uid = request.session.get('uid')
    api_id = request.POST.get("api_id", "")
    api_name = request.POST.get("api_name", "")
    api_url = request.POST.get("api_url", "")
    url_list = request.POST.get("url_list", "")
    api_domain = request.POST.get("api_domain", "")
    api_http_type = request.POST.get("api_http_type", 0)
    api_method = request.POST.get("api_method", 0)
    api_headers = request.POST.get("api_headers", "")
    api_body_type = request.POST.get("api_body_type", "")
    project_id = request.POST.get("project_id", 1)
    if api_body_type == "":
        api_body_type = None
    api_body_value = request.POST.get("api_body_value", "")
    api_remarks = request.POST.get("api_remarks", "")
    api_result = request.POST.get("api_result", "")
    create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    result = {}
    heads = {}
    if api_name == "":
        error_code = 100004
    else:
        if api_id:
            update_api = ApiApi.objects.get(id=api_id)
            try:
                error_code = 0
                update_api.name = api_name
                update_api.api_http_type = api_http_type
                update_api.api_url = api_url
                update_api.url_list = url_list
                update_api.api_domain = api_domain
                update_api.api_method = api_method
                update_api.api_headers = api_headers
                update_api.api_body_type = api_body_type
                update_api.api_body_value = api_body_value
                update_api.remarks = api_remarks
                update_api.update_time = create_time
                update_api.api_expect_result = api_result
                update_api.save()
                api_id = update_api.id
                result["api_id"] = api_id
            except Exception as ex:
                heads["exception"] = str(ex)
                error_code = 110000
        else:
            if api_result != "":
                last_execute_time = create_time
            else:
                last_execute_time = None
            add_api = ApiApi(name=api_name,
                             api_http_type=api_http_type, api_url=api_url, project_id=project_id,
                             url_list=url_list, api_domain=api_domain,
                             api_method=api_method, api_headers=api_headers,
                             api_body_type=api_body_type, api_body_value=api_body_value,
                             remarks=api_remarks, create_time=create_time, api_expect_result=api_result,
                             update_time=create_time, creater=uid, last_execute_time=last_execute_time, status=1)
            try:
                error_code = 0
                add_api.save()
                add_api_id = add_api.id
                result["api_id"] = add_api_id
            except Exception as ex:
                heads["exception"] = str(ex)
                error_code = 110000
                print ex

    heads["code"] = error_code
    heads["message"] = errorinfo.change_to_message(error_code)
    result["heads"] = heads
    return result
Beispiel #21
0
def execute(api_url,url_list,api_domain,api_http_type,api_method,api_headers,api_body_type,api_body_value,api_id):
    parameters = get_request_parameters(api_url,url_list,api_domain,api_http_type,api_method,api_headers,api_body_type,api_body_value)
    domain = parameters["api_domain"]
    if domain.split("//")[0] == "http:" or domain.split("//")[0] == "https:":
        domain = domain.split("//")[1]
    url = parameters["api_url"]
    method = parameters["api_method"]
    headers = parameters["api_headers"]
    body_type = parameters["api_body_type"]
    body_value = parameters["api_body_value"]
    result = {}
    heads = {"is_success": ""}

    # 必填项没有时,则显示错误
    if url == "":
        error_code = 100007
    elif domain == "":
        error_code = 100008
    else:
        try:
            if body_type == "1" or body_type == 1:
                register_openers()
                datagen, re_headers = multipart_encode(body_value)
                api_request = urllib2.Request(url, datagen, re_headers)
                if headers != "":
                    for keys in headers:
                        api_request.add_header(keys, headers[keys])
                response = urllib2.urlopen(api_request)
                result_data = response.read()
                http_code = response.code
            else:
                if api_http_type == 2 or api_http_type == "2":
                    connection = httplib.HTTPSConnection(domain)
                else:
                    connection = httplib.HTTPConnection(domain)
                if headers == "":
                    connection.request(method=method, url=url, body=body_value)
                else:
                    connection.request(method=method, url=url, headers=headers, body=body_value)
                response = connection.getresponse()
                http_code = response.status
                result_data = response.read()
            error_code = 0
            result["datas"] = json.dumps(result_data)
            if api_id != "":
                execute_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
                update_api = ApiApi.objects.get(id=api_id)
                update_api.last_execute_time = execute_time
                update_api.api_real_result = result_data
                if int(http_code) == 200:
                    # 如果实际结果和数据库中存的期望结果一致,显示成功
                    if str(result_data).strip().lstrip() == str(update_api.api_expect_result).strip().lstrip():
                        is_success = 1
                        update_api.api_is_success = 1
                    else:
                        is_success = 0
                        update_api.api_is_success = 0
                else:
                    is_success = -1
                    update_api.api_is_success = -1
                update_api.save()
                heads["is_success"] = is_success
                heads["http_code"] = int(http_code)
        except Exception as ex:
            error_code = 110000
            heads["exception"] = str(ex)

    heads["code"] = error_code
    heads["message"] = errorinfo.change_to_message(error_code)
    result["heads"] = heads
    return result
Beispiel #22
0
def public_save(request):
    user_id = request.POST.get("user_id", "")
    user_name = request.POST.get("user_name", "")
    user_real_name = request.POST.get("user_real_name", "")
    user_email = request.POST.get("user_email", "")
    user_password = request.POST.get("user_password", "")
    user_re_password = request.POST.get("user_re_password", "")
    user_is_admin = request.POST.get("user_is_admin", 0)
    permission_list = request.POST.get("permission_list", "")
    if user_is_admin == 1 or user_is_admin == "1":
        user_is_admin = True
    else:
        user_is_admin = False
    result = {}
    head = {}
    if user_name == "":
        error_code = 100015
    elif user_password == "" or user_re_password == "":
        error_code = 100021
    elif user_password != user_re_password:
        error_code = 100019
    elif len(user_password) < 6:
        error_code = 100020
    else:
        new_user_id = ""
        if user_id == "":
            if User.objects.filter(username=user_name, is_active=1):
                error_code = 100017
            else:
                add_user = User(username=user_name, first_name=user_real_name, is_superuser=user_is_admin, email=user_email)
                try:
                    add_user.set_password(user_password)
                    add_user.save()
                    new_user_id = add_user.id
                    error_code = 0
                except Exception as ex:
                    head["exceptions"] = ex
                    error_code = 110000
        else:
            if User.objects.filter(username=user_name, is_active=1).exclude(id=user_id):
                error_code = 100017
            else:
                edit_user = get_object_or_404(User, id=user_id)
                try:
                    edit_user.username = user_name
                    edit_user.first_name = user_real_name
                    edit_user.email = user_email
                    if user_password != edit_user.password:
                        edit_user.set_password(user_password)
                    edit_user.is_superuser = user_is_admin
                    edit_user.save()
                    new_user_id = user_id
                    error_code = 0
                except Exception as ex:
                    head["exceptions"] = ex
                    error_code = 110000
        # 保存权限
        if new_user_id != "":
            permissions = Permission.objects.filter(user_id=new_user_id)
            if permissions:
                for pers in permissions:
                    pers.delete()
            if permission_list != "":
                permission_list = permission_list.encode("utf8")
                permission_list = permission_list.split(",")
                for permission_id in permission_list:
                    permission_info = Permission.objects.filter(user_id=new_user_id, project_id=permission_id)
                    if permission_info:
                        pass
                    else:
                        new_permission = Permission(user_id=new_user_id, project_id=permission_id)
                        new_permission.save()
    head["code"] = error_code
    head["message"] = errorinfo.change_to_message(error_code)
    result["heads"] = head
    return result