| 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 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 -*- |
||
| 221 | def _create_user(self): |
||
| 222 | """Create a new user |
||
| 223 | """ |
||
| 224 | |||
| 225 | def error(field, message): |
||
| 226 | if field: |
||
| 227 | message = "%s: %s" % (field, message) |
||
| 228 | self.context.plone_utils.addPortalMessage(message, 'error') |
||
| 229 | return self.request.response.redirect( |
||
| 230 | self.context.absolute_url() + "/login_details") |
||
| 231 | |||
| 232 | form = self.request.form |
||
| 233 | contact = self.context |
||
| 234 | |||
| 235 | password = safe_unicode(form.get('password', '')).encode('utf-8') |
||
| 236 | username = safe_unicode(form.get('username', '')).encode('utf-8') |
||
| 237 | confirm = form.get('confirm', '') |
||
| 238 | email = safe_unicode(form.get('email', '')).encode('utf-8') |
||
| 239 | |||
| 240 | if not username: |
||
| 241 | return error('username', PMF("Input is required but not given.")) |
||
| 242 | |||
| 243 | if not email: |
||
| 244 | return error('email', PMF("Input is required but not given.")) |
||
| 245 | |||
| 246 | reg_tool = self.context.portal_registration |
||
| 247 | # properties = self.context.portal_properties.site_properties |
||
| 248 | # if properties.validate_email: |
||
| 249 | # password = reg_tool.generatePassword() |
||
| 250 | # else: |
||
| 251 | if password != confirm: |
||
| 252 | return error('password', PMF("Passwords do not match.")) |
||
| 253 | |||
| 254 | if not password: |
||
| 255 | return error('password', PMF("Input is required but not given.")) |
||
| 256 | |||
| 257 | if not confirm: |
||
| 258 | return error('password', PMF("Passwords do not match.")) |
||
| 259 | |||
| 260 | if len(password) < 5: |
||
| 261 | return error('password', PMF("Passwords must contain at least 5 " |
||
| 262 | "characters.")) |
||
| 263 | for user in self.get_users(): |
||
| 264 | userid = user.get("id", None) |
||
| 265 | if userid is None: |
||
| 266 | continue |
||
| 267 | user_obj = api.get_user(userid) |
||
| 268 | if user_obj.getUserName() == username: |
||
| 269 | msg = "Username {} already exists, please, choose " \ |
||
| 270 | "another one.".format(username) |
||
| 271 | return error(None, msg) |
||
| 272 | |||
| 273 | try: |
||
| 274 | reg_tool.addMember(username, |
||
| 275 | password, |
||
| 276 | properties={ |
||
| 277 | 'username': username, |
||
| 278 | 'email': email, |
||
| 279 | 'fullname': username}) |
||
| 280 | except ValueError as msg: |
||
| 281 | return error(None, msg) |
||
| 282 | |||
| 283 | # set the user to the contact |
||
| 284 | contact.setUser(username) |
||
| 285 | |||
| 286 | # Additional groups for LabContact users only! |
||
| 287 | # -> This is not visible in the Client Contact Form |
||
| 288 | if "groups" in self.request and self.request["groups"]: |
||
| 289 | groups = self.request["groups"] |
||
| 290 | if not type(groups) in (list, tuple): |
||
| 291 | groups = [groups, ] |
||
| 292 | for group in groups: |
||
| 293 | group = self.portal_groups.getGroupById(group) |
||
| 294 | group.addMember(username) |
||
| 295 | |||
| 296 | if self.request.get('mail_me', 0): |
||
| 297 | try: |
||
| 298 | reg_tool.registeredNotify(username) |
||
| 299 | except Exception: |
||
| 300 | transaction.abort() |
||
| 301 | message = _("SMTP server disconnected. User creation aborted.") |
||
| 302 | return error(None, message) |
||
| 303 | |||
| 304 | message = _("Member registered and linked to the current Contact.") |
||
| 305 | self.context.plone_utils.addPortalMessage(message, 'info') |
||
| 306 | return self.request.response.redirect( |
||
| 307 | self.context.absolute_url() + "/login_details") |
||
| 308 | |||
| 314 |