| Conditions | 1 | 
| Total Lines | 127 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 2 | ||
| Bugs | 0 | Features | 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:
| 1 | # -*- coding: utf-8 -*- | ||
| 103 | def __init__(self, **kwargs): | ||
| 104 | super(GeocompleteField, self).__init__(**kwargs) | ||
| 105 | self.resources = [ | ||
| 106 | select2_persistence_js, | ||
| 107 | twc.JSSource( | ||
| 108 | src=''' | ||
| 109 |                 function format_name(name, complement, postal_code, country) { | ||
| 110 | var display_name = name; | ||
| 111 | display_name += ' '; | ||
| 112 | |||
| 113 |                     if(complement) { | ||
| 114 | display_name += complement; | ||
| 115 | } | ||
| 116 | |||
| 117 | display_name += ' - '; | ||
| 118 | display_name += postal_code; | ||
| 119 | display_name += ', '; | ||
| 120 | display_name += country; | ||
| 121 | |||
| 122 | return display_name.toUpperCase(); | ||
| 123 | } | ||
| 124 | ''' | ||
| 125 | ) | ||
| 126 | ] | ||
| 127 | self.options = [] | ||
| 128 | self.ondemand = True | ||
| 129 | self.validator = self.validator or Validator() | ||
| 130 | |||
| 131 | self.opts.copy() | ||
| 132 | self.opts['minimumInputLength'] = 1 | ||
| 133 | self.opts['maximumInputLength'] = 125 | ||
| 134 | self.opts['allowClear'] = True | ||
| 135 | self.opts['dropdownAutoWidth'] = True | ||
| 136 | |||
| 137 | self.opts['initSelection'] = twc.js_callback( | ||
| 138 | """ | ||
| 139 |             function (element, callback) { | ||
| 140 | var init_data; | ||
| 141 | |||
| 142 | param = params['%(name)s']; | ||
| 143 | |||
| 144 |                 if(typeof param !== "undefined" && param) { | ||
| 145 |                     var elem = {}; | ||
| 146 | param_dict = JSON.parse(param); | ||
| 147 | elem.id = param; | ||
| 148 | |||
| 149 | name = format_name( | ||
| 150 | param_dict['name'], | ||
| 151 | param_dict['complement'], | ||
| 152 | param_dict['postal_code'], | ||
| 153 | param_dict['country'] | ||
| 154 | ); | ||
| 155 | |||
| 156 | elem.name = name; | ||
| 157 | elem.value = name; | ||
| 158 | |||
| 159 | init_data = elem; | ||
| 160 | } | ||
| 161 | |||
| 162 | callback(init_data); | ||
| 163 | } | ||
| 164 | """ % dict(name=self.name) | ||
| 165 | ) | ||
| 166 | self.opts['escapeMarkup'] = twc.js_callback( | ||
| 167 | """ | ||
| 168 |             function(markup) { | ||
| 169 | return markup; | ||
| 170 | } | ||
| 171 | """ | ||
| 172 | ) | ||
| 173 | self.opts['formatResult'] = twc.js_callback( | ||
| 174 | """ | ||
| 175 |             function(location) { | ||
| 176 | var markup = '<option value="' + location.value + '">' | ||
| 177 | + location.name | ||
| 178 | + '</option>'; | ||
| 179 | return markup; | ||
| 180 | } | ||
| 181 | """ | ||
| 182 | ) | ||
| 183 | self.opts['formatSelection'] = twc.js_callback( | ||
| 184 | """ | ||
| 185 |             function(location) { | ||
| 186 |                 if(typeof location !== "undefined") { | ||
| 187 | return location.value || location.text; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | """ | ||
| 191 | ) | ||
| 192 | |||
| 193 | self.opts['ajax'] = dict( | ||
| 194 | url='/geocomplete', | ||
| 195 | dataType='json', | ||
| 196 | type='POST', | ||
| 197 | quietMillis=100, | ||
| 198 | cache=True, | ||
| 199 | data=twc.js_callback( | ||
| 200 | """ | ||
| 201 |                 function(term) { | ||
| 202 |                     return {address: term}; | ||
| 203 | } | ||
| 204 | """ | ||
| 205 | ), | ||
| 206 | results=twc.js_callback( | ||
| 207 | """ | ||
| 208 |                 function(data) { | ||
| 209 | var results = []; | ||
| 210 | |||
| 211 |                     if ('results' in data) { | ||
| 212 |                         $.each(data['results'], function(i, v) { | ||
| 213 |                             var o = {}; | ||
| 214 | o.id = JSON.stringify(v); | ||
| 215 | name = format_name( | ||
| 216 | v['name'], | ||
| 217 | v['complement'], | ||
| 218 | v['postal_code'], | ||
| 219 | v['country'] | ||
| 220 | ); | ||
| 221 | o.name = name; | ||
| 222 | o.value = name; | ||
| 223 | results.push(o); | ||
| 224 | }); | ||
| 225 | } | ||
| 226 | |||
| 227 |                     return {results: results}; | ||
| 228 | } | ||
| 229 | """ | ||
| 230 | ) | ||
| 248 |