# Path: easymode/utils/languagecode.py
# def get_real_fieldname(field, lang):
#     """
#     Depending on the language a field can have a different name.
#     
#     If you want to do a query, using a localized field, you have
#     to use the name this function returns, not the default name.
#     
#     >>> get_real_fieldname('name', 'en-us')
#     'name_en_us'
#     
#     Usage::
#     
#         def index(request):
#             published_in_language = get_real_fieldname('published', request.LANGUAGE_CODE)
#             qs = SomeModel.objects.filter(**{published_in_language:True})
#             return render_to_response('index.html', {'queryset':qs})
#     
#     
#     :param field: The name of a field in an internationalised model.
#     :param lang: The language for which you want to know the real name.
#     :rtype: The actual name of the ``field`` in the ``lang``.
#     """
#     return str('%s_%s' % (field, to_locale(lang).lower()))


from django.conf import settings
from django.core.management.base import BaseCommand
from django.db.models import get_models, get_app, get_model
from django.utils import translation
from django.utils.encoding import force_unicode
from easymode.utils.languagecode import get_real_fieldname
"""



class Command(BaseCommand):
    help = __doc__
    
    def handle(self, source, target, app=None, **options):
        """ command execution """
        translation.activate(settings.LANGUAGE_CODE)
        if app:
            unpack = app.split('.')

            if len(unpack) == 2:
                models = [get_model(unpack[0], unpack[1])]
            elif len(unpack) == 1:
                models = get_models(get_app(unpack[0]))
        else:
            models = get_models()
            
        for model in models:
            if hasattr(model, 'localized_fields'):

                model_full_name = '%s.%s' % (model._meta.app_label, model._meta.module_name)
                update_instances = set()
                messages = []
                
                for instance in model.objects.all():
                    for field in model.localized_fields:
                        source_field = get_real_fieldname(field, source)

# Path: easymode/utils/languagecode.py
# def get_real_fieldname(field, lang):
#     """
#     Depending on the language a field can have a different name.
#     
#     If you want to do a query, using a localized field, you have
#     to use the name this function returns, not the default name.
#     
#     >>> get_real_fieldname('name', 'en-us')
#     'name_en_us'
#     
#     Usage::
#     
#         def index(request):
#             published_in_language = get_real_fieldname('published', request.LANGUAGE_CODE)
#             qs = SomeModel.objects.filter(**{published_in_language:True})
#             return render_to_response('index.html', {'queryset':qs})
#     
#     
#     :param field: The name of a field in an internationalised model.
#     :param lang: The language for which you want to know the real name.
#     :rtype: The actual name of the ``field`` in the ``lang``.
#     """
#     return str('%s_%s' % (field, to_locale(lang).lower()))


# Path: easymode/management/commands/easy_copy_language.py
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db.models import get_models, get_app, get_model
from django.utils import translation
from django.utils.encoding import force_unicode
from easymode.utils.languagecode import get_real_fieldname

"""



class Command(BaseCommand):
    help = __doc__
    
    def handle(self, source, target, app=None, **options):
        """ command execution """
        translation.activate(settings.LANGUAGE_CODE)
        if app:
            unpack = app.split('.')

            if len(unpack) == 2:
                models = [get_model(unpack[0], unpack[1])]
            elif len(unpack) == 1:
                models = get_models(get_app(unpack[0]))
        else:
            models = get_models()
            
        for model in models:
            if hasattr(model, 'localized_fields'):

                model_full_name = '%s.%s' % (model._meta.app_label, model._meta.module_name)
                update_instances = set()
                messages = []
                
                for instance in model.objects.all():
                    for field in model.localized_fields:
                        source_field = get_real_fieldname(field, source)

                        target_field = get_real_fieldname(field, target)