Passed
Pull Request — master (#29)
by
unknown
01:40
created

AtomicSlugRetryMixin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
dl 0
loc 33
ccs 15
cts 15
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B save() 0 29 6
1 1
from django.db import transaction
0 ignored issues
show
Configuration introduced by
The import django.db could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
2 1
from django.conf import settings
0 ignored issues
show
Configuration introduced by
The import django.conf could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
3 1
from django.db.utils import IntegrityError
0 ignored issues
show
Configuration introduced by
The import django.db.utils could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4
5
6 1
class AtomicSlugRetryMixin:
7
    """Makes :see:LocalizedUniqueSlugField work by retrying upon
8
    violation of the UNIQUE constraint."""
9
10 1
    def save(self, *args, **kwargs):
11
        """Saves this model instance to the database."""
12
13 1
        max_retries = getattr(
14
            settings,
15
            'LOCALIZED_FIELDS_MAX_RETRIES',
16
            100
17
        )
18
19 1
        if not hasattr(self, 'retries'):
20 1
            self.retries = 0
0 ignored issues
show
Coding Style introduced by
The attribute retries was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
21
22 1
        with transaction.atomic():
23 1
            try:
24 1
                return super().save(*args, **kwargs)
25 1
            except IntegrityError as ex:
26
                # this is as retarded as it looks, there's no
27
                # way we can put the retry logic inside the slug
28
                # field class... we can also not only catch exceptions
29
                # that apply to slug fields... so yea.. this is as
30
                # retarded as it gets... i am sorry :(
31 1
                if 'slug' not in str(ex):
32 1
                    raise ex
33
34 1
                if self.retries >= max_retries:
35 1
                    raise ex
36
37 1
        self.retries += 1
38
        return self.save()
39