New Text Document
New Text Document
import datetime
from pathlib import Path
from PyQt5.QtWidgets import QMessageBox
from Logger import setup_logger
from Word import WordHandler
from constants import WIRLogFilePath
from load_combobox_items_1 import ComboBoxLoader
class SearchEditing:
def __init__(self, main_window, ui):
self.main_window = main_window
self.ui = ui
self.logger = setup_logger() # New Line: Initialize logging
self.file_path = Path(WIRLogFilePath)
self.combo_loader = ComboBoxLoader()
self.combo_loader.load_activities(self.ui.Activity_QCB2)
self.combo_loader.load_data(self.ui.Villa_QCB2, 0)
self.combo_loader.load_dates(self.ui.Inspection_Date_QCB2, 27)
def Search_WIRLog(self):
try:
# Ensure inputs are strings before calling .strip()
wir_no_to_find = (self.ui.WIRNo_QLE2.text() or "").strip() # Modified
rev_to_find = (self.ui.Rev_QCB2.currentText() or "").strip() #
Modified
self.ui.WIRNo_QLE2.setEnabled(False)
self.ui.Rev_QCB2.setEnabled(False)
if not wir_no_to_find:
self.logger.warning("Search attempted without entering WIR No.") #
New Line
self._show_dialog("Error", "Please enter WIR No to search.",
QMessageBox.Warning)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
return
try:
wir_no_int = int(wir_no_to_find)
except ValueError:
self.logger.error(f"Invalid WIR No entered: {wir_no_to_find}") #
New Line
self._show_dialog("Error", "Invalid WIR No. Please enter a valid
number.", QMessageBox.Warning)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
return
latest_revision = None
found_record = None
revisions = [] # Store all revisions for the WIR No
if current_wir_no == wir_no_to_find:
try:
revisions.append(int(current_rev))
except ValueError:
self.logger.error(f"Invalid revision format:
{current_rev}") # New Line
else:
self.logger.error("WIR Log file not found!") # New Line
self._show_dialog("Error", "WIR Log file not found!",
QMessageBox.Critical)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
return
if revisions:
latest_revision = str(max(revisions))
if not rev_to_find:
if latest_revision:
self.logger.info(f"Auto-selecting latest revision
{latest_revision} for WIR No {wir_no_to_find}.") # New Line
self._show_dialog(
"Information",
f"No revision selected. The latest revision for WIR No
{wir_no_to_find} is {latest_revision}.",
QMessageBox.Information
)
self.ui.Rev_QCB2.setCurrentText(latest_revision)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
return
if found_record:
self.logger.info(f"Record found for WIR No {wir_no_to_find},
Revision {rev_to_find}.") # New Line
self.ui.groupBox_2.setEnabled(True)
self.ui.WIRNo_QLE2.setEnabled(False)
self.ui.Rev_QCB2.setEnabled(False)
self._populate_ui_fields(found_record)
else:
self.logger.warning(f"WIR No {wir_no_to_find} Rev {rev_to_find} not
found.") # New Line
self._show_dialog("Information", f"WIR No {wir_no_to_find} Rev
{rev_to_find} not found.", QMessageBox.Information)
self.ui.groupBox_2.setEnabled(False)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
except Exception as e:
self.logger.exception(f"Unexpected error during search: {str(e)}") #
New Line
self._show_dialog("Error", f"An unexpected error occurred during
search: {str(e)}", QMessageBox.Critical)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
self.ui.Description_QTE2.setPlainText(record.get('Description', ''))
def Save_WIRLog(self):
wir_no_to_update = self.ui.WIRNo_QLE2.text().strip()
rev_to_update = self.ui.Rev_QCB2.currentText().strip()
activity_index = self.ui.Activity_QCB2.currentIndex()
villa_text = self.ui.Villa_QCB2.currentText().strip()
# Collect data from controls
updated_record = {
'WIR No': wir_no_to_update,
'Revision': rev_to_update,
'Activity': self.ui.Activity_QCB2.currentText().strip(),
'Sub Activity': self.ui.Sub_Activity_QCB2.currentText().strip(),
'Description': self.ui.Description_QTE2.toPlainText().strip(),
'Villa': f"Plot # {villa_text}" if activity_index < 5 else f"Villa #
{villa_text}",
'Level': self.ui.Level_QCB2.currentText().strip(),
'Part': self.ui.Part_QCB2.currentText().strip(),
'Created Date':datetime.date.today().strftime('%d %b %Y'),
'Inspection Date': self.ui.Inspection_Date_QCB2.currentText().strip(),
'Status': "Under Review",
}
print(f" villa # {'Villa'}")
# Remove empty fields
updated_record = {k: v for k, v in updated_record.items() if v}
if not fieldnames:
self._show_dialog("Error", "CSV file is missing headers!",
QMessageBox.Critical)
return
all_records = []
record_updated = False
if record_updated:
with open(self.file_path, 'w', newline='') as writeFile:
writer = csv.DictWriter(writeFile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(all_records)
self._show_dialog("Success", f"WIR No {wir_no_to_update} Rev
{rev_to_update} updated successfully!",
QMessageBox.Information)
self.ui.groupBox_2.setEnabled(False)
self.ui.WIRNo_QLE2.setEnabled(True)
self.ui.Rev_QCB2.setEnabled(True)
self._clear_ui_fields()
else:
self._show_dialog("Error", f"WIR No {wir_no_to_update} Rev
{rev_to_update} not found for update!",
QMessageBox.Warning)
except FileNotFoundError:
self._show_dialog("Error", "WIR Log file not found!",
QMessageBox.Critical)
except Exception as e:
self._show_dialog("Error", f"An unexpected error occurred during save:
{str(e)}", QMessageBox.Critical)
def Cancel_SearchWIR(self):
"""Resets the Search WIR tab to its initial state."""
self.ui.groupBox_2.setEnabled(False) # Disable the group box
self.ui.WIRNo_QLE2.setEnabled(True) # Re-enable WIR No input
self.ui.Rev_QCB2.setEnabled(True) # Re-enable Revision input
def _clear_ui_fields(self):
"""Clears the UI fields in Tab 3."""
self.ui.WIRNo_QLE2.clear() # Clear WIR No input field (optional)
self.ui.Rev_QCB2.setCurrentIndex(0) # Reset Revision Combobox to default
(index 0, which is "") (optional)
self.ui.Activity_QCB2.setCurrentIndex(-1) # or setCurrentText("") to clear
without triggering currentIndexChanged signal if needed
self.ui.Sub_Activity_QCB2.setCurrentIndex(-1)
self.ui.Description_QTE2.clear()
self.ui.Villa_QCB2.setCurrentIndex(-1)
self.ui.Level_QCB2.setCurrentIndex(-1)
self.ui.Part_QCB2.setCurrentIndex(-1)
self.ui.Inspection_Date_QCB2.setCurrentIndex(-1)