-
Notifications
You must be signed in to change notification settings - Fork 0
/
alonwa.py
343 lines (289 loc) · 15.1 KB
/
alonwa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import datetime
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from auth import logout_from_alonwa, logout_from_cga
from cga import terminate_temp_from_cga, terminate_to_qualify_from_cga
from common import NavigationUiState, Status, Subscriber, println, InterventionState
from date_ui_state import DataUiState
"""
termination of temporary interventions
"""
def get_subs_phones_from_subs_ids(
driver: webdriver.Edge,
wait: WebDriverWait,
uis: NavigationUiState,
all_subs: list[Subscriber],
sub_ids: list[str],
d_uis: DataUiState
) -> bool:
# current_month = datetime.date.today().month
# current_year = datetime.date.today().year
# waiting and navigating to the intervention page where we can see all the pending interventions
menu_intervention = wait.until(ec.visibility_of_element_located(
(By.XPATH, "//li/a[@href='https://serviceplus.canal-plus.com/index.php?action=INTER_PENDING']"))
)
menu_intervention.click()
# Setting to show 100 intervention per page
driver.find_element(
By.XPATH, "//select[@name='tbl_inter_pending_length']/option[last()]").click()
wait.until_not(ec.visibility_of_element_located((By.ID, 'tbl_inter_pending_processing')))
set_start_date(driver, wait, d_uis.month_start, d_uis.year_start)
int_states = (InterventionState.TO_PLANIFY, InterventionState.TO_QUALIFY, InterventionState.TERMINATED_OK,
InterventionState.VALIDATED)
n = len(sub_ids)
for i, sub_id in enumerate(sub_ids):
println(f"{i + 1} sur {n}: Recherche du Tel pour l'abonné '{sub_id}'", Status.LOADING)
is_found = False
subscriber = Subscriber(sub_id=sub_id)
for state in int_states:
# Clicking the menu option inorder to get the temporary interventions
menu_intervention_type = wait.until(ec.visibility_of_element_located(
(By.ID, "intervention_status_select-button")))
menu_intervention_type.click()
temp_menu_opt = driver.find_element(By.ID, state.menu_item)
temp_menu_opt.click()
while True:
# Wait until the loading bar disappear and then Waiting till the table is fully loaded to select it
wait.until_not(ec.visibility_of_element_located((By.ID, 'tbl_inter_pending_processing')))
table = driver.find_element(By.ID, 'tbl_inter_pending')
all_rows = table.find_elements(By.XPATH, f".//tr[td[5]//span[contains(text(), '{sub_id}')]]")
println(f'{state.value}Found{len(all_rows)}')
if len(all_rows) != 0:
title = (all_rows[-1].find_element(By.XPATH, ".//td[5]")
.find_element(By.XPATH, ".//span").get_attribute(
'title'
))
subscriber.phone = title.split('-')[-1].strip()
is_found = True
break
else:
btn_next = driver.find_element(By.ID, 'tbl_inter_pending_next')
if 'ui-state-disabled' in btn_next.get_attribute('class'):
break
else:
btn_next.click()
if is_found:
break
all_subs.append(subscriber)
if is_found:
println(f"{all_subs[-1].phone}", Status.SUCCESS)
else:
println(f"Aucun résultat", Status.FAILED)
logout_from_alonwa(driver, wait, uis)
println("Terminé!", Status.SUCCESS)
return True
def set_start_date(driver: webdriver.Edge, wait: WebDriverWait, start_month: int, start_year: int):
current_date = driver.find_element(By.ID, 'intervention_to_datecrea').get_attribute("value").split("/")
current_year, current_month = int(current_date[2].strip()), int(current_date[1].strip())
start_date = driver.find_element(By.ID, 'intervention_from_datecrea')
start_date.click()
calendar = driver.find_element(By.CLASS_NAME, 'ui-datepicker-calendar')
first_day = calendar.find_element(By.XPATH, ".//td[a[contains(text(), '1')]]")
# We count the number of times to go back in time using the formula
# 12 * (current_year - start_year) - (current_month - start_month)
back_count = 12 * (current_year - start_year) + (current_month - start_month)
print(f" Bck count {back_count}")
for i in range(1, back_count):
driver.find_element(By.CSS_SELECTOR, "a.ui-datepicker-prev.ui-corner-all").click()
calendar = driver.find_element(By.CLASS_NAME, 'ui-datepicker-calendar')
first_day = calendar.find_element(By.XPATH, ".//td[a[contains(text(), '1')]]")
first_day.click()
driver.find_element(By.ID, 'btn_period_valid').click()
wait.until_not(ec.visibility_of_element_located((By.ID, 'tbl_inter_pending_processing')))
print("Done clicking")
def terminate_temp_in_alonwa(
driver: webdriver.Edge,
wait: WebDriverWait,
d_uis: DataUiState,
uis: NavigationUiState,
all_subs: list[Subscriber],
cga_driver: webdriver.Edge,
cga_wait: WebDriverWait,
max_operations: int = None
) -> bool:
current_month = datetime.date.today().month
current_year = datetime.date.today().year
# waiting and navigating to the intervention page where we can see all the pending interventions
menu_intervention = wait.until(ec.visibility_of_element_located(
(By.XPATH, "//li/a[@href='https://serviceplus.canal-plus.com/index.php?action=INTER_PENDING']"))
)
menu_intervention.click()
# Clicking the menu option inorder to get the temporary interventions
menu_intervention_type = wait.until(ec.visibility_of_element_located(
(By.ID, "intervention_status_select-button")))
menu_intervention_type.click()
temp_menu_opt = driver.find_element(By.ID, 'ui-id-7')
temp_menu_opt.click()
set_start_date(driver, wait, d_uis.month_start, d_uis.year_start)
# Setting to show 100 intervention per page
driver.find_element(
By.XPATH, "//select[@name='tbl_inter_pending_length']/option[last()]").click()
links = {}
ic = 0
# We try to click on the next button which will load the next page of 100 intervention
while True:
# Wait until the loading bar disappear and then Waiting till the table is fully loaded to select it
wait.until_not(ec.visibility_of_element_located(
(By.ID, 'tbl_inter_pending_processing')))
table = wait.until(ec.visibility_of_element_located(
(By.ID, 'tbl_inter_pending')))
# All the rows of intervention data except the 2 first rows which are just metadata
temp_links = table.find_elements(By.XPATH, "//a[@href]")
for link in temp_links:
href = link.get_attribute("href")
if "=GET_PROFIL_VIEW&" in href:
ic += 1
links[href] = href
next_button = wait.until(ec.element_to_be_clickable((By.ID, "tbl_inter_pending_next")))
next_button_class = str(next_button.get_attribute("class")).split(" ")[-1]
if next_button_class == "ui-state-disabled":
break
next_button.click()
uis.page_alonwa_interventions = driver.current_window_handle
# For each link, we click and get the tech id from profile then navigate to the planning of the technician
i = 0
num_tech = len(links.values())
println(f"{num_tech}")
println(f"Nombre total de Technicien = {num_tech}", Status.SUCCESS)
if not (max_operations is None or max_operations > num_tech):
num_tech = max_operations
println(f"Nombre a traiter = {num_tech}", Status.SUCCESS)
try:
for link in links.values():
i += 1
driver.execute_script(f"window.open('{link}')")
wait.until(ec.new_window_is_opened)
uis.page_alonwa_tech_profile = driver.window_handles[-1]
driver.switch_to.window(uis.page_alonwa_tech_profile)
tech_id = driver.find_element(By.ID, 'ID_TECH').text
tech_name = driver.find_element(By.ID, 'NOM').text
println(f"{i}/{num_tech}: [Nom = {tech_name}, Tech ID = {tech_id}]")
# Find and click on the see planning button for navigation then wait for the loading spinner to disappear
form = driver.find_elements(
By.XPATH, "//form[@action='https://serviceplus.canal-plus.com/index.php']")[-1]
form.submit()
wait.until_not(ec.visibility_of_element_located(
(By.ID, 'dialog_loader'))
)
uis.page_alonwa_tech_planning = driver.current_window_handle
back_count = 12 * (current_year - d_uis.year_start) + (current_month - d_uis.month_start)
# println(f"This is the count {back_count}", Status.SUCCESS)
k = 0
while k <= back_count:
k += 1
# We get all temporary interventions
all_events = driver.find_elements(
By.XPATH, "//div[@class='fc-event fc-event-hori fc-event-start fc-event-end']")
temp_events = []
# We filter out non-temporary events
for event in all_events:
text = event.find_element(By.CLASS_NAME, 'fc-event-title').text
if text.strip() == 'INTERVENTION':
temp_events += [event]
event_count = 0
for event in temp_events:
event_count += 1
print(f"\t\t{event_count}/{len(temp_events)} Temporary Event for {tech_name}")
event.click()
wait.until(ec.new_window_is_opened)
uis.page_alonwa_intervention_details = driver.window_handles[-1]
driver.switch_to.window(uis.page_alonwa_intervention_details)
wait.until(ec.visibility_of_element_located((By.ID, 'ui-id-11')))
subscriber = Subscriber()
# print(subscriber)
try:
subscriber = Subscriber(
tech_id=tech_id,
decoder_num=driver.find_element(By.ID, 'ref_decodeur0').get_attribute('value').strip()
)
# status = True
status = terminate_temp_from_cga(cga_driver, cga_wait, uis, subscriber)
if status:
println(f"{subscriber.decoder_num}: cloturé", Status.SUCCESS)
else:
println(
f"{subscriber.decoder_num}: non cloturé (valider, annuler ou inconnu)",
Status.FAILED)
except Exception as e:
println(f"Compte rendu incomplet, pas de numero decodeur", Status.FAILED)
all_subs.append(subscriber)
driver.close()
driver.switch_to.window(uis.page_alonwa_tech_planning)
# We finally go the previous month and continue
if k < back_count:
btn_prev = driver.find_element(By.CLASS_NAME, 'fc-button-prev')
btn_prev.click()
wait.until_not(ec.visibility_of_element_located((By.ID, 'dialog_loader')))
# Get all the events then only keep ones which are temporary only. A temporary event does not have a complete id
# E.g. Complete is "INTERVENTION: 21478798218" while incomplete is "INTERVENTION"
driver.close()
driver.switch_to.window(uis.page_alonwa_interventions)
except Exception as ex:
print(ex)
print("dfsadfdsa fsa dfdsafsadfsdafsadf")
logout_from_alonwa(driver, wait, uis)
logout_from_cga(cga_driver, cga_wait, uis)
println(f"{num_tech} clotures depuis {d_uis.month_start}/{d_uis.year_start} terminés")
return True
"""
Termination of a to qualify
"""
def terminate_qualify_from_alonwa(driver: webdriver.Edge, wait: WebDriverWait, uis: NavigationUiState,
all_subs: list[Subscriber], cga_driver: webdriver.Edge, cga_wait: WebDriverWait,
tech_ids: list[str], max: int | None, d_uis: DataUiState) -> bool:
# waiting and navigating to the intervention page where we can see all the pending interventions
menu_intervention = wait.until(ec.visibility_of_element_located(
(By.XPATH, "//li/a[@href='https://serviceplus.canal-plus.com/index.php?action=INTER_PENDING']")))
uis.is_login_alonwa = True
menu_intervention.click()
# Clicking the menu option inorder to get the temporary interventions
menu_intervention_type = wait.until(ec.visibility_of_element_located(
(By.ID, "intervention_status_select-button")))
menu_intervention_type.click()
to_qualify_menu_option = driver.find_element(By.ID, 'ui-id-4')
to_qualify_menu_option.click()
set_start_date(driver, wait, d_uis.month_start, d_uis.year_start)
# Setting to show 100 intervention per page
driver.find_element(
By.XPATH, "//select[@name='tbl_inter_pending_length']/option[last()]").click()
# Wait until the loading bar disappear and then Waiting till the table is fully loaded to select it
wait.until_not(ec.visibility_of_element_located(
(By.ID, 'tbl_inter_pending_processing')))
table = wait.until(ec.visibility_of_element_located(
(By.ID, 'tbl_inter_pending')))
# All the rows of intervention data except the 2 first rows which are just metadata
# we get the reference ids
all_rows = table.find_elements(By.TAG_NAME, "tr")[2:]
# A matrix of cells for each row
all_rows_cells = [row.find_elements(By.TAG_NAME, "td") for row in all_rows]
subscriber_ids = [cells[4].text.strip() for cells in all_rows_cells]
uis.page_alonwa_interventions = driver.current_window_handle
num_subs = len(subscriber_ids)
println(f"Nombre total de Interventions = {num_subs}", Status.SUCCESS)
println(f"Nombre a traiter = {num_subs}", Status.SUCCESS)
k = 1
# For each link, we click and get the tech id from profile then navigate to the planning of the technician
for i in range(0, num_subs):
subscriber = Subscriber(
sub_id=subscriber_ids[i]
)
println(f"{i + 1}/{num_subs}: [Abonné = {subscriber.sub_id}]")
try:
status = terminate_to_qualify_from_cga(
cga_driver, cga_wait, uis, subscriber, tech_ids)
if status:
k += 1
println(
f"Cloturer avec l'id = {subscriber.tech_id}", Status.SUCCESS)
else:
println(f"Aucun Id trouver pour cloturer", Status.FAILED)
except Exception as ex:
println("Unknown Error", Status.FAILED)
all_subs.append(subscriber)
logout_from_alonwa(driver, wait, uis)
logout_from_cga(cga_driver, cga_wait, uis)
println(f"{k}/{num_subs} Cloturer", Status.SUCCESS)
return True