| Conditions | 17 |
| Total Lines | 79 |
| Code Lines | 63 |
| 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 core.useractivity.user_logger() 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 os |
||
| 97 | def user_logger(func): |
||
| 98 | """ |
||
| 99 | Decorator for logging user activities |
||
| 100 | :param func: the decorated function |
||
| 101 | :return: the decorator |
||
| 102 | """ |
||
| 103 | @wraps(func) |
||
| 104 | def logger(*args, **kwargs): |
||
| 105 | qualified_name = func.__qualname__ |
||
| 106 | class_name = qualified_name.split(".")[0] |
||
| 107 | func_name = qualified_name.split(".")[1] |
||
| 108 | |||
| 109 | if func_name not in ("on_post", "on_put", "on_delete"): |
||
| 110 | # do not log for other HTTP Methods |
||
| 111 | func(*args, **kwargs) |
||
| 112 | return |
||
| 113 | req, resp = args |
||
| 114 | headers = req.headers |
||
| 115 | if headers is not None and 'USER-UUID' in headers.keys(): |
||
| 116 | user_uuid = headers['USER-UUID'] |
||
| 117 | else: |
||
| 118 | # todo: deal with requests with NULL user_uuid |
||
| 119 | print('user_logger: USER-UUID is NULL') |
||
| 120 | # do not log for NULL user_uuid |
||
| 121 | func(*args, **kwargs) |
||
| 122 | return |
||
| 123 | |||
| 124 | if func_name == "on_post": |
||
| 125 | try: |
||
| 126 | file_name = str(uuid.uuid4()) |
||
| 127 | with open(file_name, "wb") as fw: |
||
| 128 | reads = req.stream.read() |
||
| 129 | fw.write(reads) |
||
| 130 | raw_json = reads.decode('utf-8') |
||
| 131 | with open(file_name, "rb") as fr: |
||
| 132 | req.stream = Body(fr) |
||
| 133 | os.remove(file_name) |
||
| 134 | func(*args, **kwargs) |
||
| 135 | write_log(user_uuid=user_uuid, request_method='POST', resource_type=class_name, |
||
| 136 | resource_id=kwargs.get('id_'), request_body=raw_json) |
||
| 137 | except Exception as e: |
||
| 138 | if isinstance(e, falcon.HTTPError): |
||
| 139 | raise e |
||
| 140 | else: |
||
| 141 | print('user_logger:' + str(e)) |
||
| 142 | return |
||
| 143 | elif func_name == "on_put": |
||
| 144 | try: |
||
| 145 | file_name = str(uuid.uuid4()) |
||
| 146 | with open(file_name, "wb") as fw: |
||
| 147 | reads = req.stream.read() |
||
| 148 | fw.write(reads) |
||
| 149 | raw_json = reads.decode('utf-8') |
||
| 150 | with open(file_name, "rb") as fr: |
||
| 151 | req.stream = Body(fr) |
||
| 152 | os.remove(file_name) |
||
| 153 | func(*args, **kwargs) |
||
| 154 | write_log(user_uuid=user_uuid, request_method='PUT', resource_type=class_name, |
||
| 155 | resource_id=kwargs.get('id_'), request_body=raw_json) |
||
| 156 | except Exception as e: |
||
| 157 | if isinstance(e, falcon.HTTPError): |
||
| 158 | raise e |
||
| 159 | else: |
||
| 160 | print('user_logger:' + str(e)) |
||
| 161 | |||
| 162 | return |
||
| 163 | elif func_name == "on_delete": |
||
| 164 | try: |
||
| 165 | func(*args, **kwargs) |
||
| 166 | write_log(user_uuid=user_uuid, request_method="DELETE", resource_type=class_name, |
||
| 167 | resource_id=kwargs.get('id_'), request_body=json.dumps(kwargs)) |
||
| 168 | except Exception as e: |
||
| 169 | if isinstance(e, falcon.HTTPError): |
||
| 170 | raise e |
||
| 171 | else: |
||
| 172 | print('user_logger:' + str(e)) |
||
| 173 | return |
||
| 174 | |||
| 175 | return logger |
||
| 176 |