mirror of
https://github.com/Hopiu/django-modeltranslation.git
synced 2026-03-16 22:10:31 +00:00
Added unittest for FileField and ImageField and incorporated a small fix for the pre_save method of TranslationField (thanks to Bruno Tavares).
This commit is contained in:
parent
ee4e8a4b64
commit
a3db814551
3 changed files with 82 additions and 18 deletions
|
|
@ -3,7 +3,7 @@ from django.core.exceptions import ImproperlyConfigured
|
|||
from django.db.models.fields import CharField, TextField
|
||||
from django.db.models.fields.files import FileField, ImageField
|
||||
|
||||
from modeltranslation.settings import CUSTOM_FIELDS, DEFAULT_LANGUAGE
|
||||
from modeltranslation import settings as mt_settings
|
||||
from modeltranslation.utils import (get_language,
|
||||
build_localized_fieldname,
|
||||
build_localized_verbose_name)
|
||||
|
|
@ -28,9 +28,9 @@ def create_translation_field(model, field_name, lang):
|
|||
field = model._meta.get_field(field_name)
|
||||
cls_name = field.__class__.__name__
|
||||
if not (isinstance(field, SUPPORTED_FIELDS) or
|
||||
cls_name in CUSTOM_FIELDS):
|
||||
raise ImproperlyConfigured('%s is not supported by '
|
||||
'modeltranslation.' % cls_name)
|
||||
cls_name in mt_settings.CUSTOM_FIELDS):
|
||||
raise ImproperlyConfigured(
|
||||
'%s is not supported by modeltranslation.' % cls_name)
|
||||
translation_class = field_factory(field.__class__)
|
||||
return translation_class(translated_field=field, language=lang)
|
||||
|
||||
|
|
@ -89,9 +89,9 @@ class TranslationField(object):
|
|||
translated_field.verbose_name, language)
|
||||
|
||||
def pre_save(self, model_instance, add):
|
||||
val = super(self.translated_field.__class__, self).pre_save(
|
||||
model_instance, add)
|
||||
if DEFAULT_LANGUAGE == self.language and not add:
|
||||
val = self.translated_field.__class__.pre_save(
|
||||
self, model_instance, add)
|
||||
if mt_settings.DEFAULT_LANGUAGE == self.language and not add:
|
||||
# Rule is: 3. Assigning a value to a translation field of the
|
||||
# default language also updates the original field
|
||||
model_instance.__dict__[self.translated_field.attname] = val
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
from warnings import warn
|
||||
|
||||
from django.conf import settings
|
||||
|
|
@ -26,14 +25,8 @@ if DEFAULT_LANGUAGE and DEFAULT_LANGUAGE not in AVAILABLE_LANGUAGES:
|
|||
elif not DEFAULT_LANGUAGE:
|
||||
DEFAULT_LANGUAGE = AVAILABLE_LANGUAGES[0]
|
||||
|
||||
# FIXME: We can't seem to override this particular setting in tests.py
|
||||
# Load allowed CUSTOM_FIELDS from django settings
|
||||
CUSTOM_FIELDS = getattr(settings, 'MODELTRANSLATION_CUSTOM_FIELDS', ())
|
||||
try:
|
||||
if sys.argv[1] == 'test':
|
||||
CUSTOM_FIELDS = getattr(
|
||||
settings, 'MODELTRANSLATION_CUSTOM_FIELDS', ('BooleanField',))
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
# Don't change this setting unless you really know what you are doing
|
||||
ENABLE_REGISTRATIONS = getattr(
|
||||
|
|
|
|||
|
|
@ -10,16 +10,17 @@ from django.conf import settings
|
|||
from django.contrib.admin.sites import AdminSite
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.files.base import ContentFile
|
||||
from django.db import models
|
||||
from django.test import TestCase
|
||||
from django.utils.translation import get_language
|
||||
from django.utils.translation import trans_real
|
||||
from django.utils.translation import ugettext_lazy
|
||||
|
||||
from modeltranslation import settings as mt_settings
|
||||
from modeltranslation import translator
|
||||
from modeltranslation.admin import (TranslationAdmin,
|
||||
TranslationStackedInline)
|
||||
from modeltranslation.tests.settings import DEFAULT_LANGUAGE
|
||||
|
||||
# None of the following tests really depend on the content of the request,
|
||||
# so we'll just pass in None.
|
||||
|
|
@ -80,6 +81,19 @@ translator.translator.register(TestModelWithFallback2,
|
|||
TestTranslationOptionsWithFallback2)
|
||||
|
||||
|
||||
class TestModelWithFileFields(models.Model):
|
||||
title = models.CharField(ugettext_lazy('title'), max_length=255)
|
||||
file = models.FileField(upload_to='test', null=True, blank=True)
|
||||
image = models.ImageField(upload_to='test', null=True, blank=True)
|
||||
|
||||
|
||||
class TestTranslationOptionsModelWithFileFields(translator.TranslationOptions):
|
||||
fields = ('title', 'file', 'image')
|
||||
|
||||
translator.translator.register(TestModelWithFileFields,
|
||||
TestTranslationOptionsModelWithFileFields)
|
||||
|
||||
|
||||
class ModeltranslationTestBase(TestCase):
|
||||
urls = 'modeltranslation.tests.urls'
|
||||
|
||||
|
|
@ -104,7 +118,7 @@ class ModeltranslationTest(ModeltranslationTestBase):
|
|||
self.failUnless(translator.translator)
|
||||
|
||||
# Check that eight models are registered for translation
|
||||
self.failUnlessEqual(len(translator.translator._registry), 8)
|
||||
self.failUnlessEqual(len(translator.translator._registry), 9)
|
||||
|
||||
# Try to unregister a model that is not registered
|
||||
self.assertRaises(translator.NotRegistered,
|
||||
|
|
@ -220,6 +234,63 @@ class ModeltranslationTest(ModeltranslationTestBase):
|
|||
TestTranslationOptionsWithFallback2.fallback_values['text'])
|
||||
|
||||
|
||||
class ModeltranslationWithFileFields(ModeltranslationTestBase):
|
||||
def test_translated_models(self):
|
||||
# First create an instance of the test model to play with
|
||||
inst = TestModelWithFileFields.objects.create(
|
||||
title="Testtitle", file=None)
|
||||
field_names = dir(inst)
|
||||
self.failUnless('id' in field_names)
|
||||
self.failUnless('title' in field_names)
|
||||
self.failUnless('title_de' in field_names)
|
||||
self.failUnless('title_en' in field_names)
|
||||
self.failUnless('file' in field_names)
|
||||
self.failUnless('file_de' in field_names)
|
||||
self.failUnless('file_en' in field_names)
|
||||
inst.delete()
|
||||
|
||||
def test_translated_models(self):
|
||||
f_en = ContentFile("Just a really good file")
|
||||
inst = TestModelWithFileFields(title="Testtitle", file=None)
|
||||
|
||||
trans_real.activate("en")
|
||||
inst.title = 'title_en'
|
||||
|
||||
inst.file = 'a_en'
|
||||
inst.file.save('b_en', ContentFile('file in english'))
|
||||
|
||||
inst.image = 'i_en.jpg'
|
||||
inst.image.save('i_en.jpg', ContentFile('image in english'))
|
||||
|
||||
trans_real.activate("de")
|
||||
inst.title = 'title_de'
|
||||
|
||||
inst.file = 'a_de'
|
||||
inst.file.save('b_de', ContentFile('file in german'))
|
||||
|
||||
inst.image = 'i_de.jpg'
|
||||
inst.image.save('i_de.jpg', ContentFile('image in germany'))
|
||||
|
||||
inst.save()
|
||||
|
||||
trans_real.activate("en")
|
||||
|
||||
self.failUnlessEqual(inst.title, 'title_en')
|
||||
self.failUnless(inst.file.name.count('b_en') > 0)
|
||||
self.failUnless(inst.image.name.count('i_en') > 0)
|
||||
|
||||
trans_real.activate("de")
|
||||
self.failUnlessEqual(inst.title, 'title_de')
|
||||
self.failUnless(inst.file.name.count('b_de') > 0)
|
||||
self.failUnless(inst.image.name.count('i_de') > 0)
|
||||
|
||||
inst.file_en.delete()
|
||||
inst.image_en.delete()
|
||||
inst.file_de.delete()
|
||||
inst.image_de.delete()
|
||||
|
||||
inst.delete()
|
||||
|
||||
class ModeltranslationTestRule1(ModeltranslationTestBase):
|
||||
"""
|
||||
Rule 1: Reading the value from the original field returns the value in
|
||||
|
|
@ -441,7 +512,7 @@ class ModeltranslationTestRule3(ModeltranslationTestBase):
|
|||
title1_en = "title en"
|
||||
n = TestModel.objects.create(title_de=title1_de, title_en=title1_en)
|
||||
self.failUnlessEqual(get_language(), 'de')
|
||||
self.failUnlessEqual(DEFAULT_LANGUAGE, 'de')
|
||||
self.failUnlessEqual(mt_settings.DEFAULT_LANGUAGE, 'de')
|
||||
self.failUnlessEqual(n.title, title1_de)
|
||||
self.failUnlessEqual(n.title_de, title1_de)
|
||||
self.failUnlessEqual(n.title_en, title1_en)
|
||||
|
|
|
|||
Loading…
Reference in a new issue