From 043c39b81a9f7fef6ce95ed43c7c24066572777e Mon Sep 17 00:00:00 2001 From: tianlinzhong <673359306@qq.com> Date: Tue, 21 Dec 2021 15:04:09 +0800 Subject: [PATCH] Added POST Create New EmailMessage API and text message and wechat message --- myems-api/core/emailmessage.py | 148 +++++++++++++++++++++++++++++++- myems-api/core/textmessage.py | 119 +++++++++++++++++++++++++ myems-api/core/wechatmessage.py | 132 ++++++++++++++++++++++++++++ 3 files changed, 396 insertions(+), 3 deletions(-) diff --git a/myems-api/core/emailmessage.py b/myems-api/core/emailmessage.py index 050dbfbb..392fa6e7 100644 --- a/myems-api/core/emailmessage.py +++ b/myems-api/core/emailmessage.py @@ -34,7 +34,7 @@ class EmailMessageCollection: 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) - \ + '%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', @@ -47,7 +47,7 @@ class EmailMessageCollection: 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) - \ + '%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', @@ -92,6 +92,149 @@ class EmailMessageCollection: resp.text = json.dumps(result) + @staticmethod + @user_logger + def on_post(req, resp): + """Handles POST 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.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']) + + 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 'attachment_file_name' not in new_values['data'].keys() or \ + not isinstance(new_values['data']['attachment_file_name'], str) or \ + len(str.strip(new_values['data']['attachment_file_name'])) == 0: + raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', + description='API.INVALID_ATTACHMENT_FILE_NAME') + attachment_file_name = str.strip(new_values['data']['attachment_file_name']) + + if 'attachment_file_object' not in new_values['data'].keys() or \ + not isinstance(new_values['data']['attachment_file_object'], str) or \ + len(str.strip(new_values['data']['attachment_file_object'])) == 0: + raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', + description='API.INVALID_ATTACHMENT_FILE_OBJECT') + attachment_file_object = str.strip(new_values['data']['attachment_file_object']) + + if 'attachment_file_object' not in new_values['data'].keys() or \ + not isinstance(new_values['data']['attachment_file_object'], str) or \ + len(str.strip(new_values['data']['attachment_file_object'])) == 0: + raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', + description='API.INVALID_ATTACHMENT_FILE_OBJECT') + attachment_file_object = str.strip(new_values['data']['attachment_file_object']) + + 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_FORMAT") + 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_FORMAT") + + if scheduled_datetime_local is None: + raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', + description="API.INVALID_SCHEDULED_DATETIME_FORMAT") + 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_FORMAT") + print(created_datetime_utc) + print(scheduled_datetime_utc) + status = 'new' + + cnx = mysql.connector.connect(**config.myems_fdd_db) + cursor = cnx.cursor() + add_row = (" INSERT INTO tbl_email_messages " + " (rule_id, recipient_name, recipient_email, subject, message, " + " attachment_file_name, attachment_file_object, created_datetime_utc," + " scheduled_datetime_utc, status) " + " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") + + cursor.execute(add_row, (rule_id, + recipient_name, + recipient_email, + subject, + message, + attachment_file_name, + attachment_file_object, + created_datetime_utc, + scheduled_datetime_utc, + status)) + new_id = cursor.lastrowid + cnx.commit() + cursor.close() + cnx.disconnect() + + resp.status = falcon.HTTP_201 + resp.location = '/emailmessages/' + str(new_id) + class EmailMessageItem: @staticmethod @@ -175,4 +318,3 @@ class EmailMessageItem: cnx.disconnect() resp.status = falcon.HTTP_204 - diff --git a/myems-api/core/textmessage.py b/myems-api/core/textmessage.py index c92742e2..53250142 100644 --- a/myems-api/core/textmessage.py +++ b/myems-api/core/textmessage.py @@ -88,6 +88,125 @@ class TextMessageCollection: resp.text = json.dumps(result) + @staticmethod + @user_logger + def on_post(req, resp): + """Handles POST 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.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_MESSAGE_ACKNOWLEDGE_CODE') + acknowledge_code = str.strip(new_values['data']['acknowledge_code']) + + 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_FORMAT") + 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_FORMAT") + + if scheduled_datetime_local is None: + raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', + description="API.INVALID_SCHEDULED_DATETIME_FORMAT") + 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_FORMAT") + + status = 'new' + + cnx = mysql.connector.connect(**config.myems_fdd_db) + cursor = cnx.cursor() + add_row = (" INSERT INTO tbl_text_messages_outbox" + " (rule_id, recipient_name, recipient_mobile, message, " + " acknowledge_code, created_datetime_utc," + " scheduled_datetime_utc, status) " + " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") + + cursor.execute(add_row, (rule_id, + recipient_name, + recipient_mobile, + message, + acknowledge_code, + created_datetime_utc, + scheduled_datetime_utc, + status)) + new_id = cursor.lastrowid + cnx.commit() + cursor.close() + cnx.disconnect() + + resp.status = falcon.HTTP_201 + resp.location = '/textmessages/' + str(new_id) + class TextMessageItem: @staticmethod diff --git a/myems-api/core/wechatmessage.py b/myems-api/core/wechatmessage.py index d2bb8ff6..66ca1811 100644 --- a/myems-api/core/wechatmessage.py +++ b/myems-api/core/wechatmessage.py @@ -90,6 +90,138 @@ class WechatMessageCollection(object): resp.text = json.dumps(result) + @staticmethod + @user_logger + def on_post(req, resp): + """Handles POST 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.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']) + + 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']) + + 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_MESSAGE_ACKNOWLEDGE_CODE') + acknowledge_code = str.strip(new_values['data']['acknowledge_code']) + + 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_FORMAT") + 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_FORMAT") + + if scheduled_datetime_local is None: + raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', + description="API.INVALID_SCHEDULED_DATETIME_FORMAT") + 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_FORMAT") + + status = 'new' + + cnx = mysql.connector.connect(**config.myems_fdd_db) + cursor = cnx.cursor() + add_row = (" INSERT INTO tbl_wechat_messages_outbox" + " (rule_id, recipient_name, recipient_openid, message_template_id, message_data," + " acknowledge_code, created_datetime_utc, scheduled_datetime_utc, status) " + " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ") + + cursor.execute(add_row, (rule_id, + recipient_name, + recipient_openid, + message_template_id, + message_data, + acknowledge_code, + created_datetime_utc, + scheduled_datetime_utc, + status)) + new_id = cursor.lastrowid + cnx.commit() + cursor.close() + cnx.disconnect() + + resp.status = falcon.HTTP_201 + resp.location = '/wechatmessages/' + str(new_id) + class WechatMessageItem: @staticmethod