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

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

from mtp.apps.common.views_utils import SuccessMessageMixin
from mtp.apps.import_export.forms import ImportForm
from ..import_export.saba import UserImporter


class UsersImportView(SuccessMessageMixin, generic.FormView):
    """This view allows to import users from a .csv file exported from Saba."""

    form_class = ImportForm
    template_name = 'cms/accounts/users_import_saba.html'
    success_message = _("%(num)d users were successfully imported.")

    def get_success_url(self):
        return reverse('accounts:user-list')

    def form_valid(self, form):
        saba = UserImporter(request=self.request)
        users = saba.import_users()

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

        return HttpResponseRedirect(self.get_success_url())
