Code Duplication    Length = 96-97 lines in 2 locations

core/knowledgefile.py 1 location

@@ 76-172 (lines=97) @@
73
74
        resp.body = json.dumps(result)
75
76
    @staticmethod
77
    def on_post(req, resp):
78
        """Handles POST requests"""
79
80
        try:
81
            upload = req.get_param('file')
82
            # Read upload file as binary
83
            raw_blob = upload.file.read()
84
            # Retrieve filename
85
            filename = upload.filename
86
            file_uuid = str(uuid.uuid4())
87
88
            # Define file_path
89
            file_path = os.path.join(config.upload_path, file_uuid)
90
91
            # Write to a temporary file to prevent incomplete files from
92
            # being used.
93
            temp_file_path = file_path + '~'
94
95
            open(temp_file_path, 'wb').write(raw_blob)
96
97
            # Now that we know the file has been fully saved to disk
98
            # move it into place.
99
            os.rename(temp_file_path, file_path)
100
        except Exception as ex:
101
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
102
                                   description='API.FAILED_TO_UPLOAD_KNOWLEDGE_FILE')
103
104
        # Verify User Session
105
        token = req.headers.get('TOKEN')
106
        user_uuid = req.headers.get('USER-UUID')
107
        if token is None:
108
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
109
                                   description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN')
110
        if user_uuid is None:
111
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
112
                                   description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN')
113
114
        cnx = mysql.connector.connect(**config.myems_user_db)
115
        cursor = cnx.cursor()
116
117
        query = (" SELECT utc_expires "
118
                 " FROM tbl_sessions "
119
                 " WHERE user_uuid = %s AND token = %s")
120
        cursor.execute(query, (user_uuid, token,))
121
        row = cursor.fetchone()
122
123
        if row is None:
124
            if cursor:
125
                cursor.close()
126
            if cnx:
127
                cnx.disconnect()
128
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
129
                                   description='API.INVALID_SESSION_PLEASE_RE_LOGIN')
130
        else:
131
            utc_expires = row[0]
132
            if datetime.utcnow() > utc_expires:
133
                if cursor:
134
                    cursor.close()
135
                if cnx:
136
                    cnx.disconnect()
137
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
138
                                       description='API.USER_SESSION_TIMEOUT')
139
140
        cursor.execute(" SELECT id "
141
                       " FROM tbl_users "
142
                       " WHERE uuid = %s ",
143
                       (user_uuid,))
144
        row = cursor.fetchone()
145
        if row is None:
146
            if cursor:
147
                cursor.close()
148
            if cnx:
149
                cnx.disconnect()
150
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
151
                                   description='API.INVALID_USER_PLEASE_RE_LOGIN')
152
        else:
153
            user_id = row[0]
154
155
        cnx = mysql.connector.connect(**config.myems_system_db)
156
        cursor = cnx.cursor()
157
158
        add_values = (" INSERT INTO tbl_knowledge_files "
159
                      " (file_name, uuid, upload_datetime_utc, upload_user_uuid, file_object ) "
160
                      " VALUES (%s, %s, %s, %s, %s) ")
161
        cursor.execute(add_values, (filename,
162
                                    file_uuid,
163
                                    datetime.utcnow(),
164
                                    user_uuid,
165
                                    raw_blob))
166
        new_id = cursor.lastrowid
167
        cnx.commit()
168
        cursor.close()
169
        cnx.disconnect()
170
171
        resp.status = falcon.HTTP_201
172
        resp.location = '/knowledgefiles/' + str(new_id)
173
174
175
class KnowledgeFileItem:

core/offlinemeterfile.py 1 location

@@ 46-141 (lines=96) @@
43
44
        resp.body = json.dumps(result)
45
46
    @staticmethod
47
    def on_post(req, resp):
48
        """Handles POST requests"""
49
        try:
50
            upload = req.get_param('file')
51
            # Read upload file as binary
52
            raw_blob = upload.file.read()
53
            # Retrieve filename
54
            filename = upload.filename
55
            file_uuid = str(uuid.uuid4())
56
57
            # Define file_path
58
            file_path = os.path.join(config.upload_path, file_uuid)
59
60
            # Write to a temporary file to prevent incomplete files from
61
            # being used.
62
            temp_file_path = file_path + '~'
63
64
            open(temp_file_path, 'wb').write(raw_blob)
65
66
            # Now that we know the file has been fully saved to disk
67
            # move it into place.
68
            os.rename(temp_file_path, file_path)
69
        except Exception as ex:
70
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
71
                                   description='API.FAILED_TO_UPLOAD_OFFLINE_METER_FILE')
72
73
        # Verify User Session
74
        token = req.headers.get('TOKEN')
75
        user_uuid = req.headers.get('USER-UUID')
76
        if token is None:
77
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
78
                                   description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN')
79
        if user_uuid is None:
80
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
81
                                   description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN')
82
83
        cnx = mysql.connector.connect(**config.myems_user_db)
84
        cursor = cnx.cursor()
85
86
        query = (" SELECT utc_expires "
87
                 " FROM tbl_sessions "
88
                 " WHERE user_uuid = %s AND token = %s")
89
        cursor.execute(query, (user_uuid, token,))
90
        row = cursor.fetchone()
91
92
        if row is None:
93
            if cursor:
94
                cursor.close()
95
            if cnx:
96
                cnx.disconnect()
97
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
98
                                   description='API.INVALID_SESSION_PLEASE_RE_LOGIN')
99
        else:
100
            utc_expires = row[0]
101
            if datetime.utcnow() > utc_expires:
102
                if cursor:
103
                    cursor.close()
104
                if cnx:
105
                    cnx.disconnect()
106
                raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
107
                                       description='API.USER_SESSION_TIMEOUT')
108
109
        cursor.execute(" SELECT id "
110
                       " FROM tbl_users "
111
                       " WHERE uuid = %s ",
112
                       (user_uuid,))
113
        row = cursor.fetchone()
114
        if row is None:
115
            if cursor:
116
                cursor.close()
117
            if cnx:
118
                cnx.disconnect()
119
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
120
                                   description='API.INVALID_USER_PLEASE_RE_LOGIN')
121
        else:
122
            user_id = row[0]
123
124
        cnx = mysql.connector.connect(**config.myems_historical_db)
125
        cursor = cnx.cursor()
126
127
        add_values = (" INSERT INTO tbl_offline_meter_files "
128
                      " (file_name, uuid, upload_datetime_utc, status, file_object ) "
129
                      " VALUES (%s, %s, %s, %s, %s) ")
130
        cursor.execute(add_values, (filename,
131
                                    file_uuid,
132
                                    datetime.utcnow(),
133
                                    'new',
134
                                    raw_blob))
135
        new_id = cursor.lastrowid
136
        cnx.commit()
137
        cursor.close()
138
        cnx.disconnect()
139
140
        resp.status = falcon.HTTP_201
141
        resp.location = '/offlinemeterfiles/' + str(new_id)
142
143
144
class OfflineMeterFileItem: