# Path: eff_site/eff/forms.py
# class UserAdminForm(UserCreationForm):
#     email = forms.EmailField(required=True, label='E-mail address',
#                              max_length=254)
#     is_client = forms.BooleanField(required=False, label="Client",
#                                    help_text=("Designates whether this user"
#                                               "should be treated as a Client."))
#     company = forms.ModelChoiceField(required=False,
#                                      queryset=Client.objects.all())
# 
#     class Meta:
#         model = User
#         # Needed to define an order (hashed password not shown)
#         fields = ('username', 'password1', 'password2', 'is_client', 'company',
#                   'first_name', 'last_name', 'email', 'is_staff', 'is_active',
#                   'is_superuser', 'last_login', 'date_joined', 'groups',
#                   'user_permissions',)
# 
#     def __init__(self, *args, **kwargs):
#         super(UserCreationForm, self).__init__(*args, **kwargs)
#         try:
#             profile = self.instance.get_profile()
#             self.fields['is_client'].initial = profile.is_client()
#             self.fields['company'].initial = profile.company
#         except User.DoesNotExist:
#             pass
#         except UserProfile.DoesNotExist:
#             pass
# 
#     def clean_email(self):
#         email = self.cleaned_data.get('email')
#         username = self.cleaned_data.get('username')
#         users = User.objects.filter(email=email).exclude(username=username)
#         if email and users.count():
#             raise forms.ValidationError(u'Email address must be unique.')
#         return email
# 
#     def clean(self):
#         cleaned_data = super(UserAdminForm, self).clean()
#         is_client = cleaned_data.get("is_client")
#         company = cleaned_data.get("company")
# 
#         first_name = cleaned_data.get("first_name")
#         last_name = cleaned_data.get("last_name")
#         errors = False
# 
#         if is_client and not company:
#             self._errors['company'] = self._errors.get('company', ErrorList())
#             self._errors['company'].append("A client user must have Company")
#             errors = True
#         if is_client and not first_name:
#             self._errors['first_name'] = self._errors.get('first_name',
#                 ErrorList())
#             self._errors['first_name'].append("A client user must have First " \
#                 "name")
#             errors = True
#         if is_client and not last_name:
#             self._errors['last_name'] = self._errors.get('last_name',
#                 ErrorList())
#             self._errors['last_name'].append("A client user must have Last " \
#                 "name")
#             errors = True
# 
#         if company and not is_client:
#             self._errors['company'] = self._errors.get('company',
#                                                        ErrorList())
#             self._errors['company'].append("A default user not must have "\
#                                            "Company")
#             errors = True
# 
#         if errors:
#             raise forms.ValidationError('Some field are invalid')
# 
#         return cleaned_data
# 
#     def save(self, *args, **kwargs):
#         if self.instance.pk is None:
#             # Add new user to Attachment's group
#             try:
#                 group_attachment = Group.objects.get(name='attachments')
#                 if group_attachment not in self.cleaned_data['groups']:
#                     self.cleaned_data['groups'].append(group_attachment)
#             except Group.DoesNotExist:
#                 pass
# 
#         kwargs.pop('commit', None)
#         user = super(UserCreationForm, self).save(*args, commit=False,
#                                                       **kwargs)
#         password = self.cleaned_data["password1"]
#         if password:
#             user.set_password(password)
# 
#         user.is_client = self.cleaned_data['is_client']
#         if user.is_client:
#             user.company = self.cleaned_data['company']
#             user.is_client = self.cleaned_data['is_client']
#         user.save()
#         self.save_m2m(*args, **kwargs)
#         return user


from django.test import TestCase
from django.test.client import Client
from pyquery import PyQuery
from django.contrib.auth.models import User, Permission, Group
from django.core.exceptions import ValidationError
from django.core import mail
from eff.models import UserProfile, AvgHours, Project, TimeLog, ExternalSource
from eff.models import Wage, ClientHandles, Handle
from eff.models import Client as EffClient, Dump
from eff.views import Data
from factories import (ClientFactory, BillingFactory, CreditNoteFactory,
                       PaymentFactory, ExternalSourceFactory)
from eff_site.eff.forms import UserAdminForm
from unittest import TestSuite, makeSuite
from datetime import date, timedelta, datetime
        from django.template.defaultfilters import slugify
            view: eff
            template: eff_query.html
            """
            self._test_prev_next_button('/efi/?from_date=2008-03-04&'\
                'to_date=2008-04-29')

        def test_include_navlinks_into_eff_charts(self):
            """
            Test
            view: eff_chart
            template: profiles/eff_charts.html
            """
            self._test_prev_next_button('efi/chart/admin/?from_date=2008-03-04'\
                '&to_date=2008-04-29')


class UserCreationTest(TestCase):

    def setUp(self):
        self.group_attachment = Group()
        self.group_attachment.name = 'attachments'
        self.group_attachment.save()

    def test_group_attachment_added_to_new_user(self):
        data = {'username': 'test_att',
                'password1': 'test_att',
                'password2': 'test_att',
                'email': 'test_att@test.com',
                'last_login': datetime.now(),
                'date_joined': datetime.now()}

# Path: eff_site/eff/forms.py
# class UserAdminForm(UserCreationForm):
#     email = forms.EmailField(required=True, label='E-mail address',
#                              max_length=254)
#     is_client = forms.BooleanField(required=False, label="Client",
#                                    help_text=("Designates whether this user"
#                                               "should be treated as a Client."))
#     company = forms.ModelChoiceField(required=False,
#                                      queryset=Client.objects.all())
# 
#     class Meta:
#         model = User
#         # Needed to define an order (hashed password not shown)
#         fields = ('username', 'password1', 'password2', 'is_client', 'company',
#                   'first_name', 'last_name', 'email', 'is_staff', 'is_active',
#                   'is_superuser', 'last_login', 'date_joined', 'groups',
#                   'user_permissions',)
# 
#     def __init__(self, *args, **kwargs):
#         super(UserCreationForm, self).__init__(*args, **kwargs)
#         try:
#             profile = self.instance.get_profile()
#             self.fields['is_client'].initial = profile.is_client()
#             self.fields['company'].initial = profile.company
#         except User.DoesNotExist:
#             pass
#         except UserProfile.DoesNotExist:
#             pass
# 
#     def clean_email(self):
#         email = self.cleaned_data.get('email')
#         username = self.cleaned_data.get('username')
#         users = User.objects.filter(email=email).exclude(username=username)
#         if email and users.count():
#             raise forms.ValidationError(u'Email address must be unique.')
#         return email
# 
#     def clean(self):
#         cleaned_data = super(UserAdminForm, self).clean()
#         is_client = cleaned_data.get("is_client")
#         company = cleaned_data.get("company")
# 
#         first_name = cleaned_data.get("first_name")
#         last_name = cleaned_data.get("last_name")
#         errors = False
# 
#         if is_client and not company:
#             self._errors['company'] = self._errors.get('company', ErrorList())
#             self._errors['company'].append("A client user must have Company")
#             errors = True
#         if is_client and not first_name:
#             self._errors['first_name'] = self._errors.get('first_name',
#                 ErrorList())
#             self._errors['first_name'].append("A client user must have First " \
#                 "name")
#             errors = True
#         if is_client and not last_name:
#             self._errors['last_name'] = self._errors.get('last_name',
#                 ErrorList())
#             self._errors['last_name'].append("A client user must have Last " \
#                 "name")
#             errors = True
# 
#         if company and not is_client:
#             self._errors['company'] = self._errors.get('company',
#                                                        ErrorList())
#             self._errors['company'].append("A default user not must have "\
#                                            "Company")
#             errors = True
# 
#         if errors:
#             raise forms.ValidationError('Some field are invalid')
# 
#         return cleaned_data
# 
#     def save(self, *args, **kwargs):
#         if self.instance.pk is None:
#             # Add new user to Attachment's group
#             try:
#                 group_attachment = Group.objects.get(name='attachments')
#                 if group_attachment not in self.cleaned_data['groups']:
#                     self.cleaned_data['groups'].append(group_attachment)
#             except Group.DoesNotExist:
#                 pass
# 
#         kwargs.pop('commit', None)
#         user = super(UserCreationForm, self).save(*args, commit=False,
#                                                       **kwargs)
#         password = self.cleaned_data["password1"]
#         if password:
#             user.set_password(password)
# 
#         user.is_client = self.cleaned_data['is_client']
#         if user.is_client:
#             user.company = self.cleaned_data['company']
#             user.is_client = self.cleaned_data['is_client']
#         user.save()
#         self.save_m2m(*args, **kwargs)
#         return user


# Path: eff_site/eff/testing/testModels.py
from django.test import TestCase
from django.test.client import Client
from pyquery import PyQuery
from django.contrib.auth.models import User, Permission, Group
from django.core.exceptions import ValidationError
from django.core import mail
from eff.models import UserProfile, AvgHours, Project, TimeLog, ExternalSource
from eff.models import Wage, ClientHandles, Handle
from eff.models import Client as EffClient, Dump
from eff.views import Data
from factories import (ClientFactory, BillingFactory, CreditNoteFactory,
                       PaymentFactory, ExternalSourceFactory)
from eff_site.eff.forms import UserAdminForm
from unittest import TestSuite, makeSuite
from datetime import date, timedelta, datetime
        from django.template.defaultfilters import slugify

            view: eff
            template: eff_query.html
            """
            self._test_prev_next_button('/efi/?from_date=2008-03-04&'\
                'to_date=2008-04-29')

        def test_include_navlinks_into_eff_charts(self):
            """
            Test
            view: eff_chart
            template: profiles/eff_charts.html
            """
            self._test_prev_next_button('efi/chart/admin/?from_date=2008-03-04'\
                '&to_date=2008-04-29')


class UserCreationTest(TestCase):

    def setUp(self):
        self.group_attachment = Group()
        self.group_attachment.name = 'attachments'
        self.group_attachment.save()

    def test_group_attachment_added_to_new_user(self):
        data = {'username': 'test_att',
                'password1': 'test_att',
                'password2': 'test_att',
                'email': 'test_att@test.com',
                'last_login': datetime.now(),
                'date_joined': datetime.now()}

        form = UserAdminForm(data)