Passed
Pull Request — master (#31)
by Swen
01:21
created

AtomicSlugRetryMixin.save()   B

Complexity

Conditions 6

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
ccs 14
cts 14
cp 1
rs 7.5384
cc 6
crap 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