| Conditions | 10 | 
| Total Lines | 54 | 
| Code Lines | 36 | 
| 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.site.generate_content() 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 | # -*- coding: utf-8 -*-  | 
            ||
| 107 | def generate_content(data):  | 
            ||
| 108 |     word = data.get("word", str()) | 
            ||
| 109 |     search_language = data.get("language_id", DEFAULT_SEARCH_LANGUAGE) | 
            ||
| 110 |     event_id = data.get("event_id", 1) | 
            ||
| 111 |     is_case_sensitive = data.get("case_sensitive", False) | 
            ||
| 112 | |||
| 113 | if not word or not data:  | 
            ||
| 114 | return jsonify(result="<div></div>")  | 
            ||
| 115 | |||
| 116 | nothing = """  | 
            ||
| 117 | <div class="alert alert-secondary" role="alert" style="text-align: center;">  | 
            ||
| 118 | %s  | 
            ||
| 119 | </div>  | 
            ||
| 120 | """  | 
            ||
| 121 | |||
| 122 | if isinstance(is_case_sensitive, str):  | 
            ||
| 123 | is_case_sensitive = strtobool(is_case_sensitive)  | 
            ||
| 124 | |||
| 125 | if search_language == "log":  | 
            ||
| 126 | word_statement = LoglanItem.query_select_words(  | 
            ||
| 127 | name=word, event_id=event_id, case_sensitive=is_case_sensitive  | 
            ||
| 128 | )  | 
            ||
| 129 | with Session() as session:  | 
            ||
| 130 | word_result = session.execute(word_statement).scalars().all()  | 
            ||
| 131 | result = Composer(  | 
            ||
| 132 | words=word_result, style=DEFAULT_HTML_STYLE  | 
            ||
| 133 | ).export_as_html()  | 
            ||
| 134 | |||
| 135 | if not result:  | 
            ||
| 136 | result = (  | 
            ||
| 137 | nothing  | 
            ||
| 138 |                 % f"There is no word <b>{word}</b> in Loglan. Try switching to English" | 
            ||
| 139 |                 f"{' or disable Case sensitive search' if is_case_sensitive else ''}." | 
            ||
| 140 | )  | 
            ||
| 141 | |||
| 142 | elif search_language == "eng":  | 
            ||
| 143 | definitions_statement = EnglishItem.select_definitions_by_key(  | 
            ||
| 144 | key=word, event_id=event_id, case_sensitive=is_case_sensitive  | 
            ||
| 145 | )  | 
            ||
| 146 | with Session() as session:  | 
            ||
| 147 | definitions_result = session.execute(definitions_statement).scalars().all()  | 
            ||
| 148 | |||
| 149 | result = EnglishItem(  | 
            ||
| 150 | definitions=definitions_result, key=word, style=DEFAULT_HTML_STYLE  | 
            ||
| 151 | ).export_as_html()  | 
            ||
| 152 | if not result:  | 
            ||
| 153 | result = (  | 
            ||
| 154 | nothing  | 
            ||
| 155 |                 % f"There is no word <b>{word}</b> in English. Try switching to Loglan" | 
            ||
| 156 |                 f"{' or disable Case sensitive search' if is_case_sensitive else ''}." | 
            ||
| 157 | )  | 
            ||
| 158 | else:  | 
            ||
| 159 |         result = nothing % f"Sorry, but nothing was found for <b>{word}</b>." | 
            ||
| 160 | return jsonify(result=result)  | 
            ||
| 161 | |||
| 179 |