# Path: brasilcomvc/accounts/forms.py
# class DeleteUserForm(forms.Form):
# 
#     password = forms.CharField(label='Senha', widget=forms.PasswordInput)
# 
#     def __init__(self, user, *args, **kwargs):
#         super(DeleteUserForm, self).__init__(*args, **kwargs)
#         self.user = user
# 
#     def clean_password(self):
#         password = self.cleaned_data['password']
#         if not self.user.check_password(password):
#             self.add_error('password', 'Senha inválida')
#         return password
# 
# class LoginForm(AuthenticationForm):
# 
#     # this is named username so we can use Django's login view
#     username = forms.EmailField(required=True)
# 
# class UserAddressForm(forms.ModelForm):
# 
#     class Meta:
#         model = UserAddress
#         fields = '__all__'
# 
#     def __init__(self, *args, **kwargs):
#         super(UserAddressForm, self).__init__(*args, **kwargs)
# 
#         # Limit regions to available cities
#         states = Region.objects.filter(
#             id__in=set(City.objects.values_list('region_id', flat=True)))
#         self.fields['state'].queryset = states
#         self.fields['state'].choices = states.values_list('id', 'name')
# 
#         # Group cities by region (state)
#         self.fields['city'].choices = self._group_cities()
# 
#     def _group_cities(self):
#         '''
#         Build a choices-like list with all cities grouped by state (region)
#         '''
#         return [
#             (state, [(city.pk, city.name) for city in cities],)
#             for state, cities in groupby(
#                 self.fields['city'].queryset.order_by('region__name', 'name'),
#                 lambda city: city.region.name)]


from django.contrib.auth import get_user_model
from django.contrib.auth.forms import AuthenticationForm
from django.forms import EmailField
from django.test import TestCase
from cities_light.models import City, Region
from ..forms import DeleteUserForm, LoginForm, UserAddressForm
# encoding: utf8
from __future__ import unicode_literals





User = get_user_model()


class LoginFormTestCase(TestCase):

    def test_username_authenticationform_subclass(self):
        self.assertTrue(issubclass(LoginForm, AuthenticationForm))

    def test_username_field_is_emailfield_instance(self):
        f = LoginForm()
        self.assertTrue(isinstance(f.fields['username'], EmailField))


class UserAddressFormTestCase(TestCase):

    fixtures = ['test_cities']

    def test_form_should_have_all_fields(self):

# Path: brasilcomvc/accounts/forms.py
# class DeleteUserForm(forms.Form):
# 
#     password = forms.CharField(label='Senha', widget=forms.PasswordInput)
# 
#     def __init__(self, user, *args, **kwargs):
#         super(DeleteUserForm, self).__init__(*args, **kwargs)
#         self.user = user
# 
#     def clean_password(self):
#         password = self.cleaned_data['password']
#         if not self.user.check_password(password):
#             self.add_error('password', 'Senha inválida')
#         return password
# 
# class LoginForm(AuthenticationForm):
# 
#     # this is named username so we can use Django's login view
#     username = forms.EmailField(required=True)
# 
# class UserAddressForm(forms.ModelForm):
# 
#     class Meta:
#         model = UserAddress
#         fields = '__all__'
# 
#     def __init__(self, *args, **kwargs):
#         super(UserAddressForm, self).__init__(*args, **kwargs)
# 
#         # Limit regions to available cities
#         states = Region.objects.filter(
#             id__in=set(City.objects.values_list('region_id', flat=True)))
#         self.fields['state'].queryset = states
#         self.fields['state'].choices = states.values_list('id', 'name')
# 
#         # Group cities by region (state)
#         self.fields['city'].choices = self._group_cities()
# 
#     def _group_cities(self):
#         '''
#         Build a choices-like list with all cities grouped by state (region)
#         '''
#         return [
#             (state, [(city.pk, city.name) for city in cities],)
#             for state, cities in groupby(
#                 self.fields['city'].queryset.order_by('region__name', 'name'),
#                 lambda city: city.region.name)]


# Path: brasilcomvc/accounts/tests/test_forms.py
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import AuthenticationForm
from django.forms import EmailField
from django.test import TestCase
from cities_light.models import City, Region
from ..forms import DeleteUserForm, LoginForm, UserAddressForm

# encoding: utf8
from __future__ import unicode_literals





User = get_user_model()


class LoginFormTestCase(TestCase):

    def test_username_authenticationform_subclass(self):
        self.assertTrue(issubclass(LoginForm, AuthenticationForm))

    def test_username_field_is_emailfield_instance(self):
        f = LoginForm()
        self.assertTrue(isinstance(f.fields['username'], EmailField))


class UserAddressFormTestCase(TestCase):

    fixtures = ['test_cities']

    def test_form_should_have_all_fields(self):

        self.assertEqual(UserAddressForm.Meta.fields, '__all__')