Skip to content

Commit

Permalink
Merge pull request #55 from moshthepitt/decimal-days
Browse files Browse the repository at this point in the history
Use models.DecimalField second
  • Loading branch information
moshthepitt authored Jan 31, 2022
2 parents cecfe4f + f00aad2 commit 97252fd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
30 changes: 30 additions & 0 deletions small_small_hr/migrations/0011_auto_20220131_1640.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 3.1.2 on 2022-01-31 13:40

from decimal import Decimal
import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('small_small_hr', '0010_auto_20200626_1313'),
]

operations = [
migrations.AlterField(
model_name='annualleave',
name='allowed_days',
field=models.DecimalField(blank=True, decimal_places=1, default=21, help_text='Number of leave days allowed in a year.', max_digits=12, validators=[django.core.validators.MinValueValidator(Decimal('0.1'))], verbose_name='Allowed Leave days'),
),
migrations.AlterField(
model_name='annualleave',
name='carried_over_days',
field=models.DecimalField(blank=True, decimal_places=1, default=0, help_text='Number of leave days carried over into this year.', max_digits=12, validators=[django.core.validators.MinValueValidator(Decimal('0.1'))], verbose_name='Carried Over Leave days'),
),
migrations.AlterField(
model_name='annualleave',
name='year',
field=models.PositiveIntegerField(choices=[(2017, 2017), (2018, 2018), (2019, 2019), (2020, 2020), (2021, 2021), (2022, 2022), (2023, 2023), (2024, 2024), (2025, 2025), (2026, 2026), (2027, 2027), (2028, 2028), (2029, 2029), (2030, 2030), (2031, 2031)], db_index=True, default=2017, verbose_name='Year'),
),
]
11 changes: 9 additions & 2 deletions small_small_hr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.conf import settings
from django.contrib.postgres.fields import JSONField
from django.db import models
from django.core.validators import MinValueValidator
from django.db.models import Q
from django.utils import timezone
from django.utils.functional import cached_property
Expand Down Expand Up @@ -421,16 +422,22 @@ class AnnualLeave(TimeStampedModel, models.Model):
leave_type = models.CharField(
_("Type"), max_length=1, choices=Leave.TYPE_CHOICES, db_index=True
)
allowed_days = models.PositiveIntegerField(
allowed_days = models.DecimalField(
_("Allowed Leave days"),
default=21,
blank=True,
decimal_places=1,
max_digits=12,
validators=[MinValueValidator(Decimal('0.1'))],
help_text=_("Number of leave days allowed in a year."),
)
carried_over_days = models.PositiveIntegerField(
carried_over_days = models.DecimalField(
_("Carried Over Leave days"),
default=0,
blank=True,
decimal_places=1,
max_digits=12,
validators=[MinValueValidator(Decimal('0.1'))],
help_text=_("Number of leave days carried over into this year."),
)

Expand Down
44 changes: 44 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,50 @@ def test_annual_leave_form(self):
self.assertEqual(5, annual_leave.carried_over_days)
self.assertEqual(Leave.REGULAR, annual_leave.leave_type)

def test_annual_leave_form_decimals(self):
"""Test AnnualLeaveForm with decimal days."""
user = mommy.make("auth.User", first_name="Bob", last_name="Ndoe")
staffprofile = mommy.make("small_small_hr.StaffProfile", user=user)

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

data = {
"staff": staffprofile.id,
"year": 2018,
"leave_type": Leave.REGULAR,
"allowed_days": 16.5,
"carried_over_days": 8.5,
}

form = AnnualLeaveForm(data=data)
self.assertTrue(form.is_valid())
annual_leave = form.save()
self.assertEqual(staffprofile, annual_leave.staff)
self.assertEqual(2018, annual_leave.year)
self.assertEqual(16.5, annual_leave.allowed_days)
self.assertEqual(8.5, annual_leave.carried_over_days)
self.assertEqual(Leave.REGULAR, annual_leave.leave_type)

data2 = {
"staff": staffprofile.id,
"year": 2017,
"leave_type": Leave.REGULAR,
"allowed_days": 21,
"carried_over_days": 5,
}

form = AnnualLeaveForm(data=data2, instance=annual_leave)
self.assertTrue(form.is_valid())
form.save()
annual_leave.refresh_from_db()
self.assertEqual(staffprofile, annual_leave.staff)
self.assertEqual(2017, annual_leave.year)
self.assertEqual(21, annual_leave.allowed_days)
self.assertEqual(5, annual_leave.carried_over_days)
self.assertEqual(Leave.REGULAR, annual_leave.leave_type)

def test_role_form(self):
"""Test RoleForm."""
request = self.factory.get("/")
Expand Down

0 comments on commit 97252fd

Please sign in to comment.