Skip to content

Commit

Permalink
Merge pull request #363 from matthiask/master
Browse files Browse the repository at this point in the history
Slightly reformat code to avoid flake8 errors
  • Loading branch information
zlorf committed Feb 20, 2016
2 parents c2465c3 + dad7cc7 commit f0503c2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
29 changes: 17 additions & 12 deletions modeltranslation/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
from django.contrib.admin.options import BaseModelAdmin, flatten_fieldsets, InlineModelAdmin
from django import forms

from modeltranslation import settings as mt_settings
from modeltranslation.translator import translator
from modeltranslation.utils import (
get_translation_fields, build_css_class, build_localized_fieldname, get_language,
get_language_bidi, unique)
from modeltranslation.widgets import ClearableWidgetWrapper

# Ensure that models are registered for translation before TranslationAdmin
# runs. The import is supposed to resolve a race condition between model import
# and translation registration in production (see issue #19).
Expand All @@ -16,12 +23,6 @@
else:
from django.contrib.contenttypes.admin import GenericTabularInline
from django.contrib.contenttypes.admin import GenericStackedInline
from modeltranslation import settings as mt_settings
from modeltranslation.translator import translator
from modeltranslation.utils import (
get_translation_fields, build_css_class, build_localized_fieldname, get_language,
get_language_bidi, unique)
from modeltranslation.widgets import ClearableWidgetWrapper


class TranslationBaseModelAdmin(BaseModelAdmin):
Expand Down Expand Up @@ -74,8 +75,12 @@ def patch_translation_field(self, db_field, field, **kwargs):
b for b in form_class.__bases__ if b != NullCharField)
field.__class__ = type(
'Nullable%s' % form_class.__name__, (NullableField, form_class), {})
if ((db_field.empty_value == 'both' or orig_field.name in self.both_empty_values_fields)
and isinstance(field.widget, (forms.TextInput, forms.Textarea))):
if (
(
db_field.empty_value == 'both' or
orig_field.name in self.both_empty_values_fields
) and isinstance(field.widget, (forms.TextInput, forms.Textarea))
):
field.widget = ClearableWidgetWrapper(field.widget)
css_classes = field.widget.attrs.get('class', '').split(' ')
css_classes.append('mt')
Expand Down Expand Up @@ -271,15 +276,15 @@ def _group_fieldsets(self, fieldsets):
untranslated_fields = [
f.name for f in self.opts.fields if (
# Exclude the primary key field
f is not self.opts.auto_field
f is not self.opts.auto_field and
# Exclude non-editable fields
and f.editable
f.editable and
# Exclude the translation fields
and not hasattr(f, 'translated_field')
not hasattr(f, 'translated_field') and
# Honour field arguments. We rely on the fact that the
# passed fieldsets argument is already fully filtered
# and takes options like exclude into account.
and f.name in flattened_fieldsets
f.name in flattened_fieldsets
)
]
# TODO: Allow setting a label
Expand Down
20 changes: 11 additions & 9 deletions modeltranslation/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,9 @@ def test_indonesian(self):
self.assertNotEqual(field.attname, build_localized_fieldname(field.name, 'id'))

def assertQuerysetsEqual(self, qs1, qs2):
pk = lambda o: o.pk
def pk(o):
return o.pk

return self.assertEqual(sorted(qs1, key=pk), sorted(qs2, key=pk))


Expand Down Expand Up @@ -2459,16 +2461,16 @@ def test_q(self):
n.save()

self.assertEqual('en', get_language())
self.assertEqual(0, models.ManagerTestModel.objects.filter(Q(title='de')
| Q(pk=42)).count())
self.assertEqual(1, models.ManagerTestModel.objects.filter(Q(title='en')
| Q(pk=42)).count())
self.assertEqual(0, models.ManagerTestModel.objects.filter(Q(title='de') |
Q(pk=42)).count())
self.assertEqual(1, models.ManagerTestModel.objects.filter(Q(title='en') |
Q(pk=42)).count())

with override('de'):
self.assertEqual(1, models.ManagerTestModel.objects.filter(Q(title='de')
| Q(pk=42)).count())
self.assertEqual(0, models.ManagerTestModel.objects.filter(Q(title='en')
| Q(pk=42)).count())
self.assertEqual(1, models.ManagerTestModel.objects.filter(Q(title='de') |
Q(pk=42)).count())
self.assertEqual(0, models.ManagerTestModel.objects.filter(Q(title='en') |
Q(pk=42)).count())

def test_f(self):
"""Test if F queries are rewritten."""
Expand Down
9 changes: 6 additions & 3 deletions modeltranslation/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,12 @@ def register(self, model_or_iterable, opts_class=None, **options):
add_translation_fields(model, opts)

# Delete all fields cache for related model (parent and children)
related = ((f for f in model._meta.get_fields() if (f.one_to_many or f.one_to_one)
and f.auto_created) if NEW_RELATED_API
else model._meta.get_all_related_objects())
related = ((
f for f in model._meta.get_fields()
if (f.one_to_many or f.one_to_one) and
f.auto_created
) if NEW_RELATED_API else model._meta.get_all_related_objects())

for related_obj in related:
delete_cache_fields(related_obj.model)

Expand Down

0 comments on commit f0503c2

Please sign in to comment.