diff --git a/small_small_hr/forms.py b/small_small_hr/forms.py index 3ce3b6b..a8d7343 100644 --- a/small_small_hr/forms.py +++ b/small_small_hr/forms.py @@ -5,6 +5,7 @@ from django import forms from django.conf import settings +from django.contrib.auth.models import User from django.utils.translation import ugettext as _ import pytz @@ -484,6 +485,78 @@ def save(self, commit=True): # pylint: disable=unused-argument return staffprofile +class StaffProfileAdminCreateForm(StaffProfileAdminForm): + """ + Form used when creating new Staff Profiles + """ + user = forms.ModelChoiceField( + label=_('User'), queryset=User.objects.filter(staffprofile=None)) + + class Meta: # pylint: disable=too-few-public-methods + """ + Class meta options + """ + model = StaffProfile + fields = [ + 'user', + 'first_name', + 'last_name', + 'id_number', + 'phone', + 'sex', + 'role', + 'nhif', + 'nssf', + 'pin_number', + 'address', + 'birthday', + 'leave_days', + 'sick_days', + 'overtime_allowed', + 'start_date', + 'end_date', + 'emergency_contact_name', + 'emergency_contact_number', + ] + + def __init__(self, *args, **kwargs): + self.request = kwargs.pop('request', None) + super().__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.form_tag = True + self.helper.form_method = 'post' + self.helper.render_required_fields = True + self.helper.form_show_labels = True + self.helper.html5_required = True + self.helper.form_id = 'staffprofile-form' + self.helper.layout = Layout( + Field('user',), + Field('first_name',), + Field('last_name',), + Field('phone',), + Field('id_number',), + Field('sex',), + Field('role',), + Field('nhif',), + Field('nssf',), + Field('pin_number',), + Field('emergency_contact_name',), + Field('emergency_contact_number',), + Field('address',), + Field('birthday',), + Field('leave_days',), + Field('sick_days',), + Field('overtime_allowed',), + Field('start_date',), + Field('end_date',), + Field('emergency_contact_name',), + Field('emergency_contact_number',), + FormActions( + Submit('submitBtn', _('Submit'), css_class='btn-primary'), + ) + ) + + class StaffProfileUserForm(StaffProfileAdminForm): """ Form used when the user is updating their own data diff --git a/small_small_hr/signals.py b/small_small_hr/signals.py index 6a26b54..3aded18 100644 --- a/small_small_hr/signals.py +++ b/small_small_hr/signals.py @@ -2,19 +2,18 @@ Small small HR signals module """ from django.conf import settings -from django.db.models.signals import post_save -from django.dispatch import receiver from small_small_hr.models import StaffProfile USER = settings.AUTH_USER_MODEL -@receiver(post_save, sender=USER) # pylint: disable=unused-argument def create_staffprofile(sender, instance, created, **kwargs): """ Create staffprofile when a user object is created + + This signal is not connected by default """ if created or not instance.staffprofile: # pylint: disable=unused-variable diff --git a/tests/test_forms.py b/tests/test_forms.py index ec2d221..117e85b 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -14,6 +14,7 @@ from small_small_hr.forms import (ApplyLeaveForm, ApplyOverTimeForm, LeaveForm, OverTimeForm, RoleForm, StaffDocumentForm, + StaffProfileAdminCreateForm, StaffProfileAdminForm, StaffProfileUserForm) from small_small_hr.models import Leave, OverTime, StaffProfile from small_small_hr.serializers import StaffProfileSerializer @@ -56,7 +57,7 @@ def test_overtime_form_apply(self): Test OverTimeForm """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) request = self.factory.get('/') request.session = {} @@ -95,7 +96,7 @@ def test_overtime_form_process(self): Test OverTimeForm """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) request = self.factory.get('/') request.session = {} @@ -136,7 +137,7 @@ def test_overtime_form_start_end(self): Test OverTimeForm start end fields """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) request = self.factory.get('/') request.session = {} @@ -168,7 +169,7 @@ def test_leaveform_apply(self): Test LeaveForm apply for leave """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.leave_days = 21 staffprofile.sick_days = 10 staffprofile.save() @@ -212,7 +213,7 @@ def test_leaveform_admin(self): Test LeaveForm apply for leave """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.leave_days = 21 staffprofile.sick_days = 10 staffprofile.save() @@ -258,7 +259,7 @@ def test_leaveform_process(self): Test LeaveForm process """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.leave_days = 21 staffprofile.sick_days = 10 staffprofile.save() @@ -304,7 +305,7 @@ def test_sickleave_apply(self): Test LeaveForm apply for sick leave """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.leave_days = 21 staffprofile.sick_days = 10 staffprofile.save() @@ -348,7 +349,7 @@ def test_sickleave_process(self): Test LeaveForm process sick leave """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.leave_days = 21 staffprofile.sick_days = 10 staffprofile.save() @@ -394,7 +395,7 @@ def test_leaveform_start_end(self): Test start and end """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.leave_days = 21 staffprofile.sick_days = 10 staffprofile.save() @@ -458,7 +459,7 @@ def test_leaveform_max_days(self): Test leave days sufficient """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.leave_days = 21 staffprofile.sick_days = 10 staffprofile.save() @@ -501,7 +502,7 @@ def test_leaveform_max_sick_days(self): Test sick days sufficient """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.leave_days = 21 staffprofile.sick_days = 10 staffprofile.save() @@ -545,7 +546,7 @@ def test_staffdocumentform(self): Test StaffDocumentForm """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) request = self.factory.get('/') request.session = {} @@ -587,6 +588,7 @@ def test_staff_profile_user_form(self): Test StaffProfileUserForm """ user = mommy.make('auth.User') + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) request = self.factory.get('/') request.session = {} @@ -607,13 +609,12 @@ def test_staff_profile_user_form(self): 'birthday': '1996-01-27' } - form = StaffProfileUserForm(data=data, instance=user.staffprofile, + form = StaffProfileUserForm(data=data, instance=staffprofile, request=request) self.assertTrue(form.is_valid()) form.save() user.refresh_from_db() - staffprofile = user.staffprofile self.assertEqual('Bob Mbugua', user.staffprofile.get_name()) self.assertEqual(StaffProfile.MALE, staffprofile.sex) @@ -635,17 +636,81 @@ def test_staff_profile_user_form(self): self.assertEqual('+254722111111', staffprofile.data['emergency_contact_number']) + def test_staff_profile_admin_create_form(self): + """ + Test StaffProfileAdminCreateForm + """ + user = mommy.make('auth.User') + + request = self.factory.get('/') + request.session = {} + request.user = AnonymousUser() + + data = { + 'user': user.id, + 'first_name': 'Bob', + 'last_name': 'Mbugua', + 'id_number': '123456789', + 'sex': StaffProfile.MALE, + 'nhif': '111111', + 'nssf': '222222', + 'pin_number': 'A0000000Y', + 'emergency_contact_name': 'Bob Father', + 'emergency_contact_number': '+254722111111', + 'phone': '+254722111111', + 'address': 'This is the address.', + 'birthday': '1996-01-27', + 'leave_days': 21, + 'sick_days': 9, + 'overtime_allowed': True, + 'start_date': '2017-09-25', + 'end_date': '2018-12-31', + } + + form = StaffProfileAdminCreateForm(data=data, request=request) + self.assertTrue(form.is_valid()) + staffprofile = form.save() + + user.refresh_from_db() + + self.assertEqual('Bob Mbugua', user.staffprofile.get_name()) + self.assertEqual(StaffProfile.MALE, staffprofile.sex) + self.assertEqual('+254722111111', staffprofile.phone.as_e164) + self.assertEqual(21, staffprofile.leave_days) + self.assertEqual(9, staffprofile.sick_days) + self.assertEqual(True, staffprofile.overtime_allowed) + + self.assertEqual('This is the address.', staffprofile.address) + self.assertEqual('1996-01-27', str(staffprofile.birthday)) + self.assertEqual('2017-09-25', str(staffprofile.start_date)) + self.assertEqual('2018-12-31', str(staffprofile.end_date)) + + self.assertEqual('123456789', + staffprofile.data['id_number']) + self.assertEqual('111111', + staffprofile.data['nhif']) + self.assertEqual('222222', + staffprofile.data['nssf']) + self.assertEqual('A0000000Y', + staffprofile.data['pin_number']) + self.assertEqual('Bob Father', + staffprofile.data['emergency_contact_name']) + self.assertEqual('+254722111111', + staffprofile.data['emergency_contact_number']) + def test_staff_profile_admin_form(self): """ Test StaffProfileAdminForm """ user = mommy.make('auth.User') + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) request = self.factory.get('/') request.session = {} request.user = AnonymousUser() data = { + 'user': user.id, 'first_name': 'Bob', 'last_name': 'Mbugua', 'id_number': '123456789', @@ -665,13 +730,12 @@ def test_staff_profile_admin_form(self): 'end_date': '2018-12-31', } - form = StaffProfileAdminForm(data=data, instance=user.staffprofile, + form = StaffProfileAdminForm(data=data, instance=staffprofile, request=request) self.assertTrue(form.is_valid()) form.save() user.refresh_from_db() - staffprofile = user.staffprofile self.assertEqual('Bob Mbugua', user.staffprofile.get_name()) self.assertEqual(StaffProfile.MALE, staffprofile.sex) @@ -703,13 +767,13 @@ def test_staffprofile_unique_pin_number(self): Test unique pin_number """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.data['id_number'] = '123456789' staffprofile.data['pin_number'] = '123456789' staffprofile.save() user2 = mommy.make('auth.User', first_name='Kyle', last_name='Ndoe') - staffprofile2 = user2.staffprofile + staffprofile2 = mommy.make('small_small_hr.StaffProfile', user=user2) staffprofile2.data['id_number'] = '9999999' staffprofile2.save() @@ -735,12 +799,12 @@ def test_staffprofile_unique_id_number(self): Test unique id_number """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.data['id_number'] = '123456789' staffprofile.save() user2 = mommy.make('auth.User', first_name='Kyle', last_name='Ndoe') - staffprofile2 = user2.staffprofile + staffprofile2 = mommy.make('small_small_hr.StaffProfile', user=user2) staffprofile2.save() request = self.factory.get('/') @@ -765,13 +829,13 @@ def test_staffprofile_unique_nssf(self): Test unique NSSF """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.data['id_number'] = '123456789' staffprofile.data['nssf'] = '123456789' staffprofile.save() user2 = mommy.make('auth.User', first_name='Kyle', last_name='Ndoe') - staffprofile2 = user2.staffprofile + staffprofile2 = mommy.make('small_small_hr.StaffProfile', user=user2) staffprofile2.data['id_number'] = '9999999' staffprofile2.save() @@ -797,13 +861,13 @@ def test_staffprofile_unique_nhif(self): Test unique NHIF """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) staffprofile.data['id_number'] = '123456789' staffprofile.data['nhif'] = '123456789' staffprofile.save() user2 = mommy.make('auth.User', first_name='Kyle', last_name='Ndoe') - staffprofile2 = user2.staffprofile + staffprofile2 = mommy.make('small_small_hr.StaffProfile', user=user2) staffprofile2.data['id_number'] = '9999999' staffprofile2.save() diff --git a/tests/test_models.py b/tests/test_models.py index 3ed50ce..c0198b3 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -23,7 +23,7 @@ def test_annualleave_str(self): Test the __str__ method on AnnualLeave """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) self.assertEqual( '2018: Mosh Pitt Regular Leave', mommy.make( @@ -36,7 +36,7 @@ def test_annualleave_get_available_leave_days(self): Test get_available_leave_days """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) annual_leave = mommy.make( 'small_small_hr.AnnualLeave', staff=staff, year=2017, leave_type=Leave.REGULAR) @@ -76,7 +76,7 @@ def test_annualleave_cumulative_leave_taken(self): Test get_cumulative_leave_taken """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) annual_leave = mommy.make( 'small_small_hr.AnnualLeave', staff=staff, year=2017, leave_type=Leave.REGULAR) @@ -147,7 +147,7 @@ def test_available_leave_days(self): Test available leave days at various times of the year """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) annual_leave = mommy.make( 'small_small_hr.AnnualLeave', staff=staff, year=2017, leave_type=Leave.REGULAR, allowed_days=21, carried_over_days=0) @@ -165,7 +165,7 @@ def test_staffprofile_str(self): Test that the __str__ method on StaffProfile works """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) self.assertEqual('Mosh Pitt', staff.__str__()) def test_get_approved_leave_days(self): @@ -173,7 +173,7 @@ def test_get_approved_leave_days(self): Test get_approved_leave_days """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) start = datetime( 2018, 1, 1, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE)) @@ -194,7 +194,7 @@ def test_get_approved_sick_days(self): Test get_approved_sick_days """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) start = datetime( 2018, 1, 1, 0, 0, 0, tzinfo=pytz.timezone(settings.TIME_ZONE)) @@ -215,7 +215,7 @@ def test_staffprofile_get_available_leave_days(self): Test StaffProfile get_available_leave_days """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) mommy.make('small_small_hr.AnnualLeave', staff=staff, year=2017, leave_type=Leave.REGULAR, allowed_days=21) @@ -241,7 +241,7 @@ def test_staffprofile_get_available_sick_days(self): Test StaffProfile get_available_leave_days """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) mommy.make('small_small_hr.AnnualLeave', staff=staff, year=2017, leave_type=Leave.SICK, allowed_days=10) @@ -278,7 +278,7 @@ def test_staffdocument_str(self): Test __str__ method on StaffDocument """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) self.assertEqual( 'Mosh Pitt - Dossier', mommy.make( @@ -290,7 +290,7 @@ def test_leave_str(self): Test __str__ method on Leave """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) now = timezone.now() end = now + timedelta(days=3) self.assertEqual( @@ -304,7 +304,7 @@ def test_overtime_str(self): Test __str__ method on OverTime """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) now = timezone.now() end = now + timedelta(seconds=60 * 60 * 3) self.assertEqual( @@ -319,7 +319,7 @@ def test_overtime_duration(self): Test get_duration method on OverTime """ user = mommy.make('auth.User', first_name='Mosh', last_name='Pitt') - staff = user.staffprofile + staff = mommy.make('small_small_hr.StaffProfile', user=user) now = timezone.now() end = now + timedelta(seconds=60 * 60 * 3) self.assertEqual( diff --git a/tests/test_serializers.py b/tests/test_serializers.py index 7f7e05a..011cea5 100644 --- a/tests/test_serializers.py +++ b/tests/test_serializers.py @@ -18,7 +18,7 @@ def test_staffprofileserializer_fields(self): Test StaffProfileSerializer fields """ user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') - staffprofile = user.staffprofile + staffprofile = mommy.make('small_small_hr.StaffProfile', user=user) serializer_instance = StaffProfileSerializer(staffprofile) expected_fields = [ diff --git a/tests/test_signals.py b/tests/test_signals.py new file mode 100644 index 0000000..cc3f2ef --- /dev/null +++ b/tests/test_signals.py @@ -0,0 +1,33 @@ +""" +Module to test small_small_hr Signals +""" +from django.db.models.signals import post_save +from django.test import TestCase + +from model_mommy import mommy + +from small_small_hr.models import StaffProfile +from small_small_hr.signals import create_staffprofile + + +class TestSignals(TestCase): + """ + Test class for Scam models + """ + + def setUp(self): + """ + Setup the Signal tests + """ + post_save.connect(create_staffprofile, sender='auth.User', + dispatch_uid='create_staffprofile') + + def test_create_staffprofile(self): + """ + Test create_staffprofile + """ + user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe') + self.assertEqual( + user.staffprofile, + StaffProfile.objects.get(user=user) + )