865 lines
29 KiB
Python
865 lines
29 KiB
Python
import falcon
|
|
import simplejson as json
|
|
import mysql.connector
|
|
import config
|
|
import uuid
|
|
import re
|
|
from core.useractivity import user_logger, access_control
|
|
import requests
|
|
|
|
# BASE_API = 'http://172.16.203.116/'
|
|
BASE_API = 'http://192.168.1.4/'
|
|
|
|
|
|
def login():
|
|
url = BASE_API + "/api/v1.0/login"
|
|
|
|
payload = json.dumps({
|
|
"username": "admin",
|
|
"password": "123456",
|
|
"type": "account"
|
|
})
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
}
|
|
|
|
session = requests.session()
|
|
response = session.post(url, headers=headers, data=payload)
|
|
status = response.status_code
|
|
|
|
if status == 200:
|
|
return session
|
|
return None
|
|
|
|
|
|
class TicketTypeCollection:
|
|
@staticmethod
|
|
def __init__():
|
|
""""Initializes ContactCollection"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_get(req, resp):
|
|
cnx = mysql.connector.connect(**config.loonflow)
|
|
cursor = cnx.cursor()
|
|
|
|
query = (" SELECT id, name, description, display_form_str "
|
|
" FROM workflow_workflow "
|
|
" ORDER BY id ")
|
|
cursor.execute(query)
|
|
rows = cursor.fetchall()
|
|
cursor.close()
|
|
cnx.close()
|
|
|
|
result = list()
|
|
if rows is not None and len(rows) > 0:
|
|
for row in rows:
|
|
meta_result = {"id": row[0],
|
|
"name": row[1],
|
|
"description": row[2],
|
|
"display_form_str": row[3]}
|
|
result.append(meta_result)
|
|
|
|
resp.text = json.dumps(result)
|
|
|
|
|
|
class TicketApplicationListCollection:
|
|
@staticmethod
|
|
def __init__():
|
|
""""Initializes ContactCollection"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_post(req, resp):
|
|
|
|
try:
|
|
raw_json = req.stream.read().decode('utf-8')
|
|
except Exception as ex:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
|
|
"""
|
|
{
|
|
"user_name": "admin",
|
|
}"""
|
|
new_values = json.loads(raw_json)
|
|
|
|
checked_fields = [
|
|
{
|
|
"name": "user_name",
|
|
"type": str
|
|
},
|
|
]
|
|
|
|
for field in checked_fields:
|
|
_name = field['name']
|
|
_type = field['type']
|
|
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
|
|
user_name = payload['user_name']
|
|
|
|
cnx = mysql.connector.connect(**config.loonflow)
|
|
cursor = cnx.cursor()
|
|
|
|
query = (" SELECT id, name "
|
|
" FROM workflow_workflow "
|
|
" ORDER BY id ")
|
|
cursor.execute(query)
|
|
workflow_rows = cursor.fetchall()
|
|
|
|
workflow_result = {}
|
|
if workflow_rows is not None and len(workflow_rows) > 0:
|
|
for row in workflow_rows:
|
|
workflow_result[row[0]] = row[1]
|
|
|
|
query = (" SELECT id, name "
|
|
" FROM workflow_state "
|
|
" ORDER BY id ")
|
|
cursor.execute(query)
|
|
workflow_state_rows = cursor.fetchall()
|
|
|
|
workflow_state_result = {}
|
|
if workflow_state_rows is not None and len(workflow_state_rows) > 0:
|
|
for row in workflow_state_rows:
|
|
workflow_state_result[row[0]] = row[1]
|
|
|
|
query = (" SELECT id, title, workflow_id, sn, state_id, creator, gmt_created, gmt_modified "
|
|
" FROM ticket_ticketrecord "
|
|
" WHERE creator = %s "
|
|
" ORDER BY id DESC")
|
|
cursor.execute(query, (user_name,))
|
|
rows = cursor.fetchall()
|
|
cursor.close()
|
|
cnx.close()
|
|
|
|
result = list()
|
|
if rows is not None and len(rows) > 0:
|
|
for row in rows:
|
|
meta_result = {"id": row[0],
|
|
"title": row[1],
|
|
"ticket_type": workflow_result.get(row[2]),
|
|
"sn": row[3],
|
|
"state": workflow_state_result.get(row[4]),
|
|
"creator": row[5],
|
|
"gmt_created": str(row[6]),
|
|
"gmt_modified": str(row[7])
|
|
}
|
|
result.append(meta_result)
|
|
|
|
resp.text = json.dumps(result)
|
|
|
|
|
|
class TicketApplicationItem:
|
|
@staticmethod
|
|
def __init__():
|
|
""""Initializes ContactCollection"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_get(req, resp, id_):
|
|
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 "
|
|
" FROM workflow_customfield"
|
|
" WHERE workflow_id = %s "
|
|
" ORDER BY id ")
|
|
cursor.execute(query, (id_,))
|
|
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]
|
|
field_result[field_key] = {
|
|
'id': row[0],
|
|
'field_type_id': row[1],
|
|
'field_key': row[2],
|
|
'field_name': row[3],
|
|
'order_id': row[4],
|
|
'default_value': row[5],
|
|
'description': row[6],
|
|
'field_template': row[7],
|
|
'boolean_field_display': row[8],
|
|
'field_type': field_types.get(row[1]),
|
|
}
|
|
|
|
# Get workflow start state
|
|
query = (" SELECT id, name, state_field_str "
|
|
" FROM workflow_state "
|
|
" WHERE workflow_id = %s and type_id = 1"
|
|
" ORDER BY id ")
|
|
cursor.execute(query, (id_,))
|
|
row = cursor.fetchone()
|
|
|
|
workflow_state_id = None
|
|
workflow_state_name = None
|
|
state_field_str = None
|
|
fields = []
|
|
|
|
if row is not None and len(row) > 0:
|
|
workflow_state_id = row[0]
|
|
workflow_state_name = row[1]
|
|
state_field_str = json.loads(row[2])
|
|
for key in state_field_str.keys():
|
|
if key == "title":
|
|
fields.append(
|
|
{
|
|
"id": 0,
|
|
"field_type_id": 5,
|
|
"field_key": "title",
|
|
"field_name": "标题",
|
|
"order_id": 20,
|
|
"default_value": None,
|
|
"description": "",
|
|
"field_template": "",
|
|
'field_type': field_types.get(5),
|
|
}
|
|
)
|
|
if key in field_result.keys():
|
|
fields.append(field_result[key])
|
|
else:
|
|
continue
|
|
|
|
result['id'] = workflow_state_id
|
|
result['name'] = workflow_state_name
|
|
result['fields'] = fields
|
|
|
|
resp.text = json.dumps(result)
|
|
|
|
@staticmethod
|
|
def on_post(req, resp, id_):
|
|
"""Handles POST requests"""
|
|
try:
|
|
raw_json = req.stream.read().decode('utf-8')
|
|
except Exception as ex:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
|
|
"""
|
|
{
|
|
"title": "测试维修",
|
|
"long_field": "测试维修",
|
|
"transition_id": 29,
|
|
"workflow_id": 4
|
|
}"""
|
|
print("id_", id_)
|
|
new_values = json.loads(raw_json)
|
|
|
|
checked_fields = [
|
|
{
|
|
"name": "transition_id",
|
|
"type": int
|
|
},
|
|
{
|
|
"name": "workflow_id",
|
|
"type": int
|
|
},
|
|
|
|
]
|
|
|
|
for field in checked_fields:
|
|
_name = field['name']
|
|
_type = field['type']
|
|
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
|
|
|
|
session = login()
|
|
url = BASE_API + "/api/v1.0/tickets"
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
}
|
|
resp = session.post(url, headers=headers, data=json.dumps(payload))
|
|
content = json.loads(resp.text)
|
|
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))
|
|
resp.status = falcon.HTTP_201
|
|
resp.location = '/tickets/' + str(new_ticket_id)
|
|
|
|
|
|
class TicketAgentListCollection:
|
|
@staticmethod
|
|
def __init__():
|
|
""""Initializes ContactCollection"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_post(req, resp):
|
|
|
|
try:
|
|
raw_json = req.stream.read().decode('utf-8')
|
|
except Exception as ex:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
|
|
"""
|
|
{
|
|
"user_name": "admin",
|
|
}"""
|
|
new_values = json.loads(raw_json)
|
|
|
|
checked_fields = [
|
|
{
|
|
"name": "user_name",
|
|
"type": str
|
|
},
|
|
]
|
|
|
|
for field in checked_fields:
|
|
_name = field['name']
|
|
_type = field['type']
|
|
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
|
|
user_name = payload['user_name']
|
|
|
|
cnx = mysql.connector.connect(**config.loonflow)
|
|
cursor = cnx.cursor()
|
|
|
|
# Get Ticket ids
|
|
query = (" SELECT id, ticket_id "
|
|
" FROM ticket_ticketuser "
|
|
" WHERE in_process = 1 and worked = 0 "
|
|
" ORDER BY id DESC ")
|
|
cursor.execute(query)
|
|
rows = cursor.fetchall()
|
|
|
|
ticket_ids = []
|
|
if rows is not None and len(rows) > 0:
|
|
for row in rows:
|
|
ticket_id = row[1]
|
|
ticket_ids.append(ticket_id)
|
|
|
|
query = (" SELECT id, name "
|
|
" FROM workflow_workflow "
|
|
" ORDER BY id ")
|
|
cursor.execute(query)
|
|
workflow_rows = cursor.fetchall()
|
|
|
|
workflow_result = {}
|
|
if workflow_rows is not None and len(workflow_rows) > 0:
|
|
for row in workflow_rows:
|
|
workflow_result[row[0]] = row[1]
|
|
|
|
query = (" SELECT id, name "
|
|
" FROM workflow_state "
|
|
" ORDER BY id ")
|
|
cursor.execute(query)
|
|
workflow_state_rows = cursor.fetchall()
|
|
|
|
workflow_state_result = {}
|
|
if workflow_state_rows is not None and len(workflow_state_rows) > 0:
|
|
for row in workflow_state_rows:
|
|
workflow_state_result[row[0]] = row[1]
|
|
|
|
query = (" SELECT id, title, workflow_id, sn, state_id, creator, gmt_created, gmt_modified "
|
|
" FROM ticket_ticketrecord "
|
|
" WHERE participant = %s "
|
|
" ORDER BY id DESC ")
|
|
cursor.execute(query, (user_name,))
|
|
rows = cursor.fetchall()
|
|
cursor.close()
|
|
cnx.close()
|
|
|
|
result = list()
|
|
if rows is not None and len(rows) > 0:
|
|
for row in rows:
|
|
_id = row[0]
|
|
if _id not in ticket_ids:
|
|
continue
|
|
meta_result = {"id": row[0],
|
|
"title": row[1],
|
|
"ticket_type": workflow_result.get(row[2]),
|
|
"sn": row[3],
|
|
"state": workflow_state_result.get(row[4]),
|
|
"creator": row[5],
|
|
"gmt_created": str(row[6]),
|
|
"gmt_modified": str(row[7])
|
|
}
|
|
result.append(meta_result)
|
|
|
|
resp.text = json.dumps(result)
|
|
|
|
|
|
class TicketCompletedListCollection:
|
|
@staticmethod
|
|
def __init__():
|
|
""""Initializes ContactCollection"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_post(req, resp):
|
|
|
|
try:
|
|
raw_json = req.stream.read().decode('utf-8')
|
|
except Exception as ex:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
|
|
"""
|
|
{
|
|
"user_name": "admin",
|
|
}"""
|
|
new_values = json.loads(raw_json)
|
|
|
|
checked_fields = [
|
|
{
|
|
"name": "user_name",
|
|
"type": str
|
|
},
|
|
]
|
|
|
|
for field in checked_fields:
|
|
_name = field['name']
|
|
_type = field['type']
|
|
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
|
|
user_name = payload['user_name']
|
|
|
|
cnx = mysql.connector.connect(**config.loonflow)
|
|
cursor = cnx.cursor()
|
|
|
|
# Get Ticket ids: Completed
|
|
query = (" SELECT id, ticket_id "
|
|
" FROM ticket_ticketuser "
|
|
" WHERE username = %s"
|
|
" ORDER BY id DESC ")
|
|
cursor.execute(query, (user_name,))
|
|
rows = cursor.fetchall()
|
|
|
|
ticket_ids = []
|
|
if rows is not None and len(rows) > 0:
|
|
for row in rows:
|
|
ticket_id = row[1]
|
|
if ticket_id not in ticket_ids:
|
|
ticket_ids.append(ticket_id)
|
|
|
|
query = (" SELECT id, name "
|
|
" FROM workflow_workflow "
|
|
" ORDER BY id ")
|
|
cursor.execute(query)
|
|
workflow_rows = cursor.fetchall()
|
|
|
|
workflow_result = {}
|
|
if workflow_rows is not None and len(workflow_rows) > 0:
|
|
for row in workflow_rows:
|
|
workflow_result[row[0]] = row[1]
|
|
|
|
query = (" SELECT id, name "
|
|
" FROM workflow_state "
|
|
" ORDER BY id ")
|
|
cursor.execute(query)
|
|
workflow_state_rows = cursor.fetchall()
|
|
|
|
workflow_state_result = {}
|
|
if workflow_state_rows is not None and len(workflow_state_rows) > 0:
|
|
for row in workflow_state_rows:
|
|
workflow_state_result[row[0]] = row[1]
|
|
|
|
query = (" SELECT id, title, workflow_id, sn, state_id, creator, gmt_created, gmt_modified "
|
|
" FROM ticket_ticketrecord "
|
|
" ORDER BY id DESC ")
|
|
cursor.execute(query, )
|
|
rows = cursor.fetchall()
|
|
cursor.close()
|
|
cnx.close()
|
|
|
|
result = list()
|
|
if rows is not None and len(rows) > 0:
|
|
for row in rows:
|
|
_id = row[0]
|
|
if _id not in ticket_ids:
|
|
continue
|
|
meta_result = {"id": row[0],
|
|
"title": row[1],
|
|
"ticket_type": workflow_result.get(row[2]),
|
|
"sn": row[3],
|
|
"state": workflow_state_result.get(row[4]),
|
|
"creator": row[5],
|
|
"gmt_created": str(row[6]),
|
|
"gmt_modified": str(row[7])
|
|
}
|
|
result.append(meta_result)
|
|
|
|
resp.text = json.dumps(result)
|
|
|
|
|
|
class TicketInterventionListCollection:
|
|
@staticmethod
|
|
def __init__():
|
|
""""Initializes ContactCollection"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_post(req, resp):
|
|
|
|
try:
|
|
raw_json = req.stream.read().decode('utf-8')
|
|
except Exception as ex:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
|
|
"""
|
|
{
|
|
"user_name": "admin",
|
|
}"""
|
|
new_values = json.loads(raw_json)
|
|
|
|
checked_fields = [
|
|
{
|
|
"name": "user_name",
|
|
"type": str
|
|
},
|
|
]
|
|
|
|
for field in checked_fields:
|
|
_name = field['name']
|
|
_type = field['type']
|
|
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
|
|
user_name = payload['user_name']
|
|
|
|
if user_name != 'admin':
|
|
raise falcon.HTTPError(falcon.HTTP_401, title='API.UNAUTHENTICATED',
|
|
description='API.INVALID_FIELD' + user_name)
|
|
|
|
cnx = mysql.connector.connect(**config.loonflow)
|
|
cursor = cnx.cursor()
|
|
|
|
# Get Ticket ids: Completed
|
|
query = (" SELECT id, ticket_id "
|
|
" FROM ticket_ticketuser "
|
|
" WHERE username = %s "
|
|
" ORDER BY id DESC ")
|
|
cursor.execute(query, (user_name,))
|
|
rows = cursor.fetchall()
|
|
|
|
ticket_ids = []
|
|
if rows is not None and len(rows) > 0:
|
|
for row in rows:
|
|
ticket_id = row[1]
|
|
if ticket_id not in ticket_ids:
|
|
ticket_ids.append(ticket_id)
|
|
|
|
query = (" SELECT id, name "
|
|
" FROM workflow_workflow "
|
|
" ORDER BY id ")
|
|
cursor.execute(query)
|
|
workflow_rows = cursor.fetchall()
|
|
|
|
workflow_result = {}
|
|
if workflow_rows is not None and len(workflow_rows) > 0:
|
|
for row in workflow_rows:
|
|
workflow_result[row[0]] = row[1]
|
|
|
|
query = (" SELECT id, name "
|
|
" FROM workflow_state "
|
|
" ORDER BY id ")
|
|
cursor.execute(query)
|
|
workflow_state_rows = cursor.fetchall()
|
|
|
|
workflow_state_result = {}
|
|
if workflow_state_rows is not None and len(workflow_state_rows) > 0:
|
|
for row in workflow_state_rows:
|
|
workflow_state_result[row[0]] = row[1]
|
|
|
|
query = (" SELECT id, title, workflow_id, sn, state_id, creator, gmt_created, gmt_modified "
|
|
" FROM ticket_ticketrecord "
|
|
" ORDER BY id DESC ")
|
|
cursor.execute(query, )
|
|
rows = cursor.fetchall()
|
|
cursor.close()
|
|
cnx.close()
|
|
|
|
result = list()
|
|
if rows is not None and len(rows) > 0:
|
|
for row in rows:
|
|
_id = row[0]
|
|
if _id not in ticket_ids:
|
|
continue
|
|
meta_result = {"id": row[0],
|
|
"title": row[1],
|
|
"ticket_type": workflow_result.get(row[2]),
|
|
"sn": row[3],
|
|
"state": workflow_state_result.get(row[4]),
|
|
"creator": row[5],
|
|
"gmt_created": str(row[6]),
|
|
"gmt_modified": str(row[7])
|
|
}
|
|
result.append(meta_result)
|
|
|
|
resp.text = json.dumps(result)
|
|
|
|
|
|
class ContactItem:
|
|
@staticmethod
|
|
def __init__():
|
|
""""Initializes ContactItem"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_get(req, resp, id_):
|
|
if not id_.isdigit() or int(id_) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_CONTACT_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
query = (" SELECT id, name, uuid, email, phone, description "
|
|
" FROM tbl_contacts "
|
|
" WHERE id = %s ")
|
|
cursor.execute(query, (id_,))
|
|
row = cursor.fetchone()
|
|
cursor.close()
|
|
cnx.close()
|
|
|
|
if row is None:
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.CONTACT_NOT_FOUND')
|
|
|
|
result = {"id": row[0],
|
|
"name": row[1],
|
|
"uuid": row[2],
|
|
"email": row[3],
|
|
"phone": row[4],
|
|
"description": row[5]}
|
|
resp.text = json.dumps(result)
|
|
|
|
@staticmethod
|
|
@user_logger
|
|
def on_delete(req, resp, id_):
|
|
access_control(req)
|
|
if not id_.isdigit() or int(id_) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_CONTACT_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_contacts "
|
|
" WHERE id = %s ", (id_,))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.close()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.CONTACT_NOT_FOUND')
|
|
|
|
# check relation with shopfloors
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_shopfloors "
|
|
" WHERE contact_id = %s ", (id_,))
|
|
rows_shopfloors = cursor.fetchall()
|
|
if rows_shopfloors is not None and len(rows_shopfloors) > 0:
|
|
cursor.close()
|
|
cnx.close()
|
|
raise falcon.HTTPError(falcon.HTTP_400,
|
|
title='API.BAD_REQUEST',
|
|
description='API.THERE_IS_RELATION_WITH_SHOPFLOORS')
|
|
|
|
# check relation with spaces
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_spaces "
|
|
" WHERE contact_id = %s ", (id_,))
|
|
rows_spaces = cursor.fetchall()
|
|
if rows_spaces is not None and len(rows_spaces) > 0:
|
|
cursor.close()
|
|
cnx.close()
|
|
raise falcon.HTTPError(falcon.HTTP_400,
|
|
title='API.BAD_REQUEST',
|
|
description='API.THERE_IS_RELATION_WITH_SPACES')
|
|
|
|
# check relation with stores
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_stores "
|
|
" WHERE contact_id = %s ", (id_,))
|
|
rows_stores = cursor.fetchall()
|
|
if rows_stores is not None and len(rows_stores) > 0:
|
|
cursor.close()
|
|
cnx.close()
|
|
raise falcon.HTTPError(falcon.HTTP_400,
|
|
title='API.BAD_REQUEST',
|
|
description='API.THERE_IS_RELATION_WITH_STORES')
|
|
|
|
# check relation with tenants
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_tenants "
|
|
" WHERE contact_id = %s ", (id_,))
|
|
rows_tenants = cursor.fetchall()
|
|
if rows_tenants is not None and len(rows_tenants) > 0:
|
|
cursor.close()
|
|
cnx.close()
|
|
raise falcon.HTTPError(falcon.HTTP_400,
|
|
title='API.BAD_REQUEST',
|
|
description='API.THERE_IS_RELATION_WITH_TENANTS')
|
|
|
|
cursor.execute(" DELETE FROM tbl_contacts WHERE id = %s ", (id_,))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.close()
|
|
|
|
resp.status = falcon.HTTP_204
|
|
|
|
@staticmethod
|
|
@user_logger
|
|
def on_put(req, resp, id_):
|
|
"""Handles PUT requests"""
|
|
access_control(req)
|
|
try:
|
|
raw_json = req.stream.read().decode('utf-8')
|
|
except Exception as ex:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
|
|
|
|
if not id_.isdigit() or int(id_) <= 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_CONTACT_ID')
|
|
|
|
new_values = json.loads(raw_json)
|
|
|
|
if 'name' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['name'], str) or \
|
|
len(str.strip(new_values['data']['name'])) == 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_CONTACT_NAME')
|
|
name = str.strip(new_values['data']['name'])
|
|
|
|
if 'email' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['email'], str) or \
|
|
len(str.strip(new_values['data']['email'])) == 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_EMAIL')
|
|
email = str.lower(str.strip(new_values['data']['email']))
|
|
|
|
match = re.match(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', email)
|
|
if match is None:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_EMAIL')
|
|
|
|
if 'phone' not in new_values['data'].keys() or \
|
|
not isinstance(new_values['data']['phone'], str) or \
|
|
len(str.strip(new_values['data']['phone'])) == 0:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.INVALID_USER_PHONE')
|
|
phone = str.strip(new_values['data']['phone'])
|
|
|
|
if 'description' in new_values['data'].keys() and \
|
|
new_values['data']['description'] is not None and \
|
|
len(str(new_values['data']['description'])) > 0:
|
|
description = str.strip(new_values['data']['description'])
|
|
else:
|
|
description = None
|
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_contacts "
|
|
" WHERE id = %s ", (id_,))
|
|
if cursor.fetchone() is None:
|
|
cursor.close()
|
|
cnx.close()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.CONTACT_NOT_FOUND')
|
|
|
|
cursor.execute(" SELECT name "
|
|
" FROM tbl_contacts "
|
|
" WHERE name = %s AND id != %s ", (name, id_))
|
|
if cursor.fetchone() is not None:
|
|
cursor.close()
|
|
cnx.close()
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description='API.CONTACT_NAME_IS_ALREADY_IN_USE')
|
|
|
|
update_row = (" UPDATE tbl_contacts "
|
|
" SET name = %s, email = %s, "
|
|
" phone = %s, description = %s "
|
|
" WHERE id = %s ")
|
|
cursor.execute(update_row, (name,
|
|
email,
|
|
phone,
|
|
description,
|
|
id_,))
|
|
cnx.commit()
|
|
|
|
cursor.close()
|
|
cnx.close()
|
|
|
|
resp.status = falcon.HTTP_200
|