Skip to content

Commit c1cb5d9

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 5972e59 commit c1cb5d9

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

core/signals.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,28 @@ def update_transition_state(**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.AVAILABLE, constants.ALLOCATED)
20+
valid_current_asset_types = (constants.DAMAGED, constants.LOST)
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 & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,90 +59,91 @@ def setUp(self):
5959
).objects.get_or_create(asset_incident_report_id=self.incident_report.id)
6060
self.transition_state = self.transition_state[0]
6161

62-
def test_update_asset_status_from_lost_to_available(self):
62+
def test_update_asset_status_from_available_to_lost(self):
6363
status = (
6464
apps.get_model("core", "AssetStatus")
6565
.objects.filter(asset=self.asset)
6666
.latest('created_at')
6767
)
6868
# update status to lost
69-
status.current_status = constants.LOST
69+
status.current_status = constants.AVAILABLE
7070
status.save()
7171
# update status to available
7272
status = (
7373
apps.get_model("core", "AssetStatus")
7474
.objects.filter(asset=self.asset)
7575
.latest('created_at')
7676
)
77-
status.current_status = constants.AVAILABLE
77+
status.current_status = constants.LOST
7878
status.save()
7979
# verify that transition state has been updated to closed
8080
transition_state = apps.get_model("core", "StateTransition").objects.get(
8181
asset_incident_report=self.incident_report
8282
)
8383
self.assertEqual(transition_state.incident_report_state, constants.CLOSED)
8484

85-
def test_update_asset_status_from_damaged_to_available(self):
85+
def test_update_asset_status_from_available_to_damaged(self):
8686
status = (
8787
apps.get_model("core", "AssetStatus")
8888
.objects.filter(asset=self.asset)
8989
.latest('created_at')
9090
)
9191

9292
# update status to lost
93-
status.current_status = constants.DAMAGED
93+
status.current_status = constants.AVAILABLE
9494
status.save()
9595
# update status to available
9696
status = (
9797
apps.get_model("core", "AssetStatus")
9898
.objects.filter(asset=self.asset)
9999
.latest('created_at')
100100
)
101-
status.current_status = constants.AVAILABLE
101+
status.current_status = constants.DAMAGED
102102
status.save()
103103
# verify that transition state has been updated to closed
104104
transition_state = apps.get_model("core", "StateTransition").objects.get(
105105
asset_incident_report=self.incident_report
106106
)
107107
self.assertEqual(transition_state.incident_report_state, constants.CLOSED)
108108

109-
def test_update_asset_status_from_lost_to_allocated(self):
109+
def test_update_asset_status_from_allocated_to_lost(self):
110110
status = (
111111
apps.get_model("core", "AssetStatus")
112112
.objects.filter(asset=self.asset)
113113
.latest('created_at')
114114
)
115115

116-
# update status to lost
117-
status.current_status = constants.LOST
118-
status.save()
119-
120116
# allocate asset to a user
121117
apps.get_model("core", "AllocationHistory").objects.create(
122118
asset=self.asset, current_assignee=self.asset_assignee2
123119
)
124120

121+
# update status to lost
122+
status.current_status = constants.LOST
123+
status.save()
124+
125125
transition_state = apps.get_model("core", "StateTransition").objects.get(
126126
asset_incident_report=self.incident_report
127127
)
128128

129129
self.assertEqual(transition_state.incident_report_state, constants.CLOSED)
130130

131-
def test_update_asset_status_from_damaged_to_allocated(self):
131+
def test_update_asset_status_from_allocated_to_damaged(self):
132132
status = (
133133
apps.get_model("core", "AssetStatus")
134134
.objects.filter(asset=self.asset)
135135
.latest('created_at')
136136
)
137137

138-
# update status to lost
139-
status.current_status = constants.DAMAGED
140-
status.save()
141138
# allocate asset to a user
142139
apps.get_model("core", "AllocationHistory").objects.create(
143140
asset=self.asset, current_assignee=self.asset_assignee2
144141
)
145142

143+
# update status to lost
144+
status.current_status = constants.DAMAGED
145+
status.save()
146+
146147
transition_state = apps.get_model("core", "StateTransition").objects.get(
147148
asset_incident_report=self.incident_report
148149
)

0 commit comments

Comments
 (0)