1
1
"""Disable work start date action handler"""
2
+
2
3
from datetime import timedelta
3
4
from typing import List
5
+ from operator import attrgetter
4
6
from sqlalchemy import and_
5
7
6
8
from api .actions .base import ActionFactory
7
9
from api .models import Event , EventConfiguration , WorkPhase , Work , db
8
10
from api .models .phase_code import PhaseCode , PhaseVisibilityEnum
9
- from api .models .event_template import EventTemplateVisibilityEnum , EventPositionEnum
11
+ from api .models .event_template import (
12
+ EventTemplateVisibilityEnum ,
13
+ EventPositionEnum ,
14
+ )
15
+ from api .models .event_category import PRIMARY_CATEGORIES
10
16
from api .services .event_template import EventTemplateService
11
17
from api .schemas import response as res
12
18
@@ -24,7 +30,10 @@ def run(self, source_event: Event, params) -> None:
24
30
number_of_phases = len (params )
25
31
# Push the sort order of the existing work phases after the new ones
26
32
self .preset_sort_order (source_event , number_of_phases )
27
- phase_start_date = source_event .actual_date + timedelta (days = 1 )
33
+ number_of_days_to_be_added = self ._get_number_of_days_to_be_added (source_event )
34
+ phase_start_date = source_event .actual_date + timedelta (
35
+ days = number_of_days_to_be_added
36
+ )
28
37
sort_order = source_event .event_configuration .work_phase .sort_order + 1
29
38
total_number_of_days = 0
30
39
for param in params :
@@ -43,22 +52,15 @@ def run(self, source_event: Event, params) -> None:
43
52
"sort_order" : sort_order ,
44
53
}
45
54
)
46
- event_templates_for_the_phase = EventTemplateService .find_by_phase_id (work_phase_data .get ("phase_id" ))
47
- event_templates_for_the_phase_json = res .EventTemplateResponseSchema (many = True ).dump (
48
- event_templates_for_the_phase
55
+ event_templates_for_the_phase = EventTemplateService .find_by_phase_id (
56
+ work_phase_data .get ("phase_id" )
57
+ )
58
+ event_templates_for_the_phase_json = res .EventTemplateResponseSchema (
59
+ many = True
60
+ ).dump (event_templates_for_the_phase )
61
+ work_phase = WorkService .create_events_by_template (
62
+ work_phase_data , event_templates_for_the_phase_json
49
63
)
50
- # event_configurations = self.get_configurations(source_event, param)
51
- # work_phase = WorkPhase.flush(WorkPhase(**work_phase_data))
52
- # event_configurations = res.EventConfigurationResponseSchema(many=True).dump(
53
- # event_configurations
54
- # )
55
- work_phase = WorkService .create_events_by_template (work_phase_data , event_templates_for_the_phase_json )
56
- # new_event_configurations = WorkService.create_configurations(
57
- # work_phase, event_configurations, False
58
- # )
59
- # WorkService.create_events_by_configuration(
60
- # work_phase, new_event_configurations
61
- # )
62
64
sort_order = sort_order + 1
63
65
phase_start_date = end_date + timedelta (days = 1 )
64
66
# update the current work phase
@@ -73,6 +75,32 @@ def run(self, source_event: Event, params) -> None:
73
75
if work_phase :
74
76
self .update_susequent_work_phases (work_phase )
75
77
78
+ def _get_number_of_days_to_be_added (self , source_event ) -> int :
79
+ """Returns the phase start date"""
80
+ if source_event .event_position == EventPositionEnum .INTERMEDIATE .value :
81
+ work_phase_id = source_event .event_configuration .work_phase .id
82
+ event_configurations = EventConfiguration .find_by_params (
83
+ {
84
+ "work_phase_id" : work_phase_id ,
85
+ "visibility" : EventTemplateVisibilityEnum .MANDATORY .value ,
86
+ }
87
+ )
88
+ primary_categories = list (map (lambda x : x .value , PRIMARY_CATEGORIES ))
89
+ filtered_configurations = [
90
+ template
91
+ for template in event_configurations
92
+ if template .event_category_id in primary_categories
93
+ and template .sort_order > source_event .event_configuration .sort_order
94
+ ]
95
+ last_mandatory_configuration = max (
96
+ filtered_configurations , key = attrgetter ("sort_order" )
97
+ )
98
+ return (
99
+ int (last_mandatory_configuration .start_at )
100
+ - int (source_event .event_configuration .start_at )
101
+ ) + 1
102
+ return 1
103
+
76
104
def preset_sort_order (self , source_event , number_of_new_work_phases : int ) -> None :
77
105
"""Adjust the sort order of the existing work phases for the new ones"""
78
106
db .session .query (WorkPhase ).filter (
@@ -130,9 +158,9 @@ def update_susequent_work_phases(
130
158
# Update the start event anticipated date by the number of total days added by all the new phases
131
159
# This will push all the susequent work phases and all the events
132
160
start_event_dict = start_event .as_dict (recursive = False )
133
- start_event_dict [
134
- "anticipated_date"
135
- ] = latest_work_phase_added . end_date + timedelta ( days = 1 )
161
+ start_event_dict ["anticipated_date" ] = (
162
+ latest_work_phase_added . end_date + timedelta ( days = 1 )
163
+ )
136
164
EventService .update_event (
137
165
start_event_dict , start_event .id , True , commit = False
138
166
)
@@ -159,7 +187,9 @@ def get_additional_params(self, source_event, params):
159
187
}
160
188
return work_phase_data
161
189
162
- def get_configurations (self , source_event : Event , params ) -> List [EventConfiguration ]:
190
+ def get_configurations (
191
+ self , source_event : Event , params
192
+ ) -> List [EventConfiguration ]:
163
193
"""Find the latest event configurations per the given params"""
164
194
work_phase = (
165
195
db .session .query (WorkPhase )
0 commit comments