| Conditions | 10 |
| Total Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 24 |
| CRAP Score | 10.0064 |
| 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:
Complex classes like LocalizedUniqueSlugField.pre_save() 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 | 1 | from datetime import datetime |
|
| 52 | 1 | def pre_save(self, instance, add: bool): |
|
| 53 | """Ran just before the model is saved, allows us to built |
||
| 54 | the slug. |
||
| 55 | |||
| 56 | Arguments: |
||
| 57 | instance: |
||
| 58 | The model that is being saved. |
||
| 59 | |||
| 60 | add: |
||
| 61 | Indicates whether this is a new entry |
||
| 62 | to the database or an update. |
||
| 63 | |||
| 64 | Returns: |
||
| 65 | The localized slug that was generated. |
||
| 66 | """ |
||
| 67 | |||
| 68 | 1 | if not isinstance(instance, AtomicSlugRetryMixin): |
|
| 69 | raise ImproperlyConfigured(( |
||
| 70 | 'Model \'%s\' does not inherit from AtomicSlugRetryMixin. ' |
||
| 71 | 'Without this, the LocalizedUniqueSlugField will not work.' |
||
| 72 | ) % type(instance).__name__) |
||
| 73 | |||
| 74 | 1 | slugs = LocalizedValue() |
|
| 75 | |||
| 76 | 1 | for lang_code, _ in settings.LANGUAGES: |
|
| 77 | 1 | value = self._get_populate_from_value( |
|
| 78 | instance, |
||
| 79 | self.populate_from, |
||
| 80 | lang_code |
||
| 81 | ) |
||
| 82 | |||
| 83 | 1 | if not value: |
|
| 84 | 1 | continue |
|
| 85 | |||
| 86 | 1 | slug = slugify(value, allow_unicode=True) |
|
| 87 | |||
| 88 | # verify whether it's needed to re-generate a slug, |
||
| 89 | # if not, re-use the same slug |
||
| 90 | 1 | if instance.pk is not None: |
|
| 91 | 1 | current_slug = getattr(instance, self.name).get(lang_code) |
|
| 92 | 1 | if current_slug is not None: |
|
| 93 | 1 | stripped_slug = current_slug[0:current_slug.rfind('-')] |
|
| 94 | 1 | if slug == stripped_slug: |
|
| 95 | 1 | slugs.set(lang_code, current_slug) |
|
| 96 | 1 | continue |
|
| 97 | |||
| 98 | 1 | if self.include_time: |
|
| 99 | 1 | slug += '-%d' % datetime.now().microsecond |
|
| 100 | |||
| 101 | 1 | if instance.retries > 0: |
|
| 102 | # do not add another - if we already added time |
||
| 103 | 1 | if not self.include_time: |
|
| 104 | 1 | slug += '-' |
|
| 105 | 1 | slug += '%d' % instance.retries |
|
| 106 | |||
| 107 | 1 | slugs.set(lang_code, slug) |
|
| 108 | |||
| 109 | 1 | setattr(instance, self.name, slugs) |
|
| 110 | return slugs |
||
| 111 |