| 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 | populated, it should be hidden from the form. |
||
| 53 | """ |
||
| 54 | |||
| 55 | 1 | defaults = { |
|
| 56 | 'form_class': forms.CharField, |
||
| 57 | 'required': False |
||
| 58 | } |
||
| 59 | |||
| 60 | 1 | defaults.update(kwargs) |
|
| 61 | |||
| 62 | 1 | form_field = super().formfield(**defaults) |
|
| 63 | 1 | form_field.widget = forms.HiddenInput() |
|
| 64 | |||
| 65 | 1 | return form_field |
|
| 66 | |||
| 67 | 1 | def contribute_to_class(self, cls, name, *args, **kwargs): |
|
| 68 | """Hook that allow us to operate with model class. We overwrite save() |
||
| 69 | method to run retry logic. |
||
| 70 | |||
| 71 | Arguments: |
||
| 72 | cls: |
||
| 73 | Model class. |
||
| 74 | |||
| 75 | name: |
||
| 76 | Name of field in model. |
||
| 77 | """ |
||
| 78 | # apparently in inheritance cases, contribute_to_class is called more |
||
| 79 | # than once, so we have to be careful not to overwrite the original |
||
| 80 | # save method. |
||
| 81 | 1 | if not hasattr(cls, '_orig_save'): |
|
| 82 | 1 | cls._orig_save = cls.save |
|
| 83 | 1 | max_retries = getattr( |
|
| 84 | settings, |
||
| 85 | 'LOCALIZED_FIELDS_MAX_RETRIES', |
||
| 86 | 100 |
||
| 87 | ) |
||
| 88 | |||
| 89 | 1 | def _new_save(instance, *args_, **kwargs_): |
|
| 90 | 1 | retries = 0 |
|
| 91 | 1 | while True: |
|
| 92 | 1 | with transaction.atomic(): |
|
| 93 | 1 | try: |
|
| 94 | 1 | slugs = self.populate_slugs(instance, retries) |
|
| 95 | 1 | setattr(instance, name, slugs) |
|
| 96 | 1 | instance._orig_save(*args_, **kwargs_) |
|
| 97 | 1 | break |
|
| 98 | 1 | except IntegrityError as e: |
|
| 99 | 1 | if retries >= max_retries: |
|
| 100 | 1 | raise e |
|
| 101 | # check to be sure a slug fight caused |
||
| 102 | # the IntegrityError |
||
| 103 | 1 | s_e = str(e) |
|
| 104 | 1 | if name in s_e and 'unique' in s_e: |
|
| 105 | 1 | retries += 1 |
|
| 106 | else: |
||
| 107 | raise e |
||
| 108 | |||
| 109 | 1 | cls.save = _new_save |
|
| 110 | 1 | super().contribute_to_class(cls, name, *args, **kwargs) |
|
| 111 | |||
| 157 |
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.