Code Duplication    Length = 71-75 lines in 3 locations

myems-api/core/emailmessage.py 1 location

@@ 9-83 (lines=75) @@
6
from core.useractivity import user_logger, access_control
7
8
9
class EmailMessageCollection:
10
    @staticmethod
11
    def __init__():
12
        """"Initializes EmailMessageCollection"""
13
        pass
14
15
    @staticmethod
16
    def on_options(req, resp, startdate, enddate):
17
        resp.status = falcon.HTTP_200
18
19
    @staticmethod
20
    def on_get(req, resp, startdate, enddate):
21
        access_control(req)
22
        try:
23
            start_datetime_local = datetime.strptime(startdate, '%Y-%m-%d')
24
        except Exception:
25
            raise falcon.HTTPError(falcon.HTTP_400,
26
                                   title='API.BAD_REQUEST',
27
                                   description='API.INVALID_START_DATE_FORMAT')
28
        try:
29
            end_datetime_local = datetime.strptime(enddate, '%Y-%m-%d')
30
        except Exception:
31
            raise falcon.HTTPError(falcon.HTTP_400,
32
                                   title='API.BAD_REQUEST',
33
                                   description='API.INVALID_END_DATE_FORMAT')
34
35
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
36
        if config.utc_offset[0] == '-':
37
            timezone_offset = -timezone_offset
38
39
        start_datetime_utc = start_datetime_local.replace(tzinfo=timezone.utc)
40
        start_datetime_utc -= timedelta(minutes=timezone_offset)
41
42
        end_datetime_utc = end_datetime_local.replace(tzinfo=timezone.utc)
43
        end_datetime_utc -= timedelta(minutes=timezone_offset)
44
        end_datetime_utc += timedelta(days=1)
45
46
        if start_datetime_utc >= end_datetime_utc:
47
            raise falcon.HTTPError(falcon.HTTP_400,
48
                                   title='API.BAD_REQUEST',
49
                                   description='API.START_DATETIME_MUST_BE_EARLIER_THAN_END_DATETIME')
50
51
        cnx = mysql.connector.connect(**config.myems_fdd_db)
52
        cursor = cnx.cursor()
53
54
        query = (" SELECT id, recipient_name, recipient_email, "
55
                 "        subject, message, attachment_file_name, "
56
                 "        created_datetime_utc, scheduled_datetime_utc, status "
57
                 " FROM tbl_email_messages "
58
                 " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s "
59
                 " ORDER BY created_datetime_utc ")
60
        cursor.execute(query, (start_datetime_utc, end_datetime_utc))
61
        rows = cursor.fetchall()
62
63
        if cursor:
64
            cursor.close()
65
        if cnx:
66
            cnx.disconnect()
67
68
        result = list()
69
        if rows is not None and len(rows) > 0:
70
            for row in rows:
71
                meta_result = {"id": row[0],
72
                               "recipient_name": row[1],
73
                               "recipient_email": row[2],
74
                               "subject": row[3],
75
                               "message": row[4].replace("<br>", ""),
76
                               "attachment_file_name": row[5],
77
                               "created_datetime": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None,
78
                               "scheduled_datetime":
79
                                   row[7].timestamp() * 1000 if isinstance(row[7], datetime) else None,
80
                               "status": row[8]}
81
                result.append(meta_result)
82
83
        resp.text = json.dumps(result)
84
85
86
class EmailMessageItem:

myems-api/core/wechatmessage.py 1 location

@@ 9-81 (lines=73) @@
6
from core.useractivity import user_logger, access_control
7
8
9
class WechatMessageCollection(object):
10
    @staticmethod
11
    def __init__():
12
        """"Initializes WechatMessageCollection"""
13
        pass
14
15
    @staticmethod
16
    def on_options(req, resp, startdate, enddate):
17
        resp.status = falcon.HTTP_200
18
19
    @staticmethod
20
    def on_get(req, resp, startdate, enddate):
21
        access_control(req)
22
        try:
23
            start_datetime_local = datetime.strptime(startdate, '%Y-%m-%d')
24
        except Exception:
25
            raise falcon.HTTPError(falcon.HTTP_400,
26
                                   title='API.BAD_REQUEST',
27
                                   description='API.INVALID_START_DATE_FORMAT')
28
        try:
29
            end_datetime_local = datetime.strptime(enddate, '%Y-%m-%d')
30
        except Exception:
31
            raise falcon.HTTPError(falcon.HTTP_400,
32
                                   title='API.BAD_REQUEST',
33
                                   description='API.INVALID_END_DATE_FORMAT')
34
35
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
36
        if config.utc_offset[0] == '-':
37
            timezone_offset = -timezone_offset
38
39
        start_datetime_utc = start_datetime_local.replace(tzinfo=timezone.utc)
40
        start_datetime_utc -= timedelta(minutes=timezone_offset)
41
42
        end_datetime_utc = end_datetime_local.replace(tzinfo=timezone.utc)
43
        end_datetime_utc -= timedelta(minutes=timezone_offset)
44
        end_datetime_utc += timedelta(days=1)
45
46
        if start_datetime_utc >= end_datetime_utc:
47
            raise falcon.HTTPError(falcon.HTTP_400,
48
                                   title='API.BAD_REQUEST',
49
                                   description='API.START_DATETIME_MUST_BE_EARLIER_THAN_END_DATETIME')
50
        cnx = mysql.connector.connect(**config.myems_fdd_db)
51
        cursor = cnx.cursor()
52
53
        query = (" SELECT id, recipient_name, recipient_openid, message_template_id, "
54
                 "        message_data, created_datetime_utc, scheduled_datetime_utc, "
55
                 "        acknowledge_code, status "
56
                 " FROM tbl_wechat_messages_outbox "
57
                 " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s "
58
                 " ORDER BY created_datetime_utc DESC ")
59
        cursor.execute(query, (start_datetime_utc, end_datetime_utc))
60
        rows = cursor.fetchall()
61
62
        if cursor:
63
            cursor.close()
64
        if cnx:
65
            cnx.disconnect()
66
67
        result = list()
68
        if rows is not None and len(rows) > 0:
69
            for row in rows:
70
                meta_result = {"id": row[0],
71
                               "recipient_name": row[1],
72
                               "recipient_openid": row[2],
73
                               "message_template_id": row[3],
74
                               "message_data": row[4],
75
                               "created_datetime_utc": row[5].timestamp() * 1000 if isinstance(row[5], datetime) else None,
76
                               "scheduled_datetime_utc": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None,
77
                               "acknowledge_code": row[7],
78
                               "status": row[8]}
79
                result.append(meta_result)
80
81
        resp.text = json.dumps(result)
82
83
84
class WechatMessageItem:

myems-api/core/textmessage.py 1 location

@@ 9-79 (lines=71) @@
6
from core.useractivity import user_logger, access_control
7
8
9
class TextMessageCollection:
10
    @staticmethod
11
    def __init__():
12
        """"Initializes TextMessageCollection"""
13
        pass
14
15
    @staticmethod
16
    def on_options(req, resp, startdate, enddate):
17
        resp.status = falcon.HTTP_200
18
19
    @staticmethod
20
    def on_get(req, resp, startdate, enddate):
21
        access_control(req)
22
        try:
23
            start_datetime_local = datetime.strptime(startdate, '%Y-%m-%d')
24
        except Exception:
25
            raise falcon.HTTPError(falcon.HTTP_400,
26
                                   title='API.BAD_REQUEST',
27
                                   description='API.INVALID_START_DATE_FORMAT')
28
        try:
29
            end_datetime_local = datetime.strptime(enddate, '%Y-%m-%d')
30
        except Exception:
31
            raise falcon.HTTPError(falcon.HTTP_400,
32
                                   title='API.BAD_REQUEST',
33
                                   description='API.INVALID_END_DATE_FORMAT')
34
35
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
36
        if config.utc_offset[0] == '-':
37
            timezone_offset = -timezone_offset
38
39
        start_datetime_utc = start_datetime_local.replace(tzinfo=timezone.utc)
40
        start_datetime_utc -= timedelta(minutes=timezone_offset)
41
42
        end_datetime_utc = end_datetime_local.replace(tzinfo=timezone.utc)
43
        end_datetime_utc -= timedelta(minutes=timezone_offset)
44
        end_datetime_utc += timedelta(days=1)
45
46
        if start_datetime_utc >= end_datetime_utc:
47
            raise falcon.HTTPError(falcon.HTTP_400,
48
                                   title='API.BAD_REQUEST',
49
                                   description='API.START_DATETIME_MUST_BE_EARLIER_THAN_END_DATETIME')
50
        cnx = mysql.connector.connect(**config.myems_fdd_db)
51
        cursor = cnx.cursor()
52
53
        query = (" SELECT id, recipient_name, recipient_mobile, "
54
                 "        message, created_datetime_utc, scheduled_datetime_utc, acknowledge_code, status "
55
                 " FROM tbl_text_messages_outbox "
56
                 " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s "
57
                 " ORDER BY created_datetime_utc DESC ")
58
        cursor.execute(query, (start_datetime_utc, end_datetime_utc))
59
        rows = cursor.fetchall()
60
61
        if cursor:
62
            cursor.close()
63
        if cnx:
64
            cnx.disconnect()
65
66
        result = list()
67
        if rows is not None and len(rows) > 0:
68
            for row in rows:
69
                meta_result = {"id": row[0],
70
                               "recipient_name": row[1],
71
                               "recipient_mobile": row[2],
72
                               "message": row[3],
73
                               "created_datetime": row[4].timestamp() * 1000 if isinstance(row[4], datetime) else None,
74
                               "scheduled_datetime": row[5].timestamp() * 1000 if isinstance(row[5], datetime) else None,
75
                               "acknowledge_code": row[6],
76
                               "status": row[7]}
77
                result.append(meta_result)
78
79
        resp.text = json.dumps(result)
80
81
82
class TextMessageItem: