Passed
Push — master ( 5db877...bb1125 )
by Swen
01:45
created

LocalizedUniqueSlugField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 68
ccs 18
cts 19
cp 0.9474
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 9 1
B pre_save() 0 42 5
1 1
from django.conf import settings
2 1
from django.utils.text import slugify
3 1
from django.core.exceptions import ImproperlyConfigured
4
5 1
from ..util import get_language_codes
6 1
from ..mixins import AtomicSlugRetryMixin
7 1
from ..localized_value import LocalizedValue
8 1
from .localized_autoslug_field import LocalizedAutoSlugField
9
10
11 1
class LocalizedUniqueSlugField(LocalizedAutoSlugField):
12
    """Automatically provides slugs for a localized
13
    field upon saving."
14
15
    An improved version of :see:LocalizedAutoSlugField,
16
    which adds:
17
18
        - Concurrency safety
19
        - Improved performance
20
21
    When in doubt, use this over :see:LocalizedAutoSlugField.
22
    Inherit from :see:AtomicSlugRetryMixin in your model to
23
    make this field work properly.
24
    """
25
26 1
    def __init__(self, *args, **kwargs):
27
        """Initializes a new instance of :see:LocalizedUniqueSlugField."""
28
29 1
        self.populate_from = kwargs.pop('populate_from')
30 1
        kwargs['uniqueness'] = kwargs.pop('uniqueness', get_language_codes())
31
32 1
        super(LocalizedAutoSlugField, self).__init__(
0 ignored issues
show
Bug introduced by
The first argument passed to super() should be the super-class name, but LocalizedAutoSlugField was given.
Loading history...
33
            *args,
34
            **kwargs
35
        )
36
37 1
    def pre_save(self, instance, add: bool):
38
        """Ran just before the model is saved, allows us to built
39
        the slug.
40
41
        Arguments:
42
            instance:
43
                The model that is being saved.
44
45
            add:
46
                Indicates whether this is a new entry
47
                to the database or an update.
48
49
        Returns:
50
            The localized slug that was generated.
51
        """
52
53 1
        if not isinstance(instance, AtomicSlugRetryMixin):
54
            raise ImproperlyConfigured((
55
                'Model \'%s\' does not inherit from AtomicSlugRetryMixin. '
56
                'Without this, the LocalizedUniqueSlugField will not work.'
57
            ) % type(instance).__name__)
58
59 1
        slugs = LocalizedValue()
60
61 1
        for lang_code, _ in settings.LANGUAGES:
62 1
            value = self._get_populate_from_value(
63
                instance,
64
                self.populate_from,
65
                lang_code
66
            )
67
68 1
            if not value:
69 1
                continue
70
71 1
            slug = slugify(value, allow_unicode=True)
72 1
            if instance.retries > 0:
73 1
                slug += '-%d' % instance.retries
74
75 1
            slugs.set(lang_code, slug)
76
77 1
        setattr(instance, self.name, slugs)
78
        return slugs
79