# Path: feincms/extensions/translations.py
# def translation_set_language(request, select_language):
#     """
#     Set and activate a language, if that language is available.
#     """
# 
#     select_language = translation_allowed_language(select_language)
# 
#     if translation.check_for_language(select_language):
#         fallback = False
#     else:
#         # The page is in a language that Django has no messages for.
#         # We display anyhow, but fall back to primary language for
#         # other messages and other applications. It is *highly* recommended to
#         # create a new django.po for the language instead of
#         # using this behaviour.
#         select_language = django_settings.LANGUAGES[0][0]
#         fallback = True
# 
#     translation.activate(select_language)
#     request.LANGUAGE_CODE = translation.get_language()
# 
#     if hasattr(request, "session"):
#         # User has a session, then set this language there
#         if select_language != request.session.get(LANGUAGE_SESSION_KEY):
#             request.session[LANGUAGE_SESSION_KEY] = select_language
#     elif request.method == "GET" and not fallback:
#         # No session is active. We need to set a cookie for the language
#         # so that it persists when users change their location to somewhere
#         # not under the control of the CMS.
#         # Only do this when request method is GET (mainly, do not abort
#         # POST requests)
#         response = HttpResponseRedirect(request.get_full_path())
#         response.set_cookie(str(LANGUAGE_COOKIE_NAME), select_language)
#         return response
# 
# def user_has_language_set(request):
#     """
#     Determine whether the user has explicitely set a language earlier on.
#     This is taken later on as an indication that we should not mess with the
#     site's language settings, after all, the user's decision is what counts.
#     """
#     if (
#         hasattr(request, "session")
#         and request.session.get(LANGUAGE_SESSION_KEY) is not None
#     ):
#         return True
#     if LANGUAGE_COOKIE_NAME in request.COOKIES:
#         return True
#     return False
# 
# Path: feincms/module/page/models.py
# class Page(BasePage):
#     class Meta:
#         ordering = ["tree_id", "lft"]
#         verbose_name = _("page")
#         verbose_name_plural = _("pages")
#         app_label = "page"
#         # not yet # permissions = (("edit_page", _("Can edit page metadata")),)


from django.conf import settings as django_settings
from django.contrib.sites.models import Site
from django.template.defaultfilters import slugify
from django.test import RequestFactory, TestCase
from django.utils import translation
from feincms.extensions.translations import (
    translation_set_language,
    user_has_language_set,
)
from feincms.module.page.models import Page
            slug=kwargs.get("slug", slugify(title)),
            parent=parent,
            **defaults
        )

    def create_default_page_set(self, **kwargs):
        return self.create_page("Test child page", parent=self.create_page(**kwargs))

    def testPage(self):
        page = Page()
        self.assertTrue(hasattr(page, "language"))
        self.assertTrue(hasattr(page, "translation_of"))
        self.assertEqual(self.page_de.translation_of, self.page_en)
        self.assertEqual(self.page_de.original_translation, self.page_en)

        # TODO:  add request tests
        # with translation.override('de'):

    def test_user_has_language_set_with_session(self):
        factory = RequestFactory()
        request = factory.get(self.page_en.get_navigation_url())
        setattr(request, "session", dict())
        request.session[self.language_session_key] = "en"
        self.assertEqual(user_has_language_set(request), True)

    def test_user_has_language_set_with_cookie(self):
        factory = RequestFactory()
        request = factory.get(self.page_en.get_navigation_url())
        request.COOKIES[django_settings.LANGUAGE_COOKIE_NAME] = "en"


# Path: feincms/extensions/translations.py
# def translation_set_language(request, select_language):
#     """
#     Set and activate a language, if that language is available.
#     """
# 
#     select_language = translation_allowed_language(select_language)
# 
#     if translation.check_for_language(select_language):
#         fallback = False
#     else:
#         # The page is in a language that Django has no messages for.
#         # We display anyhow, but fall back to primary language for
#         # other messages and other applications. It is *highly* recommended to
#         # create a new django.po for the language instead of
#         # using this behaviour.
#         select_language = django_settings.LANGUAGES[0][0]
#         fallback = True
# 
#     translation.activate(select_language)
#     request.LANGUAGE_CODE = translation.get_language()
# 
#     if hasattr(request, "session"):
#         # User has a session, then set this language there
#         if select_language != request.session.get(LANGUAGE_SESSION_KEY):
#             request.session[LANGUAGE_SESSION_KEY] = select_language
#     elif request.method == "GET" and not fallback:
#         # No session is active. We need to set a cookie for the language
#         # so that it persists when users change their location to somewhere
#         # not under the control of the CMS.
#         # Only do this when request method is GET (mainly, do not abort
#         # POST requests)
#         response = HttpResponseRedirect(request.get_full_path())
#         response.set_cookie(str(LANGUAGE_COOKIE_NAME), select_language)
#         return response
# 
# def user_has_language_set(request):
#     """
#     Determine whether the user has explicitely set a language earlier on.
#     This is taken later on as an indication that we should not mess with the
#     site's language settings, after all, the user's decision is what counts.
#     """
#     if (
#         hasattr(request, "session")
#         and request.session.get(LANGUAGE_SESSION_KEY) is not None
#     ):
#         return True
#     if LANGUAGE_COOKIE_NAME in request.COOKIES:
#         return True
#     return False
# 
# Path: feincms/module/page/models.py
# class Page(BasePage):
#     class Meta:
#         ordering = ["tree_id", "lft"]
#         verbose_name = _("page")
#         verbose_name_plural = _("pages")
#         app_label = "page"
#         # not yet # permissions = (("edit_page", _("Can edit page metadata")),)


# Path: tests/testapp/tests/test_extensions.py
from django.conf import settings as django_settings
from django.contrib.sites.models import Site
from django.template.defaultfilters import slugify
from django.test import RequestFactory, TestCase
from django.utils import translation
from feincms.extensions.translations import (
    translation_set_language,
    user_has_language_set,
)
from feincms.module.page.models import Page

            slug=kwargs.get("slug", slugify(title)),
            parent=parent,
            **defaults
        )

    def create_default_page_set(self, **kwargs):
        return self.create_page("Test child page", parent=self.create_page(**kwargs))

    def testPage(self):
        page = Page()
        self.assertTrue(hasattr(page, "language"))
        self.assertTrue(hasattr(page, "translation_of"))
        self.assertEqual(self.page_de.translation_of, self.page_en)
        self.assertEqual(self.page_de.original_translation, self.page_en)

        # TODO:  add request tests
        # with translation.override('de'):

    def test_user_has_language_set_with_session(self):
        factory = RequestFactory()
        request = factory.get(self.page_en.get_navigation_url())
        setattr(request, "session", dict())
        request.session[self.language_session_key] = "en"
        self.assertEqual(user_has_language_set(request), True)

    def test_user_has_language_set_with_cookie(self):
        factory = RequestFactory()
        request = factory.get(self.page_en.get_navigation_url())
        request.COOKIES[django_settings.LANGUAGE_COOKIE_NAME] = "en"


        self.assertEqual(user_has_language_set(request), True)