Completed
Push — master ( df072c...9919f2 )
by Diederik van der
01:21
created

TranslatableModelFormMetaclass.__new__()   F

Complexity

Conditions 19

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 63
c 1
b 0
f 0
rs 2.9282
cc 19

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like TranslatableModelFormMetaclass.__new__() 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
from django import forms
2
import django
3
from django.core.exceptions import NON_FIELD_ERRORS, ObjectDoesNotExist, ValidationError
4
from django.forms.forms import BoundField
5
from django.forms.models import ModelFormMetaclass, BaseInlineFormSet
6
from django.utils.functional import cached_property
7
from django.utils.translation import get_language
8
from django.utils import six
9
from parler.models import TranslationDoesNotExist
10
from parler.utils import compat
11
12
13
__all__ = (
14
    'TranslatableModelForm',
15
    'TranslatedField',
16
    'BaseTranslatableModelForm',
17
    #'TranslatableModelFormMetaclass',
18
)
19
20
21
class TranslatedField(object):
22
    """
23
    A wrapper for a translated form field.
24
25
    This wrapper can be used to declare translated fields on the form, e.g.
26
27
    .. code-block:: python
28
29
        class MyForm(TranslatableModelForm):
30
            title = TranslatedField()
31
            slug = TranslatedField()
32
33
            description = TranslatedField(form_class=forms.CharField, widget=TinyMCE)
34
    """
35
36
    def __init__(self, **kwargs):
37
        # The metaclass performs the magic replacement with the actual formfield.
38
        self.kwargs = kwargs
39
40
41
class BaseTranslatableModelForm(forms.BaseModelForm):
42
    """
43
    The base methods added to :class:`TranslatableModelForm` to fetch and store translated fields.
44
    """
45
    language_code = None    # Set by TranslatableAdmin.get_form() on the constructed subclass.
46
47
    def __init__(self, *args, **kwargs):
48
        current_language = kwargs.pop('_current_language', None)   # Used for TranslatableViewMixin
49
        super(BaseTranslatableModelForm, self).__init__(*args, **kwargs)
50
51
        # Load the initial values for the translated fields
52
        instance = kwargs.get('instance', None)
53
        if instance:
54
            for meta in instance._parler_meta:
55
                try:
56
                    # By not auto creating a model, any template code that reads the fields
57
                    # will continue to see one of the other translations.
58
                    # This also causes admin inlines to show the fallback title in __unicode__.
59
                    translation = instance._get_translated_model(meta=meta)
60
                except TranslationDoesNotExist:
61
                    pass
62
                else:
63
                    for field in meta.get_translated_fields():
64
                        try:
65
                            self.initial.setdefault(field, getattr(translation, field))
66
                        except ObjectDoesNotExist:
67
                            # This occurs when a ForeignKey field is part of the translation,
68
                            # but it's value is still not yet, and the field has null=False.
69
                            pass
70
71
        # Typically already set by admin
72
        if self.language_code is None:
73
            if instance:
74
                self.language_code = instance.get_current_language()
75
                return
76
            else:
77
                self.language_code = current_language or get_language()
78
79
    def _post_clean(self):
80
        # Copy the translated fields into the model
81
        # Make sure the language code is set as early as possible (so it's active during most clean() methods)
82
        self.instance.set_current_language(self.language_code)
83
        self.save_translated_fields()
84
85
        # Perform the regular clean checks, this also updates self.instance
86
        super(BaseTranslatableModelForm, self)._post_clean()
87
88
    def save_translated_fields(self):
89
        """
90
        Save all translated fields.
91
        """
92
        fields = {}
93
94
        # Collect all translated fields {'name': 'value'}
95
        for field in self._translated_fields:
96
            try:
97
                value = self.cleaned_data[field]
98
            except KeyError:  # Field has a ValidationError
99
                continue
100
            fields[field] = value
101
102
        # Set the field values on their relevant models
103
        translation_models = self.instance._set_translated_fields(**fields)
104
105
        # Perform full clean on models
106
        non_translated_fields = ['id', 'master_id', 'language_code']
107
        base_exclude = self._get_validation_exclusions()
108
        for translation_model in translation_models:
109
            exclude = base_exclude + ['master']
110
            self._clean_translation_model(translation_model, exclude)
111
112
            # Assign translated fields to the model (using the TranslatedAttribute descriptor)
113
            for field in translation_model._get_field_names():
114
                if field in non_translated_fields:
115
                    continue
116
                setattr(self.instance, field, getattr(translation_model, field))
117
118
    if django.VERSION >= (1, 6):
119
120
        def _clean_translation_model(self, translation_model, exclude):
121
            try:
122
                translation_model.full_clean(
123
                    exclude=exclude, validate_unique=False)
124
            except ValidationError as e:
125
                self._update_errors(e)
126
127
            # Validate uniqueness if needed.
128
            if self._validate_unique:
129
                try:
130
                    translation_model.validate_unique()
131
                except ValidationError as e:
132
                    self._update_errors(e)
133
    else:
134
135
        def _clean_translation_model(self, translation_model, exclude):
136
            # Clean the model instance's fields.
137
            try:
138
                translation_model.clean_fields(exclude=exclude)
139
            except ValidationError as e:
140
                self._update_errors(e.message_dict)
141
142
            # Call the model instance's clean method.
143
            try:
144
                translation_model.clean()
145
            except ValidationError as e:
146
                self._update_errors({NON_FIELD_ERRORS: e.messages})
147
148
            # Validate uniqueness if needed.
149
            if self._validate_unique:
150
                try:
151
                    translation_model.validate_unique()
152
                except ValidationError as e:
153
                    self._update_errors(e.message_dict)
154
155
    @cached_property
156
    def _translated_fields(self):
157
        field_names = self._meta.model._parler_meta.get_all_fields()
158
        return [f_name for f_name in field_names if f_name in self.fields]
159
160
    def __getitem__(self, name):
161
        """
162
        Return a :class:`TranslatableBoundField` for translated models.
163
        This extends the default ``form[field]`` interface that produces the BoundField for HTML templates.
164
        """
165
        boundfield = super(BaseTranslatableModelForm, self).__getitem__(name)
166
        if name in self._translated_fields:
167
            # Oh the wonders of Python :)
168
            boundfield.__class__ = _upgrade_boundfield_class(boundfield.__class__)
169
        return boundfield
170
171
172
UPGRADED_CLASSES = {}
173
174
175
def _upgrade_boundfield_class(cls):
176
    if cls is BoundField:
177
        return TranslatableBoundField
178
    elif issubclass(cls, TranslatableBoundField):
179
        return cls
180
181
    # When some other package also performs this same trick,
182
    # combine both classes on the fly. Avoid having to do that each time.
183
    # This is needed for django-slug-preview
184
    try:
185
        return UPGRADED_CLASSES[cls]
186
    except KeyError:
187
        # Create once
188
        new_cls = type('Translatable{0}'.format(cls.__name__), (cls, TranslatableBoundField), {})
189
        UPGRADED_CLASSES[cls] = new_cls
190
        return new_cls
191
192
193
class TranslatableBoundField(BoundField):
194
    """
195
    Decorating the regular BoundField to distinguish translatable fields in the admin.
196
    """
197
    #: A tagging attribute, making it easy for templates to identify these fields
198
    is_translatable = True
199
200
    def label_tag(self, contents=None, attrs=None, *args, **kwargs):  # extra args differ per Django version
201
        if attrs is None:
202
            attrs = {}
203
204
        attrs['class'] = (attrs.get('class', '') + " translatable-field").strip()
205
        return super(TranslatableBoundField, self).label_tag(contents, attrs, *args, **kwargs)
206
207
    # The as_widget() won't be overwritten to add a 'class' attr,
208
    # because it will overwrite what AdminTextInputWidget and fields have as default.
209
210
211
class TranslatableModelFormMetaclass(ModelFormMetaclass):
212
    """
213
    Meta class to add translated form fields to the form.
214
    """
215
    def __new__(mcs, name, bases, attrs):
216
        # Before constructing class, fetch attributes from bases list.
217
        form_meta = _get_mro_attribute(bases, '_meta')
218
        form_base_fields = _get_mro_attribute(bases, 'base_fields', {})  # set by previous class level.
219
220
        if form_meta:
221
            # Not declaring the base class itself, this is a subclass.
222
223
            # Read the model from the 'Meta' attribute. This even works in the admin,
224
            # as `modelform_factory()` includes a 'Meta' attribute.
225
            # The other options can be read from the base classes.
226
            form_new_meta = attrs.get('Meta', form_meta)
227
            form_model = form_new_meta.model if form_new_meta else form_meta.model
228
229
            # Detect all placeholders at this class level.
230
            placeholder_fields = [
231
                f_name for f_name, attr_value in six.iteritems(attrs) if isinstance(attr_value, TranslatedField)
232
            ]
233
234
            # Include the translated fields as attributes, pretend that these exist on the form.
235
            # This also works when assigning `form = TranslatableModelForm` in the admin,
236
            # since the admin always uses modelform_factory() on the form class, and therefore triggering this metaclass.
237
            if form_model:
238
                for translations_model in form_model._parler_meta.get_all_models():
239
                    fields = getattr(form_new_meta, 'fields', form_meta.fields)
240
                    exclude = getattr(form_new_meta, 'exclude', form_meta.exclude) or ()
241
                    widgets = getattr(form_new_meta, 'widgets', form_meta.widgets) or ()
242
                    formfield_callback = attrs.get('formfield_callback', None)
243
244
                    if fields == '__all__':
245
                        fields = None
246
247
                    for f_name in translations_model.get_translated_fields():
248
                        # Add translated field if not already added, and respect exclude options.
249
                        if f_name in placeholder_fields:
250
                            # The TranslatedField placeholder can be replaced directly with actual field, so do that.
251
                            attrs[f_name] = _get_model_form_field(translations_model, f_name, formfield_callback=formfield_callback, **attrs[f_name].kwargs)
252
253
                        # The next code holds the same logic as fields_for_model()
254
                        # The f.editable check happens in _get_model_form_field()
255
                        elif f_name not in form_base_fields \
256
                         and (fields is None or f_name in fields) \
257
                         and f_name not in exclude \
258
                         and not f_name in attrs:
259
                            # Get declared widget kwargs
260
                            if f_name in widgets:
261
                                # Not combined with declared fields (e.g. the TranslatedField placeholder)
262
                                kwargs = {'widget': widgets[f_name]}
263
                            else:
264
                                kwargs = {}
265
266
                            # See if this formfield was previously defined using a TranslatedField placeholder.
267
                            placeholder = _get_mro_attribute(bases, f_name)
268
                            if placeholder and isinstance(placeholder, TranslatedField):
269
                                kwargs.update(placeholder.kwargs)
270
271
                            # Add the form field as attribute to the class.
272
                            formfield = _get_model_form_field(translations_model, f_name, formfield_callback=formfield_callback, **kwargs)
273
                            if formfield is not None:
274
                                attrs[f_name] = formfield
275
276
        # Call the super class with updated `attrs` dict.
277
        return super(TranslatableModelFormMetaclass, mcs).__new__(mcs, name, bases, attrs)
278
279
280
def _get_mro_attribute(bases, name, default=None):
281
    for base in bases:
282
        try:
283
            return getattr(base, name)
284
        except AttributeError:
285
            continue
286
    return default
287
288
289
def _get_model_form_field(model, name, formfield_callback=None, **kwargs):
290
    """
291
    Utility to create the formfield from a model field.
292
    When a field is not editable, a ``None`` will be returned.
293
    """
294
    field = model._meta.get_field(name)
295
    if not field.editable:  # see fields_for_model() logic in Django.
296
        return None
297
298
    # Apply admin formfield_overrides
299
    if formfield_callback is None:
300
        formfield = field.formfield(**kwargs)
301
    elif not callable(formfield_callback):
302
        raise TypeError('formfield_callback must be a function or callable')
303
    else:
304
        formfield = formfield_callback(field, **kwargs)
305
306
    return formfield
307
308
309
if django.VERSION < (1, 5):
310
    # Django 1.4 doesn't recognize the use of with_metaclass.
311
    # This breaks the form initialization in modelform_factory()
312
    class TranslatableModelForm(BaseTranslatableModelForm, forms.ModelForm):
313
        __metaclass__ = TranslatableModelFormMetaclass
314
else:
315
    class TranslatableModelForm(compat.with_metaclass(TranslatableModelFormMetaclass, BaseTranslatableModelForm, forms.ModelForm)):
316
        """
317
        The model form to use for translated models.
318
        """
319
320
    # six.with_metaclass does not handle more than 2 parent classes for django < 1.6
321
    # but we need all of them in django 1.7 to pass check admin.E016:
322
    #       "The value of 'form' must inherit from 'BaseModelForm'"
323
    # so we use our copied version in parler.utils.compat
324
    #
325
    # Also, the class must inherit from ModelForm,
326
    # or the ModelFormMetaclass will skip initialization.
327
    # It only adds the _meta from anything that extends ModelForm.
328
329
330
class TranslatableBaseInlineFormSet(BaseInlineFormSet):
331
    """
332
    The formset base for creating inlines with translatable models.
333
    """
334
    language_code = None
335
336
    def _construct_form(self, i, **kwargs):
337
        form = super(TranslatableBaseInlineFormSet, self)._construct_form(i, **kwargs)
338
        form.language_code = self.language_code   # Pass the language code for new objects!
339
        return form
340
341
    def save_new(self, form, commit=True):
342
        obj = super(TranslatableBaseInlineFormSet, self).save_new(form, commit)
343
        return obj
344
345
346
# Backwards compatibility
347
TranslatableModelFormMixin = BaseTranslatableModelForm
348