Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified is_late to check is_billable #828

Merged
merged 1 commit into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion tock/employees/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def save(self, *args, **kwargs):
)
super(EmployeeGrade, self).save(*args, **kwargs)


class UserData(models.Model):

UNIT_CHOICES = (
Expand Down Expand Up @@ -141,8 +142,14 @@ def is_late(self):
most recent Reporting Period.

We're using get_model() to avoid circular imports
since so many things use UserData
since so many things use UserData.

If they're not billable staff, they don't have to tock,
so we'll just bail out.
"""
if not self.current_employee or not self.is_billable:
return False
# They are billable, so go ahead with other checks.
RP_model = apps.get_model('hours', 'ReportingPeriod')
TC_model = apps.get_model('hours', 'Timecard')
rp = RP_model.objects.order_by('end_date').filter(
Expand Down
55 changes: 24 additions & 31 deletions tock/employees/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,27 @@ def setUp(self):
is_18f_employee=True,
current_employee=True
)
# Create a sample reporting period
self.reporting_period = ReportingPeriod.objects.create(
start_date=datetime.date(2015, 1, 1),
end_date=datetime.date(2015, 1, 7),
exact_working_hours=40,
min_working_hours=40,
max_working_hours=60,
message='This is not a vacation'
)
# Create API token for regular_user.
self.token = Token.objects.create(user=self.regular_user)

def test_string_method(self):
"""Check that string method override works correctly."""
userdata = UserData.objects.get(
user=self.regular_user
)
userdata = self.regular_user_userdata
expected_string = str(userdata.user.username)
self.assertEqual(expected_string, str(userdata))

def test_user_data_is_stored(self):
""" Check that user data was stored correctly """
userdata = UserData.objects.get(user=self.regular_user)
userdata = self.regular_user_userdata
self.assertEqual(
userdata.start_date,
datetime.date(2014, 1, 1)
Expand All @@ -88,53 +95,39 @@ def test_user_data_is_stored(self):

def test_is_late(self):
""" Check if the user is late when no Timecard is present """
userdata = UserData.objects.get(user=self.regular_user)
reporting_period = ReportingPeriod(
start_date=datetime.date(2015, 1, 1),
end_date=datetime.date(2015, 1, 7),
exact_working_hours=40,
min_working_hours=40,
max_working_hours=60,
message='This is not a vacation')
reporting_period.save()
userdata = self.regular_user_userdata
self.assertEqual(userdata.is_late, True)
# Now set is_billable to false and re-check:
userdata.is_billable = False
userdata.save()
self.assertEqual(userdata.is_late, False)

def test_is_not_late(self):
""" Check if the user is not late when Timecard is present """
userdata = UserData.objects.get(user=self.regular_user)
reporting_period = ReportingPeriod.objects.create(
start_date=datetime.date(2015, 1, 1),
end_date=datetime.date(2015, 1, 7),
exact_working_hours=40,
min_working_hours=40,
max_working_hours=60,
message='This is not a vacation')
reporting_period.save()
userdata = self.regular_user_userdata
timecard = Timecard.objects.create(
user=self.regular_user,
reporting_period=reporting_period)
reporting_period=self.reporting_period,
submitted=True
)
project = Project.objects.get(name="Platform as a Service")
timecard.submitted = True
timecard.save()
TimecardObject.objects.create(
timecard=timecard,
project=project,
hours_spent=40)

self.assertEqual(userdata.is_late, False)

def test_employee_active(self):
""" Check that the save() method correctly aligns UserData and User
attributes when current_employee is True."""
user = User.objects.get(
username=self.regular_user.username)
user = self.regular_user
user.is_active = False
user.save()
status_before_save = User.objects.get(
username=self.regular_user.username).is_active
status_before_save = user.is_active
self.regular_user_userdata.current_employee = True
self.regular_user_userdata.save()

# now re-get the user object so we can see if the status
# changed when userdata changed.
status_after_save = User.objects.get(
username=self.regular_user.username).is_active
self.assertNotEqual(status_before_save, status_after_save)
Expand Down