|
| 1 | +import keyring |
| 2 | +import getpass |
| 3 | +import os |
| 4 | +import re |
| 5 | +import time |
| 6 | +from selenium import webdriver |
| 7 | +from selenium.webdriver.chrome.service import Service |
| 8 | +from selenium.webdriver.common.by import By |
| 9 | +from selenium.webdriver.support.ui import WebDriverWait |
| 10 | +from selenium.webdriver.support import expected_conditions as EC |
| 11 | + |
| 12 | +# Service name for storing credentials in keyring |
| 13 | +SERVICE_NAME = "OSPOS_AutoUploader" |
| 14 | + |
| 15 | +# Default values (can be overridden by user input) |
| 16 | +DEFAULT_OSPOS_URL = "http://localhost/ospos/public/" |
| 17 | +DEFAULT_USERNAME = "admin" |
| 18 | +DEFAULT_PASSWORD = "pointofsale" |
| 19 | +DEFAULT_CHROMEDRIVER_PATH = "C:\\path\\to\\chromedriver.exe" |
| 20 | + |
| 21 | +def get_credentials(): |
| 22 | + """Retrieve stored credentials or prompt for new ones.""" |
| 23 | + username = keyring.get_password(SERVICE_NAME, "username") or DEFAULT_USERNAME |
| 24 | + password = keyring.get_password(SERVICE_NAME, "password") or DEFAULT_PASSWORD |
| 25 | + |
| 26 | + use_default = input(f"Use default credentials ({username})? (y/n): ").strip().lower() |
| 27 | + if use_default != 'y': |
| 28 | + username = input("Enter OSPOS Username: ") |
| 29 | + password = getpass.getpass("Enter OSPOS Password: ") # Hides password input |
| 30 | + keyring.set_password(SERVICE_NAME, "username", username) |
| 31 | + keyring.set_password(SERVICE_NAME, "password", password) |
| 32 | + print("Credentials saved securely!") |
| 33 | + return username, password |
| 34 | + |
| 35 | +def get_ospos_url(): |
| 36 | + """Retrieve stored OSPOS URL or prompt for new one.""" |
| 37 | + ospos_url = keyring.get_password(SERVICE_NAME, "ospos_url") or DEFAULT_OSPOS_URL |
| 38 | + |
| 39 | + use_default = input(f"Use default OSPOS URL ({ospos_url})? (y/n): ").strip().lower() |
| 40 | + if use_default != 'y': |
| 41 | + ospos_url = input("Enter OSPOS URL: ") |
| 42 | + keyring.set_password(SERVICE_NAME, "ospos_url", ospos_url) |
| 43 | + print("OSPOS URL saved!") |
| 44 | + return ospos_url |
| 45 | + |
| 46 | +def get_csv_directory(section): |
| 47 | + """Retrieve stored CSV directory for section or prompt for new one.""" |
| 48 | + csv_directory = keyring.get_password(SERVICE_NAME, f"csv_directory_{section}") |
| 49 | + if not csv_directory: |
| 50 | + csv_directory = input(f"Enter CSV Directory Path for {section}: ") |
| 51 | + keyring.set_password(SERVICE_NAME, f"csv_directory_{section}", csv_directory) |
| 52 | + print(f"CSV directory for {section} saved!") |
| 53 | + return csv_directory |
| 54 | + |
| 55 | +def get_chromedriver_path(): |
| 56 | + """Retrieve stored ChromeDriver path or prompt for new one.""" |
| 57 | + chromedriver_path = keyring.get_password(SERVICE_NAME, "chromedriver_path") or DEFAULT_CHROMEDRIVER_PATH |
| 58 | + |
| 59 | + use_default = input(f"Use default ChromeDriver path ({chromedriver_path})? (y/n): ").strip().lower() |
| 60 | + if use_default != 'y': |
| 61 | + chromedriver_path = input("Enter ChromeDriver Path: ") |
| 62 | + keyring.set_password(SERVICE_NAME, "chromedriver_path", chromedriver_path) |
| 63 | + print("ChromeDriver path saved!") |
| 64 | + return chromedriver_path |
| 65 | + |
| 66 | +def natural_sort_key(file_name): |
| 67 | + """Sort files in natural order (numbers in file names sorted correctly).""" |
| 68 | + return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', file_name)] |
| 69 | + |
| 70 | +# Prompt user to choose which section to upload files to |
| 71 | +section = input("Enter the section to upload CSV files to (Items/Customers): ").strip().capitalize() |
| 72 | + |
| 73 | +# Get user credentials, OSPOS URL, CSV directory, and ChromeDriver path |
| 74 | +USERNAME, PASSWORD = get_credentials() |
| 75 | +OSPOS_URL = get_ospos_url() |
| 76 | +CSV_DIRECTORY = get_csv_directory(section) |
| 77 | +CHROMEDRIVER_PATH = get_chromedriver_path() |
| 78 | + |
| 79 | +# Initialize WebDriver |
| 80 | +def setup_driver(): |
| 81 | + options = webdriver.ChromeOptions() |
| 82 | + service = Service(CHROMEDRIVER_PATH) # Correct way to set ChromeDriver path in Selenium 4 |
| 83 | + driver = webdriver.Chrome(service=service, options=options) |
| 84 | + driver.get(OSPOS_URL) |
| 85 | + return driver |
| 86 | + |
| 87 | +# Login to OSPOS |
| 88 | +def login(driver): |
| 89 | + WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "input-username"))).send_keys(USERNAME) |
| 90 | + WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "input-password"))).send_keys(PASSWORD) |
| 91 | + WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']"))).click() |
| 92 | + time.sleep(3) # Allow time for login |
| 93 | + |
| 94 | +# Ensure modal is closed before proceeding |
| 95 | +def close_modal_if_open(driver): |
| 96 | + try: |
| 97 | + close_button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Close')]"))) |
| 98 | + close_button.click() |
| 99 | + WebDriverWait(driver, 5).until_not(EC.presence_of_element_located((By.CLASS_NAME, "modal-dialog"))) |
| 100 | + time.sleep(2) |
| 101 | + print("Closed stuck modal.") |
| 102 | + except: |
| 103 | + pass # If no modal is open, continue |
| 104 | + |
| 105 | +# Navigate to CSV Import Page |
| 106 | +def navigate_to_csv_import(driver, section): |
| 107 | + WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, section))).click() |
| 108 | + time.sleep(3) |
| 109 | + close_modal_if_open(driver) |
| 110 | + csv_import_button = WebDriverWait(driver, 10).until( |
| 111 | + EC.element_to_be_clickable((By.XPATH, f"//button[contains(@data-href, '{section.lower()}/csvImport')]")) |
| 112 | + ) |
| 113 | + csv_import_button.click() |
| 114 | + time.sleep(3) # Allow modal to open |
| 115 | + |
| 116 | +# Upload a single CSV file |
| 117 | +def upload_csv(driver, file_path, section): |
| 118 | + navigate_to_csv_import(driver, section) |
| 119 | + |
| 120 | + try: |
| 121 | + file_input = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "file_path"))) |
| 122 | + file_input.send_keys(file_path) |
| 123 | + |
| 124 | + submit_button = WebDriverWait(driver, 10).until( |
| 125 | + EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Submit')]")) |
| 126 | + ) |
| 127 | + submit_button.click() |
| 128 | + |
| 129 | + time.sleep(5) # Wait for the upload to complete |
| 130 | + |
| 131 | + # Wait for modal to close before proceeding |
| 132 | + WebDriverWait(driver, 10).until_not(EC.presence_of_element_located((By.CLASS_NAME, "modal-dialog"))) |
| 133 | + time.sleep(2) |
| 134 | + |
| 135 | + print(f"Uploaded: {file_path}") |
| 136 | + return True |
| 137 | + except Exception as e: |
| 138 | + print(f"Failed to upload {file_path}: {e}") |
| 139 | + close_modal_if_open(driver) # Force close modal if stuck |
| 140 | + return False |
| 141 | + |
| 142 | +# Process all CSV files |
| 143 | +def upload_csv_files(): |
| 144 | + driver = setup_driver() |
| 145 | + login(driver) |
| 146 | + |
| 147 | + # Get all CSV files |
| 148 | + files = [f for f in os.listdir(CSV_DIRECTORY) if f.endswith(".csv")] |
| 149 | + |
| 150 | + # Sort files naturally if they have numbers, else keep original order |
| 151 | + if any(re.search(r'\d+', f) for f in files): |
| 152 | + sorted_files = sorted(files, key=natural_sort_key) |
| 153 | + else: |
| 154 | + sorted_files = files |
| 155 | + |
| 156 | + for filename in sorted_files: |
| 157 | + file_path = os.path.join(CSV_DIRECTORY, filename) |
| 158 | + upload_csv(driver, file_path, section) |
| 159 | + |
| 160 | + print("Process Completed.") |
| 161 | + driver.quit() |
| 162 | + |
| 163 | +# Run the script |
| 164 | +upload_csv_files() |
0 commit comments