Esempio n. 1
0
def encrypt(request):
    post = request.POST
    servers = get_list_or_404(Server, active=True)
    service = post.get('service', 'systemTime')
    version = post.get('version', '2.0')
    data = post.get('data', '{}')
    # files = request.FILES.get('files', None)
    files = request.FILES.getlist('files', list())
    service = service or 'systemTime'
    data = data or '{}'
    try:
        dataj = json.loads(get_func_val(data))
    except ValueError:
        content = 'Please input JSON value\n\n\n\n%s' % data
        return HttpResponse(content=content,
                            content_type='text/plain;charset=utf-8')
    else:
        data = dataj
    resp = list()
    for server in servers:
        if files:
            url = server.address + server.postfix_file
        else:
            url = server.address + server.postfix
        necessary = encrypt_necessary(service, version, data, server.key
                                      or DEFAULT_EKEY)
        necessary2 = necessary.copy()
        djson = json.dumps(data, ensure_ascii=False).encode('utf-8')
        necessary2.update(dict(data=djson, test='yes'))
        urls = [
            url + '?' + urlencode(necessary2), url + '?' + urlencode(necessary)
        ]
        resp.append({server.name: urls})
    pretty = json.dumps(resp, indent=4, ensure_ascii=False).encode('utf-8')
    return HttpResponse(pretty, content_type='text/plain;charset=utf-8')
Esempio n. 2
0
def module_query(request):
    post = request.POST
    server_id = post.get('server', 1)
    server_id = server_id or 1
    server = get_object_or_404(Server, pk=server_id)
    services = post.get('services', '[]')
    params = post.get('params', '{}')
    services = services or '[]'
    params = params or '{}'
    services = json.loads(services)
    params = json.loads(get_func_val(params))
    service_response = dict()
    results = list()
    execute_begin = timezone.now()
    for s in services:
        srvc = get_service_by_name(s)
        if not srvc:
            break
        key = '%s(%s)' % (srvc.service, srvc.version)
        current_service_request_data = params.get(key,
                                                  params.get(srvc.service))
        # get params
        for curt_ser_key, curt_ser_key_val in current_service_request_data.items(
        ):
            try:
                if isinstance(curt_ser_key_val, (dict, )):
                    # 匹配"logout":{"uid":{"login":"******"}}类型
                    for pre_ser, pre_ser_resp_key in curt_ser_key_val.items():
                        pre_srvc = get_service_by_name(pre_ser)
                        pre_key = '%s(%s)' % (pre_srvc.service,
                                              pre_srvc.version)
                        try:
                            pre_ser_resp = service_response[pre_key]
                            current_service_request_data[
                                curt_ser_key] = pre_ser_resp[pre_ser_resp_key]
                        except Exception, e:
                            # raise e
                            log.error(e)
                elif isinstance(curt_ser_key_val, (list, )):
                    # 匹配"kRoomComment":{"kRoomId":["kRoomMyList",0,"kRoomId"]}类型
                    if len(curt_ser_key_val) == 3:
                        pre_ser = curt_ser_key_val[0]
                        pre_ser_resp_index = curt_ser_key_val[1]
                        pre_ser_resp_index_key = curt_ser_key_val[2]
                        pre_srvc = get_service_by_name(pre_ser)
                        pre_key = '%s(%s)' % (pre_srvc.service,
                                              pre_srvc.version)
                        try:
                            pre_ser_resp = service_response[pre_key]
                            current_service_request_data[
                                curt_ser_key] = pre_ser_resp[
                                    pre_ser_resp_index][pre_ser_resp_index_key]
                        except Exception, e:
                            log.error(e)
                else:
                    pass
Esempio n. 3
0
def ajax_module_params(request):
    post = request.POST
    module_id = post.get('module', 1)
    module_id = module_id or 1
    module = get_object_or_404(Module, pk=module_id)
    params = json.loads(module.param_dict)
    for key in params.keys():
        try:
            params[key].update(json.loads(get_func_val(module.additional)))
        except ValueError:
            pass
    result = dict(services=json.loads(module.service_list), params=params)
    resp = json.dumps(result, ensure_ascii=False).encode('utf-8')
    return HttpResponse(content=resp, content_type='text/plain;charset=UTF-8')
Esempio n. 4
0
def repeat_query(request):
    post = request.POST
    server = get_object_or_404(Server, pk=post.get('server', 1))
    service = post.get('service', 'systemTime')
    version = post.get('version', '2.0')
    data = post.get('data', '{}')
    # files = request.FILES.get('files', None)
    files = request.FILES.getlist('files', list())
    repeat = post.get('repeat', 1)
    thread = post.get('thread', False)
    service = service or 'systemTime'
    data = data or '{}'
    try:
        dataj = json.loads(get_func_val(data))
    except ValueError:
        content = 'Please input JSON value\n\n\n\n%s' % data
        return HttpResponse(content=content,
                            content_type='text/plain;charset=utf-8')
    else:
        data = dataj
    repeat = int(repeat) or 1
    thread = True if thread else False
    results = list()
    create = timezone.now()
    if thread:
        results.extend(
            migu_multi_thread(djprocess,
                              (server, service, version, data, files), repeat))
    else:
        for i in range(1, repeat + 1 if repeat > 0 else 2):
            try:
                results.append(
                    djprocess(server=server,
                              service=service,
                              version=version,
                              data=data,
                              files=files))
            except:
                pass
    execute_time = timezone.now() - create
    return render_to_response(template_name='migu/results.html',
                              context={
                                  'server': server,
                                  'results': results,
                                  'create': create,
                                  'execute_time': execute_time
                              },
                              context_instance=RequestContext(request))
Esempio n. 5
0
def query(request):
    post = request.POST
    server = get_object_or_404(Server, pk=post.get('server', 1))
    service = post.get('service', 'systemTime')
    version = post.get('version', '2.0')
    data = post.get('data', '{}')
    # files = request.FILES.get('files', None)
    files = request.FILES.getlist('files', list())
    service = service or 'systemTime'
    data = data or '{}'
    try:
        dataj = json.loads(get_func_val(data))
    except ValueError, value_error:
        log.error(value_error)
        content = 'Please input JSON value\n\n\n\n%s' % data
        return HttpResponse(content=content,
                            content_type='text/plain;charset=utf-8')
Esempio n. 6
0
def multi_query(request):
    post = request.POST
    server = get_object_or_404(Server, pk=post.get('server', 1))
    service = post.get('service', 'systemTime')
    version = post.get('version', '2.0')
    data = post.get('data', '{}')
    # files = request.FILES.get('files', None)
    files = request.FILES.getlist('files', list())
    params = post.get('params', '{}')
    thread = post.get('thread', False)
    service = service or 'systemTime'
    data = data or '{}'
    try:
        dataj = json.loads(get_func_val(data))
    except ValueError:
        content = 'Please input JSON value\n\n\n\n%s' % data
        return HttpResponse(content=content,
                            content_type='text/plain;charset=utf-8')
    else:
        data = dataj
    params = params or '{}'
    params = json.loads(params)
    thread = True if thread else False
    results = list()
    create = timezone.now()
    if params and isinstance(params, (dict, )) and params.keys():
        # for key in params.keys():
        # 只接受单参数变化
        key = params.keys()[0]
        k_value = params[key]
        if isinstance(k_value, (list, )):
            if thread:
                threads = list()
                length = len(k_value)
                sequence = range(length)
                for i in sequence:
                    data[key] = k_value[i]
                    t = MiguThread(
                        djprocess,
                        (server, service, version, data.copy(), files),
                        djprocess.__name__)
                    threads.append(t)

                for i in sequence:
                    threads[i].start()
                for i in sequence:
                    threads[i].join()
                for i in sequence:
                    try:
                        results.append(threads[i].get_result())
                    except:
                        pass
            else:
                for value in k_value:
                    data[key] = value
                    try:
                        results.append(
                            djprocess(server=server,
                                      service=service,
                                      version=version,
                                      data=data,
                                      files=files))
                    except:
                        pass
        elif isinstance(k_value, (
                basestring,
                int,
                float,
                bool,
        )):
            data[key] = k_value
            results.append(
                djprocess(server=server,
                          service=service,
                          version=version,
                          data=data,
                          files=files))
    else:
        results.append(
            djprocess(server=server,
                      service=service,
                      version=version,
                      data=data,
                      files=files))
    execute_time = timezone.now() - create
    return render_to_response(template_name='migu/results.html',
                              context={
                                  'server': server,
                                  'results': results,
                                  'create': create,
                                  'execute_time': execute_time
                              },
                              context_instance=RequestContext(request))