Skip to content

Commit

Permalink
Merge pull request #14 from moshthepitt/birFday
Browse files Browse the repository at this point in the history
Minor form fixes
  • Loading branch information
moshthepitt authored Jul 25, 2018
2 parents 93d4a8c + d27395e commit 01bbed1
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 2 deletions.
2 changes: 1 addition & 1 deletion small_small_hr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Main init file for small_small_hr
"""
VERSION = (0, 0, 8)
VERSION = (0, 0, 9)
__version__ = '.'.join(str(v) for v in VERSION)
8 changes: 8 additions & 0 deletions small_small_hr/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ class Meta: # pylint: disable=too-few-public-methods
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super().__init__(*args, **kwargs)
if self.instance and self.instance.file:
self.fields['file'].required = False
self.helper = FormHelper()
self.helper.form_tag = True
self.helper.form_method = 'post'
Expand Down Expand Up @@ -461,6 +463,8 @@ def __init__(self, *args, **kwargs):
else:
self.fields['staff'].queryset = StaffProfile.objects.filter(
id=self.request.user.staffprofile.id)
if self.instance and self.instance.file:
self.fields['file'].required = False
self.helper = FormHelper()
self.helper.form_tag = True
self.helper.form_method = 'post'
Expand Down Expand Up @@ -527,6 +531,8 @@ class Meta: # pylint: disable=too-few-public-methods
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super().__init__(*args, **kwargs)
if self.instance and self.instance.image:
self.fields['image'].required = False
self.helper = FormHelper()
self.helper.form_tag = True
self.helper.form_method = 'post'
Expand Down Expand Up @@ -678,6 +684,8 @@ class Meta: # pylint: disable=too-few-public-methods
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super().__init__(*args, **kwargs)
if self.instance and self.instance.image:
self.fields['image'].required = False
self.helper = FormHelper()
self.helper.form_tag = True
self.helper.form_method = 'post'
Expand Down
19 changes: 19 additions & 0 deletions small_small_hr/migrations/0004_auto_20180725_2127.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.0.7 on 2018-07-25 18:27

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('small_small_hr', '0003_staffdocument_public'),
]

operations = [
migrations.AlterField(
model_name='staffprofile',
name='birthday',
field=models.DateField(
blank=True, default=None, null=True, verbose_name='Birthday'),
),
]
2 changes: 1 addition & 1 deletion small_small_hr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class StaffProfile(TimeStampedModel, models.Model):
on_delete=models.SET_NULL)
phone = PhoneNumberField(_('Phone'), blank=True, default='')
address = models.TextField(_('Addresss'), blank=True, default="")
birthday = models.DateField(_('Birth day'), blank=True, default=None,
birthday = models.DateField(_('Birthday'), blank=True, default=None,
null=True)
leave_days = models.PositiveIntegerField(
_('Leave days'), default=21, blank=True,
Expand Down
140 changes: 140 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,15 @@ def test_staffdocumentform(self):
with open(path, 'r+b') as contract_file:
self.assertTrue(contract_file.read(), doc.file.read())

# on updating it, check that file is not required
data2 = {
'staff': staffprofile.id,
'name': 'Employment Contract',
'description': 'This is the employment contract!'
}
form2 = StaffDocumentForm(data=data2, instance=doc, request=request)
self.assertTrue(form2.is_valid())

@override_settings(PRIVATE_STORAGE_ROOT='/tmp/')
def test_userstaffdocumentform(self):
"""
Expand Down Expand Up @@ -776,6 +785,15 @@ def test_userstaffdocumentform(self):
with open(path, 'r+b') as contract_file:
self.assertTrue(contract_file.read(), doc.file.read())

# on updating it, check that file is not required
data2 = {
'staff': staffprofile.id,
'name': 'Employment Contract',
'description': 'This is the employment contract!'
}
form2 = StaffDocumentForm(data=data2, instance=doc, request=request)
self.assertTrue(form2.is_valid())

def test_staff_profile_user_form(self):
"""
Test StaffProfileUserForm
Expand Down Expand Up @@ -847,6 +865,64 @@ def test_staff_profile_user_form(self):
with open(path, 'r+b') as image_file:
self.assertTrue(image_file.read(), staffprofile.image.read())

def test_staffprofile_user_form_no_image(self):
"""
Test StaffProfileUserForm image not required on update
"""
user = mommy.make('auth.User')
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)

request = self.factory.get('/')
request.session = {}
request.user = AnonymousUser()

path = os.path.join(
BASE_DIR, 'tests', 'fixtures', 'profile.png')

with open(path, 'r+b') as image_file:
data = {
'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_relationship': 'Father',
'emergency_contact_number': '+254722111111',
'phone': '+254722111111',
'address': 'This is the address.',
'birthday': '1996-01-27',
'image': image_file,
}

file_dict = {
'image': SimpleUploadedFile(
name=image_file.name,
content=image_file.read(),
content_type='image/png'
)}

form = StaffProfileUserForm(data=data, instance=staffprofile,
request=request, files=file_dict)
self.assertTrue(form.is_valid())
form.save()

staffprofile.refresh_from_db()
data2 = {
'first_name': 'Bobbie',
'last_name': 'B',
'id_number': 6666,
}

form2 = StaffProfileUserForm(data=data2, instance=staffprofile,
request=request)
self.assertTrue(form2.is_valid())
form2.save()
staffprofile.refresh_from_db()
self.assertEqual('Bobbie B', user.staffprofile.get_name())

def test_staff_profile_admin_create_form(self):
"""
Test StaffProfileAdminCreateForm
Expand Down Expand Up @@ -1004,6 +1080,70 @@ def test_staff_profile_admin_form(self):
with open(path, 'r+b') as image_file:
self.assertTrue(image_file.read(), staffprofile.image.read())

def test_staffprofile_admin_form_no_image(self):
"""
Test StaffProfileAdminForm image not required when editting
"""
user = mommy.make('auth.User')
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)

request = self.factory.get('/')
request.session = {}
request.user = AnonymousUser()

path = os.path.join(
BASE_DIR, 'tests', 'fixtures', 'profile.png')

with open(path, 'r+b') as image_file:
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',
'image': image_file,
}

file_dict = {
'image': SimpleUploadedFile(
name=image_file.name,
content=image_file.read(),
content_type='image/png'
)}

form = StaffProfileAdminForm(data=data, instance=staffprofile,
request=request, files=file_dict)
self.assertTrue(form.is_valid())
form.save()

staffprofile.refresh_from_db()
data2 = {
'user': user.id,
'first_name': 'Bobbie',
'last_name': 'B',
'id_number': 6666,
}

form2 = StaffProfileAdminForm(data=data2, instance=staffprofile,
request=request)
self.assertTrue(form2.is_valid())
form2.save()
staffprofile.refresh_from_db()
self.assertEqual('Bobbie B', user.staffprofile.get_name())

def test_staffprofile_unique_pin_number(self):
"""
Test unique pin_number
Expand Down

0 comments on commit 01bbed1

Please sign in to comment.