示例#1
0
def _base_context(request, target_lang=None):
    context = {
        "request": request,
        "target_lang": (target_lang or util.get_target_lang_from_request(request)),
        "gettext": ugettext_for_locale(target_lang),
        "active_languages": get_well_translated_langs(),
    }

    context.update(util.rtl_context_stuff(target_lang))
    return context
示例#2
0
def _base_context(request, target_lang=None):
    context = {
        'request': request,
        'target_lang': (target_lang
                        or util.get_target_lang_from_request(request)),
        'gettext': ugettext_for_locale(target_lang),
        'active_languages': get_well_translated_langs(),
    }

    context.update(util.rtl_context_stuff(target_lang))
    return context
def _base_context(request, target_lang=None):
    context = {
        'request': request,
        'target_lang': (
            target_lang
            or util.get_target_lang_from_request(request)),
        'gettext': ugettext_for_locale(target_lang),
        'active_languages': get_well_translated_langs(),
        }

    context.update(util.rtl_context_stuff(target_lang))
    return context
示例#4
0
def licenses_view(request):
    target_lang = util.get_target_lang_from_request(request)

    context = {
        'active_languages': get_well_translated_langs(),
        'page_style': "bare"
    }
    context.update(util.rtl_context_stuff(target_lang))

    # Don't cache the response for internationalization reasons
    response = Response(
        util.render_template(request, target_lang,
                             'catalog_pages/licenses-index.html', context))
    response.headers.add('Cache-Control', 'no-cache')
    return response
示例#5
0
def licenses_view(request):
    target_lang = util.get_target_lang_from_request(request)

    context = {
        'active_languages': get_well_translated_langs(),
        'page_style': "bare"}
    context.update(util.rtl_context_stuff(target_lang))

    # Don't cache the response for internationalization reasons
    response = Response(
        util.render_template(
            request, target_lang,
            'catalog_pages/licenses-index.html', context))
    response.headers.add('Cache-Control', 'no-cache')
    return response
示例#6
0
def test_get_well_translated_langs():
    """
    Test the util.get_well_translated_langs function.
    """
    temp_dir = tempfile.mkdtemp()

    # write a fake CSV file
    transstats = os.path.join(temp_dir, "transstats.csv")
    ts_file = file(transstats, "w")
    ts_file.write(
        (
            "es_AR,564,343,27,60\n"
            "en,564,0,0,0\n"
            "en_US,564,0,1,0\n"
            "hr,564,447,17,79\n"
            "no,564,400,14,70\n"
            "nl,564,456,16,80\n"
            "fi,564,460,14,81"
        )
    )
    ts_file.close()

    expected_zero = [
        {"code": "es_AR", "name": u"Castellano (AR)"},
        {"code": "en", "name": u"English"},
        {"code": "en_US", "name": u"English (US)"},
        {"code": "hr", "name": u"hrvatski"},
        {"code": "nl", "name": u"Nederlands"},
        {"code": "no", "name": u"Norsk"},
        {"code": "fi", "name": u"Suomeksi"},
    ]
    assert util.get_well_translated_langs(0, transstats) == expected_zero

    expected_seventy = [
        {"code": "en", "name": u"English"},
        {"code": "hr", "name": u"hrvatski"},
        {"code": "nl", "name": u"Nederlands"},
        {"code": "no", "name": u"Norsk"},
        {"code": "fi", "name": u"Suomeksi"},
    ]
    assert util.get_well_translated_langs(70, transstats) == expected_seventy

    expected_eighty = [
        {"code": "en", "name": u"English"},
        {"code": "nl", "name": u"Nederlands"},
        {"code": "fi", "name": u"Suomeksi"},
    ]
    assert util.get_well_translated_langs(80, transstats) == expected_eighty

    # Test caching: overwrite the old file with bogus data, we should
    # get the old results
    ts_file = file(transstats, "w")
    ts_file.write(
        (
            "es_AR,999,999,999,999\n"
            "en,999,999,999,999\n"
            "en_US,999,999,999,999\n"
            "hr,999,999,999,999\n"
            "no,999,999,999,999\n"
            "nl,999,999,999,999\n"
            "fi,999,999,999,999"
        )
    )
    ts_file.close()
    assert util.get_well_translated_langs(80, transstats) == expected_eighty

    # English shouldn't show up if we tell it not to
    expected_eighty_noenglish = [{"code": "nl", "name": u"Nederlands"}, {"code": "fi", "name": u"Suomeksi"}]
    assert util.get_well_translated_langs(80, transstats, False) == expected_eighty_noenglish
示例#7
0
def license_deed_view(request):
    """
    The main and major deed generating view.
    """
    ##########################
    # Try and get the license.
    ##########################
    license = by_code(request.matchdict['code'],
                      jurisdiction=request.matchdict.get('jurisdiction'),
                      version=request.matchdict.get('version'))
    if not license:
        license_versions = util.catch_license_versions_from_request(request)

        if license_versions:
            # If we can't get it, but others of that code exist, give
            # a special 404.
            return license_catcher(request)
        else:
            # Otherwise, give the normal 404.
            return exc.HTTPNotFound()

    ####################
    # Everything else ;)
    ####################
    # "color" of the license; the color reflects the relative amount
    # of freedom.
    if license.license_code in ('devnations', 'sampling'):
        color = 'red'
    elif license.license_code.find('sampling') > -1 or \
             license.license_code.find('nc') > -1 or \
             license.license_code.find('nd') > -1:
        color = 'yellow'
    else:
        color = 'green'

    # Get the language this view will be displayed in.
    #  - First checks to see if the routing matchdict specifies the language
    #  - Or, next gets the jurisdictions' default language if the jurisdiction
    #    specifies one
    #  - Otherwise it's english!
    if request.matchdict.has_key('target_lang'):
        target_lang = request.matchdict.get('target_lang')
    elif license.jurisdiction.default_language:
        target_lang = locale_to_lower_upper(
            license.jurisdiction.default_language)
    else:
        target_lang = 'en'

    # True if the legalcode for this license is available in
    # multiple languages (or a single language with a language code different
    # than that of the jurisdiction).
    #
    # Stored in the RDF, we'll just check license.legalcodes() :)
    legalcodes = license.legalcodes(target_lang)
    if len(legalcodes) > 1 \
            or list(legalcodes)[0][2] is not None:
        multi_language = True
        legalcodes = sorted(legalcodes, key=lambda lc: lc[2])
    else:
        multi_language = False

    # Use the lower-dash style for all RDF-related locale stuff
    rdf_style_target_lang = locale_to_lower_lower(target_lang)

    license_title = None
    try:
        license_title = license.title(rdf_style_target_lang)
    except KeyError:
        # don't have one for that language, use default
        license_title = license.title()

    conditions = {}
    for code in license.license_code.split('-'):
        conditions[code] = 1

    # Find out all the active languages
    active_languages = get_well_translated_langs()
    negotiated_locale = negotiate_locale(target_lang)

    # If negotiating the locale says that this isn't a valid language,
    # let's fall back to something that is.
    if target_lang != negotiated_locale:
        base_url = REMOVE_DEED_URL_RE.match(request.path_info).groups()[0]
        redirect_to = base_url + 'deed.' + negotiated_locale
        return exc.HTTPFound(location=redirect_to)

    if DEED_TEMPLATE_MAPPING.has_key(license.license_code):
        main_template = DEED_TEMPLATE_MAPPING[license.license_code]
    else:
        main_template = 'licenses/standard_deed.html'

    get_this = "/choose/results-one?license_code=%s&jurisdiction=%s&version=%s&lang=%s" % (
        urllib.quote(license.license_code), license.jurisdiction.code,
        license.version, target_lang)

    context = {
        'request': request,
        'license_code': license.license_code,
        'license_code_quoted': urllib.quote(license.license_code),
        'license_title': license_title,
        'license': license,
        'multi_language': multi_language,
        'legalcodes': legalcodes,
        'color': color,
        'conditions': conditions,
        'active_languages': active_languages,
        'target_lang': target_lang,
        'jurisdiction': license.jurisdiction.code,
        'get_this': get_this,
    }
    context.update(util.rtl_context_stuff(target_lang))

    return Response(
        util.render_template(request, target_lang, main_template, context))
示例#8
0
def license_deed_view(request):
    """
    The main and major deed generating view.
    """
    ##########################
    # Try and get the license.
    ##########################
    license = by_code(
        request.matchdict['code'],
        jurisdiction=request.matchdict.get('jurisdiction'),
        version=request.matchdict.get('version'))
    if not license:
        license_versions = util.catch_license_versions_from_request(request)

        if license_versions:
            # If we can't get it, but others of that code exist, give
            # a special 404.
            return license_catcher(request)
        else:
            # Otherwise, give the normal 404.
            return exc.HTTPNotFound()

    ####################
    # Everything else ;)
    ####################
    # "color" of the license; the color reflects the relative amount
    # of freedom.
    if license.license_code in ('devnations', 'sampling'):
       color = 'red'
    elif license.license_code.find('sampling') > -1 or \
             license.license_code.find('nc') > -1 or \
             license.license_code.find('nd') > -1:
       color = 'yellow'
    else:
       color = 'green'

    # Get the language this view will be displayed in.
    #  - First checks to see if the routing matchdict specifies the language
    #  - Or, next gets the jurisdictions' default language if the jurisdiction
    #    specifies one
    #  - Otherwise it's english!
    if request.matchdict.has_key('target_lang'):
        target_lang = request.matchdict.get('target_lang')
    elif license.jurisdiction.default_language:
        target_lang = locale_to_lower_upper(
            license.jurisdiction.default_language)
    else:
        target_lang = 'en'

    # True if the legalcode for this license is available in
    # multiple languages (or a single language with a language code different
    # than that of the jurisdiction).
    #
    # Stored in the RDF, we'll just check license.legalcodes() :)
    legalcodes = license.legalcodes(target_lang)
    if len(legalcodes) > 1 \
            or list(legalcodes)[0][2] is not None:
        multi_language = True
        legalcodes = sorted(legalcodes, key=lambda lc: lc[2])
    else:
        multi_language = False

    # Use the lower-dash style for all RDF-related locale stuff
    rdf_style_target_lang = locale_to_lower_lower(target_lang)

    license_title = None
    try:
        license_title = license.title(rdf_style_target_lang)
    except KeyError:
        # don't have one for that language, use default
        license_title = license.title()

    conditions = {}
    for code in license.license_code.split('-'):
        conditions[code] = 1

    # Find out all the active languages
    active_languages = get_well_translated_langs()
    negotiated_locale = negotiate_locale(target_lang)

    # If negotiating the locale says that this isn't a valid language,
    # let's fall back to something that is.
    if target_lang != negotiated_locale:
        base_url = REMOVE_DEED_URL_RE.match(request.path_info).groups()[0]
        redirect_to = base_url + 'deed.' + negotiated_locale
        return exc.HTTPFound(location=redirect_to)

    if DEED_TEMPLATE_MAPPING.has_key(license.license_code):
        main_template = DEED_TEMPLATE_MAPPING[license.license_code]
    else:
        main_template = 'licenses/standard_deed.html'

    get_this = "/choose/results-one?license_code=%s&jurisdiction=%s&version=%s&lang=%s" % (urllib.quote(license.license_code), license.jurisdiction.code, license.version, target_lang)

    context = {
        'request': request,
        'license_code': license.license_code,
        'license_code_quoted': urllib.quote(license.license_code),
        'license_title': license_title,
        'license': license,
        'multi_language': multi_language,
        'legalcodes': legalcodes,
        'color': color,
        'conditions': conditions,
        'active_languages': active_languages,
        'target_lang': target_lang,
        'jurisdiction':license.jurisdiction.code,
        'get_this': get_this,
        }
    context.update(util.rtl_context_stuff(target_lang))

    return Response(
        util.render_template(
            request, target_lang,
            main_template, context))
示例#9
0
def test_get_well_translated_langs():
    """
    Test the util.get_well_translated_langs function.
    """
    temp_dir = tempfile.mkdtemp()

    # write a fake CSV file
    transstats = os.path.join(temp_dir, 'transstats.csv')
    ts_file = file(transstats, 'w')
    ts_file.write(('es_AR,564,343,27,60\n'
                   'en,564,0,0,0\n'
                   'en_US,564,0,1,0\n'
                   'hr,564,447,17,79\n'
                   'no,564,400,14,70\n'
                   'nl,564,456,16,80\n'
                   'fi,564,460,14,81'))
    ts_file.close()

    expected_zero = [{
        'code': 'es_AR',
        'name': u'Castellano (AR)'
    }, {
        'code': 'en',
        'name': u'English'
    }, {
        'code': 'en_US',
        'name': u'English (US)'
    }, {
        'code': 'hr',
        'name': u'hrvatski'
    }, {
        'code': 'nl',
        'name': u'Nederlands'
    }, {
        'code': 'no',
        'name': u'Norsk'
    }, {
        'code': 'fi',
        'name': u'Suomeksi'
    }]
    assert util.get_well_translated_langs(0, transstats) == expected_zero

    expected_seventy = [{
        'code': 'en',
        'name': u'English'
    }, {
        'code': 'hr',
        'name': u'hrvatski'
    }, {
        'code': 'nl',
        'name': u'Nederlands'
    }, {
        'code': 'no',
        'name': u'Norsk'
    }, {
        'code': 'fi',
        'name': u'Suomeksi'
    }]
    assert util.get_well_translated_langs(70, transstats) == expected_seventy

    expected_eighty = [{
        'code': 'en',
        'name': u'English'
    }, {
        'code': 'nl',
        'name': u'Nederlands'
    }, {
        'code': 'fi',
        'name': u'Suomeksi'
    }]
    assert util.get_well_translated_langs(80, transstats) == expected_eighty

    # Test caching: overwrite the old file with bogus data, we should
    # get the old results
    ts_file = file(transstats, 'w')
    ts_file.write(('es_AR,999,999,999,999\n'
                   'en,999,999,999,999\n'
                   'en_US,999,999,999,999\n'
                   'hr,999,999,999,999\n'
                   'no,999,999,999,999\n'
                   'nl,999,999,999,999\n'
                   'fi,999,999,999,999'))
    ts_file.close()
    assert util.get_well_translated_langs(80, transstats) == expected_eighty

    # English shouldn't show up if we tell it not to
    expected_eighty_noenglish = [{
        'code': 'nl',
        'name': u'Nederlands'
    }, {
        'code': 'fi',
        'name': u'Suomeksi'
    }]
    assert util.get_well_translated_langs(80, transstats,
                                          False) == expected_eighty_noenglish