def test_form_data_subform_widget(self): raw_payload = '{"app_id":"app_id","entry_id":"entry_id","data":{"数字":123,"子表单":[{"子表单单行文本":"子表单单行文本1","子表单数字":123.01},{"子表单单行文本":"子表单单行文本2","子表单数字":123.02}]},"op":"create","query_fields":["数字"]}' raw_widgets = '{"widgets":[{"name":"_widget_number","label":"数字","type":"number"},{"name":"_widget_subform","label":"子表单","type":"subform","items":[{"name":"_widget_subfrom_text","label":"子表单单行文本","type":"text"},{"name":"_widget_subform_number","label":"子表单数字","type":"number"}]}]}' widgets = json.loads(raw_widgets).get('widgets') payload = json.loads(raw_payload) form_data = FormData(widgets, payload) self.assertDictEqual( form_data.schema.data['_widget_subform'], { 'value': [{ '_widget_subform_number': { 'value': 123.01 }, '_widget_subfrom_text': { 'value': '子表单单行文本1' } }, { '_widget_subform_number': { 'value': 123.02 }, '_widget_subfrom_text': { 'value': '子表单单行文本2' } }] })
def test_create_form_data(self): raw_payload = '{"app_id":"app_id","entry_id":"entry_id","data":{"单行文本":"单行文本测试数据","数字":123.12,"日期时间":"2018-06-28","子表单":[{"单行文本":"子表单单行文本1","数字":123.12},{"单行文本":"子表单单行文本2","数字":123.12}]},"op":"create","query_fields":["单行文本"]}' raw_widgets = '{"widgets":[{"name":"_widget_1534402957633","label":"单行文本","type":"text"},{"name":"_widget_1534410141534","label":"数字","type":"number"},{"name":"_widget_1534486898848","label":"日期时间","type":"datetime"},{"name":"_widget_1534927008217","label":"下拉复选框","type":"combocheck"},{"name":"_widget_1534927008179","label":"复选框组","type":"checkboxgroup"},{"name":"_widget_1534732137668","label":"单选按钮组","type":"radiogroup"},{"name":"_widget_1534732137742","label":"下拉框","type":"combo"},{"name":"_widget_1534927008108","label":"部门多选","type":"deptgroup"},{"name":"_widget_1534732137803","label":"成员单选","type":"user"},{"name":"_widget_1534732137822","label":"部门单选","type":"dept"},{"name":"_widget_1534732138005","label":"子表单","type":"subform","items":[{"name":"_widget_1534732138017","label":"单行文本","type":"text"},{"name":"_widget_1534732138020","label":"数字","type":"number"},{"name":"_widget_1534732138024","label":"日期时间","type":"datetime"}]}]}' widgets = json.loads(raw_widgets).get('widgets') payload = json.loads(raw_payload) form_data = FormData(widgets, payload) self.assertEqual(form_data.operate, 'create') self.assertEqual(form_data.identifier, 'app_id-entry_id')
def test_form_data_deptgroup_widget(self): raw_payload = '{"app_id":"app_id","entry_id":"entry_id","data":{"部门多选":["dept_id_1","dept_id_2"]},"op":"create","query_fields":["部门多选"]}' raw_widgets = '{"widgets":[{"name":"_widget_deptgroup","label":"部门多选","type":"deptgroup"}]}' widgets = json.loads(raw_widgets).get('widgets') payload = json.loads(raw_payload) form_data = FormData(widgets, payload) self.assertDictEqual(form_data.schema.data['_widget_deptgroup'], {'value': ['dept_id_1', 'dept_id_2']})
def test_create_from_data_payload_without_query_fields_key(self): raw_widgets = '{"widgets":[{"name":"_widget_1534402957633","label":"单行文本","type":"text"},{"name":"_widget_1534410141534","label":"数字","type":"number"},{"name":"_widget_1534486898848","label":"日期时间","type":"datetime"},{"name":"_widget_1534927008217","label":"下拉复选框","type":"combocheck"},{"name":"_widget_1534927008179","label":"复选框组","type":"checkboxgroup"},{"name":"_widget_1534732137668","label":"单选按钮组","type":"radiogroup"},{"name":"_widget_1534732137742","label":"下拉框","type":"combo"},{"name":"_widget_1534927008108","label":"部门多选","type":"deptgroup"},{"name":"_widget_1534732137803","label":"成员单选","type":"user"},{"name":"_widget_1534732137822","label":"部门单选","type":"dept"},{"name":"_widget_1534732138005","label":"子表单","type":"subform","items":[{"name":"_widget_1534732138017","label":"单行文本","type":"text"},{"name":"_widget_1534732138020","label":"数字","type":"number"},{"name":"_widget_1534732138024","label":"日期时间","type":"datetime"}]}]}' widgets = json.loads(raw_widgets).get('widgets') raw_payload = '{"app_id":"app_id","entry_id":"entry_id","data":{},"op":"create"}' payload = json.loads(raw_payload) try: form_data = FormData(widgets, payload) except Exception: self.assertRaises(KeyError)
def test_form_data_user_widget(self): raw_payload = '{"app_id":"app_id","entry_id":"entry_id","data":{"成员单选":"user_id"},"op":"create","query_fields":["成员单选"]}' raw_widgets = '{"widgets":[{"name":"_widget_user","label":"成员单选","type":"user"}]}' widgets = json.loads(raw_widgets).get('widgets') payload = json.loads(raw_payload) form_data = FormData(widgets, payload) self.assertDictEqual(form_data.schema.data['_widget_user'], {'value': 'user_id'})
def _handle_form_data(self, form_data: FormData): handle_funcs = { 'create': self._create_from_data, 'update': self._update_from_data, 'delete': self._delete_form_data } exists_data = self._query_exists_form_data(form_data) form_data.exists_data = exists_data handle_func = handle_funcs[form_data.operate.lower()] handle_func(form_data)
def test_form_data_number_widget(self): raw_payload = '{"app_id":"app_id","entry_id":"entry_id","data":{"数字1":123,"数字2":123.1},"op":"create","query_fields":["数字1"]}' raw_widgets = '{"widgets":[{"name":"_widget_number_1","label":"数字1","type":"number"},{"name":"_widget_number_2","label":"数字2","type":"number"}]}' widgets = json.loads(raw_widgets).get('widgets') payload = json.loads(raw_payload) form_data = FormData(widgets, payload) self.assertDictEqual(form_data.schema.data['_widget_number_1'], {'value': 123}) self.assertDictEqual(form_data.schema.data['_widget_number_2'], {'value': 123.1})
def _init_form_data(self, message: QueueMessage) -> FormData: try: payload = json.loads(message.payload) widgets = self._get_from_data_widgets(payload) form_data = FormData(widgets=widgets, payload=payload) except json.decoder.JSONDecodeError as e: raise PayloadDecodeError(e.msg) except KeyError as e: raise PayloadKeyError(e) else: return form_data
def test_form_data_string_widget(self): raw_payload = '{"app_id":"app_id","entry_id":"entry_id","data":{"单行文本":"单行文本"},"op":"create","query_fields":["单行文本"]}' raw_widgets = '{"widgets":[{"name":"_widget_text","label":"单行文本","type":"text"}]}' widgets = json.loads(raw_widgets).get('widgets') payload = json.loads(raw_payload) form_data = FormData(widgets, payload) self.assertEqual(form_data.app_id, 'app_id') self.assertEqual(form_data.entry_id, 'entry_id') self.assertListEqual(form_data.query_fields, [ '单行文本', ]) self.assertDictEqual(form_data.schema.data['_widget_text'], {'value': '单行文本'})