Conditions | 19 |
Total Lines | 56 |
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 PageForm.clean_slug() 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 -*- |
||
118 | def clean_slug(self): |
||
119 | """Handle move action on the pages""" |
||
120 | |||
121 | slug = slugify(self.cleaned_data['slug']) |
||
122 | target = self.data.get('target', None) |
||
123 | position = self.data.get('position', None) |
||
124 | |||
125 | # this enforce a unique slug for every page |
||
126 | if settings.PAGE_AUTOMATIC_SLUG_RENAMING: |
||
127 | def is_slug_safe(slug): |
||
128 | content = Content.objects.get_content_slug_by_slug(slug) |
||
129 | if content is None: |
||
130 | return True |
||
131 | if self.instance.id: |
||
132 | if content.page.id == self.instance.id: |
||
133 | return True |
||
134 | else: |
||
135 | return False |
||
136 | |||
137 | return automatic_slug_renaming(slug, is_slug_safe) |
||
138 | |||
139 | if settings.PAGE_UNIQUE_SLUG_REQUIRED: |
||
140 | # We can return here as not futher checks |
||
141 | # are necessary |
||
142 | return unique_slug_required(self, slug) |
||
143 | |||
144 | intersects_sites = intersect_sites_method(self) |
||
145 | |||
146 | if not settings.PAGE_UNIQUE_SLUG_REQUIRED: |
||
147 | if target and position: |
||
148 | target = Page.objects.get(pk=target) |
||
149 | if position in ['right', 'left']: |
||
150 | slugs = [sibling.slug() for sibling in |
||
151 | target.get_siblings() |
||
152 | if intersects_sites(sibling)] |
||
153 | slugs.append(target.slug()) |
||
154 | if slug in slugs: |
||
155 | raise forms.ValidationError(error_dict['sibling_position_error']) |
||
156 | if position == 'first-child': |
||
157 | if slug in [sibling.slug() for sibling in |
||
158 | target.get_children() |
||
159 | if intersects_sites(sibling)]: |
||
160 | raise forms.ValidationError(error_dict['child_error']) |
||
161 | else: |
||
162 | if self.instance.id: |
||
163 | if (slug in [sibling.slug() for sibling in |
||
164 | self.instance.get_siblings().exclude( |
||
165 | id=self.instance.id |
||
166 | ) if intersects_sites(sibling)]): |
||
167 | raise forms.ValidationError(error_dict['sibling_error']) |
||
168 | else: |
||
169 | if slug in [sibling.slug() for sibling in |
||
170 | Page.objects.root() |
||
171 | if intersects_sites(sibling)]: |
||
172 | raise forms.ValidationError(error_dict['sibling_root_error']) |
||
173 | return slug |
||
174 | |||
176 |