|
1 | 1 | # Third-Party Imports
|
2 |
| -from django.db.models.signals import post_save |
| 2 | +from django.db.models.signals import pre_save |
3 | 3 | from django.dispatch import receiver
|
4 | 4 |
|
5 | 5 | # App Imports
|
6 | 6 | from core import constants
|
7 |
| -from core.models import AssetIncidentReport, StateTransition |
| 7 | +from core.models import AssetIncidentReport, AssetStatus, StateTransition |
8 | 8 |
|
9 |
| -# TODO add functionality to close incident report when asset statuses are changed. story clarification being awaited |
10 |
| -# @receiver(post_save, sender=AssetStatus) |
11 |
| -# def close_incident_report(**kwargs): |
12 |
| -# """ |
13 |
| -# close an incident report if asset status is updated to [`damaged`,`lost`,'available] |
14 |
| -# :param kwargs: |
15 |
| -# :return: |
16 |
| -# """ |
17 |
| -# instance = kwargs.get("instance") |
18 |
| -# current_asset_status = instance.current_status |
19 |
| -# |
20 |
| -# valid_asset_states = [constants.DAMAGED, constants.LOST, constants.AVAILABLE] |
21 |
| -# |
22 |
| -# if current_asset_status in valid_asset_states: |
23 |
| -# if current_asset_status == constants.AVAILABLE: |
24 |
| -# # if an incident report already exists, update the transitional state to closed |
25 |
| -# try: |
26 |
| -# incident_report = AssetIncidentReport.objects.get(asset=instance.asset) |
27 |
| -# transition_state = StateTransition.objects.get_or_create(asset_incident_report=incident_report) |
28 |
| -# transition_state.incident_report_state = constants.CLOSED |
29 |
| -# transition_state.save() |
30 |
| -# except AssetIncidentReport.DoesNotExist: |
31 |
| -# pass |
32 |
| -# else: |
33 |
| -# # incident_report = |
34 |
| -# pass |
35 | 9 |
|
36 |
| - |
37 |
| -@receiver(post_save, sender=AssetIncidentReport) |
| 10 | +@receiver(pre_save, sender=AssetStatus) |
38 | 11 | def update_transition_state(**kwargs):
|
39 | 12 | """
|
40 |
| - update the transition state when an Asset incident report is filed |
| 13 | + update the transition state to CLOSED when an Asset status is changed from (DAMAGED,LOST) to |
| 14 | + either (AVAILABLE,ALLOCATED) |
41 | 15 | :param kwargs:
|
42 | 16 | :return None:
|
43 | 17 | """
|
44 | 18 | instance = kwargs.get("instance") # the instance of the incident report being saved
|
45 |
| - current_incident_type = instance.incident_type |
46 |
| - transition_state = StateTransition.objects.get_or_create( |
47 |
| - asset_incident_report=instance |
48 |
| - ) |
49 |
| - transition_state = transition_state[0] |
50 |
| - current_transition_state = transition_state.incident_report_state |
| 19 | + valid_previous_asset_types = (constants.DAMAGED, constants.LOST) |
| 20 | + valid_current_asset_types = (constants.AVAILABLE, constants.ALLOCATED) |
| 21 | + |
| 22 | + try: |
| 23 | + previous_instance = AssetStatus.objects.get(id=instance.id) |
| 24 | + current_asset_status = instance.current_status |
| 25 | + previous_asset_status = previous_instance.current_status |
| 26 | + |
| 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 | + |
| 43 | + except AssetStatus.DoesNotExist: |
| 44 | + pass |
51 | 45 |
|
52 |
| - # tuples of states for which the logic of this signal is triggered |
53 |
| - valid_incident_types = (constants.LOSS, constants.DAMAGE) |
54 |
| - valid_transition_states = ( |
55 |
| - constants.INTERNAL_ASSESSMENT, |
56 |
| - constants.EXTERNAL_ASSESSMENT, |
57 |
| - constants.OUT_FOR_REPAIR, |
58 |
| - ) |
59 |
| - # if the current incident type and transition state are valid, update the state |
60 |
| - if ( |
61 |
| - current_incident_type in valid_incident_types |
62 |
| - and current_transition_state in valid_transition_states |
63 |
| - ): |
64 |
| - transition_state.incident_report_state = constants.CLOSED |
65 |
| - transition_state.save() |
| 46 | + # current_incident_type = instance.incident_type |
| 47 | + # transition_state = StateTransition.objects.get_or_create( |
| 48 | + # asset_incident_report=instance |
| 49 | + # ) |
| 50 | + # transition_state = transition_state[0] |
| 51 | + # current_transition_state = transition_state.incident_report_state |
| 52 | + # |
| 53 | + # # tuples of states for which the logic of this signal is triggered |
| 54 | + # valid_incident_types = (constants.LOSS, constants.DAMAGE) |
| 55 | + # valid_transition_states = ( |
| 56 | + # constants.INTERNAL_ASSESSMENT, |
| 57 | + # constants.EXTERNAL_ASSESSMENT, |
| 58 | + # constants.OUT_FOR_REPAIR, |
| 59 | + # ) |
| 60 | + # # if the current incident type and transition state are valid, update the state |
| 61 | + # if ( |
| 62 | + # current_incident_type in valid_incident_types |
| 63 | + # and current_transition_state in valid_transition_states |
| 64 | + # ): |
| 65 | + # transition_state.incident_report_state = constants.CLOSED |
| 66 | + # transition_state.save() |
0 commit comments