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

LocalizedModel.save()   B

Complexity

Conditions 6

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
c 2
b 0
f 0
dl 0
loc 29
ccs 13
cts 14
cp 0.9286
crap 6.0131
rs 7.5384
1 1
from django.db import models, transaction
0 ignored issues
show
Unused Code introduced by
Unused transaction imported from django.db
Loading history...
2 1
from django.db.utils import IntegrityError
0 ignored issues
show
Unused Code introduced by
Unused IntegrityError imported from django.db.utils
Loading history...
3 1
from django.conf import settings
0 ignored issues
show
Unused Code introduced by
Unused settings imported from django.conf
Loading history...
4
5 1
from .fields import LocalizedField
6 1
from .localized_value import LocalizedValue
7
8
9 1
class LocalizedModel(models.Model):
10
    """A model that contains localized fields."""
11
12 1
    class Meta:
13 1
        abstract = True
14
15 1
    def __init__(self, *args, **kwargs):
16
        """Initializes a new instance of :see:LocalizedModel.
17
18
        Here we set all the fields that are of :see:LocalizedField
19
        to an instance of :see:LocalizedValue in case they are none
20
        so that the user doesn't explicitely have to do so."""
21
22 1
        super(LocalizedModel, self).__init__(*args, **kwargs)
23
24 1
        for field in self._meta.get_fields():
25 1
            if not isinstance(field, LocalizedField):
26 1
                continue
27
28 1
            value = getattr(self, field.name, None)
29
30 1
            if not isinstance(value, LocalizedValue):
31 1
                if isinstance(value, dict):
32 1
                    value = LocalizedValue(value)
33
                else:
34 1
                    value = LocalizedValue()
35
36
            setattr(self, field.name, value)
37