def _get_test_value(): """Gets a :see:LocalizedValue instance for testing.""" value = LocalizedValue() for lang_code, lang_name in settings.LANGUAGES: value.set(lang_code, "<script>%s</script>" % lang_name) return value
def test_set(): """Tests whether the :see:LocalizedValue class's see:set function works properly.""" localized_value = LocalizedValue() for language, value in get_init_values(): localized_value.set(language, value) assert localized_value.get(language) == value assert getattr(localized_value, language) == value
def test_decompress(): """Tests whether a :see:LocalizedValue instance can correctly be "decompressed" over the available widgets.""" localized_value = LocalizedValue() for lang_code, lang_name in settings.LANGUAGES: localized_value.set(lang_code, lang_name) widget = LocalizedFieldWidget() decompressed_values = widget.decompress(localized_value) for (lang_code, _), value in zip(settings.LANGUAGES, decompressed_values): assert localized_value.get(lang_code) == value
def get_prep_value(self, value): """Returns field's value prepared for saving into a database.""" if isinstance(value, LocalizedValue): prep_value = LocalizedValue() for k, v in value.__dict__.items(): if v is None: prep_value.set(k, "") else: # Need to convert File objects provided via a form to # unicode for database insertion prep_value.set(k, str(v)) return super().get_prep_value(prep_value) return super().get_prep_value(value)