Added Put Update New EmailMessage API and text message and wechat message
parent
da14bd2376
commit
be7c51b2c1
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"info": {
|
"info": {
|
||||||
"_postman_id": "5020b7e9-5538-4b05-a649-f881fe5ae637",
|
"_postman_id": "a7452db1-c073-4155-81e6-f64434164125",
|
||||||
"name": "MyEMS",
|
"name": "MyEMS",
|
||||||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
|
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
|
||||||
},
|
},
|
||||||
|
@ -2250,8 +2250,30 @@
|
||||||
"description": "Login to get a valid token"
|
"description": "Login to get a valid token"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"body": {
|
||||||
|
"mode": "formdata",
|
||||||
|
"formdata": [
|
||||||
|
{
|
||||||
|
"key": "file",
|
||||||
|
"type": "file",
|
||||||
|
"src": "/home/zhongtianlin/offline_meter_data.xlsx"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "req",
|
||||||
|
"value": "{\"data\":{\"rule_id\":1, \"recipient_name\":\"myems\", \"recipient_email\":\"myems@163.com\", \"subject\":\"report_01_2021_12_24\", \"message\":\"report_01_2021_12_24\", \"status\":\"timeout\", \"created_datetime\":\"2021-12-24T00:00:00\", \"scheduled_datetime\":\"2021-12-24T00:00:00\"}}",
|
||||||
|
"type": "text"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
"url": {
|
"url": {
|
||||||
"raw": ""
|
"raw": "{{base_url}}/emailmessages/1000001",
|
||||||
|
"host": [
|
||||||
|
"{{base_url}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"emailmessages",
|
||||||
|
"1000001"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"response": []
|
"response": []
|
||||||
|
@ -8925,14 +8947,23 @@
|
||||||
"description": "Login to get a valid token"
|
"description": "Login to get a valid token"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"body": {
|
||||||
|
"mode": "raw",
|
||||||
|
"raw": "{\"data\":{\"rule_id\":1, \"recipient_name\":\"myems\", \"recipient_mobile\":\"13888888888\", \"message\":\"report_01_2021年12月24日\", \"acknowledge_code\":\"123456\", \"created_datetime\":\"2021-12-24T00:00:00\", \"scheduled_datetime\":\"2021-12-24T00:00:00\", \"status\":\"new\"}}",
|
||||||
|
"options": {
|
||||||
|
"raw": {
|
||||||
|
"language": "text"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"url": {
|
"url": {
|
||||||
"raw": "{{base_url}}/textmessages/1",
|
"raw": "{{base_url}}/textmessages/1000001",
|
||||||
"host": [
|
"host": [
|
||||||
"{{base_url}}"
|
"{{base_url}}"
|
||||||
],
|
],
|
||||||
"path": [
|
"path": [
|
||||||
"textmessages",
|
"textmessages",
|
||||||
"1"
|
"1000001"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -9884,14 +9915,18 @@
|
||||||
"description": "Login to get a valid token"
|
"description": "Login to get a valid token"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"body": {
|
||||||
|
"mode": "raw",
|
||||||
|
"raw": "{\"data\":{\"rule_id\":1, \"recipient_name\":\"myems\", \"recipient_openid\":\"oia2TjuEGTNoeX76QEjQNrcURxG8\", \"message_template_id\":\"Doclyl5uP7Aciu-qZ7mJNPtWkbkYnWBWVja26EGbNyk\", \"message_data\":\"{\\\"space_id\\\":1, \\\"high_limit\\\":1000.000}\", \"acknowledge_code\":\"9e52ad6d\", \"created_datetime\":\"2021-12-24T00:00:00\", \"scheduled_datetime\":\"2021-12-24T00:00:00\", \"status\":\"acknowledged\"}}"
|
||||||
|
},
|
||||||
"url": {
|
"url": {
|
||||||
"raw": "{{base_url}}/wechatmessages/1",
|
"raw": "{{base_url}}/wechatmessages/1000001",
|
||||||
"host": [
|
"host": [
|
||||||
"{{base_url}}"
|
"{{base_url}}"
|
||||||
],
|
],
|
||||||
"path": [
|
"path": [
|
||||||
"wechatmessages",
|
"wechatmessages",
|
||||||
"1"
|
"1000001"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -12813,11 +12848,5 @@
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
|
||||||
"variable": [
|
|
||||||
{
|
|
||||||
"value": "",
|
|
||||||
"disabled": true
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -289,6 +289,173 @@ class EmailMessageItem:
|
||||||
|
|
||||||
resp.text = json.dumps(result)
|
resp.text = json.dumps(result)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@user_logger
|
||||||
|
def on_put(req, resp, id_):
|
||||||
|
"""Handles PUT requests"""
|
||||||
|
access_control(req)
|
||||||
|
|
||||||
|
if not id_.isdigit() or int(id_) <= 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_EMAIL_MESSAGE_ID')
|
||||||
|
try:
|
||||||
|
upload = req.get_param('file')
|
||||||
|
# Read upload file as binary
|
||||||
|
attachment_file_object = upload.file.read()
|
||||||
|
# Retrieve filename
|
||||||
|
attachment_file_name = upload.filename
|
||||||
|
except Exception as ex:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
|
||||||
|
description='API.FAILED_TO_UPLOAD_ATTACHMENT_FILE')
|
||||||
|
|
||||||
|
try:
|
||||||
|
raw_json = req.get_param('req')
|
||||||
|
except Exception as ex:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
|
||||||
|
|
||||||
|
new_values = json.loads(raw_json)
|
||||||
|
|
||||||
|
if 'rule_id' in new_values['data'].keys():
|
||||||
|
if new_values['data']['rule_id'] <= 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_RULE_ID')
|
||||||
|
rule_id = new_values['data']['rule_id']
|
||||||
|
else:
|
||||||
|
rule_id = None
|
||||||
|
|
||||||
|
if 'recipient_name' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['recipient_name'], str) or \
|
||||||
|
len(str.strip(new_values['data']['recipient_name'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_RECIPIENT_NAME')
|
||||||
|
recipient_name = str.strip(new_values['data']['recipient_name'])
|
||||||
|
|
||||||
|
if 'recipient_email' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['recipient_email'], str) or \
|
||||||
|
len(str.strip(new_values['data']['recipient_email'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_RECIPIENT_EMAIL')
|
||||||
|
recipient_email = str.strip(new_values['data']['recipient_email'])
|
||||||
|
match = re.match(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', recipient_email)
|
||||||
|
if match is None:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_EMAIL')
|
||||||
|
|
||||||
|
if 'subject' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['subject'], str) or \
|
||||||
|
len(str.strip(new_values['data']['subject'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_SUBJECT_VALUE')
|
||||||
|
subject = str.strip(new_values['data']['subject'])
|
||||||
|
|
||||||
|
if 'message' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['message'], str) or \
|
||||||
|
len(str.strip(new_values['data']['message'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_MESSAGE_VALUE')
|
||||||
|
message = str.strip(new_values['data']['message'])
|
||||||
|
|
||||||
|
if 'status' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['status'], str) or \
|
||||||
|
len(str.strip(new_values['data']['status'])) == 0 or \
|
||||||
|
str.strip(new_values['data']['status']) not in ('new', 'acknowledged', 'timeout'):
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_STATUS')
|
||||||
|
status = str.strip(new_values['data']['status'])
|
||||||
|
|
||||||
|
if 'created_datetime' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['created_datetime'], str) or \
|
||||||
|
len(str.strip(new_values['data']['created_datetime'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_CREATED_DATETIME')
|
||||||
|
created_datetime_local = str.strip(new_values['data']['created_datetime'])
|
||||||
|
|
||||||
|
if 'scheduled_datetime' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['scheduled_datetime'], str) or \
|
||||||
|
len(str.strip(new_values['data']['scheduled_datetime'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_SCHEDULED_DATETIME')
|
||||||
|
scheduled_datetime_local = str.strip(new_values['data']['scheduled_datetime'])
|
||||||
|
|
||||||
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
|
||||||
|
if config.utc_offset[0] == '-':
|
||||||
|
timezone_offset = -timezone_offset
|
||||||
|
|
||||||
|
if created_datetime_local is None:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_CREATED_DATETIME")
|
||||||
|
else:
|
||||||
|
created_datetime_local = str.strip(created_datetime_local)
|
||||||
|
try:
|
||||||
|
created_datetime_utc = datetime.strptime(created_datetime_local,
|
||||||
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
|
||||||
|
timedelta(minutes=timezone_offset)
|
||||||
|
except ValueError:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_CREATED_DATETIME")
|
||||||
|
|
||||||
|
if scheduled_datetime_local is None:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_SCHEDULED_DATETIME")
|
||||||
|
else:
|
||||||
|
scheduled_datetime_local = str.strip(scheduled_datetime_local)
|
||||||
|
try:
|
||||||
|
scheduled_datetime_utc = datetime.strptime(scheduled_datetime_local,
|
||||||
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
|
||||||
|
timedelta(minutes=timezone_offset)
|
||||||
|
except ValueError:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_SCHEDULED_DATETIME")
|
||||||
|
|
||||||
|
cnx = mysql.connector.connect(**config.myems_fdd_db)
|
||||||
|
cursor = cnx.cursor()
|
||||||
|
|
||||||
|
cursor.execute(" SELECT recipient_name "
|
||||||
|
" FROM tbl_email_messages "
|
||||||
|
" WHERE id = %s ", (id_,))
|
||||||
|
|
||||||
|
if cursor.fetchone() is None:
|
||||||
|
cursor.close()
|
||||||
|
cnx.disconnect()
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
||||||
|
description='API.EMAIL_MESSAGE_NOT_FOUND')
|
||||||
|
|
||||||
|
if rule_id is not None:
|
||||||
|
cursor.execute(" SELECT name "
|
||||||
|
" FROM tbl_rules "
|
||||||
|
" WHERE id = %s ",
|
||||||
|
(new_values['data']['rule_id'],))
|
||||||
|
row = cursor.fetchone()
|
||||||
|
if row is None:
|
||||||
|
cursor.close()
|
||||||
|
cnx.disconnect()
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
||||||
|
description='API.RULE_NOT_FOUND')
|
||||||
|
|
||||||
|
update_row = (" UPDATE tbl_email_messages "
|
||||||
|
" SET rule_id = %s, recipient_name = %s, recipient_email = %s, subject = %s, message = %s,"
|
||||||
|
" attachment_file_name = %s, attachment_file_object = %s, created_datetime_utc = %s,"
|
||||||
|
" scheduled_datetime_utc = %s, status = %s"
|
||||||
|
" WHERE id = %s ")
|
||||||
|
|
||||||
|
cursor.execute(update_row, (rule_id,
|
||||||
|
recipient_name,
|
||||||
|
recipient_email,
|
||||||
|
subject,
|
||||||
|
message,
|
||||||
|
attachment_file_name,
|
||||||
|
attachment_file_object,
|
||||||
|
created_datetime_utc,
|
||||||
|
scheduled_datetime_utc,
|
||||||
|
status,
|
||||||
|
id_))
|
||||||
|
|
||||||
|
cnx.commit()
|
||||||
|
cursor.close()
|
||||||
|
cnx.disconnect()
|
||||||
|
|
||||||
|
resp.status = falcon.HTTP_200
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@user_logger
|
@user_logger
|
||||||
def on_delete(req, resp, id_):
|
def on_delete(req, resp, id_):
|
||||||
|
|
|
@ -268,6 +268,158 @@ class TextMessageItem:
|
||||||
|
|
||||||
resp.text = json.dumps(result)
|
resp.text = json.dumps(result)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@user_logger
|
||||||
|
def on_put(req, resp, id_):
|
||||||
|
"""Handles PUT requests"""
|
||||||
|
access_control(req)
|
||||||
|
|
||||||
|
if not id_.isdigit() or int(id_) <= 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_TEXT_MESSAGE_ID')
|
||||||
|
|
||||||
|
try:
|
||||||
|
raw_json = req.stream.read().decode('utf-8')
|
||||||
|
except Exception as ex:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
|
||||||
|
|
||||||
|
new_values = json.loads(raw_json)
|
||||||
|
|
||||||
|
if 'rule_id' in new_values['data'].keys():
|
||||||
|
if new_values['data']['rule_id'] <= 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_RULE_ID')
|
||||||
|
rule_id = new_values['data']['rule_id']
|
||||||
|
else:
|
||||||
|
rule_id = None
|
||||||
|
|
||||||
|
if 'recipient_name' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['recipient_name'], str) or \
|
||||||
|
len(str.strip(new_values['data']['recipient_name'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_RECIPIENT_NAME')
|
||||||
|
recipient_name = str.strip(new_values['data']['recipient_name'])
|
||||||
|
|
||||||
|
if 'recipient_mobile' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['recipient_mobile'], str) or \
|
||||||
|
len(str.strip(new_values['data']['recipient_mobile'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_RECIPIENT_MOBILE')
|
||||||
|
recipient_mobile = str.strip(new_values['data']['recipient_mobile'])
|
||||||
|
|
||||||
|
if 'message' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['message'], str) or \
|
||||||
|
len(str.strip(new_values['data']['message'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_MESSAGE_VALUE')
|
||||||
|
message = str.strip(new_values['data']['message'])
|
||||||
|
|
||||||
|
if 'acknowledge_code' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['acknowledge_code'], str) or \
|
||||||
|
len(str.strip(new_values['data']['acknowledge_code'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_ACKNOWLEDGE_CODE')
|
||||||
|
acknowledge_code = str.strip(new_values['data']['acknowledge_code'])
|
||||||
|
|
||||||
|
if 'status' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['status'], str) or \
|
||||||
|
len(str.strip(new_values['data']['status'])) == 0 or \
|
||||||
|
str.strip(new_values['data']['status']) not in ('new', 'acknowledged', 'timeout'):
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_STATUS')
|
||||||
|
status = str.strip(new_values['data']['status'])
|
||||||
|
|
||||||
|
if 'created_datetime' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['created_datetime'], str) or \
|
||||||
|
len(str.strip(new_values['data']['created_datetime'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_CREATED_DATETIME')
|
||||||
|
created_datetime_local = str.strip(new_values['data']['created_datetime'])
|
||||||
|
|
||||||
|
if 'scheduled_datetime' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['scheduled_datetime'], str) or \
|
||||||
|
len(str.strip(new_values['data']['scheduled_datetime'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_SCHEDULED_DATETIME')
|
||||||
|
scheduled_datetime_local = str.strip(new_values['data']['scheduled_datetime'])
|
||||||
|
|
||||||
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
|
||||||
|
if config.utc_offset[0] == '-':
|
||||||
|
timezone_offset = -timezone_offset
|
||||||
|
|
||||||
|
if created_datetime_local is None:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_CREATED_DATETIME")
|
||||||
|
else:
|
||||||
|
created_datetime_local = str.strip(created_datetime_local)
|
||||||
|
try:
|
||||||
|
created_datetime_utc = datetime.strptime(created_datetime_local,
|
||||||
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
|
||||||
|
timedelta(minutes=timezone_offset)
|
||||||
|
except ValueError:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_CREATED_DATETIME")
|
||||||
|
|
||||||
|
if scheduled_datetime_local is None:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_SCHEDULED_DATETIME")
|
||||||
|
else:
|
||||||
|
scheduled_datetime_local = str.strip(scheduled_datetime_local)
|
||||||
|
try:
|
||||||
|
scheduled_datetime_utc = datetime.strptime(scheduled_datetime_local,
|
||||||
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
|
||||||
|
timedelta(minutes=timezone_offset)
|
||||||
|
except ValueError:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_SCHEDULED_DATETIME")
|
||||||
|
|
||||||
|
cnx = mysql.connector.connect(**config.myems_fdd_db)
|
||||||
|
cursor = cnx.cursor()
|
||||||
|
|
||||||
|
cursor.execute(" SELECT recipient_name "
|
||||||
|
" FROM tbl_text_messages_outbox "
|
||||||
|
" WHERE id = %s ", (id_,))
|
||||||
|
|
||||||
|
if cursor.fetchone() is None:
|
||||||
|
cursor.close()
|
||||||
|
cnx.disconnect()
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
||||||
|
description='API.TEXT_MESSAGE_NOT_FOUND')
|
||||||
|
|
||||||
|
if rule_id is not None:
|
||||||
|
cursor.execute(" SELECT name "
|
||||||
|
" FROM tbl_rules "
|
||||||
|
" WHERE id = %s ",
|
||||||
|
(new_values['data']['rule_id'],))
|
||||||
|
row = cursor.fetchone()
|
||||||
|
if row is None:
|
||||||
|
cursor.close()
|
||||||
|
cnx.disconnect()
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
||||||
|
description='API.RULE_NOT_FOUND')
|
||||||
|
|
||||||
|
update_row = (" UPDATE tbl_text_messages_outbox "
|
||||||
|
" SET rule_id = %s, recipient_name = %s, recipient_mobile = %s, message = %s,"
|
||||||
|
" acknowledge_code = %s, created_datetime_utc = %s,"
|
||||||
|
" scheduled_datetime_utc = %s, status = %s"
|
||||||
|
" WHERE id = %s ")
|
||||||
|
|
||||||
|
cursor.execute(update_row, (rule_id,
|
||||||
|
recipient_name,
|
||||||
|
recipient_mobile,
|
||||||
|
message,
|
||||||
|
acknowledge_code,
|
||||||
|
created_datetime_utc,
|
||||||
|
scheduled_datetime_utc,
|
||||||
|
status,
|
||||||
|
id_))
|
||||||
|
|
||||||
|
cnx.commit()
|
||||||
|
cursor.close()
|
||||||
|
cnx.disconnect()
|
||||||
|
|
||||||
|
resp.status = falcon.HTTP_200
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@user_logger
|
@user_logger
|
||||||
def on_delete(req, resp, id_):
|
def on_delete(req, resp, id_):
|
||||||
|
|
|
@ -294,6 +294,180 @@ class WechatMessageItem:
|
||||||
|
|
||||||
resp.text = json.dumps(result)
|
resp.text = json.dumps(result)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@user_logger
|
||||||
|
def on_put(req, resp, id_):
|
||||||
|
"""Handles PUT requests"""
|
||||||
|
access_control(req)
|
||||||
|
|
||||||
|
if not id_.isdigit() or int(id_) <= 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_TEXT_MESSAGE_ID')
|
||||||
|
|
||||||
|
try:
|
||||||
|
raw_json = req.stream.read().decode('utf-8')
|
||||||
|
except Exception as ex:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
|
||||||
|
|
||||||
|
new_values = json.loads(raw_json)
|
||||||
|
|
||||||
|
if 'rule_id' in new_values['data'].keys():
|
||||||
|
if new_values['data']['rule_id'] <= 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_RULE_ID')
|
||||||
|
rule_id = new_values['data']['rule_id']
|
||||||
|
else:
|
||||||
|
rule_id = None
|
||||||
|
|
||||||
|
if 'recipient_name' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['recipient_name'], str) or \
|
||||||
|
len(str.strip(new_values['data']['recipient_name'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_RECIPIENT_NAME')
|
||||||
|
recipient_name = str.strip(new_values['data']['recipient_name'])
|
||||||
|
|
||||||
|
if 'recipient_openid' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['recipient_openid'], str) or \
|
||||||
|
len(str.strip(new_values['data']['recipient_openid'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_RECIPIENT_OPENID')
|
||||||
|
recipient_openid = str.strip(new_values['data']['recipient_openid'])
|
||||||
|
match = re.match(r'^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[\da-zA-Z-_]{28}$', recipient_openid)
|
||||||
|
if match is None:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_OPENID')
|
||||||
|
|
||||||
|
if 'message_template_id' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['message_template_id'], str) or \
|
||||||
|
len(str.strip(new_values['data']['message_template_id'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_MESSAGE_TEMPLATE_ID')
|
||||||
|
message_template_id = str.strip(new_values['data']['message_template_id'])
|
||||||
|
match = re.match(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[\w-]{43}$', message_template_id)
|
||||||
|
if match is None:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_MESSAGE_TEMPLATE_ID')
|
||||||
|
|
||||||
|
if 'message_data' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['message_data'], str) or \
|
||||||
|
len(str.strip(new_values['data']['message_data'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400,
|
||||||
|
title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_MESSAGE_DATA')
|
||||||
|
message_data = str.strip(new_values['data']['message_data'])
|
||||||
|
# validate expression in json
|
||||||
|
try:
|
||||||
|
json.loads(message_data)
|
||||||
|
except Exception as ex:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description=ex)
|
||||||
|
|
||||||
|
if 'acknowledge_code' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['acknowledge_code'], str) or \
|
||||||
|
len(str.strip(new_values['data']['acknowledge_code'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_ACKNOWLEDGE_CODE')
|
||||||
|
acknowledge_code = str.strip(new_values['data']['acknowledge_code'])
|
||||||
|
|
||||||
|
if 'status' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['status'], str) or \
|
||||||
|
len(str.strip(new_values['data']['status'])) == 0 or \
|
||||||
|
str.strip(new_values['data']['status']) not in ('new', 'acknowledged', 'timeout'):
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_STATUS')
|
||||||
|
status = str.strip(new_values['data']['status'])
|
||||||
|
|
||||||
|
if 'created_datetime' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['created_datetime'], str) or \
|
||||||
|
len(str.strip(new_values['data']['created_datetime'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_CREATED_DATETIME')
|
||||||
|
created_datetime_local = str.strip(new_values['data']['created_datetime'])
|
||||||
|
|
||||||
|
if 'scheduled_datetime' not in new_values['data'].keys() or \
|
||||||
|
not isinstance(new_values['data']['scheduled_datetime'], str) or \
|
||||||
|
len(str.strip(new_values['data']['scheduled_datetime'])) == 0:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description='API.INVALID_SCHEDULED_DATETIME')
|
||||||
|
scheduled_datetime_local = str.strip(new_values['data']['scheduled_datetime'])
|
||||||
|
|
||||||
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
|
||||||
|
if config.utc_offset[0] == '-':
|
||||||
|
timezone_offset = -timezone_offset
|
||||||
|
|
||||||
|
if created_datetime_local is None:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_CREATED_DATETIME")
|
||||||
|
else:
|
||||||
|
created_datetime_local = str.strip(created_datetime_local)
|
||||||
|
try:
|
||||||
|
created_datetime_utc = datetime.strptime(created_datetime_local,
|
||||||
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
|
||||||
|
timedelta(minutes=timezone_offset)
|
||||||
|
except ValueError:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_CREATED_DATETIME")
|
||||||
|
|
||||||
|
if scheduled_datetime_local is None:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_SCHEDULED_DATETIME")
|
||||||
|
else:
|
||||||
|
scheduled_datetime_local = str.strip(scheduled_datetime_local)
|
||||||
|
try:
|
||||||
|
scheduled_datetime_utc = datetime.strptime(scheduled_datetime_local,
|
||||||
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
|
||||||
|
timedelta(minutes=timezone_offset)
|
||||||
|
except ValueError:
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
||||||
|
description="API.INVALID_SCHEDULED_DATETIME")
|
||||||
|
|
||||||
|
cnx = mysql.connector.connect(**config.myems_fdd_db)
|
||||||
|
cursor = cnx.cursor()
|
||||||
|
|
||||||
|
cursor.execute(" SELECT recipient_name "
|
||||||
|
" FROM tbl_wechat_messages_outbox "
|
||||||
|
" WHERE id = %s ", (id_,))
|
||||||
|
|
||||||
|
if cursor.fetchone() is None:
|
||||||
|
cursor.close()
|
||||||
|
cnx.disconnect()
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
||||||
|
description='API.WECHAT_MESSAGE_NOT_FOUND')
|
||||||
|
|
||||||
|
if rule_id is not None:
|
||||||
|
cursor.execute(" SELECT name "
|
||||||
|
" FROM tbl_rules "
|
||||||
|
" WHERE id = %s ",
|
||||||
|
(new_values['data']['rule_id'],))
|
||||||
|
row = cursor.fetchone()
|
||||||
|
if row is None:
|
||||||
|
cursor.close()
|
||||||
|
cnx.disconnect()
|
||||||
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
||||||
|
description='API.RULE_NOT_FOUND')
|
||||||
|
|
||||||
|
update_row = (" UPDATE tbl_wechat_messages_outbox "
|
||||||
|
" SET rule_id = %s, recipient_name = %s, recipient_openid = %s, message_template_id = %s,"
|
||||||
|
" message_data = %s, acknowledge_code = %s, created_datetime_utc = %s,"
|
||||||
|
" scheduled_datetime_utc = %s, status = %s"
|
||||||
|
" WHERE id = %s ")
|
||||||
|
|
||||||
|
cursor.execute(update_row, (rule_id,
|
||||||
|
recipient_name,
|
||||||
|
recipient_openid,
|
||||||
|
message_template_id,
|
||||||
|
message_data,
|
||||||
|
acknowledge_code,
|
||||||
|
created_datetime_utc,
|
||||||
|
scheduled_datetime_utc,
|
||||||
|
status,
|
||||||
|
id_))
|
||||||
|
|
||||||
|
cnx.commit()
|
||||||
|
cursor.close()
|
||||||
|
cnx.disconnect()
|
||||||
|
|
||||||
|
resp.status = falcon.HTTP_200
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@user_logger
|
@user_logger
|
||||||
def on_delete(req, resp, id_):
|
def on_delete(req, resp, id_):
|
||||||
|
|
Loading…
Reference in New Issue