# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import messages
from django.core.urlresolvers import reverse_lazy
from django.views.generic import FormView
from millionmilestogether.forms.children import ChildRegisterForm
from millionmilestogether.models import Child
from millionmilestogether.views.generic import MMTAuthorizationMixin


class ChildRegisterView(MMTAuthorizationMixin, FormView):
    form_class = ChildRegisterForm
    template_name = "millionmilestogether/children/register.html"
    success_url = reverse_lazy('mmt-home')

    def form_valid(self, form):
        child = Child(
            first_name=form.cleaned_data['first_name'],
            date_of_birth=form.cleaned_data['date_of_birth']
        )
        child.save()
        child.guardians.add(self.request.user)

        messages.success(
            self.request,
            '%s was registered successfully.' % child.first_name
        )

        return super(ChildRegisterView, self).form_valid(form)
