From 45a4792730416a1194db3f5a5f3be5a5734c7960 Mon Sep 17 00:00:00 2001 From: hyh123a Date: Mon, 18 Apr 2022 15:49:15 +0800 Subject: [PATCH] modify the ticket application post api --- myems-api/reports/ticket.py | 61 +++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/myems-api/reports/ticket.py b/myems-api/reports/ticket.py index aa72e400..d5f61149 100644 --- a/myems-api/reports/ticket.py +++ b/myems-api/reports/ticket.py @@ -9,10 +9,10 @@ import requests def login(): - url = "http://192.168.1.4/api/v1.0/login" + url = "http://172.16.203.116/api/v1.0/login" payload = json.dumps({ - "username": "admin", + "username": "user", "password": "123456", "type": "account" }) @@ -141,9 +141,29 @@ class TicketApplicationCollection: if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, '400 Bad Request') + result = { + "id": None, + "name": None, + "transition_id": None, + "workflow_id": int(id_), + "fields": [] + } + cnx = mysql.connector.connect(**config.loonflow) cursor = cnx.cursor() + # Get workflow transition id: only the first line + query = (" SELECT id, name, transition_type_id, source_state_id, destination_state_id " + " FROM workflow_transition" + " WHERE workflow_id = %s " + " ORDER BY id ") + cursor.execute(query, (id_,)) + rows = cursor.fetchall() + + if rows is not None and len(rows) > 0: + row = rows[0] + result['transition_id'] = row[0] + # Get workflow custom field query = (" SELECT id, field_type_id, field_key, field_name, order_id, default_value, description, " " field_template, boolean_field_display " @@ -154,6 +174,10 @@ class TicketApplicationCollection: rows = cursor.fetchall() field_result = {} + field_types = { + 5: 'input', + 55: 'text_area', + } if rows is not None and len(rows) > 0: for row in rows: field_key = row[2] @@ -167,6 +191,7 @@ class TicketApplicationCollection: 'description': row[6], 'field_template': row[7], 'boolean_field_display': row[8], + 'field_type': field_types.get(row[1]), } # Get workflow start state @@ -181,11 +206,7 @@ class TicketApplicationCollection: workflow_state_name = None state_field_str = None fields = [] - result = { - "id": None, - "name": None, - "fields": [] - } + if row is not None and len(row) > 0: workflow_state_id = row[0] workflow_state_name = row[1] @@ -202,6 +223,7 @@ class TicketApplicationCollection: "default_value": None, "description": "", "field_template": "", + 'field_type': field_types.get(5), } ) if key in field_result.keys(): @@ -218,7 +240,6 @@ class TicketApplicationCollection: @staticmethod def on_post(req, resp, id_): """Handles POST requests""" - access_control(req) try: raw_json = req.stream.read().decode('utf-8') except Exception as ex: @@ -233,14 +254,6 @@ class TicketApplicationCollection: new_values = json.loads(raw_json) checked_fields = [ - { - "name": "title", - "type": str - }, - { - "name": "long_field", - "type": str - }, { "name": "transition_id", "type": int @@ -255,22 +268,24 @@ class TicketApplicationCollection: for field in checked_fields: _name = field['name'] _type = field['type'] - if _name not in new_values['data'].keys() or \ - not isinstance(new_values['data'][_name], _type) or \ - len(str.strip(new_values['data'][_name])) == 0: + if _name not in new_values.keys() or \ + not isinstance(new_values[_name], _type) or \ + len(str(new_values[_name])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_FIELD' + _name) - payload = new_values['data'] + payload = new_values session = login() - url = 'http://192.168.1.4/api/v1.0/tickets' + url = 'http://172.16.203.116/api/v1.0/tickets' headers = { 'Content-Type': 'application/json', } - resp = session.post(url, headers=headers, data=payload) + resp = session.post(url, headers=headers, data=json.dumps(payload)) content = json.loads(resp.text) - new_ticket_id = content['data']['ticket_id'] + print("content", content) + new_ticket_id = content.get('ticket_id') + print(resp.status_code) if resp.status_code != 200: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_COOKIE', description='API.INVALID_STATUS_CODE:' + str(resp.status_code))