将脚本公开给Django Rest API

时间:2018-10-03 13:54:57

标签: python django django-rest-framework

我有一个简单的现有Django应用,该应用对第三方工具进行了一些REST调用以创建一些用户。现在,我的任务是将现有的Django应用程序公开给来自另一个平台的REST API调用。我的问题是,如何将通过REST请求从第三方工具发送的参数传递给我现有的应用程序,以便可以使用这些参数来驱动在我的应用程序中执行的REST请求,从而在另外三分之一中创建/更新用户派对平台。我认为这是可能的。

这是views.py文件的一部分:(我想将此视图页面公开为REST API,用户可以调用该REST API并传递必要的信息(server_nameuser_name,{{1 }},password),以便可以将其传递给addUser函数以驱动后续的REST请求

site_url

以下是应用程序url.py文件的一部分

def signIn(server_name, user_name, password, site_url, contentUrl):
    site_url = contentUrl[site_url]

    payload = {
        "credentials": {
            "name": user_name,
            "password": password,
            "site": {
                "contentUrl": site_url
            }
        }
    }

    headers = {
        "accept" : "application/json",
        "content-type" : "application/json"
    }

    signIn_req_url = "https://{server}/api/{api}/auth/signin".format(server=server_name, api=api)
    signIn_req = requests.post(signIn_req_url, json=payload, headers=headers, verify=False)
    response = json.loads(signIn_req.content)
    headers["X-tableau-auth"] = response["credentials"]["token"]
    site_id = response["credentials"]["site"]["id"]
    return(headers, site_id)

# Create your views here.
def index(request):
    return(render(request, "index.html"))

def checkTest(request):
    cluster = request.GET.get("cluster")

    contentURLs = { "Default" : "" }

    site_url = "Default"

    server_name = server_dict[cluster]
    headers, site_id = signIn(server_name, user_name, password, site_url, contentURLs)

    sites_req_url = "https://{server}/api/{api}/sites".format(server=server_name, api=api)
    sites_req = requests.get(sites_req_url, headers=headers, verify=False)
    sites_res = json.loads(sites_req.content)
    data = {}
    for item in sites_res["sites"]["site"]:
        data[item["name"]] = item["name"]
        contentURLs[item["name"]] = item["contentUrl"]

    signout_req_url = "http://{server}/api/{api}/auth/signout".format(server=server_name, api=api)
    signout_req = requests.post(signout_req_url, data=b'', headers=headers, verify=False)

    request.session["content"] = contentURLs
    return JsonResponse(data)

def addUser(request):
    if request.method == "POST":
        form = addUserForm(request.POST)
        if form.is_valid():

            server_name = server_dict[form.cleaned_data["cluster"]]
            add_payload = {
                "user": {
                    "name" : form.cleaned_data["user_id"],
                    "siteRole" : form.cleaned_data["privelege"],
                    "authSetting" : "ServerDefault"
                }
            }

            site_url = form.cleaned_data["site"]
            headers, site_id = signIn(server_name, user_name, password, site_url, request.session["content"])

            add_req_url = "https://{server}/api/{api}/sites/{site}/users".format(server=server_name, api=api, site=site_id)
            add_req = requests.post(add_req_url, json=add_payload, headers=headers, verify=False)

            signout_req_url = "http://{server}/api/{api}/auth/signout".format(server=server_name, api=api)
            signout_req = requests.post(signout_req_url, data=b'', headers=headers, verify=False)

            if add_req.status_code == 201:
                messages.success(request, 'User added successfully!')
            else:
                messages.success(request, 'Something went wrong while adding user!')
            return HttpResponseRedirect("index")
    else:
        form = addUserForm()
    return(render(request, "addUser.html", { "form" : form }))  

forms.py文件包含类

urlpatterns = [

    # CONTROL
    path("checkTest", views.checkTest, name="checkTest"),

    # PAGES
    path("index", views.index, name="index"),
    path("logon", views.logon, name="logon"),
    path("addUser", views.addUser, name="addUser"),
    ...
]

0 个答案:

没有答案