<commit_before># -*- coding: utf-8 -*-

"""The base Controller API."""

from tg import TGController, tmpl_context, request
from tg.i18n import get_lang
from babel import parse_locale
from babel.util import distinct

from skylines.config.i18n import languages, language_info
from skylines.model import Notification


__all__ = ['BaseController']


class BaseController(TGController):
    """
    Base class for the controllers in the application.

    Your web application should have one of these. The root of
    your application is used to compute URLs used by your app.

    """

    def __call__(self, environ, start_response):
        """Invoke the Controller"""
        # TGController.__call__ dispatches to the Controller method
        # the request is routed to. This routing information is
        # available in environ['pylons.routes_dict']

        request.identity = request.environ.get('repoze.who.identity')
        tmpl_context.identity = request.identity
        return TGController.__call__(self, environ, start_response)

    def _before(self, *args, **kw):
        def get_primary_languages():
            available = [lang['language_code'] for lang in languages()]
            requested = distinct(get_lang() or ['en'])

            # add primary languages
            primary = []
            for language in requested:
                if language in available:
                    primary.append(language)
                else:
                    try:
                        locale = parse_locale(language)
                    except:
                        continue

                    if locale[0] in available:
                        primary.append(locale[0])

            if len(primary) == 0:
                return [language_info('en')]

            return [language_info(lang) for lang in distinct(primary)]

        tmpl_context.available_languages = languages()

        tmpl_context.primary_languages = get_primary_languages()
        tmpl_context.secondary_languages = [lang for lang in tmpl_context.available_languages if lang not in tmpl_context.primary_languages]

        tmpl_context.current_language = tmpl_context.primary_languages[0]

        if request.identity:
            request.identity['notifications'] = Notification.count_unread(request.identity['user'])<commit_msg>Revert "controllers/base: Removed obsolete identity handling code"<commit_after># -*- coding: utf-8 -*-

"""The base Controller API."""

from tg import TGController, tmpl_context, request, redirect, url
from tg.i18n import get_lang
from babel import parse_locale
from babel.util import distinct

from skylines.config.i18n import languages, language_info
from skylines.model import Notification


__all__ = ['BaseController']


class BaseController(TGController):
    """
    Base class for the controllers in the application.

    Your web application should have one of these. The root of
    your application is used to compute URLs used by your app.

    """

    def __call__(self, environ, start_response):
        """Invoke the Controller"""
        # TGController.__call__ dispatches to the Controller method
        # the request is routed to. This routing information is
        # available in environ['pylons.routes_dict']

        request.identity = request.environ.get('repoze.who.identity')
        tmpl_context.identity = request.identity
        return TGController.__call__(self, environ, start_response)

    def _before(self, *args, **kw):
        def get_primary_languages():
            available = [lang['language_code'] for lang in languages()]
            requested = distinct(get_lang() or ['en'])

            # add primary languages
            primary = []
            for language in requested:
                if language in available:
                    primary.append(language)
                else:
                    try:
                        locale = parse_locale(language)
                    except:
                        continue

                    if locale[0] in available:
                        primary.append(locale[0])

            if len(primary) == 0:
                return [language_info('en')]

            return [language_info(lang) for lang in distinct(primary)]

        tmpl_context.available_languages = languages()

        tmpl_context.primary_languages = get_primary_languages()
        tmpl_context.secondary_languages = [lang for lang in tmpl_context.available_languages if lang not in tmpl_context.primary_languages]

        tmpl_context.current_language = tmpl_context.primary_languages[0]

        if request.identity and (not 'user' in request.identity or
                                 not request.identity['user']):
            raise redirect(url('/logout_handler',
                               params=dict(came_from=request.url.encode('utf-8'))))

        if request.identity:
            request.identity['notifications'] = Notification.count_unread(request.identity['user'])
{'fraction_non_alphanumeric': 0.0825301204819277, 'fraction_numerical': 0.003815261044176707, 'mean_word_length': 2.3769491525423727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4094587', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1223, 'n_words': 366}