| Conditions | 18 |
| Total Lines | 87 |
| 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 build.bika.lims.browser.contact.ContactLoginDetailsView._create_user() 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 -*- |
||
| 193 | def _create_user(self): |
||
| 194 | """Create a new user |
||
| 195 | """ |
||
| 196 | |||
| 197 | def error(field, message): |
||
| 198 | if field: |
||
| 199 | message = "%s: %s" % (field, message) |
||
| 200 | self.context.plone_utils.addPortalMessage(message, 'error') |
||
| 201 | return self.request.response.redirect( |
||
| 202 | self.context.absolute_url() + "/login_details") |
||
| 203 | |||
| 204 | form = self.request.form |
||
| 205 | contact = self.context |
||
| 206 | |||
| 207 | password = safe_unicode(form.get('password', '')).encode('utf-8') |
||
| 208 | username = safe_unicode(form.get('username', '')).encode('utf-8') |
||
| 209 | confirm = form.get('confirm', '') |
||
| 210 | email = safe_unicode(form.get('email', '')).encode('utf-8') |
||
| 211 | |||
| 212 | if not username: |
||
| 213 | return error('username', PMF("Input is required but not given.")) |
||
| 214 | |||
| 215 | if not email: |
||
| 216 | return error('email', PMF("Input is required but not given.")) |
||
| 217 | |||
| 218 | reg_tool = self.context.portal_registration |
||
| 219 | # properties = self.context.portal_properties.site_properties |
||
| 220 | # if properties.validate_email: |
||
| 221 | # password = reg_tool.generatePassword() |
||
| 222 | # else: |
||
| 223 | if password != confirm: |
||
| 224 | return error('password', PMF("Passwords do not match.")) |
||
| 225 | |||
| 226 | if not password: |
||
| 227 | return error('password', PMF("Input is required but not given.")) |
||
| 228 | |||
| 229 | if not confirm: |
||
| 230 | return error('password', PMF("Passwords do not match.")) |
||
| 231 | |||
| 232 | if len(password) < 5: |
||
| 233 | return error('password', PMF("Passwords must contain at least 5 " |
||
| 234 | "characters.")) |
||
| 235 | for user in self.get_users(): |
||
| 236 | userid = user.get("id", None) |
||
| 237 | if userid is None: |
||
| 238 | continue |
||
| 239 | user_obj = api.get_user(userid) |
||
| 240 | if user_obj.getUserName() == username: |
||
| 241 | msg = "Username {} already exists, please, choose " \ |
||
| 242 | "another one.".format(username) |
||
| 243 | return error(None, msg) |
||
| 244 | |||
| 245 | try: |
||
| 246 | reg_tool.addMember(username, |
||
| 247 | password, |
||
| 248 | properties={ |
||
| 249 | 'username': username, |
||
| 250 | 'email': email, |
||
| 251 | 'fullname': username}) |
||
| 252 | except ValueError, msg: |
||
| 253 | return error(None, msg) |
||
| 254 | |||
| 255 | # set the user to the contact |
||
| 256 | contact.setUser(username) |
||
| 257 | |||
| 258 | # Additional groups for LabContact users only! |
||
| 259 | # -> This is not visible in the Client Contact Form |
||
| 260 | if "groups" in self.request and self.request["groups"]: |
||
| 261 | groups = self.request["groups"] |
||
| 262 | if not type(groups) in (list, tuple): |
||
| 263 | groups = [groups, ] |
||
| 264 | for group in groups: |
||
| 265 | group = self.portal_groups.getGroupById(group) |
||
| 266 | group.addMember(username) |
||
| 267 | |||
| 268 | if self.request.get('mail_me', 0): |
||
| 269 | try: |
||
| 270 | reg_tool.registeredNotify(username) |
||
| 271 | except Exception: |
||
| 272 | transaction.abort() |
||
| 273 | message = _("SMTP server disconnected. User creation aborted.") |
||
| 274 | return error(None, message) |
||
| 275 | |||
| 276 | message = _("Member registered and linked to the current Contact.") |
||
| 277 | self.context.plone_utils.addPortalMessage(message, 'info') |
||
| 278 | return self.request.response.redirect( |
||
| 279 | self.context.absolute_url() + "/login_details") |
||
| 280 | |||
| 286 |