| Conditions | 10 |
| Total Lines | 63 |
| Code Lines | 39 |
| 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 app.api.views.universal_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 os |
||
| 20 | def universal_get(session, schema_full, schema_nested, model, many: bool = True): |
||
| 21 | """ |
||
| 22 | Return entity from DB through GET request |
||
| 23 | :param session: |
||
| 24 | :param schema_full: |
||
| 25 | :param schema_nested: |
||
| 26 | :param model: |
||
| 27 | :param many: |
||
| 28 | :return: |
||
| 29 | """ |
||
| 30 | args = {**request.args} |
||
| 31 | |||
| 32 | detailed = bool(strtobool(args.pop("detailed", "False"))) |
||
| 33 | event_id = args.pop("event_id", None) |
||
| 34 | case_sensitive = bool(strtobool(args.pop("case_sensitive", "False"))) |
||
| 35 | model_args, skipped_args = separate_arguments(model, args) |
||
| 36 | model_query = session.query(model) |
||
| 37 | |||
| 38 | # TODO REFACTORING |
||
| 39 | api_section = request.path.strip("/").split("/")[-1] |
||
| 40 | if event_id and (api_section in ["words", "keys"]): |
||
| 41 | model_query = model.by_event(event_id=int(event_id)) |
||
| 42 | |||
| 43 | if model_args: |
||
| 44 | for attr, value in model_args.items(): |
||
| 45 | if str(value).isdigit(): |
||
| 46 | value = int(value) |
||
| 47 | model_query = model_query.filter(getattr(model, attr) == value) |
||
| 48 | continue |
||
| 49 | |||
| 50 | value = value.replace("*", "%") |
||
| 51 | name_attr = getattr(model, attr) |
||
| 52 | name_filter = ( |
||
| 53 | name_attr.like(value) if case_sensitive else name_attr.ilike(value) |
||
| 54 | ) |
||
| 55 | |||
| 56 | model_query = model_query.filter(name_filter) |
||
| 57 | |||
| 58 | model_entities = model_query.all() if many else model_query.first() |
||
| 59 | count = ( |
||
| 60 | len(model_entities) |
||
| 61 | if many |
||
| 62 | else len( |
||
| 63 | [ |
||
| 64 | model_entities, |
||
| 65 | ] |
||
| 66 | ) |
||
| 67 | ) |
||
| 68 | |||
| 69 | schema = schema_full if detailed else schema_nested |
||
| 70 | data = schema.dump(model_entities, many=many) |
||
| 71 | |||
| 72 | return Response( |
||
| 73 | mimetype="application/json", |
||
| 74 | response=json.dumps( |
||
| 75 | { |
||
| 76 | "result": True, |
||
| 77 | "data": data, |
||
| 78 | "count": count, |
||
| 79 | "skipped_arguments": skipped_args, |
||
| 80 | } |
||
| 81 | ), |
||
| 82 | status=200, |
||
| 83 | ) |
||
| 140 |