| @@ 131-247 (lines=117) @@ | ||
| 128 | cnx.close() |
|
| 129 | resp.text = json.dumps(result) |
|
| 130 | ||
| 131 | @staticmethod |
|
| 132 | @user_logger |
|
| 133 | def on_post(req, resp): |
|
| 134 | """Handles POST requests""" |
|
| 135 | admin_control(req) |
|
| 136 | try: |
|
| 137 | raw_json = req.stream.read().decode('utf-8') |
|
| 138 | except Exception as ex: |
|
| 139 | print(ex) |
|
| 140 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 141 | title='API.BAD_REQUEST', |
|
| 142 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 143 | ||
| 144 | new_values = json.loads(raw_json) |
|
| 145 | ||
| 146 | if 'name' not in new_values['data'].keys() or \ |
|
| 147 | not isinstance(new_values['data']['name'], str) or \ |
|
| 148 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 149 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 150 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
|
| 151 | name = str.strip(new_values['data']['name']) |
|
| 152 | ||
| 153 | if 'is_input_counted' not in new_values['data'].keys() or \ |
|
| 154 | not isinstance(new_values['data']['is_input_counted'], bool): |
|
| 155 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 156 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
|
| 157 | is_input_counted = new_values['data']['is_input_counted'] |
|
| 158 | ||
| 159 | if 'is_output_counted' not in new_values['data'].keys() or \ |
|
| 160 | not isinstance(new_values['data']['is_output_counted'], bool): |
|
| 161 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 162 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
|
| 163 | is_output_counted = new_values['data']['is_output_counted'] |
|
| 164 | ||
| 165 | if 'cost_center_id' not in new_values['data'].keys() or \ |
|
| 166 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
|
| 167 | new_values['data']['cost_center_id'] <= 0: |
|
| 168 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 169 | description='API.INVALID_COST_CENTER_ID') |
|
| 170 | cost_center_id = new_values['data']['cost_center_id'] |
|
| 171 | ||
| 172 | if 'svg_id' in new_values['data'].keys() and \ |
|
| 173 | isinstance(new_values['data']['svg_id'], int) and \ |
|
| 174 | new_values['data']['svg_id'] > 0: |
|
| 175 | svg_id = new_values['data']['svg_id'] |
|
| 176 | else: |
|
| 177 | svg_id = None |
|
| 178 | ||
| 179 | if 'camera_url' in new_values['data'].keys() and \ |
|
| 180 | new_values['data']['camera_url'] is not None and \ |
|
| 181 | len(str(new_values['data']['camera_url'])) > 0: |
|
| 182 | camera_url = str.strip(new_values['data']['camera_url']) |
|
| 183 | else: |
|
| 184 | camera_url = None |
|
| 185 | ||
| 186 | if 'description' in new_values['data'].keys() and \ |
|
| 187 | new_values['data']['description'] is not None and \ |
|
| 188 | len(str(new_values['data']['description'])) > 0: |
|
| 189 | description = str.strip(new_values['data']['description']) |
|
| 190 | else: |
|
| 191 | description = None |
|
| 192 | ||
| 193 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 194 | cursor = cnx.cursor() |
|
| 195 | ||
| 196 | cursor.execute(" SELECT name " |
|
| 197 | " FROM tbl_combined_equipments " |
|
| 198 | " WHERE name = %s ", (name,)) |
|
| 199 | if cursor.fetchone() is not None: |
|
| 200 | cursor.close() |
|
| 201 | cnx.close() |
|
| 202 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 203 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
|
| 204 | ||
| 205 | if cost_center_id is not None: |
|
| 206 | cursor.execute(" SELECT name " |
|
| 207 | " FROM tbl_cost_centers " |
|
| 208 | " WHERE id = %s ", |
|
| 209 | (new_values['data']['cost_center_id'],)) |
|
| 210 | row = cursor.fetchone() |
|
| 211 | if row is None: |
|
| 212 | cursor.close() |
|
| 213 | cnx.close() |
|
| 214 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 215 | description='API.COST_CENTER_NOT_FOUND') |
|
| 216 | ||
| 217 | if svg_id is not None: |
|
| 218 | cursor.execute(" SELECT name " |
|
| 219 | " FROM tbl_svgs " |
|
| 220 | " WHERE id = %s ", |
|
| 221 | (svg_id,)) |
|
| 222 | row = cursor.fetchone() |
|
| 223 | if row is None: |
|
| 224 | cursor.close() |
|
| 225 | cnx.close() |
|
| 226 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 227 | description='API.SVG_NOT_FOUND') |
|
| 228 | ||
| 229 | add_values = (" INSERT INTO tbl_combined_equipments " |
|
| 230 | " (name, uuid, is_input_counted, is_output_counted, " |
|
| 231 | " cost_center_id, svg_id, camera_url, description) " |
|
| 232 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
|
| 233 | cursor.execute(add_values, (name, |
|
| 234 | str(uuid.uuid4()), |
|
| 235 | is_input_counted, |
|
| 236 | is_output_counted, |
|
| 237 | cost_center_id, |
|
| 238 | svg_id, |
|
| 239 | camera_url, |
|
| 240 | description)) |
|
| 241 | new_id = cursor.lastrowid |
|
| 242 | cnx.commit() |
|
| 243 | cursor.close() |
|
| 244 | cnx.close() |
|
| 245 | ||
| 246 | resp.status = falcon.HTTP_201 |
|
| 247 | resp.location = '/combinedequipments/' + str(new_id) |
|
| 248 | ||
| 249 | ||
| 250 | class CombinedEquipmentItem: |
|
| @@ 99-215 (lines=117) @@ | ||
| 96 | cnx.close() |
|
| 97 | resp.text = json.dumps(result) |
|
| 98 | ||
| 99 | @staticmethod |
|
| 100 | @user_logger |
|
| 101 | def on_post(req, resp): |
|
| 102 | """Handles POST requests""" |
|
| 103 | admin_control(req) |
|
| 104 | try: |
|
| 105 | raw_json = req.stream.read().decode('utf-8') |
|
| 106 | except Exception as ex: |
|
| 107 | print(str(ex)) |
|
| 108 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 109 | title='API.BAD_REQUEST', |
|
| 110 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 111 | ||
| 112 | new_values = json.loads(raw_json) |
|
| 113 | ||
| 114 | if 'name' not in new_values['data'].keys() or \ |
|
| 115 | not isinstance(new_values['data']['name'], str) or \ |
|
| 116 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 117 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 118 | description='API.INVALID_EQUIPMENT_NAME') |
|
| 119 | name = str.strip(new_values['data']['name']) |
|
| 120 | ||
| 121 | if 'is_input_counted' not in new_values['data'].keys() or \ |
|
| 122 | not isinstance(new_values['data']['is_input_counted'], bool): |
|
| 123 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 124 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
|
| 125 | is_input_counted = new_values['data']['is_input_counted'] |
|
| 126 | ||
| 127 | if 'is_output_counted' not in new_values['data'].keys() or \ |
|
| 128 | not isinstance(new_values['data']['is_output_counted'], bool): |
|
| 129 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 130 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
|
| 131 | is_output_counted = new_values['data']['is_output_counted'] |
|
| 132 | ||
| 133 | if 'cost_center_id' not in new_values['data'].keys() or \ |
|
| 134 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
|
| 135 | new_values['data']['cost_center_id'] <= 0: |
|
| 136 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 137 | description='API.INVALID_COST_CENTER_ID') |
|
| 138 | cost_center_id = new_values['data']['cost_center_id'] |
|
| 139 | ||
| 140 | if 'svg_id' in new_values['data'].keys() and \ |
|
| 141 | isinstance(new_values['data']['svg_id'], int) and \ |
|
| 142 | new_values['data']['svg_id'] > 0: |
|
| 143 | svg_id = new_values['data']['svg_id'] |
|
| 144 | else: |
|
| 145 | svg_id = None |
|
| 146 | ||
| 147 | if 'camera_url' in new_values['data'].keys() and \ |
|
| 148 | new_values['data']['camera_url'] is not None and \ |
|
| 149 | len(str(new_values['data']['camera_url'])) > 0: |
|
| 150 | camera_url = str.strip(new_values['data']['camera_url']) |
|
| 151 | else: |
|
| 152 | camera_url = None |
|
| 153 | ||
| 154 | if 'description' in new_values['data'].keys() and \ |
|
| 155 | new_values['data']['description'] is not None and \ |
|
| 156 | len(str(new_values['data']['description'])) > 0: |
|
| 157 | description = str.strip(new_values['data']['description']) |
|
| 158 | else: |
|
| 159 | description = None |
|
| 160 | ||
| 161 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 162 | cursor = cnx.cursor() |
|
| 163 | ||
| 164 | cursor.execute(" SELECT name " |
|
| 165 | " FROM tbl_equipments " |
|
| 166 | " WHERE name = %s ", (name,)) |
|
| 167 | if cursor.fetchone() is not None: |
|
| 168 | cursor.close() |
|
| 169 | cnx.close() |
|
| 170 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 171 | description='API.EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
|
| 172 | ||
| 173 | if cost_center_id is not None: |
|
| 174 | cursor.execute(" SELECT name " |
|
| 175 | " FROM tbl_cost_centers " |
|
| 176 | " WHERE id = %s ", |
|
| 177 | (new_values['data']['cost_center_id'],)) |
|
| 178 | row = cursor.fetchone() |
|
| 179 | if row is None: |
|
| 180 | cursor.close() |
|
| 181 | cnx.close() |
|
| 182 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 183 | description='API.COST_CENTER_NOT_FOUND') |
|
| 184 | ||
| 185 | if svg_id is not None: |
|
| 186 | cursor.execute(" SELECT name " |
|
| 187 | " FROM tbl_svgs " |
|
| 188 | " WHERE id = %s ", |
|
| 189 | (svg_id,)) |
|
| 190 | row = cursor.fetchone() |
|
| 191 | if row is None: |
|
| 192 | cursor.close() |
|
| 193 | cnx.close() |
|
| 194 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 195 | description='API.SVG_NOT_FOUND') |
|
| 196 | ||
| 197 | add_values = (" INSERT INTO tbl_equipments " |
|
| 198 | " (name, uuid, is_input_counted, is_output_counted, " |
|
| 199 | " cost_center_id, svg_id, camera_url, description) " |
|
| 200 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
|
| 201 | cursor.execute(add_values, (name, |
|
| 202 | str(uuid.uuid4()), |
|
| 203 | is_input_counted, |
|
| 204 | is_output_counted, |
|
| 205 | cost_center_id, |
|
| 206 | svg_id, |
|
| 207 | camera_url, |
|
| 208 | description)) |
|
| 209 | new_id = cursor.lastrowid |
|
| 210 | cnx.commit() |
|
| 211 | cursor.close() |
|
| 212 | cnx.close() |
|
| 213 | ||
| 214 | resp.status = falcon.HTTP_201 |
|
| 215 | resp.location = '/equipments/' + str(new_id) |
|
| 216 | ||
| 217 | ||
| 218 | class EquipmentItem: |
|