Code Duplication    Length = 78-82 lines in 3 locations

myems-api/core/wechatmessage.py 1 location

@@ 248-329 (lines=82) @@
245
        resp.location = '/wechatmessages/' + str(new_id)
246
247
248
class WechatMessageItem:
249
    @staticmethod
250
    def __init__():
251
        """"Initializes WechatMessageItem"""
252
        pass
253
254
    @staticmethod
255
    def on_options(req, resp, id_):
256
        resp.status = falcon.HTTP_200
257
258
    @staticmethod
259
    def on_get(req, resp, id_):
260
        access_control(req)
261
        if not id_.isdigit() or int(id_) <= 0:
262
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
263
                                   description='API.INVALID_WECHAT_MESSAGE_ID')
264
265
        cnx = mysql.connector.connect(**config.myems_fdd_db)
266
        cursor = cnx.cursor()
267
268
        query = (" SELECT id, recipient_name, recipient_openid, message_template_id, "
269
                 "        message_data, created_datetime_utc, scheduled_datetime_utc, "
270
                 "        acknowledge_code, status "
271
                 " FROM tbl_wechat_messages_outbox "
272
                 " WHERE id = %s ")
273
        cursor.execute(query, (id_,))
274
        row = cursor.fetchone()
275
276
        if cursor:
277
            cursor.close()
278
        if cnx:
279
            cnx.disconnect()
280
281
        if row is None:
282
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
283
                                   description='API.WECHAT_MESSAGE_NOT_FOUND')
284
285
        result = {"id": row[0],
286
                  "recipient_name": row[1],
287
                  "recipient_openid": row[2],
288
                  "recipient_template_id": row[3],
289
                  "message_data": row[4],
290
                  "created_datetime_utc": row[5].timestamp() * 1000 if isinstance(row[5], datetime) else None,
291
                  "scheduled_datetime_utc": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None,
292
                  "acknowledge_code": row[7],
293
                  "status": row[8]}
294
295
        resp.text = json.dumps(result)
296
297
    @staticmethod
298
    @user_logger
299
    def on_delete(req, resp, id_):
300
        access_control(req)
301
        if not id_.isdigit() or int(id_) <= 0:
302
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
303
                                   description='API.INVALID_WECHAT_MESSAGE_ID')
304
305
        cnx = mysql.connector.connect(**config.myems_fdd_db)
306
        cursor = cnx.cursor()
307
308
        cursor.execute(" SELECT id "
309
                       " FROM tbl_wechat_messages_outbox "
310
                       " WHERE id = %s ", (id_,))
311
        row = cursor.fetchone()
312
313
        if row is None:
314
            if cursor:
315
                cursor.close()
316
            if cnx:
317
                cnx.disconnect()
318
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
319
                                   description='API.WECHAT_MESSAGE_NOT_FOUND')
320
321
        cursor.execute(" DELETE FROM tbl_wechat_messages_outbox WHERE id = %s ", (id_,))
322
        cnx.commit()
323
324
        if cursor:
325
            cursor.close()
326
        if cnx:
327
            cnx.disconnect()
328
329
        resp.status = falcon.HTTP_204
330

myems-api/core/emailmessage.py 1 location

@@ 243-324 (lines=82) @@
240
        resp.location = '/emailmessages/' + str(new_id)
241
242
243
class EmailMessageItem:
244
    @staticmethod
245
    def __init__():
246
        """"Initializes EmailMessageItem"""
247
        pass
248
249
    @staticmethod
250
    def on_options(req, resp, id_):
251
        resp.status = falcon.HTTP_200
252
253
    @staticmethod
254
    def on_get(req, resp, id_):
255
        access_control(req)
256
        if not id_.isdigit() or int(id_) <= 0:
257
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
258
                                   description='API.INVALID_EMAIL_MESSAGE_ID')
259
260
        cnx = mysql.connector.connect(**config.myems_fdd_db)
261
        cursor = cnx.cursor()
262
263
        query = (" SELECT id, recipient_name, recipient_email, "
264
                 "        subject, message, attachment_file_name, "
265
                 "        created_datetime_utc, scheduled_datetime_utc, status "
266
                 " FROM tbl_email_messages "
267
                 " WHERE id = %s ")
268
        cursor.execute(query, (id_,))
269
        row = cursor.fetchone()
270
271
        if cursor:
272
            cursor.close()
273
        if cnx:
274
            cnx.disconnect()
275
276
        if row is None:
277
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
278
                                   description='API.EMAIL_MESSAGE_NOT_FOUND')
279
280
        result = {"id": row[0],
281
                  "recipient_name": row[1],
282
                  "recipient_email": row[2],
283
                  "subject": row[3],
284
                  "message": row[4].replace("<br>", ""),
285
                  "attachment_file_name": row[5],
286
                  "created_datetime": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None,
287
                  "scheduled_datetime": row[7].timestamp() * 1000 if isinstance(row[7], datetime) else None,
288
                  "status": row[8]}
289
290
        resp.text = json.dumps(result)
291
292
    @staticmethod
293
    @user_logger
294
    def on_delete(req, resp, id_):
295
        access_control(req)
296
        if not id_.isdigit() or int(id_) <= 0:
297
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
298
                                   description='API.INVALID_EMAIL_MESSAGE_ID')
299
300
        cnx = mysql.connector.connect(**config.myems_fdd_db)
301
        cursor = cnx.cursor()
302
303
        cursor.execute(" SELECT id "
304
                       " FROM tbl_email_messages "
305
                       " WHERE id = %s ", (id_,))
306
        row = cursor.fetchone()
307
308
        if row is None:
309
            if cursor:
310
                cursor.close()
311
            if cnx:
312
                cnx.disconnect()
313
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
314
                                   description='API.EMAIL_MESSAGE_NOT_FOUND')
315
316
        cursor.execute(" DELETE FROM tbl_email_messages WHERE id = %s ", (id_,))
317
        cnx.commit()
318
319
        if cursor:
320
            cursor.close()
321
        if cnx:
322
            cnx.disconnect()
323
324
        resp.status = falcon.HTTP_204
325

myems-api/core/textmessage.py 1 location

@@ 224-301 (lines=78) @@
221
        resp.location = '/textmessages/' + str(new_id)
222
223
224
class TextMessageItem:
225
    @staticmethod
226
    def __init__():
227
        """"Initializes TextMessageItem"""
228
        pass
229
230
    @staticmethod
231
    def on_options(req, resp, id_):
232
        resp.status = falcon.HTTP_200
233
234
    @staticmethod
235
    def on_get(req, resp, id_):
236
        access_control(req)
237
        if not id_.isdigit() or int(id_) <= 0:
238
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
239
                                   description='API.INVALID_TEXT_MESSAGE_ID')
240
241
        cnx = mysql.connector.connect(**config.myems_fdd_db)
242
        cursor = cnx.cursor()
243
244
        query = (" SELECT id, recipient_name, recipient_mobile, "
245
                 "        message, created_datetime_utc, scheduled_datetime_utc, acknowledge_code, status "
246
                 " FROM tbl_text_messages_outbox "
247
                 " WHERE id = %s ")
248
        cursor.execute(query, (id_,))
249
        row = cursor.fetchone()
250
251
        if cursor:
252
            cursor.close()
253
        if cnx:
254
            cnx.disconnect()
255
256
        if row is None:
257
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
258
                                   description='API.TEXT_MESSAGE_NOT_FOUND')
259
260
        result = {"id": row[0],
261
                  "recipient_name": row[1],
262
                  "recipient_mobile": row[2],
263
                  "message": row[3],
264
                  "created_datetime": row[4].timestamp() * 1000 if isinstance(row[4], datetime) else None,
265
                  "scheduled_datetime": row[5].timestamp() * 1000 if isinstance(row[5], datetime) else None,
266
                  "acknowledge_code": row[6],
267
                  "status": row[7]}
268
269
        resp.text = json.dumps(result)
270
271
    @staticmethod
272
    @user_logger
273
    def on_delete(req, resp, id_):
274
        access_control(req)
275
        if not id_.isdigit() or int(id_) <= 0:
276
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
277
                                   description='API.INVALID_TEXT_MESSAGE_ID')
278
279
        cnx = mysql.connector.connect(**config.myems_fdd_db)
280
        cursor = cnx.cursor()
281
282
        cursor.execute(" SELECT id FROM tbl_text_messages_outbox WHERE id = %s ", (id_,))
283
        row = cursor.fetchone()
284
285
        if row is None:
286
            if cursor:
287
                cursor.close()
288
            if cnx:
289
                cnx.disconnect()
290
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
291
                                   description='API.TEXT_MESSAGE_NOT_FOUND')
292
293
        cursor.execute(" DELETE FROM tbl_text_messages_outbox WHERE id = %s ", (id_,))
294
        cnx.commit()
295
296
        if cursor:
297
            cursor.close()
298
        if cnx:
299
            cnx.disconnect()
300
301
        resp.status = falcon.HTTP_204
302