Skip to content

Commit e4fec7c

Browse files
ft-add-notifications: Add notifications model
- Add notifications model - add signals to create notifications - add unit tests - configure django_q and django_cache - resolve merge conflicts - update README.md - apply PR feedback - remove curly braces from notification body template - update functionality to close transition report on asset status change to `lost` or `damaged` [Delivers #161678177]
1 parent 4ef9484 commit e4fec7c

File tree

2 files changed

+38
-80
lines changed

2 files changed

+38
-80
lines changed

core/signals.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,34 @@
1010
@receiver(post_save, sender=AssetStatus)
1111
def update_transition_state(**kwargs):
1212
"""
13-
update the transition state to CLOSED when an Asset status is changed from (DAMAGED,LOST) to
14-
either (AVAILABLE,ALLOCATED)
13+
update the transition state to CLOSED when an Asset status is changed from ALLOCATED to
14+
either (AVAILABLE,DAMAGED,LOST)
1515
:param kwargs:
1616
:return None:
1717
"""
1818
instance = kwargs.get("instance") # the instance of the incident report being saved
19-
valid_previous_asset_types = (constants.DAMAGED, constants.LOST)
20-
valid_current_asset_types = (constants.AVAILABLE, constants.ALLOCATED)
19+
valid_previous_asset_types = (constants.ALLOCATED,)
20+
valid_current_asset_types = (constants.DAMAGED, constants.LOST, constants.AVAILABLE)
2121

2222
# previous_instance = AssetStatus.objects.filter(id=instance.id).latest('created_at')
2323
current_asset_status = instance.current_status
2424
previous_asset_status = instance.previous_status
2525

26-
if previous_asset_status in valid_previous_asset_types:
27-
if current_asset_status in valid_current_asset_types:
28-
# get the latest incident report
29-
# filter out all results that dont have a recorded created_at date
30-
latest_incident_report = AssetIncidentReport.objects.filter(
31-
asset=instance.asset, created_at__isnull=False
32-
).latest('created_at')
33-
# get the transition state associated with the incident report
34-
transition_state = StateTransition.objects.get_or_create(
35-
asset_incident_report=latest_incident_report
36-
)
37-
transition_state = transition_state[0]
38-
# update the transition state
39-
transition_state.incident_report_state = constants.CLOSED
40-
transition_state.save()
26+
try:
27+
if previous_asset_status in valid_previous_asset_types:
28+
if current_asset_status in valid_current_asset_types:
29+
# get the latest incident report
30+
# filter out all results that dont have a recorded created_at date
31+
latest_incident_report = AssetIncidentReport.objects.filter(
32+
asset=instance.asset, created_at__isnull=False
33+
).latest('created_at')
34+
# get the transition state associated with the incident report
35+
transition_state = StateTransition.objects.get_or_create(
36+
asset_incident_report=latest_incident_report
37+
)
38+
transition_state = transition_state[0]
39+
# update the transition state
40+
transition_state.incident_report_state = constants.CLOSED
41+
transition_state.save()
42+
except AssetIncidentReport.DoesNotExist:
43+
pass

core/tests/test_state_transition_model.py

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ def test_create_incident_report_creates_new_state_transition(self):
3939

4040
class TestTransitionStateUpdateFromAssetStatusModification(CoreBaseTestCase):
4141
"""
42-
Test updating of transition state when Asset status is modified
43-
Update the transition state to CLOSED when an Asset status is changed from (DAMAGED,LOST) to
44-
either (AVAILABLE,ALLOCATED)
42+
Test updating the transition state to CLOSED when an Asset status is changed from ALLOCATED to
43+
either (AVAILABLE,DAMAGED,LOST)
4544
"""
4645

4746
def setUp(self):
@@ -59,90 +58,44 @@ def setUp(self):
5958
).objects.get_or_create(asset_incident_report_id=self.incident_report.id)
6059
self.transition_state = self.transition_state[0]
6160

62-
def test_update_asset_status_from_lost_to_available(self):
63-
status = (
64-
apps.get_model("core", "AssetStatus")
65-
.objects.filter(asset=self.asset)
66-
.latest('created_at')
67-
)
68-
# update status to lost
69-
status.current_status = constants.LOST
70-
status.save()
71-
# update status to available
72-
status = (
73-
apps.get_model("core", "AssetStatus")
74-
.objects.filter(asset=self.asset)
75-
.latest('created_at')
76-
)
77-
status.current_status = constants.AVAILABLE
78-
status.save()
79-
# verify that transition state has been updated to closed
80-
transition_state = apps.get_model("core", "StateTransition").objects.get(
81-
asset_incident_report=self.incident_report
82-
)
83-
self.assertEqual(transition_state.incident_report_state, constants.CLOSED)
84-
85-
def test_update_asset_status_from_damaged_to_available(self):
61+
def test_update_asset_status_from_allocated_to_lost(self):
8662
status = (
8763
apps.get_model("core", "AssetStatus")
8864
.objects.filter(asset=self.asset)
8965
.latest('created_at')
9066
)
9167

92-
# update status to lost
93-
status.current_status = constants.DAMAGED
94-
status.save()
95-
# update status to available
96-
status = (
97-
apps.get_model("core", "AssetStatus")
98-
.objects.filter(asset=self.asset)
99-
.latest('created_at')
100-
)
101-
status.current_status = constants.AVAILABLE
102-
status.save()
103-
# verify that transition state has been updated to closed
104-
transition_state = apps.get_model("core", "StateTransition").objects.get(
105-
asset_incident_report=self.incident_report
106-
)
107-
self.assertEqual(transition_state.incident_report_state, constants.CLOSED)
108-
109-
def test_update_asset_status_from_lost_to_allocated(self):
110-
status = (
111-
apps.get_model("core", "AssetStatus")
112-
.objects.filter(asset=self.asset)
113-
.latest('created_at')
68+
# allocate asset to a user
69+
apps.get_model("core", "AllocationHistory").objects.create(
70+
asset=self.asset, current_assignee=self.asset_assignee2
11471
)
11572

11673
# update status to lost
11774
status.current_status = constants.LOST
11875
status.save()
11976

120-
# allocate asset to a user
121-
apps.get_model("core", "AllocationHistory").objects.create(
122-
asset=self.asset, current_assignee=self.asset_assignee2
123-
)
124-
12577
transition_state = apps.get_model("core", "StateTransition").objects.get(
12678
asset_incident_report=self.incident_report
12779
)
12880

12981
self.assertEqual(transition_state.incident_report_state, constants.CLOSED)
13082

131-
def test_update_asset_status_from_damaged_to_allocated(self):
83+
def test_update_asset_status_from_allocated_to_damaged(self):
13284
status = (
13385
apps.get_model("core", "AssetStatus")
13486
.objects.filter(asset=self.asset)
13587
.latest('created_at')
13688
)
13789

138-
# update status to lost
139-
status.current_status = constants.DAMAGED
140-
status.save()
14190
# allocate asset to a user
14291
apps.get_model("core", "AllocationHistory").objects.create(
14392
asset=self.asset, current_assignee=self.asset_assignee2
14493
)
14594

95+
# update status to DAMAGED
96+
status.current_status = constants.DAMAGED
97+
status.save()
98+
14699
transition_state = apps.get_model("core", "StateTransition").objects.get(
147100
asset_incident_report=self.incident_report
148101
)
@@ -167,9 +120,11 @@ def test_update_asset_status_from_allocated_to_available(self):
167120
)
168121
self.assertEqual(status.current_status, constants.ALLOCATED)
169122

123+
# update status to AVAILABLE
124+
status.current_status = constants.AVAILABLE
125+
status.save()
126+
170127
transition_state = apps.get_model("core", "StateTransition").objects.get(
171128
asset_incident_report=self.incident_report
172129
)
173-
self.assertEqual(
174-
transition_state.incident_report_state, constants.NEWLY_REPORTED
175-
)
130+
self.assertEqual(transition_state.incident_report_state, constants.CLOSED)

0 commit comments

Comments
 (0)