|
| 1 | +"""Module for handling cases in eFlyt""" |
| 2 | +from dataclasses import dataclass |
| 3 | +from datetime import date, datetime |
| 4 | + |
| 5 | +from selenium import webdriver |
| 6 | +from selenium.webdriver.common.by import By |
| 7 | + |
| 8 | + |
| 9 | +@dataclass |
| 10 | +class Case: |
| 11 | + """A dataclass representing an Eflyt case.""" |
| 12 | + case_number: str |
| 13 | + deadline: date | None |
| 14 | + case_types: list[str] |
| 15 | + |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class Inhabitant: |
| 19 | + """A dataclass representing an inhabitant.""" |
| 20 | + cpr: str |
| 21 | + name: str |
| 22 | + move_in_date: date |
| 23 | + relations: list[str] |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class Applicant: |
| 28 | + """A dataclass representing an applicant.""" |
| 29 | + cpr: str |
| 30 | + name: str |
| 31 | + |
| 32 | + |
| 33 | +def get_beboere(browser: webdriver.Chrome) -> list[Inhabitant]: |
| 34 | + """Get a list of current inhabitants on the case currently open. |
| 35 | +
|
| 36 | + Args: |
| 37 | + browser: The webdriver browser object. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + A list of Inhabitants. |
| 41 | + """ |
| 42 | + # Go to the correct tab and scrape data |
| 43 | + change_tab(browser, 1) |
| 44 | + beboer_table = browser.find_element(By.ID, "ctl00_ContentPlaceHolder2_ptFanePerson_becPersonTab_GridViewBeboere") |
| 45 | + rows = beboer_table.find_elements(By.TAG_NAME, "tr") |
| 46 | + |
| 47 | + # Remove header |
| 48 | + rows.pop(0) |
| 49 | + |
| 50 | + inhabitants = [] |
| 51 | + for inhabitant in rows: |
| 52 | + # Get date for moving in, CPR and name |
| 53 | + moving_in = datetime.strptime(inhabitant.find_element(By.XPATH, "td[1]/span | td[1]/a").text, "%d-%m-%Y").date() |
| 54 | + cpr = inhabitant.find_element(By.XPATH, "td[2]").text.replace("-", "") |
| 55 | + name = inhabitant.find_element(By.XPATH, "td[3]").text |
| 56 | + |
| 57 | + # Check for a list of relations |
| 58 | + relations = [] |
| 59 | + elements = inhabitant.find_elements(By.XPATH, "td[4]/span") |
| 60 | + if elements: |
| 61 | + relations = elements[0].text.replace("<br>", ";").replace("\n", ";").split(";") |
| 62 | + |
| 63 | + # Create an Inhabitant and add to list |
| 64 | + new_inhabitant = Inhabitant(cpr, name, moving_in, relations) |
| 65 | + inhabitants.append(new_inhabitant) |
| 66 | + |
| 67 | + return inhabitants |
| 68 | + |
| 69 | + |
| 70 | +def get_room_count(browser: webdriver.Chrome) -> int: |
| 71 | + """Get the number of rooms on the address. |
| 72 | +
|
| 73 | + Args: |
| 74 | + browser: The webdriver browser object. |
| 75 | +
|
| 76 | + Returns: |
| 77 | + The number of rooms on the address. |
| 78 | + """ |
| 79 | + change_tab(browser, 1) |
| 80 | + area_room_text = browser.find_element(By.ID, "ctl00_ContentPlaceHolder2_ptFanePerson_stcPersonTab6_lblAreaText").text |
| 81 | + room_text = area_room_text.split("/")[1] |
| 82 | + return int(room_text) |
| 83 | + |
| 84 | + |
| 85 | +def get_applicants(browser: webdriver.Chrome) -> list[Applicant]: |
| 86 | + """Get a list of applicants' cpr numbers from the applicant table. |
| 87 | +
|
| 88 | + Args: |
| 89 | + browser: The webdriver browser object. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + A list of cpr numbers. |
| 93 | + """ |
| 94 | + table = browser.find_element(By.ID, "ctl00_ContentPlaceHolder2_GridViewMovingPersons") |
| 95 | + rows = table.find_elements(By.TAG_NAME, "tr") |
| 96 | + |
| 97 | + # Remove header row |
| 98 | + rows.pop(0) |
| 99 | + |
| 100 | + applicants = [] |
| 101 | + |
| 102 | + for row in rows: |
| 103 | + cpr = row.find_element(By.XPATH, "td[2]/a[2]").text.replace("-", "") |
| 104 | + name = row.find_element(By.XPATH, "td[3]/a").text |
| 105 | + applicant = Applicant(cpr, name) |
| 106 | + applicants.append(applicant) |
| 107 | + |
| 108 | + return applicants |
| 109 | + |
| 110 | + |
| 111 | +def change_tab(browser: webdriver.Chrome, tab_index: int): |
| 112 | + """Change the tab in the case view e.g. 'Sagslog', 'Breve'. |
| 113 | +
|
| 114 | + Args: |
| 115 | + browser: The webdriver browser object. |
| 116 | + tab_index: The zero-based index of the tab to select. |
| 117 | + """ |
| 118 | + browser.execute_script(f"__doPostBack('ctl00$ContentPlaceHolder2$ptFanePerson$ImgJournalMap','{tab_index}')") |
0 commit comments