Code Duplication    Length = 108-108 lines in 2 locations

emailmessage.py 1 location

@@ 89-196 (lines=108) @@
86
        resp.body = json.dumps(result)
87
88
89
class EmailMessageItem:
90
    @staticmethod
91
    def __init__():
92
        pass
93
94
    @staticmethod
95
    def on_options(req, resp, id_):
96
        resp.status = falcon.HTTP_200
97
98
    @staticmethod
99
    def on_get(req, resp, id_):
100
        if not id_.isdigit() or int(id_) <= 0:
101
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
102
                                   description='API.INVALID_EMAIL_MESSAGE_ID')
103
104
        try:
105
            cnx = mysql.connector.connect(**config.myems_fdd_db)
106
            cursor = cnx.cursor()
107
        except Exception as e:
108
            raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e))
109
110
        try:
111
            query = (" SELECT id, recipient_name, recipient_email, "
112
                     "        subject, message, attachment_file_name, "
113
                     "        created_datetime_utc, scheduled_datetime_utc, status "
114
                     " FROM tbl_email_messages "
115
                     " WHERE id = %s ")
116
            cursor.execute(query, (id_,))
117
            row = cursor.fetchone()
118
119
            if cursor:
120
                cursor.close()
121
            if cnx:
122
                cnx.disconnect()
123
        except Exception as e:
124
            if cursor:
125
                cursor.close()
126
            if cnx:
127
                cnx.disconnect()
128
            raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e))
129
130
        if row is None:
131
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
132
                                   description='API.EMAIL_MESSAGE_NOT_FOUND')
133
134
        result = {"id": row[0],
135
                  "recipient_name": row[1],
136
                  "recipient_email": row[2],
137
                  "subject": row[3],
138
                  "message": row[4].replace("<br>", ""),
139
                  "attachment_file_name": row[5],
140
                  "created_datetime": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None,
141
                  "scheduled_datetime": row[7].timestamp() * 1000 if isinstance(row[7], datetime) else None,
142
                  "status": row[8]}
143
144
        resp.body = json.dumps(result)
145
146
    @staticmethod
147
    def on_delete(req, resp, id_):
148
        if not id_.isdigit() or int(id_) <= 0:
149
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
150
                                   description='API.INVALID_EMAIL_MESSAGE_ID')
151
152
        cnx = None
153
        cursor = None
154
        try:
155
            cnx = mysql.connector.connect(**config.myems_fdd_db)
156
            cursor = cnx.cursor()
157
        except Exception as e:
158
            if cursor:
159
                cursor.close()
160
            if cnx:
161
                cnx.disconnect()
162
            raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e))
163
164
        try:
165
            cursor.execute(" SELECT id FROM tbl_email_messages WHERE id = %s ", (id_,))
166
            row = cursor.fetchone()
167
        except Exception as e:
168
            if cursor:
169
                cursor.close()
170
            if cnx:
171
                cnx.disconnect()
172
            raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e))
173
174
        if row is None:
175
            if cursor:
176
                cursor.close()
177
            if cnx:
178
                cnx.disconnect()
179
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
180
                                   description='API.EMAIL_MESSAGE_NOT_FOUND')
181
182
        try:
183
            cursor.execute(" DELETE FROM tbl_email_messages WHERE id = %s ", (id_,))
184
            cnx.commit()
185
            if cursor:
186
                cursor.close()
187
            if cnx:
188
                cnx.disconnect()
189
        except Exception as e:
190
            if cursor:
191
                cursor.close()
192
            if cnx:
193
                cnx.disconnect()
194
            raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e))
195
196
        resp.status = falcon.HTTP_204
197
198

textmessage.py 1 location

@@ 86-193 (lines=108) @@
83
        resp.body = json.dumps(result)
84
85
86
class TextMessageItem:
87
    @staticmethod
88
    def __init__():
89
        pass
90
91
    @staticmethod
92
    def on_options(req, resp, id_):
93
        resp.status = falcon.HTTP_200
94
95
    @staticmethod
96
    def on_get(req, resp, id_):
97
        if not id_.isdigit() or int(id_) <= 0:
98
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
99
                                   description='API.INVALID_TEXT_MESSAGE_ID')
100
101
        try:
102
            cnx = mysql.connector.connect(**config.myems_fdd_db)
103
            cursor = cnx.cursor()
104
        except Exception as e:
105
            raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e))
106
107
        try:
108
            query = (" SELECT id, recipient_name, recipient_mobile, "
109
                     "        message, created_datetime_utc, scheduled_datetime_utc, acknowledge_code, status "
110
                     " FROM tbl_text_messages_outbox "
111
                     " WHERE id = %s ")
112
            cursor.execute(query, (id_,))
113
            row = cursor.fetchone()
114
115
            if cursor:
116
                cursor.close()
117
            if cnx:
118
                cnx.disconnect()
119
120
        except Exception as e:
121
            if cursor:
122
                cursor.close()
123
            if cnx:
124
                cnx.disconnect()
125
            raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e))
126
127
        if row is None:
128
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
129
                                   description='API.TEXT_MESSAGE_NOT_FOUND')
130
131
        result = {"id": row[0],
132
                  "recipient_name": row[1],
133
                  "recipient_mobile": row[2],
134
                  "message": row[3],
135
                  "created_datetime": row[4].timestamp() * 1000 if isinstance(row[4], datetime) else None,
136
                  "scheduled_datetime": row[5].timestamp() * 1000 if isinstance(row[5], datetime) else None,
137
                  "acknowledge_code": row[6],
138
                  "status": row[7]}
139
140
        resp.body = json.dumps(result)
141
142
    @staticmethod
143
    def on_delete(req, resp, id_):
144
        if not id_.isdigit() or int(id_) <= 0:
145
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
146
                                   description='API.INVALID_TEXT_MESSAGE_ID')
147
148
        cnx = None
149
        cursor = None
150
        try:
151
            cnx = mysql.connector.connect(**config.myems_fdd_db)
152
            cursor = cnx.cursor()
153
        except Exception as e:
154
            if cursor:
155
                cursor.close()
156
            if cnx:
157
                cnx.disconnect()
158
            raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e))
159
160
        try:
161
            cursor.execute(" SELECT id FROM tbl_text_messages_outbox WHERE id = %s ", (id_,))
162
            row = cursor.fetchone()
163
        except Exception as e:
164
            if cursor:
165
                cursor.close()
166
            if cnx:
167
                cnx.disconnect()
168
            raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e))
169
170
        if row is None:
171
            if cursor:
172
                cursor.close()
173
            if cnx:
174
                cnx.disconnect()
175
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
176
                                   description='API.TEXT_MESSAGE_NOT_FOUND')
177
178
        try:
179
            cursor.execute(" DELETE FROM tbl_text_messages_outbox WHERE id = %s ", (id_,))
180
            cnx.commit()
181
        except Exception as e:
182
            if cursor:
183
                cursor.close()
184
            if cnx:
185
                cnx.disconnect()
186
            raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e))
187
188
        if cursor:
189
            cursor.close()
190
        if cnx:
191
            cnx.disconnect()
192
193
        resp.status = falcon.HTTP_204
194
195