# Path: modeltrans/manager.py
# def transform_translatable_fields(model, fields):
#     """
#     Transform the kwargs for a <Model>.objects.create() or <Model>()
#     to allow passing translated field names.
# 
#     Arguments:
#         fields (dict): kwargs to a model __init__ or Model.objects.create() method
#             for which the field names need to be translated to values in the i18n field
#     """
#     # If the current model does have the TranslationField, we must not apply
#     # any transformation for it will result in a:
#     # TypeError: 'i18n' is an invalid keyword argument for this function
#     if not hasattr(model, "i18n"):
#         return fields
# 
#     ret = {"i18n": fields.pop("i18n", None) or {}}
# 
#     # keep track of translated fields, and do not return an `i18n` key if no
#     # translated fields are found.
#     has_translated_fields = len(ret["i18n"].items()) > 0
# 
#     for field_name, value in fields.items():
#         try:
#             field = model._meta.get_field(field_name)
#         except FieldDoesNotExist:
#             ret[field_name] = value
#             continue
# 
#         if isinstance(field, TranslatedVirtualField):
#             has_translated_fields = True
#             if field.get_language() == get_default_language():
#                 if field.original_name in fields:
#                     raise ValueError(
#                         'Attempted override of "{}" with "{}". '
#                         "Only one of the two is allowed.".format(field.original_name, field_name)
#                     )
#                 ret[field.original_name] = value
#             else:
#                 ret["i18n"][field.name] = value
#         else:
#             ret[field_name] = value
# 
#     if not has_translated_fields:
#         return fields
# 
#     return ret
# 
# Path: modeltrans/utils.py
# def build_localized_fieldname(field_name, lang, ignore_default=False):
#     if lang == "id":
#         # The 2-letter Indonesian language code is problematic with the
#         # current naming scheme as Django foreign keys also add "id" suffix.
#         lang = "ind"
#     if ignore_default and lang == get_default_language():
#         return field_name
#     return "{}_{}".format(field_name, lang.replace("-", "_"))
# 
# def get_instance_field_value(instance, path):
#     """
#     Return the value of a field or related field for a model instance.
#     """
#     value = instance
#     for bit in path.split(LOOKUP_SEP):
#         try:
#             value = getattr(value, bit)
#         except AttributeError:
#             return None
# 
#     return value
# 
# def get_language():
#     """
#     Return an active language code that is guaranteed to be in settings.LANGUAGES
# 
#     (Django does not seem to guarantee this for us.)
#     """
#     lang = _get_language()
#     if lang in get_available_languages():
#         return lang
#     return get_default_language()
# 
# def get_model_field(model, path):
#     """
#     Return the django model field for model in context, following relations.
#     """
# 
#     if not hasattr(model, "_meta"):
#         raise ValueError("Argument 'Model' must be a django.db.models.Model instance.")
# 
#     field = None
#     for bit in path.split(LOOKUP_SEP):
#         try:
#             field = model._meta.get_field(bit)
#         except FieldDoesNotExist:
#             return None
# 
#         if hasattr(field, "remote_field"):
#             rel = getattr(field, "remote_field", None)
#             model = getattr(rel, "model", model)
# 
#     return field
# 
# def split_translated_fieldname(field_name):
#     _pos = field_name.rfind("_")
#     return (field_name[0:_pos], field_name[_pos + 1 :])
# 
# Path: tests/app/models.py
# class Blog(models.Model):
#     title = models.CharField(max_length=255)
#     body = models.TextField(null=True)
# 
#     category = models.ForeignKey(Category, null=True, blank=True, on_delete=models.CASCADE)
#     site = models.ForeignKey(Site, null=True, blank=True, on_delete=models.CASCADE)
# 
#     i18n = TranslationField(fields=("title", "body"), required_languages=("nl",))
# 
#     def __str__(self):
#         return self.title
# 
# class Category(models.Model):
# 
#     name = models.CharField(max_length=255)
#     title = models.CharField(max_length=255)
# 
#     i18n = TranslationField(fields=("name", "title"))
# 
#     objects = CategoryQueryset.as_manager()
# 
#     def __str__(self):
#         return self.name


from django.test import TestCase
from django.utils.translation import override
from modeltrans.manager import transform_translatable_fields
from modeltrans.utils import (
    build_localized_fieldname,
    get_instance_field_value,
    get_language,
    get_model_field,
    split_translated_fieldname,
)
from .app.models import Blog, Category




class UtilsTest(TestCase):
    def test_get_language(self):
        self.assertEqual(get_language(), "en")

        with override("nl"):
            self.assertEqual(get_language(), "nl")

        with override("id"):
            self.assertEqual(get_language(), "en")

    def test_split_translated_fieldname(self):
        self.assertEqual(split_translated_fieldname("title_nl"), ("title", "nl"))

        self.assertEqual(split_translated_fieldname("full_name_nl"), ("full_name", "nl"))

    def test_transform_translatable_fields(self):
        self.assertEqual(

# Path: modeltrans/manager.py
# def transform_translatable_fields(model, fields):
#     """
#     Transform the kwargs for a <Model>.objects.create() or <Model>()
#     to allow passing translated field names.
# 
#     Arguments:
#         fields (dict): kwargs to a model __init__ or Model.objects.create() method
#             for which the field names need to be translated to values in the i18n field
#     """
#     # If the current model does have the TranslationField, we must not apply
#     # any transformation for it will result in a:
#     # TypeError: 'i18n' is an invalid keyword argument for this function
#     if not hasattr(model, "i18n"):
#         return fields
# 
#     ret = {"i18n": fields.pop("i18n", None) or {}}
# 
#     # keep track of translated fields, and do not return an `i18n` key if no
#     # translated fields are found.
#     has_translated_fields = len(ret["i18n"].items()) > 0
# 
#     for field_name, value in fields.items():
#         try:
#             field = model._meta.get_field(field_name)
#         except FieldDoesNotExist:
#             ret[field_name] = value
#             continue
# 
#         if isinstance(field, TranslatedVirtualField):
#             has_translated_fields = True
#             if field.get_language() == get_default_language():
#                 if field.original_name in fields:
#                     raise ValueError(
#                         'Attempted override of "{}" with "{}". '
#                         "Only one of the two is allowed.".format(field.original_name, field_name)
#                     )
#                 ret[field.original_name] = value
#             else:
#                 ret["i18n"][field.name] = value
#         else:
#             ret[field_name] = value
# 
#     if not has_translated_fields:
#         return fields
# 
#     return ret
# 
# Path: modeltrans/utils.py
# def build_localized_fieldname(field_name, lang, ignore_default=False):
#     if lang == "id":
#         # The 2-letter Indonesian language code is problematic with the
#         # current naming scheme as Django foreign keys also add "id" suffix.
#         lang = "ind"
#     if ignore_default and lang == get_default_language():
#         return field_name
#     return "{}_{}".format(field_name, lang.replace("-", "_"))
# 
# def get_instance_field_value(instance, path):
#     """
#     Return the value of a field or related field for a model instance.
#     """
#     value = instance
#     for bit in path.split(LOOKUP_SEP):
#         try:
#             value = getattr(value, bit)
#         except AttributeError:
#             return None
# 
#     return value
# 
# def get_language():
#     """
#     Return an active language code that is guaranteed to be in settings.LANGUAGES
# 
#     (Django does not seem to guarantee this for us.)
#     """
#     lang = _get_language()
#     if lang in get_available_languages():
#         return lang
#     return get_default_language()
# 
# def get_model_field(model, path):
#     """
#     Return the django model field for model in context, following relations.
#     """
# 
#     if not hasattr(model, "_meta"):
#         raise ValueError("Argument 'Model' must be a django.db.models.Model instance.")
# 
#     field = None
#     for bit in path.split(LOOKUP_SEP):
#         try:
#             field = model._meta.get_field(bit)
#         except FieldDoesNotExist:
#             return None
# 
#         if hasattr(field, "remote_field"):
#             rel = getattr(field, "remote_field", None)
#             model = getattr(rel, "model", model)
# 
#     return field
# 
# def split_translated_fieldname(field_name):
#     _pos = field_name.rfind("_")
#     return (field_name[0:_pos], field_name[_pos + 1 :])
# 
# Path: tests/app/models.py
# class Blog(models.Model):
#     title = models.CharField(max_length=255)
#     body = models.TextField(null=True)
# 
#     category = models.ForeignKey(Category, null=True, blank=True, on_delete=models.CASCADE)
#     site = models.ForeignKey(Site, null=True, blank=True, on_delete=models.CASCADE)
# 
#     i18n = TranslationField(fields=("title", "body"), required_languages=("nl",))
# 
#     def __str__(self):
#         return self.title
# 
# class Category(models.Model):
# 
#     name = models.CharField(max_length=255)
#     title = models.CharField(max_length=255)
# 
#     i18n = TranslationField(fields=("name", "title"))
# 
#     objects = CategoryQueryset.as_manager()
# 
#     def __str__(self):
#         return self.name


# Path: tests/test_utils.py
from django.test import TestCase
from django.utils.translation import override
from modeltrans.manager import transform_translatable_fields
from modeltrans.utils import (
    build_localized_fieldname,
    get_instance_field_value,
    get_language,
    get_model_field,
    split_translated_fieldname,
)
from .app.models import Blog, Category





class UtilsTest(TestCase):
    def test_get_language(self):
        self.assertEqual(get_language(), "en")

        with override("nl"):
            self.assertEqual(get_language(), "nl")

        with override("id"):
            self.assertEqual(get_language(), "en")

    def test_split_translated_fieldname(self):
        self.assertEqual(split_translated_fieldname("title_nl"), ("title", "nl"))

        self.assertEqual(split_translated_fieldname("full_name_nl"), ("full_name", "nl"))

    def test_transform_translatable_fields(self):
        self.assertEqual(

            transform_translatable_fields(Blog, {"title": "bar", "title_nl": "foo"}),