Conditions | 11 |
Total Lines | 52 |
Code Lines | 27 |
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 senaite.core.browser.usergroup.usergroups_usersoverview.UsersOverviewControlPanel.deleteMembers() 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 -*- |
||
87 | def deleteMembers(self, member_ids): |
||
88 | # this method exists to bypass the 'Manage Users' permission check |
||
89 | # in the CMF member tool's version |
||
90 | context = aq_inner(self.context) |
||
91 | mtool = api.get_tool("portal_membership") |
||
92 | |||
93 | # Delete members in acl_users. |
||
94 | acl_users = context.acl_users |
||
95 | if isinstance(member_ids, six.string_types): |
||
96 | member_ids = (member_ids,) |
||
97 | member_ids = list(member_ids) |
||
98 | for member_id in member_ids[:]: |
||
99 | member = mtool.getMemberById(member_id) |
||
100 | if member is None: |
||
101 | member_ids.remove(member_id) |
||
102 | else: |
||
103 | if not member.canDelete(): |
||
104 | raise Forbidden |
||
105 | if "Manager" in member.getRoles() and not self.is_zope_manager: |
||
106 | raise Forbidden |
||
107 | # clear all role/group assignments |
||
108 | self.clear_user_groups(member) |
||
109 | self.clear_user_roles(member) |
||
110 | |||
111 | try: |
||
112 | acl_users.userFolderDelUsers(member_ids) |
||
113 | except (AttributeError, NotImplementedError): |
||
114 | raise NotImplementedError('The underlying User Folder ' |
||
115 | 'doesn\'t support deleting members.') |
||
116 | |||
117 | # Delete member data in portal_memberdata. |
||
118 | mdtool = api.get_tool("portal_memberdata") |
||
119 | if mdtool is not None: |
||
120 | for member_id in member_ids: |
||
121 | mdtool.deleteMemberData(member_id) |
||
122 | |||
123 | # NOTE: the original call below iterates over **all** objects |
||
124 | # recursively to remove the local roles, which takes ages! |
||
125 | # The only place we allow local roles to be assigned are clients. |
||
126 | # Therefore, we want to make sure to remove them just from there |
||
127 | # |
||
128 | # Delete members' local roles. |
||
129 | # mtool.deleteLocalRoles( |
||
130 | # getUtility(ISiteRoot), |
||
131 | # member_ids, |
||
132 | # reindex=1, |
||
133 | # recursive=1 |
||
134 | # ) |
||
135 | # |
||
136 | # XXX: Maybe we could index local role assignments in the future? |
||
137 | for client in self.get_clients(): |
||
138 | mtool.deleteLocalRoles(client, member_ids, reindex=0, recursive=0) |
||
139 |