modify the ticket application post api

pull/141/MERGE^2
hyh123a 2022-04-18 15:49:15 +08:00
parent 16f994323f
commit 45a4792730
1 changed files with 38 additions and 23 deletions

View File

@ -9,10 +9,10 @@ import requests
def login(): 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({ payload = json.dumps({
"username": "admin", "username": "user",
"password": "123456", "password": "123456",
"type": "account" "type": "account"
}) })
@ -141,9 +141,29 @@ class TicketApplicationCollection:
if not id_.isdigit() or int(id_) <= 0: if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(falcon.HTTP_400, '400 Bad Request') 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) cnx = mysql.connector.connect(**config.loonflow)
cursor = cnx.cursor() 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 # Get workflow custom field
query = (" SELECT id, field_type_id, field_key, field_name, order_id, default_value, description, " query = (" SELECT id, field_type_id, field_key, field_name, order_id, default_value, description, "
" field_template, boolean_field_display " " field_template, boolean_field_display "
@ -154,6 +174,10 @@ class TicketApplicationCollection:
rows = cursor.fetchall() rows = cursor.fetchall()
field_result = {} field_result = {}
field_types = {
5: 'input',
55: 'text_area',
}
if rows is not None and len(rows) > 0: if rows is not None and len(rows) > 0:
for row in rows: for row in rows:
field_key = row[2] field_key = row[2]
@ -167,6 +191,7 @@ class TicketApplicationCollection:
'description': row[6], 'description': row[6],
'field_template': row[7], 'field_template': row[7],
'boolean_field_display': row[8], 'boolean_field_display': row[8],
'field_type': field_types.get(row[1]),
} }
# Get workflow start state # Get workflow start state
@ -181,11 +206,7 @@ class TicketApplicationCollection:
workflow_state_name = None workflow_state_name = None
state_field_str = None state_field_str = None
fields = [] fields = []
result = {
"id": None,
"name": None,
"fields": []
}
if row is not None and len(row) > 0: if row is not None and len(row) > 0:
workflow_state_id = row[0] workflow_state_id = row[0]
workflow_state_name = row[1] workflow_state_name = row[1]
@ -202,6 +223,7 @@ class TicketApplicationCollection:
"default_value": None, "default_value": None,
"description": "", "description": "",
"field_template": "", "field_template": "",
'field_type': field_types.get(5),
} }
) )
if key in field_result.keys(): if key in field_result.keys():
@ -218,7 +240,6 @@ class TicketApplicationCollection:
@staticmethod @staticmethod
def on_post(req, resp, id_): def on_post(req, resp, id_):
"""Handles POST requests""" """Handles POST requests"""
access_control(req)
try: try:
raw_json = req.stream.read().decode('utf-8') raw_json = req.stream.read().decode('utf-8')
except Exception as ex: except Exception as ex:
@ -233,14 +254,6 @@ class TicketApplicationCollection:
new_values = json.loads(raw_json) new_values = json.loads(raw_json)
checked_fields = [ checked_fields = [
{
"name": "title",
"type": str
},
{
"name": "long_field",
"type": str
},
{ {
"name": "transition_id", "name": "transition_id",
"type": int "type": int
@ -255,22 +268,24 @@ class TicketApplicationCollection:
for field in checked_fields: for field in checked_fields:
_name = field['name'] _name = field['name']
_type = field['type'] _type = field['type']
if _name not in new_values['data'].keys() or \ if _name not in new_values.keys() or \
not isinstance(new_values['data'][_name], _type) or \ not isinstance(new_values[_name], _type) or \
len(str.strip(new_values['data'][_name])) == 0: len(str(new_values[_name])) == 0:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_FIELD' + _name) description='API.INVALID_FIELD' + _name)
payload = new_values['data'] payload = new_values
session = login() session = login()
url = 'http://192.168.1.4/api/v1.0/tickets' url = 'http://172.16.203.116/api/v1.0/tickets'
headers = { headers = {
'Content-Type': 'application/json', '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) 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: if resp.status_code != 200:
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_COOKIE', raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_COOKIE',
description='API.INVALID_STATUS_CODE:' + str(resp.status_code)) description='API.INVALID_STATUS_CODE:' + str(resp.status_code))