"""This module holds Nissan-specific views for the ``assessments`` app."""

from django.core.urlresolvers import reverse_lazy
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.views import generic

from mtp.apps.assessments.views import RecommendationsBaseView
from mtp.apps.import_export.forms import ImportForm

from ..import_export.saba import CourseImporter


class LessonReferencesImportView(RecommendationsBaseView, generic.FormView):
    """This view allows to import courses from a .csv file exported from Saba."""

    form_class = ImportForm
    template_name = 'cms/assessments/lesson_references_import.html'
    success_message = _("%(num)d lesson references were successfully imported.")
    success_url = reverse_lazy('assessments:certification-results-list')

    def form_valid(self, form):
        saba = CourseImporter(request=self.request)
        courses = saba.import_courses()

        self.send_message(data={'num': len(courses)})

        return HttpResponseRedirect(self.get_success_url())
