<commit_before>from datetime import datetime, timedelta

from django.conf import settings
from django.contrib import admin, messages
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.utils.module_loading import import_by_path

from fjord.base.utils import smart_date, smart_str
from fjord.feedback.models import Product
from fjord.journal.models import Record
from fjord.translations.gengo_utils import FjordGengo
from fjord.translations.models import GengoJob, GengoOrder
from fjord.translations.tasks import translate_tasks_by_id_list
from fjord.translations.utils import locale_equals_language


class GengoJobAdmin(admin.ModelAdmin):
    list_display = (
        'id',
        'content_type',
        'object_id',
        'content_object',
        'tier',
        'src_lang',
        'src_field',
        'dst_lang',
        'dst_field',
        'status',
        'order',
        'created',
        'completed'
    )
    list_filter = ('status', 'content_type')

admin.site.register(GengoJob, GengoJobAdmin)


class GengoOrderAdmin(admin.ModelAdmin):
    list_display = (
        'id',
        'order_id',
        'status',
        'created',
        'completed'
    )
    list_filter = ('status',)

admin.site.register(GengoOrder, GengoOrderAdmin)


def gengo_translator_view(request):
    """Covers Gengo-specific translation system status"""
    products = Product.objects.all()
    balance = None
    configured = False
    gengo_languages = None
    missing_prod_locales = None

    if settings.GENGO_PUBLIC_KEY and settings.GENGO_PRIVATE_KEY:
        gengo_api = FjordGengo()
        balance = gengo_api.get_balance()
        configured = True

        # Figure out the list of languages Gengo supports and relate them
        # to PROD locales.
        languages = sorted(gengo_api.get_languages(raw=True)['response'],
                           key=lambda item: item['lc'])
        gengo_languages = []
        PROD_LANG = settings.PROD_LANGUAGES
        for lang in languages:
            lang_lc = lang['lc']
            prod_langs = [item for item in PROD_LANG
                          if locale_equals_language(item, lang_lc)]
            prod_langs = ' '.join(prod_langs)

            gengo_languages.append(
                (lang_lc, lang['language'], prod_langs)<commit_msg>[bug 1225937] Fix UnboundLocalError in /admin/gengo-translator-view<commit_after>from datetime import datetime, timedelta

from django.conf import settings
from django.contrib import admin, messages
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.utils.module_loading import import_by_path

from fjord.base.utils import smart_date, smart_str
from fjord.feedback.models import Product
from fjord.journal.models import Record
from fjord.translations.gengo_utils import FjordGengo
from fjord.translations.models import GengoJob, GengoOrder
from fjord.translations.tasks import translate_tasks_by_id_list
from fjord.translations.utils import locale_equals_language


class GengoJobAdmin(admin.ModelAdmin):
    list_display = (
        'id',
        'content_type',
        'object_id',
        'content_object',
        'tier',
        'src_lang',
        'src_field',
        'dst_lang',
        'dst_field',
        'status',
        'order',
        'created',
        'completed'
    )
    list_filter = ('status', 'content_type')

admin.site.register(GengoJob, GengoJobAdmin)


class GengoOrderAdmin(admin.ModelAdmin):
    list_display = (
        'id',
        'order_id',
        'status',
        'created',
        'completed'
    )
    list_filter = ('status',)

admin.site.register(GengoOrder, GengoOrderAdmin)


def gengo_translator_view(request):
    """Covers Gengo-specific translation system status"""
    products = Product.objects.all()
    balance = None
    configured = False
    gengo_languages = None
    missing_prod_locales = None
    outstanding = []

    if settings.GENGO_PUBLIC_KEY and settings.GENGO_PRIVATE_KEY:
        gengo_api = FjordGengo()
        balance = gengo_api.get_balance()
        configured = True

        # Figure out the list of languages Gengo supports and relate them
        # to PROD locales.
        languages = sorted(gengo_api.get_languages(raw=True)['response'],
                           key=lambda item: item['lc'])
        gengo_languages = []
        PROD_LANG = settings.PROD_LANGUAGES
        for lang in languages:
            lang_lc = lang['lc']
            prod_langs = [item for item in PROD_LANG
                          if locale_equals_language(item, lang_lc)]
            prod_langs = ' '.join(prod_langs)

            gengo_languages.append(
                (lang_lc, lang['language'], prod_langs)
{'fraction_non_alphanumeric': 0.08650810355714586, 'fraction_numerical': 0.0014733740265207324, 'mean_word_length': 2.7864541832669323, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4909320', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1395, 'n_words': 323}