Code Duplication    Length = 73-75 lines in 3 locations

myems-api/core/emailmessage.py 1 location

@@ 20-94 (lines=75) @@
17
        _ = req
18
        resp.status = falcon.HTTP_200
19
20
    @staticmethod
21
    def on_get(req, resp):
22
        admin_control(req)
23
        print(req.params)
24
        start_datetime_local = req.params.get('startdatetime')
25
        end_datetime_local = req.params.get('enddatetime')
26
27
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
28
        if config.utc_offset[0] == '-':
29
            timezone_offset = -timezone_offset
30
31
        if start_datetime_local is None:
32
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
33
                                   description="API.INVALID_START_DATETIME_FORMAT")
34
        else:
35
            start_datetime_local = str.strip(start_datetime_local)
36
            try:
37
                start_datetime_utc = datetime.strptime(start_datetime_local,
38
                                                       '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
39
                    timedelta(minutes=timezone_offset)
40
            except ValueError:
41
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
42
                                       description="API.INVALID_START_DATETIME_FORMAT")
43
44
        if end_datetime_local is None:
45
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
46
                                   description="API.INVALID_END_DATETIME_FORMAT")
47
        else:
48
            end_datetime_local = str.strip(end_datetime_local)
49
            try:
50
                end_datetime_utc = datetime.strptime(end_datetime_local,
51
                                                     '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
52
                    timedelta(minutes=timezone_offset)
53
            except ValueError:
54
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
55
                                       description="API.INVALID_END_DATETIME_FORMAT")
56
57
        if start_datetime_utc >= end_datetime_utc:
58
            raise falcon.HTTPError(status=falcon.HTTP_400,
59
                                   title='API.BAD_REQUEST',
60
                                   description='API.START_DATETIME_MUST_BE_EARLIER_THAN_END_DATETIME')
61
62
        cnx = mysql.connector.connect(**config.myems_fdd_db)
63
        cursor = cnx.cursor()
64
65
        query = (" SELECT id, recipient_name, recipient_email, "
66
                 "        subject, message, attachment_file_name, "
67
                 "        created_datetime_utc, scheduled_datetime_utc, status "
68
                 " FROM tbl_email_messages "
69
                 " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s "
70
                 " ORDER BY created_datetime_utc ")
71
        cursor.execute(query, (start_datetime_utc, end_datetime_utc))
72
        rows = cursor.fetchall()
73
74
        if cursor:
75
            cursor.close()
76
        if cnx:
77
            cnx.close()
78
79
        result = list()
80
        if rows is not None and len(rows) > 0:
81
            for row in rows:
82
                meta_result = {"id": row[0],
83
                               "recipient_name": row[1],
84
                               "recipient_email": row[2],
85
                               "subject": row[3],
86
                               "message": row[4].replace("<br>", ""),
87
                               "attachment_file_name": row[5],
88
                               "created_datetime": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None,
89
                               "scheduled_datetime":
90
                                   row[7].timestamp() * 1000 if isinstance(row[7], datetime) else None,
91
                               "status": row[8]}
92
                result.append(meta_result)
93
94
        resp.text = json.dumps(result)
95
96
    @staticmethod
97
    @user_logger

myems-api/core/wechatmessage.py 1 location

@@ 20-94 (lines=75) @@
17
        _ = req
18
        resp.status = falcon.HTTP_200
19
20
    @staticmethod
21
    def on_get(req, resp):
22
        admin_control(req)
23
24
        start_datetime_local = req.params.get('startdatetime')
25
        end_datetime_local = req.params.get('enddatetime')
26
27
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
28
        if config.utc_offset[0] == '-':
29
            timezone_offset = -timezone_offset
30
31
        if start_datetime_local is None:
32
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
33
                                   description="API.INVALID_START_DATETIME_FORMAT")
34
        else:
35
            start_datetime_local = str.strip(start_datetime_local)
36
            try:
37
                start_datetime_utc = datetime.strptime(start_datetime_local,
38
                                                       '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
39
                                     timedelta(minutes=timezone_offset)
40
            except ValueError:
41
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
42
                                       description="API.INVALID_START_DATETIME_FORMAT")
43
44
        if end_datetime_local is None:
45
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
46
                                   description="API.INVALID_END_DATETIME_FORMAT")
47
        else:
48
            end_datetime_local = str.strip(end_datetime_local)
49
            try:
50
                end_datetime_utc = datetime.strptime(end_datetime_local,
51
                                                     '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
52
                                   timedelta(minutes=timezone_offset)
53
            except ValueError:
54
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
55
                                       description="API.INVALID_END_DATETIME_FORMAT")
56
57
        if start_datetime_utc >= end_datetime_utc:
58
            raise falcon.HTTPError(status=falcon.HTTP_400,
59
                                   title='API.BAD_REQUEST',
60
                                   description='API.START_DATETIME_MUST_BE_EARLIER_THAN_END_DATETIME')
61
        cnx = mysql.connector.connect(**config.myems_fdd_db)
62
        cursor = cnx.cursor()
63
64
        query = (" SELECT id, recipient_name, recipient_openid, message_template_id, "
65
                 "        message_data, created_datetime_utc, scheduled_datetime_utc, "
66
                 "        acknowledge_code, status "
67
                 " FROM tbl_wechat_messages_outbox "
68
                 " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s "
69
                 " ORDER BY created_datetime_utc DESC ")
70
        cursor.execute(query, (start_datetime_utc, end_datetime_utc))
71
        rows = cursor.fetchall()
72
73
        if cursor:
74
            cursor.close()
75
        if cnx:
76
            cnx.close()
77
78
        result = list()
79
        if rows is not None and len(rows) > 0:
80
            for row in rows:
81
                meta_result = {"id": row[0],
82
                               "recipient_name": row[1],
83
                               "recipient_openid": row[2],
84
                               "message_template_id": row[3],
85
                               "message_data": row[4],
86
                               "created_datetime_utc":
87
                                   row[5].timestamp() * 1000 if isinstance(row[5], datetime) else None,
88
                               "scheduled_datetime_utc":
89
                                   row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None,
90
                               "acknowledge_code": row[7],
91
                               "status": row[8]}
92
                result.append(meta_result)
93
94
        resp.text = json.dumps(result)
95
96
    @staticmethod
97
    @user_logger

myems-api/core/user.py 1 location

@@ 1151-1223 (lines=73) @@
1148
        _ = req
1149
        resp.status = falcon.HTTP_200
1150
1151
    @staticmethod
1152
    def on_get(req, resp):
1153
        admin_control(req)
1154
        start_datetime_local = req.params.get('startdatetime')
1155
        end_datetime_local = req.params.get('enddatetime')
1156
1157
        timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
1158
        if config.utc_offset[0] == '-':
1159
            timezone_offset = -timezone_offset
1160
1161
        if start_datetime_local is None:
1162
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1163
                                   description="API.INVALID_START_DATETIME_FORMAT")
1164
        else:
1165
            start_datetime_local = str.strip(start_datetime_local)
1166
            try:
1167
                start_datetime_utc = datetime.strptime(start_datetime_local,
1168
                                                       '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
1169
                                     timedelta(minutes=timezone_offset)
1170
            except ValueError:
1171
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1172
                                       description="API.INVALID_START_DATETIME_FORMAT")
1173
1174
        if end_datetime_local is None:
1175
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1176
                                   description="API.INVALID_END_DATETIME_FORMAT")
1177
        else:
1178
            end_datetime_local = str.strip(end_datetime_local)
1179
            try:
1180
                end_datetime_utc = datetime.strptime(end_datetime_local,
1181
                                                     '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \
1182
                                   timedelta(minutes=timezone_offset)
1183
            except ValueError:
1184
                raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1185
                                       description="API.INVALID_END_DATETIME_FORMAT")
1186
1187
        if start_datetime_utc >= end_datetime_utc:
1188
            raise falcon.HTTPError(status=falcon.HTTP_400,
1189
                                   title='API.BAD_REQUEST',
1190
                                   description='API.START_DATETIME_MUST_BE_EARLIER_THAN_END_DATETIME')
1191
1192
        cnx = mysql.connector.connect(**config.myems_user_db)
1193
        cursor = cnx.cursor()
1194
1195
        query = (" SELECT id, recipient_name, recipient_email, "
1196
                 "        subject, message, created_datetime_utc, "
1197
                 "        scheduled_datetime_utc, status "
1198
                 " FROM tbl_email_messages "
1199
                 " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s "
1200
                 " ORDER BY created_datetime_utc ")
1201
        cursor.execute(query, (start_datetime_utc, end_datetime_utc))
1202
        rows = cursor.fetchall()
1203
1204
        if cursor:
1205
            cursor.close()
1206
        if cnx:
1207
            cnx.close()
1208
1209
        result = list()
1210
        if rows is not None and len(rows) > 0:
1211
            for row in rows:
1212
                meta_result = {"id": row[0],
1213
                               "recipient_name": row[1],
1214
                               "recipient_email": row[2],
1215
                               "subject": row[3],
1216
                               "message": row[4].replace("<br>", ""),
1217
                               "created_datetime": row[5].timestamp() * 1000 if isinstance(row[5], datetime) else None,
1218
                               "scheduled_datetime":
1219
                                   row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None,
1220
                               "status": row[7]}
1221
                result.append(meta_result)
1222
1223
        resp.text = json.dumps(result)
1224
1225
    @staticmethod
1226
    def on_post(req, resp):