Example #1
0
def maintenance_new_server(request):
    is_add = True
    source_model = None
    params = request.REQUEST
    server_id = params.get("server_id", 0)
    if not server_id:
        return {"code": 1, "msg": "没有参数"}

    model = Server.objects.filter(id=int(server_id))
    if model:
        return HttpResponse(json.dumps({"code": 1, "msg": "已存在该服务器ID"}))

    try:
        model = Server()
        model.id = server_id
        model.name = params.get("name", "")
        model.tabId = params.get("tabId", 1)
        model.status = -1
        model.commend = params.get("commend", 0)
        model.game_addr = params.get("game_addr", "")
        model.game_port = params.get("game_port", "")
        model.log_db_config = params.get("log_db_config", "")
        model.order = params.get("server_id")
        model.alias = params.get("alias", "")
        model.create_time = datetime.datetime.strptime(
            params.get("create_time"), '%Y-%m-%d %H:%M:%S')
        model.last_time = datetime.datetime.now()
        model.save(using="write")
        # 保存详细操作日志
        save_server_log(server_id, is_add, request, source_model, model)
        return HttpResponse(json.dumps({"code": 0, "msg": "success"}))
    except BaseException as e:
        return HttpResponse(json.dumps({"code": 1, "msg": "save fail!"}))
Example #2
0
def server_save(request, server_id=0):
    server_id = server_id or int(request.REQUEST.get('sid', '') or '0')
    new_server_id = int(request.REQUEST.get('new_server_id', '') or 0)
    model = None
    source_model = None
    is_add = True

    if server_id:
        model = Server.objects.get(id=server_id)
        source_model = copy.copy(model)
        is_add = False

    if not model:
        model = Server()
        model.id = new_server_id

    list_group = Group.objects.all()
    exists = False
    update_id = False
    err_msg = ''

    if server_id != new_server_id:
        exists = Server.objects.filter(id=new_server_id).exists()
        if exists:
            err_msg = '保存的服务器ID已存在'
            return render_to_response('feedback.html', locals())
        else:
            update_id = True

    if not request.POST.get("tabId"):
        err_msg = '请填入分组ID'
        return render_to_response('feedback.html', locals())

    model.tabId = int(request.POST.get("tabId") or 0)
    # model.client_ver = request.POST.get('client_ver', '') # 客户端版本
    model.name = request.POST.get('name', '')
    model.status = int(request.POST.get('status', '2'))
    model.commend = int(request.POST.get('commend', '2'))
    # model.require_ver = int(request.POST.get('require_ver', '0')) # 要求最低版本
    model.game_addr = request.POST.get('game_addr', '')
    model.game_port = int(request.POST.get('game_port', '2008'))
    model.report_url = request.POST.get('report_url', '')
    model.log_db_config = request.POST.get('log_db_config', '')
    model.remark = request.POST.get('remark', '')
    model.order = new_server_id if server_id == 0 else server_id
    model.json_data = request.POST.get('json_data', '')
    model.alias = request.REQUEST.get('alias', '')
    # model.is_ios = int(request.POST.get('is_ios', '0'))   #ios标识
    create_time = request.POST.get('create_time', '')
    model.last_time = datetime.datetime.now()
    model.game_db_addr_port = request.POST.get('game_db_addr_port', '')
    model.game_db_name = request.POST.get('game_db_name', '')
    model.game_db_user = request.POST.get('game_db_user', '')
    model.game_db_password = request.POST.get('game_db_password', '')
    if create_time == '':
        create_time = datetime.datetime.now()
    else:
        create_time = datetime.datetime.strptime(create_time,
                                                 '%Y-%m-%d %H:%M:%S')
    model.create_time = create_time

    # 保存详细操作日志
    save_server_log(server_id, is_add, request, source_model, model)

    if not exists:
        if model.name != '' and model.game_addr != '':
            try:
                # 修改ID
                old_server = copy.copy(model)
                if update_id:
                    model.id = new_server_id
                model.save()

                group_ids = request.POST.getlist('group_id')
                group_ids = [int(x) for x in group_ids]

                for group_model in list_group:
                    if group_model.id in group_ids:
                        group_model.server.add(model)
                        try:
                            # 添加服务器分区后,在增加到角色资源里面
                            from models.admin import Role, Resource
                            role_exclude = Role.objects.exclude(
                                name__contains="系统管理员")
                            for r in role_exclude:
                                role_group_list = r.get_resource(
                                    'server_group')
                                if group_model in role_group_list:
                                    ser_l = list(
                                        group_model.server.values_list('id'))
                                    extend_l = r.get_resource(
                                        'server').values_list('id')
                                    ser_l.extend(extend_l)
                                    ser_l = list(set([i[0] for i in ser_l]))
                                    r.create_resource('server', ser_l)
                                    r.save()
                        except:

                            print trace_msg()

                    else:
                        group_model.server.remove(model)

                # 前面保存没有出错
                if update_id:
                    old_server.status = Server.Status.TEST
                    old_server.save()

            except Exception, e:

                err_msg = trace_msg()

        else:
            err_msg = '填写必要数据!'