Esempio n. 1
0
def insert_test_result_with_form(request):
    try:
        if request.method == 'POST':
            
            # 标准的从网页提交的数据会直接被django将数据转换到了requst.POST中,表现为QueryDict. 
            # form = VexLoadTestInsertionForm(request.POST)
            # 我们这里是从request body中post过去的,因此需要转换request.body中的数据到dict,才能使用form的validation
            # 且validation要求UI上的post对象的名称和model中的对象的名称完全一致。因此需要转换index_results和bitrate_results为test_result_index和test_result_bitrate
            data = request.POST if request.POST else convert_request_body_to_form_validation(request.body)
            form = VexLoadTestInsertionForm(data)
            errors = {}
            # 验证表单是否正确
            if form.is_valid():
                form.save()
                return HttpResponse("Test result has been saved.")
            else:
                errors.update(form.errors)
                return HttpResponse(json.dumps({"errors": errors}), content_type="application/json", status=400)
                '''
                # 如果是UI,则可以直接在HttpResponse中直接返回form对象,由form.***.errors直接在页面显示错误
                <form action="" method="post">
                    <div class="field">
                        {{ form.subject.errors }}
                        <label for="id_subject">Subject:</label>
                        {{ form.subject }}
                    </div>
                    <div class="field">
                        {{ form.email.errors }}
                        <label for="id_email">Your e-mail address:</label>
                        {{ form.email }}
                    </div>
                    <div class="field">
                        {{ form.message.errors }}
                        <label for="id_message">Message:</label>
                        {{ form.message }}
                    </div>
                    <input type="submit" value="Submit">
                </form>
                '''
        else:   
            return HttpResponse("Request must be posted with XML body", status=400)
    except Exception, e:
        logger.error(e)
        return HttpResponse(e.message, status=400)
Esempio n. 2
0
class VexLoadTestInsertionFormTests(TestCase):
    error_loadtest_data_file = 'request_body_with_error_test_type.xml'
    test_data_dir = os.path.join(os.path.dirname(__file__), 'testdatas')
    
    def setUp(self):
        self.test_client = Client()
        with open(os.path.join(self.test_data_dir, self.error_loadtest_data_file)) as f:
            self.form_data = f.read()
            self.converted_content = convert_request_body_to_form_validation(self.form_data)
            self.form = VexLoadTestInsertionForm(data=self.converted_content)
    
    def test_invalidate_forms(self):
        self.form.is_valid()
        self.assertFalse(self.form.is_valid())
        
    def test_invalid_forms_response(self):
        response = self.test_client.post(reverse("insert"), data=self.form_data, content_type='application/xml')
        # self.assertFormError(response, self.form, 'test_type', 'This field is required.')
        self.assertContains(response, self.converted_content['test_type'], status_code=400)
Esempio n. 3
0
 def setUp(self):
     self.test_client = Client()
     with open(os.path.join(self.test_data_dir, self.error_loadtest_data_file)) as f:
         self.form_data = f.read()
         self.converted_content = convert_request_body_to_form_validation(self.form_data)
         self.form = VexLoadTestInsertionForm(data=self.converted_content)