Skip to content

Commit

Permalink
Fix style issues and dangling imports
Browse files Browse the repository at this point in the history
  • Loading branch information
spectras committed Feb 18, 2017
1 parent 23832ee commit 6820405
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 44 deletions.
8 changes: 8 additions & 0 deletions hvad/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
from hvad.utils import load_translation
from hvad.manager import TranslationQueryset

__all__ = (
'TranslatableAdmin',
'TranslatableInlineModelAdmin',
'TranslatableStackedInline',
'TranslatableTabularInline',
'InlineModelForm',
)


class InlineModelForm(TranslatableModelForm):
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
Expand Down
1 change: 1 addition & 0 deletions hvad/contrib/restframework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
TranslationsMixin, TranslatableModelSerializer, HyperlinkedTranslatableModelSerializer,
NestedTranslationSerializer,
)

__all__ = (
'TranslationsMixin',
'TranslatableModelSerializer',
Expand Down
14 changes: 10 additions & 4 deletions hvad/contrib/restframework/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db.models.fields import FieldDoesNotExist
from django.utils.translation import get_language, ugettext_lazy as _l
from django.utils.translation import get_language, ugettext_lazy as _
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.fields import SkipField
Expand All @@ -8,8 +8,14 @@
from hvad.contrib.restframework.utils import TranslationListSerializer
from collections import OrderedDict

veto_fields = ('id', 'master')
__all__ = (
'TranslationsMixin',
'TranslatableModelSerializer',
'HyperlinkedTranslatableModelSerializer',
'NestedTranslationSerializer',
)

veto_fields = ('id', 'master')

#=============================================================================

Expand Down Expand Up @@ -152,8 +158,8 @@ def update_translation(self, instance, data):
class TranslatableModelMixin(object):
''' Adds support for translated fields on a serializer '''
default_error_messages = {
'enforce_violation': _l('Sending a language_code is invalid on serializers '
'that enforce a language'),
'enforce_violation': _('Sending a language_code is invalid on serializers '
'that enforce a language'),
}

def __init__(self, *args, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions hvad/contrib/restframework/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.utils.translation import ugettext_lazy as _l
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.settings import api_settings
Expand All @@ -10,8 +10,8 @@ class TranslationListSerializer(serializers.ListSerializer):
'A custom serializer to output translations in a nice dict'
many = True
default_error_messages = {
'not_a_dict': _l('Expected a dictionary of items, but got a {input_type}.'),
'no_translation': _l('At least one translation must be provided.'),
'not_a_dict': _('Expected a dictionary of items, but got a {input_type}.'),
'no_translation': _('At least one translation must be provided.'),
}

def to_internal_value(self, data):
Expand Down
3 changes: 3 additions & 0 deletions hvad/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from hvad.settings import hvad_settings
from hvad.utils import get_translation, set_cached_translation

__all__ = ()


class TranslatedAttribute(object):
""" Proxy descriptor, forwarding attribute access to loaded translation.
If no translation is loaded, it will attempt to load one depending on settings
Expand Down
23 changes: 22 additions & 1 deletion hvad/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
__all__ = ('WrongManager', )

class WrongManager(Exception):
pass
""" Raised when attempting to introspect translated fields from
shared models without going through hvad. The most likely cause
for this being accessing translated fields from
translation-unaware QuerySets.
"""
def __init__(self, meta, name):
self.meta = meta
self.name = name

def __str__(self):
return (
"Accessing translated fields like {model_name}.{field_name} from "
"an regular model requires a translation-aware queryset, "
"obtained with the .language() method. "
"For regular, non-translatable models, you can get one using "
"hvad.utils.get_translation_aware_manager"
).format(
app_label=self.meta.app_label,
model_name=self.meta.model_name,
field_name=self.name,
)
11 changes: 10 additions & 1 deletion hvad/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
from hvad.utils import (set_cached_translation, get_cached_translation, load_translation)
from collections import OrderedDict

__all__ = (
'TranslatableModelForm',
'BaseTranslationFormSet',
'translatable_modelform_factory',
'translatable_modelformset_factory',
'translatable_inlineformset_factory',
'translationformset_factory',
)

veto_fields = {'id', 'master', 'master_id', 'language_code'}

#=============================================================================
Expand Down Expand Up @@ -56,7 +65,7 @@ def __new__(cls, name, bases, attrs):
tfields = fields_for_model(
model._meta.translations_model,
fields=fields,
exclude=exclude | set(veto_fields),
exclude=exclude | veto_fields,
widgets=getattr(meta, 'widgets', None),
formfield_callback=attrs.get('formfield_callback')
)
Expand Down
6 changes: 1 addition & 5 deletions hvad/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@
from hvad.settings import hvad_settings
from hvad.utils import combine
from copy import deepcopy
import logging
import sys

#===============================================================================

# Logging-related globals
_logger = logging.getLogger(__name__)
__all__ = ('TranslationQueryset', 'TranslationManager')

#===============================================================================

Expand Down
2 changes: 2 additions & 0 deletions hvad/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from itertools import chain
import sys

__all__ = ('TranslatableModel', 'TranslatedFields', 'NoTranslation')

#===============================================================================

class TranslatedFields(object):
Expand Down
2 changes: 2 additions & 0 deletions hvad/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.db.models.sql.where import WhereNode, AND
from collections import namedtuple

__all__ = ()

#===============================================================================
# Generators abstracting walking through internal django structures

Expand Down
2 changes: 2 additions & 0 deletions hvad/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.utils.functional import SimpleLazyObject, empty
from collections import namedtuple

__all__ = ('hvad_settings', )

#===============================================================================

_default_settings = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from hvad.test_utils.project.alternate_models_app.models.normal import NormalAlternate

__all__ = (
'NormalAlternate',
)
1 change: 0 additions & 1 deletion hvad/tests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ def test_admin_change_popup(self):
IS_POPUP_VAR: '1',
}
response = self.client.post(url, data)
expected_url = reverse('admin:app_normal_changelist')
self.assertIn(response.status_code, [200, 302], response.content)
obj = Normal.objects.language('en').get(pk=self.normal_id[1])
self.assertEqual(obj.translated_field, "English NEW")
Expand Down
14 changes: 7 additions & 7 deletions hvad/tests/contrib/restframework.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def test_create(self):
obj = serializer.save()
self.assertIsNot(obj.pk, None)
qs = Normal.objects.language('all').filter(pk=obj.pk)
self.assertCountEqual([(obj.language_code, obj.translated_field) for obj in qs],
self.assertCountEqual([(item.language_code, item.translated_field) for item in qs],
[('en', 'English'), ('sr', u'српски')])

def test_update(self):
Expand All @@ -408,7 +408,7 @@ def test_update(self):
obj = serializer.save()
self.assertEqual(obj.pk, self.normal_id[1])
qs = Normal.objects.language('all').filter(pk=self.normal_id[1])
self.assertCountEqual([(obj.language_code, obj.translated_field) for obj in qs],
self.assertCountEqual([(item.language_code, item.translated_field) for item in qs],
[('en', 'English'), ('sr', u'српски')])

def test_update_partial(self):
Expand All @@ -424,7 +424,7 @@ def test_update_partial(self):
obj = serializer.save()
self.assertEqual(obj.pk, self.normal_id[1])
qs = Normal.objects.language('all').filter(pk=self.normal_id[1], shared_field='shared')
self.assertCountEqual([obj.language_code for obj in qs], self.translations)
self.assertCountEqual([item.language_code for item in qs], self.translations)

def test_update_translated_many(self):
'Update an existing instance, with an excluded translated M2M field'
Expand Down Expand Up @@ -509,7 +509,7 @@ def test_create_translations(self):
obj = serializer.save()
self.assertIsNot(obj.pk, None)
qs = Normal.objects.language('all').filter(pk=obj.pk)
self.assertCountEqual([(obj.language_code, obj.translated_field) for obj in qs],
self.assertCountEqual([(item.language_code, item.translated_field) for item in qs],
[('en', 'English'), ('sr', u'српски')])

def test_create_translatable(self):
Expand All @@ -525,7 +525,7 @@ def test_create_translatable(self):
obj = serializer.save()
self.assertIsNot(obj.pk, None)
qs = Normal.objects.language('all').filter(pk=obj.pk)
self.assertCountEqual([(obj.language_code, obj.translated_field) for obj in qs],
self.assertCountEqual([(item.language_code, item.translated_field) for item in qs],
[('sr', u'српски')])

def test_update_translations(self):
Expand All @@ -545,7 +545,7 @@ def test_update_translations(self):
obj = serializer.save()
self.assertEqual(obj.pk, self.normal_id[1])
qs = Normal.objects.language('all').filter(pk=self.normal_id[1])
self.assertCountEqual([(obj.language_code, obj.translated_field) for obj in qs],
self.assertCountEqual([(item.language_code, item.translated_field) for item in qs],
[('en', 'English'), ('sr', u'српски')])

def test_update_translatable(self):
Expand All @@ -562,7 +562,7 @@ def test_update_translatable(self):
obj = serializer.save()
self.assertEqual(obj.pk, self.normal_id[1])
qs = Normal.objects.language('all').filter(pk=self.normal_id[1])
self.assertCountEqual([(obj.language_code, obj.translated_field) for obj in qs],
self.assertCountEqual([(item.language_code, item.translated_field) for item in qs],
[('en', NORMAL[1].translated_field['en']),
('ja', NORMAL[1].translated_field['ja']),
('sr', u'српски')])
5 changes: 0 additions & 5 deletions hvad/tests/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,7 @@ def test_cached_autoload(self):

def test_cached_no_autoload(self):
with self.settings(HVAD={'AUTOLOAD_TRANSLATIONS': False}):
# get the english instance
en = Normal.objects.untranslated().prefetch_related('translations').get()
with self.assertNumQueries(0):
ja_trans = en.translations.get_language('ja')

# get the japanese *combined*
ja = Normal.objects.language('ja').get(pk=en.pk)

self.assertEqual(en.shared_field, NORMAL[1].shared_field)
Expand Down
3 changes: 1 addition & 2 deletions hvad/tests/related.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ def test_lookup_by_translated_field_requires_translation_aware_manager(self):

def test_lookup_by_non_existing_field(self):
with translation.override('en'):
self.assertRaises(TypeError, Standard.objects.get,
normal__non_existing_field=1)
self.assertRaises(TypeError, Standard.objects.get, normal__non_existing_field=1)

def test_lookup_by_translated_field_using_q_objects(self):
en = Normal.objects.language('en').get(pk=self.normal_id[1])
Expand Down
21 changes: 6 additions & 15 deletions hvad/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from django.utils.translation import get_language
from hvad.exceptions import WrongManager

__all__ = (
'get_translation_aware_manager',
)

#=============================================================================
# Translation manipulators

Expand Down Expand Up @@ -103,14 +107,7 @@ def __call__(self, meta, name):
except FieldDoesNotExist:
raise e
else:
raise WrongManager("To access translated fields like %r from "
"an untranslated model, you must use a "
"translation aware manager. For non-translatable "
"models, you can get one using "
"hvad.utils.get_translation_aware_manager.\n"
"For translatable models, use the language() "
"method."%
name)
raise WrongManager(meta, name)


class SmartGetField(object):
Expand All @@ -128,13 +125,7 @@ def __call__(self, meta, name, *args, **kwargs):
except FieldDoesNotExist:
raise e
else:
raise WrongManager("To access translated fields like %r from "
"an untranslated model, you must use a "
"translation aware manager. For non-translatable "
"models, you can get one using "
"hvad.utils.get_translation_aware_manager.\n"
"For translatable models, use the language() "
"method." % name)
raise WrongManager(meta, name)

#=============================================================================
# Internal sugar
Expand Down
2 changes: 2 additions & 0 deletions hvad/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.utils.translation import get_language
from hvad.forms import translatable_modelform_factory

__all__ = ('TranslatableCreateView', 'TranslatableUpdateView', 'TranslatableDeleteView')


class TranslatableModelFormMixin(ModelFormMixin):
''' ModelFormMixin that works with an TranslatableModelForm in **enforce** mode '''
Expand Down

0 comments on commit 6820405

Please sign in to comment.