| Conditions | 12 |
| Total Lines | 83 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like reports.advancedreport.AdvancedReportCollection.on_get() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import falcon |
||
| 25 | @staticmethod |
||
| 26 | def on_get(req, resp): |
||
| 27 | print(req.params) |
||
| 28 | reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
||
| 29 | reporting_end_datetime_local = req.params.get('reportingperiodenddatetime') |
||
| 30 | |||
| 31 | ################################################################################################################ |
||
| 32 | # Step 1: valid parameters |
||
| 33 | ################################################################################################################ |
||
| 34 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 35 | if config.utc_offset[0] == '-': |
||
| 36 | timezone_offset = -timezone_offset |
||
| 37 | |||
| 38 | if reporting_start_datetime_local is None: |
||
| 39 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 40 | description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
||
| 41 | else: |
||
| 42 | reporting_start_datetime_local = str.strip(reporting_start_datetime_local) |
||
| 43 | try: |
||
| 44 | reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, |
||
| 45 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 46 | timedelta(minutes=timezone_offset) |
||
| 47 | except ValueError: |
||
| 48 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 49 | description="API.INVALID_REPORTING_PERIOD_BEGINS_DATETIME") |
||
| 50 | |||
| 51 | if reporting_end_datetime_local is None: |
||
| 52 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 53 | description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
||
| 54 | else: |
||
| 55 | reporting_end_datetime_local = str.strip(reporting_end_datetime_local) |
||
| 56 | try: |
||
| 57 | reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, |
||
| 58 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 59 | timedelta(minutes=timezone_offset) |
||
| 60 | except ValueError: |
||
| 61 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 62 | description="API.INVALID_REPORTING_PERIOD_ENDS_DATETIME") |
||
| 63 | |||
| 64 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
||
| 65 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 66 | description='API.INVALID_REPORTING_PERIOD_ENDS_DATETIME') |
||
| 67 | |||
| 68 | ################################################################################################################ |
||
| 69 | # Step 2: query advanced reports |
||
| 70 | ################################################################################################################ |
||
| 71 | |||
| 72 | cnx_reporting = mysql.connector.connect(**config.myems_reporting_db) |
||
| 73 | cursor_reporting = cnx_reporting.cursor(dictionary=True) |
||
| 74 | |||
| 75 | query = (" SELECT id, file_name, uuid, create_datetime_utc, file_type, file_object " |
||
| 76 | " FROM tbl_reports_files " |
||
| 77 | " WHERE create_datetime_utc >= %s AND create_datetime_utc < %s " |
||
| 78 | " ORDER BY create_datetime_utc desc ") |
||
| 79 | cursor_reporting.execute(query, (reporting_start_datetime_utc, reporting_end_datetime_utc)) |
||
| 80 | rows = cursor_reporting.fetchall() |
||
| 81 | if cursor_reporting: |
||
| 82 | cursor_reporting.close() |
||
| 83 | if cnx_reporting: |
||
| 84 | cnx_reporting.disconnect() |
||
| 85 | |||
| 86 | ################################################################################################################ |
||
| 87 | # Step 3: construct the result |
||
| 88 | ################################################################################################################ |
||
| 89 | result = list() |
||
| 90 | if rows is not None and len(rows) > 0: |
||
| 91 | for row in rows: |
||
| 92 | # Base64 encode the bytes |
||
| 93 | base64_encoded_data = base64.b64encode(row['file_object']) |
||
| 94 | # get the Base64 encoded data using human-readable characters. |
||
| 95 | base64_message = base64_encoded_data.decode('utf-8') |
||
| 96 | create_datetime_local = row['create_datetime_utc'].replace(tzinfo=None) + \ |
||
| 97 | timedelta(minutes=timezone_offset) |
||
| 98 | meta_result = {"id": row['id'], |
||
| 99 | "file_name": row['file_name'], |
||
| 100 | "uuid": row['uuid'], |
||
| 101 | "create_datetime_local": create_datetime_local.isoformat(), |
||
| 102 | "file_type": row['file_type'], |
||
| 103 | "file_size_bytes": sys.getsizeof(row['file_object']), |
||
| 104 | "file_bytes_base64": base64_message} |
||
| 105 | result.append(meta_result) |
||
| 106 | |||
| 107 | resp.body = json.dumps(result) |
||
| 108 | |||
| 188 |