# 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
                    ("sidebar", "Sidebar", "inherited"),
                ),
            }
        )
        self.site_1 = Site.objects.all()[0]

        # create a bunch of pages
        en = self.create_default_page_set(language="en")
        de = self.create_default_page_set(language="de", title="Testseite")
        de.translation_of = en
        de.save()
        de.parent.translation_of = en.parent
        de.parent.save()
        self.page_de = de.parent
        self.page_en = en.parent

        if hasattr(translation, "LANGUAGE_SESSION_KEY"):
            self.language_session_key = translation.LANGUAGE_SESSION_KEY
        else:
            # Django 1.6
            self.language_session_key = django_settings.LANGUAGE_COOKIE_NAME

    def create_page(self, title="Test page", parent=None, **kwargs):
        defaults = {
            "template_key": "base",
            "site": self.site_1,
            "in_navigation": False,
            "active": False,
        }
        defaults.update(kwargs)

# 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

                    ("sidebar", "Sidebar", "inherited"),
                ),
            }
        )
        self.site_1 = Site.objects.all()[0]

        # create a bunch of pages
        en = self.create_default_page_set(language="en")
        de = self.create_default_page_set(language="de", title="Testseite")
        de.translation_of = en
        de.save()
        de.parent.translation_of = en.parent
        de.parent.save()
        self.page_de = de.parent
        self.page_en = en.parent

        if hasattr(translation, "LANGUAGE_SESSION_KEY"):
            self.language_session_key = translation.LANGUAGE_SESSION_KEY
        else:
            # Django 1.6
            self.language_session_key = django_settings.LANGUAGE_COOKIE_NAME

    def create_page(self, title="Test page", parent=None, **kwargs):
        defaults = {
            "template_key": "base",
            "site": self.site_1,
            "in_navigation": False,
            "active": False,
        }
        defaults.update(kwargs)

        return Page.objects.create(