176 lines
6.8 KiB
Python
176 lines
6.8 KiB
Python
import falcon
|
|
import simplejson as json
|
|
import mysql.connector
|
|
import config
|
|
from datetime import datetime, timedelta, timezone
|
|
from core.useractivity import user_logger, access_control
|
|
|
|
|
|
class WechatMessageCollection(object):
|
|
@staticmethod
|
|
def __init__():
|
|
""""Initializes WechatMessageCollection"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_get(req, resp):
|
|
access_control(req)
|
|
|
|
start_datetime_local = req.params.get('startdatetime')
|
|
end_datetime_local = req.params.get('enddatetime')
|
|
|
|
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 start_datetime_local is None:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description="API.INVALID_START_DATETIME_FORMAT")
|
|
else:
|
|
start_datetime_local = str.strip(start_datetime_local)
|
|
try:
|
|
start_datetime_utc = datetime.strptime(start_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_START_DATETIME_FORMAT")
|
|
|
|
if end_datetime_local is None:
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
|
|
description="API.INVALID_END_DATETIME_FORMAT")
|
|
else:
|
|
end_datetime_local = str.strip(end_datetime_local)
|
|
try:
|
|
end_datetime_utc = datetime.strptime(end_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_END_DATETIME_FORMAT")
|
|
|
|
if start_datetime_utc >= end_datetime_utc:
|
|
raise falcon.HTTPError(falcon.HTTP_400,
|
|
title='API.BAD_REQUEST',
|
|
description='API.START_DATETIME_MUST_BE_EARLIER_THAN_END_DATETIME')
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db)
|
|
cursor = cnx.cursor()
|
|
|
|
query = (" SELECT id, recipient_name, recipient_openid, message_template_id, "
|
|
" message_data, created_datetime_utc, scheduled_datetime_utc, "
|
|
" acknowledge_code, status "
|
|
" FROM tbl_wechat_messages_outbox "
|
|
" WHERE created_datetime_utc >= %s AND created_datetime_utc < %s "
|
|
" ORDER BY created_datetime_utc DESC ")
|
|
cursor.execute(query, (start_datetime_utc, end_datetime_utc))
|
|
rows = cursor.fetchall()
|
|
|
|
if cursor:
|
|
cursor.close()
|
|
if cnx:
|
|
cnx.disconnect()
|
|
|
|
result = list()
|
|
if rows is not None and len(rows) > 0:
|
|
for row in rows:
|
|
meta_result = {"id": row[0],
|
|
"recipient_name": row[1],
|
|
"recipient_openid": row[2],
|
|
"message_template_id": row[3],
|
|
"message_data": row[4],
|
|
"created_datetime_utc": row[5].timestamp() * 1000 if isinstance(row[5], datetime) else None,
|
|
"scheduled_datetime_utc": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None,
|
|
"acknowledge_code": row[7],
|
|
"status": row[8]}
|
|
result.append(meta_result)
|
|
|
|
resp.text = json.dumps(result)
|
|
|
|
|
|
class WechatMessageItem:
|
|
@staticmethod
|
|
def __init__():
|
|
""""Initializes WechatMessageItem"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def on_options(req, resp, id_):
|
|
resp.status = falcon.HTTP_200
|
|
|
|
@staticmethod
|
|
def on_get(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_WECHAT_MESSAGE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db)
|
|
cursor = cnx.cursor()
|
|
|
|
query = (" SELECT id, recipient_name, recipient_openid, message_template_id, "
|
|
" message_data, created_datetime_utc, scheduled_datetime_utc, "
|
|
" acknowledge_code, status "
|
|
" FROM tbl_wechat_messages_outbox "
|
|
" WHERE id = %s ")
|
|
cursor.execute(query, (id_,))
|
|
row = cursor.fetchone()
|
|
|
|
if cursor:
|
|
cursor.close()
|
|
if cnx:
|
|
cnx.disconnect()
|
|
|
|
if row is None:
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.WECHAT_MESSAGE_NOT_FOUND')
|
|
|
|
result = {"id": row[0],
|
|
"recipient_name": row[1],
|
|
"recipient_openid": row[2],
|
|
"recipient_template_id": row[3],
|
|
"message_data": row[4],
|
|
"created_datetime_utc": row[5].timestamp() * 1000 if isinstance(row[5], datetime) else None,
|
|
"scheduled_datetime_utc": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None,
|
|
"acknowledge_code": row[7],
|
|
"status": row[8]}
|
|
|
|
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_WECHAT_MESSAGE_ID')
|
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db)
|
|
cursor = cnx.cursor()
|
|
|
|
cursor.execute(" SELECT id "
|
|
" FROM tbl_wechat_messages_outbox "
|
|
" WHERE id = %s ", (id_,))
|
|
row = cursor.fetchone()
|
|
|
|
if row is None:
|
|
if cursor:
|
|
cursor.close()
|
|
if cnx:
|
|
cnx.disconnect()
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
|
|
description='API.WECHAT_MESSAGE_NOT_FOUND')
|
|
|
|
cursor.execute(" DELETE FROM tbl_wechat_messages_outbox WHERE id = %s ", (id_,))
|
|
cnx.commit()
|
|
|
|
if cursor:
|
|
cursor.close()
|
|
if cnx:
|
|
cnx.disconnect()
|
|
|
|
resp.status = falcon.HTTP_204
|