simulating passing parameters of front end by requests module

@action(detail=False, methods=['post'], url_name="新增模板")
    def create_template(self, request):
        success = True
        message = "创建成功"
        # logging.error("request.body%s" % request.body)
        logging.error("request.POST: %s" % request.POST)
        param = request.POST["params"]
        logging.error("params: %s" % param)
        p = json.loads(param)
        uuid = UUIDTools()
        template_info = {
            "id": uuid.uuid1_hex(),
            "name": p["name"],
            "categories": p["categories"],
            "secondCategories": p["secondCategories"],
            "thirdCategories": p["thirdCategories"],
            "benchmarkScore": p["benchmarkScore"],
            "indexJson": p["indexJson"],
            "creater": user_info["username"],
            "createrId": user_info["createrId"],
            # "creater":"user",
            "gmtCreate": datetime.now(),
            "gmtModify": None,
            "deleted": 0
        }
        logging.info("template_info%s" % template_info)
        result = PerformanceTemplate.objects.filter(name=p["name"], deleted=0)
        if len(result) > 0:
            success = False
            message = "创建失败:该模板名称已存在!"
            return getResponse(Response({"data": "该模板名称已存在"}), message, success)
        else:
            try:
                PerformanceTemplate.objects.create(**template_info)
                template_obj = PerformanceTemplate.objects.order_by("gmtCreate").last()
                ret = {
                    "id": template_obj.id,
                    "name": template_obj.name,
                    "categories": template_obj.categories,
                    "secondCategories": template_obj.secondCategories,
                    "thirdCategories": template_obj.thirdCategories,
                    "benchmarkScore": template_obj.benchmarkScore,
                    "indexJson": template_obj.indexJson,
                    "gmtCreate": template_obj.gmtCreate,
                    "gmtModify": template_obj.gmtModify,
                    "createrId": template_obj.createrId,
                    "menderId": template_obj.menderId,
                    "creater": template_obj.creater,
                    "mender": template_obj.mender,
                    "deleted": template_obj.deleted,
                }
                logging.info("ret%s" % ret)
            except Exception as e:
                logging.warning("create_template_error%s" % (str(e)))
                success = False
                message = "创建失败"
                ret = {'data': message}
            return getResponse(Response(ret), message, success)

    def test_create_template(self,):
        params = {"name": "test_001",
                  "categories": "一级分类",
                  "secondCategories": "二级分类",
                  "thirdCategories": "三级分类",
                  "benchmarkScore": 70,
                  "indexJson": ["index_test"]
                  }
        params = json.dumps(params)
        data ={"params":params}
        url = 'http://127.0.0.1:8000/performance_template/Template/create_template/'
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
        r = requests.post(url, data=data, headers=headers, verify=False)
        logging.info("test_create_template:%s" % r.text)
        self.assertEqual(r.status_code, 200)

# params为key,params的value是个json字符串,由json.dumps()生成

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值