# Path: easymode/utils/languagecode.py
# def fix_language_code(url, current_language):
#     """
#     Fixes the language code as follows:
#     
#     If there is only one language used in the site, it strips the language code.
#     If there are more languages used, it will make sure that the url has the current
#     language as a prefix.
#     
#     >>> # multiple languages defined
#     >>> settings.LANGUAGES = (('en-us','English'),('de','German'),('nl-be','Belgium dutch'),('fr-be','Belgium french'),)
#     >>> settings.USE_SHORT_LANGUAGE_CODES = False
#     >>> activate('en-us')
#     >>> fix_language_code('/de/example.html', 'en-us')
#     '/en-us/example.html'
#     >>> settings.USE_SHORT_LANGUAGE_CODES = True
#     >>> fix_language_code('/de/example.html', 'en-us')
#     '/en/example.html'
#     >>> fix_language_code('/en/example.html', 'en-us')
#     '/example.html'
#     >>> # only one language defined
#     >>> settings.LANGUAGES = (('en-us', 'English'), )
#     >>> fix_language_code('/en-us/example.html', 'en-us')
#     '/example.html'
#     
#     :param url: The (absolute) url to be fixed eg. '/hi/how/is/it'.
#     :param current_language: A language code defined in ``settings.LANGUAGES``.
#     :rtype: The fixed url.
#     """
#     stripped_url = strip_language_code(url)
#     if not getattr(settings, 'MASTER_SITE', False) and len(settings.LANGUAGES) == 1:
#         # no MASTER_SITE and only one language, do not add language code to url
#         return stripped_url
#     
#     # add the language code to the url        
#     return u"/%s%s" % (get_shorthand_from_language_code(current_language), stripped_url)


from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils.translation import get_language, activate
from easymode.tests.testcases import initdb
from easymode.utils.languagecode import fix_language_code
            
            bottom_url = reverse('admin:tests_bottommodel_add')
            data = {
                'top':1,
                'comment':'Chicken is good',
            }
            response = self.client.post(bottom_url, data)
            self.assertEqual(response.status_code, 302)
            
            response = self.client.get(top_url)
            self.assertContains(response, 'Chicken is good')
        else:
            self.fail('Can not log in with admin:admin')
    
    def test_the_plus_button_goes_to_the_correct_add_view(self):
        "There should be a plus button for the inline group that goes to the correct add view"
        if self.client.login(username='admin', password='admin'):
            top_url = reverse('admin:tests_topmodel_change', args=[1])
            response = self.client.get(top_url)
            self.assertEqual(response.status_code, 200)
            
            #/en/admin/tests/bottommodel/add/?top=1
            add_url = "%s?top=1" % reverse('admin:tests_bottommodel_add')
            localized_add_url = fix_language_code(add_url, get_language())
            
            self.assertContains(response, ADD_BUTTON % localized_add_url)
            
            # test the same for a different locale
            activate('de')
            self.assertEqual('de', get_language())

# Path: easymode/utils/languagecode.py
# def fix_language_code(url, current_language):
#     """
#     Fixes the language code as follows:
#     
#     If there is only one language used in the site, it strips the language code.
#     If there are more languages used, it will make sure that the url has the current
#     language as a prefix.
#     
#     >>> # multiple languages defined
#     >>> settings.LANGUAGES = (('en-us','English'),('de','German'),('nl-be','Belgium dutch'),('fr-be','Belgium french'),)
#     >>> settings.USE_SHORT_LANGUAGE_CODES = False
#     >>> activate('en-us')
#     >>> fix_language_code('/de/example.html', 'en-us')
#     '/en-us/example.html'
#     >>> settings.USE_SHORT_LANGUAGE_CODES = True
#     >>> fix_language_code('/de/example.html', 'en-us')
#     '/en/example.html'
#     >>> fix_language_code('/en/example.html', 'en-us')
#     '/example.html'
#     >>> # only one language defined
#     >>> settings.LANGUAGES = (('en-us', 'English'), )
#     >>> fix_language_code('/en-us/example.html', 'en-us')
#     '/example.html'
#     
#     :param url: The (absolute) url to be fixed eg. '/hi/how/is/it'.
#     :param current_language: A language code defined in ``settings.LANGUAGES``.
#     :rtype: The fixed url.
#     """
#     stripped_url = strip_language_code(url)
#     if not getattr(settings, 'MASTER_SITE', False) and len(settings.LANGUAGES) == 1:
#         # no MASTER_SITE and only one language, do not add language code to url
#         return stripped_url
#     
#     # add the language code to the url        
#     return u"/%s%s" % (get_shorthand_from_language_code(current_language), stripped_url)


# Path: easymode/tests/testcases/testrelatedadmin.py
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils.translation import get_language, activate
from easymode.tests.testcases import initdb
from easymode.utils.languagecode import fix_language_code

            
            bottom_url = reverse('admin:tests_bottommodel_add')
            data = {
                'top':1,
                'comment':'Chicken is good',
            }
            response = self.client.post(bottom_url, data)
            self.assertEqual(response.status_code, 302)
            
            response = self.client.get(top_url)
            self.assertContains(response, 'Chicken is good')
        else:
            self.fail('Can not log in with admin:admin')
    
    def test_the_plus_button_goes_to_the_correct_add_view(self):
        "There should be a plus button for the inline group that goes to the correct add view"
        if self.client.login(username='admin', password='admin'):
            top_url = reverse('admin:tests_topmodel_change', args=[1])
            response = self.client.get(top_url)
            self.assertEqual(response.status_code, 200)
            
            #/en/admin/tests/bottommodel/add/?top=1
            add_url = "%s?top=1" % reverse('admin:tests_bottommodel_add')
            localized_add_url = fix_language_code(add_url, get_language())
            
            self.assertContains(response, ADD_BUTTON % localized_add_url)
            
            # test the same for a different locale
            activate('de')
            self.assertEqual('de', get_language())

            localized_add_url = fix_language_code(add_url, get_language())