""" This module contains views for the ``translation`` app. """

from django.core.urlresolvers import reverse
from django.views.generic.edit import CreateView
from mtp.apps.common.views_utils import RestrictToSuperuserOrTrainerMixin

from .forms import LanguageForm
from .models import TranslatableLanguage, Translation
from .utils import get_available_languages


class DashboardView(RestrictToSuperuserOrTrainerMixin, CreateView):
    template_name = "translation/dashboard.html"
    model = TranslatableLanguage
    form_class = LanguageForm

    def get_success_url(self):
        return reverse('translation:dashboard')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['current_main_section'] = 'translation'

        translations = []
        for code, language in get_available_languages():
            translation = Translation.objects.filter(
                language=code, domain='int').first()
            translations += [
                {
                    'language': language,
                    'language_code': code,
                    'translation': translation,
                }
            ]
        context['translations'] = translations
        return context
