from django import forms
from django.contrib.auth import authenticate
from django.utils.translation import ugettext_lazy as _


class LoginForm(forms.Form):
    username = forms.CharField(widget=forms.TextInput())
    password = forms.CharField(widget=forms.PasswordInput())

    class Meta:
        fields = ['username', 'password']

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')
        if self.errors:
            return self.cleaned_data
        is_auth = authenticate(username=username, password=password)
        if not is_auth:
            raise forms.ValidationError(
                _("Your username and/or password were incorrect.")
            )
        # elif is_auth.validated == 0:
        #     raise forms.ValidationError(
        #         _("Your account is not activated yet.")
        #     )
        return self.cleaned_data

    def clean_username(self):
        username = self.cleaned_data.get('username')
        return username
